Project

General

Profile

Statistics
| Revision:

root / src / messages / ResponsePredecessorMessage.java @ 1

History | View | Annotate | Download (1.41 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 ResponsePredecessorMessage extends Message {
11
    private ConnectionInfo ci;
12
    private String ipAddress;
13
    private int port;
14

    
15
    public ResponsePredecessorMessage(ConnectionInfo ci,String ipAddress,int port) {
16
        this.ci = ci;
17
        this.ipAddress = ipAddress;
18
        this.port = port;
19
    }
20
    @Override
21
    public void handleMessage() throws UnknownHostException {
22
        if(ci.getHashedKey() == null){
23
        } else if(ChordManager.numberInInterval(ChordManager.peerHash, ChordManager.getFingerTable().get(0).getHashedKey(),ci.getHashedKey())) {
24
            ChordManager.getFingerTable().set(0, new ConnectionInfo(ci.getHashedKey(), ci.getIp(), ci.getPort()));
25
        }
26
        MessageForwarder.sendMessage(new PredecessorMessage(new ConnectionInfo(ChordManager.peerHash, InetAddress.getLocalHost().getHostAddress(), Peer.port), ci.getIp(), ci.getPort()));
27

    
28
    }
29

    
30
    @Override
31
    public String toString() {
32
        String returnString;
33
        returnString = "RESPONSE_PREDECESSOR " + ci.getHashedKey() + " " + ci.getIp() + " " + ci.getPort();
34
        return returnString;
35
    }
36

    
37
    @Override
38
    public String getIpAddress() {
39
        return this.ipAddress;
40
    }
41

    
42
    @Override
43
    public int getPort() {
44
        return this.port;
45
    }
46
}