This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
peerProcess.java
251 lines (219 loc) · 6.44 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package cnt5106c.p2p_file_sharing;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.BitSet;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.HashMap;
public class peerProcess {
private static final String commonConfigPath = "Common.cfg";
private static final String peerInfoConfigPath = "PeerInfo.cfg";
final int numPrefNeighbor;
final int unchokeInterval;
final int optUnchokeInterval;
final String fileName;
final int fileSize;
final int pieceSize;
final int pieceCount;
final String peerID;
HashMap<String, PeerInfo> peerInfos;
ArrayList<String> peerIDs;
ArrayList<String> previousPeersID;
Controller controller;
FileHandler fileHandler;
Logger logger;
final ScheduledExecutorService progressBar;
public static void main(String[] args) throws InterruptedException, IOException {
peerProcess peer;
try {
peer = new peerProcess(args[0]);
}
catch(Exception e) {
e.printStackTrace();
return;
}
peer.start();
Thread console = new Thread(new ConsoleControl(peer));
console.setDaemon(true);
console.start();
}
// peerProcess constructor : initialize config parameters
peerProcess(String arg) throws FileNotFoundException, IOException {
BufferedReader commonConfigReader = new BufferedReader(new FileReader(commonConfigPath));
try {
String line;
line = commonConfigReader.readLine();
numPrefNeighbor = Integer.parseInt(line.substring(27));
line = commonConfigReader.readLine();
unchokeInterval = Integer.parseInt(line.substring(18));
line = commonConfigReader.readLine();
optUnchokeInterval = Integer.parseInt(line.substring(28));
line = commonConfigReader.readLine();
fileName = line.substring(9);
line = commonConfigReader.readLine();
fileSize = Integer.parseInt(line.substring(9));
line = commonConfigReader.readLine();
pieceSize = Integer.parseInt(line.substring(10));
int temp = fileSize / pieceSize;
if(fileSize % pieceSize != 0)
temp++;
pieceCount = temp;
}
catch(FileNotFoundException e) {
System.out.println("Cannot find " + commonConfigPath + ".");
throw e;
}
catch(IOException e) {
System.out.println("Failed to read " + commonConfigPath + ".");
throw e;
}
finally {
commonConfigReader.close();
}
BufferedReader peerInfoConfigReader = new BufferedReader(new FileReader(peerInfoConfigPath));
try {
peerID = new String(arg);
peerInfos = new HashMap<String, PeerInfo>();
peerIDs = new ArrayList<String>();
previousPeersID = new ArrayList<String>();
// Collect info for all peers
boolean readSelf = false;
String line;
line = peerInfoConfigReader.readLine();
while(line != null) {
String info[] = line.split(" ");
if(info[0].equals(peerID))
readSelf = true;
PeerInfo newPeer;
if(Integer.parseInt(info[3]) == 1)
newPeer = new PeerInfo(info[0], info[1], Integer.parseInt(info[2]), true);
else
newPeer = new PeerInfo(info[0], info[1], Integer.parseInt(info[2]), false);
peerInfos.put(info[0], newPeer);
peerIDs.add(info[0]);
if(!readSelf)
previousPeersID.add(info[0]);
line = peerInfoConfigReader.readLine();
}
// Set up own info
PeerInfo peer = peerInfos.get(peerID);
if(peer == null) {
System.out.println("Cannot find peer info in " + peerInfoConfigPath + ".");
throw new IOException();
}
if(peer.hasFile) {
for(int i = 0; i < peer.bitfield.size(); i++)
peer.bitfield.set(i);
}
controller = new Controller(this);
fileHandler = new FileHandler(this);
logger = new Logger(peerID);
progressBar = Executors.newSingleThreadScheduledExecutor();
}
catch(FileNotFoundException e) {
System.out.println("Cannot find " + peerInfoConfigPath + ".");
throw e;
}
catch(IOException e) {
System.out.println("Failed to read " + peerInfoConfigPath + ".");
throw e;
}
finally {
peerInfoConfigReader.close();
}
}
// Holds information for a peer
class PeerInfo {
final String ID;
final String addr;
final int port;
boolean hasFile;
BitSet bitfield;
boolean choked;
boolean interested;
PeerHandler handler;
PeerInfo(String id, String addr, int port, boolean hasFile) {
ID = id;
this.addr = addr;
this.port = port;
this.hasFile = hasFile;
bitfield = new BitSet(pieceCount);
choked = true;
interested = false;
handler = null;
}
}
// Daemon thread for console control
private static class ConsoleControl implements Runnable {
final peerProcess peer;
ConsoleControl(peerProcess peer) {
this.peer = peer;
}
public void run() {
Scanner scanner = new Scanner(System.in);
String op;
while(true) {
System.out.println("Option('h' for help): ");
peer.progressBar.scheduleAtFixedRate(new ProgressBar(peer), 0, 300, TimeUnit.MILLISECONDS);
op = scanner.nextLine();
switch(op) {
case "h":
synchronized(System.out) {
System.out.print("\r");
System.out.println("'h' for help");
System.out.println("'q' to terminate process");
System.out.println("'d' to toggle console display");
}
break;
case "q":
scanner.close();
try {
peer.controller.shutdown();
peer.logger.closeFile();
}
catch(IOException | InterruptedException e) {
System.out.println("Failed to shutdown controller.");
return;
}
return;
case "d":
peer.logger.toggleConsoleDisplay();
break;
default:
break;
}
}
}
}
// Thread for displaying progress bar on console
private static class ProgressBar implements Runnable {
final peerProcess peer;
ProgressBar(peerProcess peer) {
this.peer = peer;
}
public void run() {
displayProcessBar(peer);
}
}
// Print progress bar
private static void displayProcessBar(peerProcess peer) {
int p = (int)((float)peer.peerInfos.get(peer.peerID).bitfield.cardinality() / (float)peer.pieceCount * 100);
System.out.format("\rProgress: [%3d%%] [", p);
for(int i = 0; i < p/2; i++)
System.out.print('#');
for(int i = p/2; i < 50; i++)
System.out.print('.');
System.out.print(']');
System.out.flush();
}
// Start peerProcess : open a thread for main loop
void start() {
Thread control = new Thread(controller);
control.start();
}
}