-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerProcess.java
111 lines (101 loc) · 4.57 KB
/
peerProcess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Arrays;
public class peerProcess {
private int peerid;
private int portnum;
private Commoncfg commoncfg;
private Map<Integer, PeerInfo> peerinfo;
private Map<Integer, NeighborInfo> neighborlist;
private CommunicationManager cm;
private boolean hasFile;
private Bitfield bitfield;
private int sizeofpiece;
private int numofpiece;
private int lastpiecesize;
private Logger log;
public peerProcess(int myPeerID) {
try {
this.peerid = myPeerID;
String filepath = "Common.cfg";
Parser parser1 = new Parser(filepath);
commoncfg = parser1.parseCommon();
String filepath_peer = "PeerInfo.cfg";
Parser parser2 = new Parser(filepath_peer);
peerinfo = parser2.parsePeerInfo();
neighborlist = new ConcurrentHashMap<Integer, NeighborInfo>();
System.out.println("peerID = " + peerid);
System.out.println("NumberOfPreferredNeighbors = " + commoncfg.getNum_Of_PreferredNeighbors());
System.out.println("UnchokingInterval = " + commoncfg.getUnchoking_Interval() + "s");
System.out.println("OptimisticUnchokingInt;rval = " + commoncfg.getOptimistic_Unchoking_Interval() + "s");
System.out.println("FileName is " + commoncfg.getFileName());
System.out.println("FileSize = " + commoncfg.getFileSize() + "bytes");
System.out.println("PieceSize = " + commoncfg.getPieceSize() + "bytes");
this.sizeofpiece = commoncfg.getPieceSize();
int sizeoffile = commoncfg.getFileSize();
log = new Logger();
this.numofpiece = sizeoffile / this.sizeofpiece;
int tmp = sizeoffile % this.sizeofpiece;
if (tmp == 0) {
this.lastpiecesize = this.sizeofpiece;
} else {
this.lastpiecesize = tmp;
this.numofpiece = this.numofpiece + 1;
}
for (Integer peerID : peerinfo.keySet()) {
PeerInfo peer = peerinfo.get(peerID);
if (peer.getPeerID() != peerid) {
neighborlist.put(peer.getPeerID(), new NeighborInfo(peer.getPeerID(), peer.getHostName(), peer.getPort(), peer.getFileStatus()));
} else {
portnum = peer.getPort();
hasFile = peer.getFileStatus();
this.bitfield = new Bitfield(this.numofpiece, this.hasFile);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void startProcess() {
Thread serverThread;
log.CreateLog("" + peerid);
try {
cm = new CommunicationManager(peerid, commoncfg, neighborlist, bitfield, peerinfo);
serverThread = new Thread(new Server(portnum, cm, log));
serverThread.start();
int tmp[] = new int[peerinfo.size()];
int i = 0;
for (Integer peerID : peerinfo.keySet()){
tmp[i++] = peerID;
}
Arrays.sort(tmp);
for (Integer peerID : peerinfo.keySet()) {
if (peerID < peerid) {
//peer has already been started, try to make a connection
Client newClient = new Client(peerinfo.get(peerID).getHostName(),
peerinfo.get(peerID).getPort());
Thread handlerThread = new Thread(new MessageHandler(peerID, newClient, cm, log));
newClient.connect();
handlerThread.start();
log.TcpConnectionOutgoing("" + peerid, "" + peerID);
}
else if(peerid == tmp[tmp.length - 1]) serverThread.stop();
}
Thread preferredThread = new Thread(new PreferredNeighborsHandler(commoncfg, neighborlist,bitfield, log, peerid));
Thread optimisticThread = new Thread(new OptimisticalUnchokedHandler(commoncfg, neighborlist, log, peerid));
preferredThread.start();
optimisticThread.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
peerProcess peer = new peerProcess(Integer.valueOf(args[0]));
peer.startProcess();
System.out.println("Peer process is started!");
}
}