Project

General

Profile

Statistics
| Revision:

root / src / messages / GetPredecessorMessage.java @ 1

History | View | Annotate | Download (1.31 KB)

1
package messages;
2

    
3
import chord.ChordManager;
4
import chord.ConnectionInfo;
5
import peer.Peer;
6

    
7
import java.net.InetAddress;
8
import java.net.UnknownHostException;
9

    
10
public class GetPredecessorMessage extends Message {
11

    
12
    private ConnectionInfo ci;
13
    private String ipAddress;
14
    private int port;
15
    public GetPredecessorMessage(ConnectionInfo ci,String ipAddress,int port){
16
        this.ci = ci;
17
        this.ipAddress = ipAddress;
18
        this.port = port;
19
    }
20
    @Override
21
    public String getIpAddress() {
22
        return this.ipAddress;
23
    }
24

    
25
    @Override
26
    public int getPort() {
27
        return this.port;
28
    }
29
    @Override
30
    public void handleMessage() throws UnknownHostException {
31
        if(ChordManager.predecessor == null){
32
            MessageForwarder.sendMessage(new ResponsePredecessorMessage(new ConnectionInfo(null, InetAddress.getLocalHost().getHostAddress(), Peer.port), ci.getIp(), ci.getPort()));
33
        } else {
34
            MessageForwarder.sendMessage(new ResponsePredecessorMessage(new ConnectionInfo(ChordManager.getPredecessor().getHashedKey(), ChordManager.getPredecessor().getIp(), ChordManager.getPredecessor().getPort()), ci.getIp(), ci.getPort()));
35
        }
36
    }
37

    
38
    @Override
39
    public String toString() {
40
        return "GET_PREDECESSOR " + this.ci.getIp() + " " + this.ci.getPort();
41
    }
42
}