Project

General

Profile

Statistics
| Revision:

root / src / messages / SucessorMessage.java @ 1

History | View | Annotate | Download (1.91 KB)

1 1 up20150524
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 SucessorMessage extends Message {
11
12
    private String receivedKey;
13
    private ConnectionInfo ci;
14
    private String ipAddress;
15
    private int port;
16
17
    public SucessorMessage(String receivedKey, ConnectionInfo ci,String ipAddress,int port) {
18
        this.receivedKey = receivedKey;
19
        this.ci = ci;
20
        this.ipAddress = ipAddress;
21
        this.port = port;
22
    }
23
24
    @Override
25
    public void handleMessage() {
26
27
        int index;
28
29
        if(receivedKey.equals(ChordManager.peerHash.toString())) {
30
            index = 0;
31
        } else {
32
            for(index = 0; index < ChordManager.getM(); index++)
33
            {
34
                String res = ChordManager.calculateNextKey(ChordManager.peerHash, index, ChordManager.getM());
35
                if(res.equals(receivedKey))
36
                    break;
37
            }
38
        }
39
40
        ChordManager.getFingerTable().set(index,ci);
41
42
        if(receivedKey.equals(ChordManager.peerHash))
43
            return;
44
45
        try {
46
            PredecessorMessage predecessorMessage = new PredecessorMessage(new ConnectionInfo(ChordManager.peerHash, InetAddress.getLocalHost().getHostAddress(), Peer.port),ci.getIp(), ci.getPort());
47
            MessageForwarder.sendMessage(predecessorMessage);
48
        } catch (UnknownHostException e) {
49
            e.printStackTrace();
50
        }
51
    }
52
53
    @Override
54
    public String toString() {
55
56
        String returnString =  "SUCCESSOR " + receivedKey + " " + ci.getHashedKey().toString() + " " + ci.getIp() + " " + ci.getPort();
57
58
        return returnString;
59
    }
60
    @Override
61
    public String getIpAddress() {
62
        return this.ipAddress;
63
    }
64
65
    @Override
66
    public int getPort() {
67
        return this.port;
68
    }
69
70
    public ConnectionInfo getCi() {
71
        return ci;
72
    }
73
}