Project

General

Profile

Statistics
| Revision:

root / src / chord / FixFingers.java @ 1

History | View | Annotate | Download (1.63 KB)

1 1 up20150524
package chord;
2
3
import chord.ChordManager;
4
import chord.ConnectionInfo;
5
import messages.LookupMessage;
6
import messages.Message;
7
import messages.MessageForwarder;
8
import messages.SucessorMessage;
9
import peer.Peer;
10
11
import java.math.BigInteger;
12
import java.net.InetAddress;
13
import java.net.UnknownHostException;
14
import java.util.ArrayList;
15
16
public class FixFingers implements Runnable {
17
18
    private int index = -1;
19
20
    @Override
21
    public void run() {
22
        index++;
23
24
        if(index == ChordManager.getM() ) {
25
            ChordManager.printFingerTable();
26
            index = 0;
27
        }
28
29
        String key = ChordManager.calculateNextKey(ChordManager.peerHash, index, ChordManager.getM() );
30
        ArrayList<ConnectionInfo> fingerTable = ChordManager.getFingerTable();
31
32
        if(index > (fingerTable.size() - 1)) {
33
            try {
34
                fingerTable.add(new ConnectionInfo(ChordManager.peerHash, InetAddress.getLocalHost().getHostAddress(), Peer.port));
35
            } catch (UnknownHostException e) {
36
                e.printStackTrace();
37
            }
38
        }
39
40
        Message res = null;
41
        try {
42
            res = ChordManager.searchSuccessor2(new ConnectionInfo(new BigInteger(key), InetAddress.getLocalHost().getHostAddress(), Peer.port));
43
        } catch (UnknownHostException e) {
44
            e.printStackTrace();
45
        }
46
47
        if(res != null){
48
            if(res instanceof SucessorMessage) {
49
                fingerTable.set(index, ((SucessorMessage) res).getCi());
50
            }
51
            else if (res instanceof LookupMessage)
52
            {
53
                MessageForwarder.sendMessage(res);
54
            }
55
        }
56
    }
57
}