-
Notifications
You must be signed in to change notification settings - Fork 1
/
Member.java
365 lines (289 loc) · 11.4 KB
/
Member.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
public class Member {
public MemberInfo myInfo;
private ExecutorService pool;
private MemberInfo predecessor;
private MemberInfo[] fingerTable;
ArrayList<MyFile> files;
int next; // for fix fingers
/*
Constructors
At minimum, a Member must be created with knowledge of its receiving port number.
*/
public Member(int receivePort, MemberInfo[] fingerEntries, MemberInfo predecessor){
this.myInfo = new MemberInfo(receivePort);
this.fingerTable = new MemberInfo[myInfo.chordIDLength];
this.predecessor = predecessor;
this.pool = Executors.newFixedThreadPool(20);
this.files = new ArrayList<MyFile>();
}
public Member(int receivePort, MemberInfo[] fingerEntries){
this(receivePort, fingerEntries, null);
}
public Member(int receivePort){
this(receivePort, null, null);
}
/*
Add any functions that will create a SendSocket here, whether it's the ReceivingSocket class, Stabilizer class, or the main workflow that will call them
The ReceivingSocket and Stabilizer properties Member myself exist so they can call these functions
*/
//Returns a MemberInfo with the successor of the chordID if we can find it in our fingerTable
//Else, send a message to the closest chordID in the fingerTable and return null
public MemberInfo findSuccessor(int chordID, MemberInfo requester) {
// if it's us
synchronized(this) {
if(this.compareChordIds(this.myInfo.chordID, chordID) && (this.compareChordIds(this.fingerTable[0].chordID, chordID) || this.fingerTable[0].chordID == chordID)) {
return this.myInfo;
}
}
MemberInfo nextClosest = closestPreceding(chordID);
RequestSuccessor message = new RequestSuccessor(chordID, requester, nextClosest);
this.send(message);
// RETURNING NULL BECAUSE NO OPTION TYPE #ANGERY
return null;
}
public synchronized MemberInfo closestPreceding(int chordID) {
for (int i = this.myInfo.chordIDLength - 1; i >= 1; i--) {
int tableID = (this.fingerTable[i].chordID);
if(this.compareChordIds(this.myInfo.chordID, tableID) && (this.compareChordIds(chordID, this.myInfo.chordID) || chordID == this.myInfo.chordID)) {
return fingerTable[i];
}
}
return this.myInfo;
}
public String fileSearch(int key, MemberInfo requester) throws FileNotFoundException {
System.out.println("Searching for " + key + " on " + requester.chordID);
synchronized(this) {
//if((this.myInfo.chordID > key) && (key <= this.fingerTable[0].chordID)) {
if((this.compareChordIds(this.myInfo.chordID, key) &&
(this.compareChordIds(this.fingerTable[0].chordID, key) || (key == this.fingerTable[0].chordID)))) {
for (MyFile file: this.files) {
if(file.chordID == key) {
System.out.println("I have the file!");
return file.fileName;
}
}
// we didn't find the file
throw new FileNotFoundException();
}
}
MemberInfo nextClosest = closestPreceding(key);
RequestFile message = new RequestFile(key, requester, nextClosest);
this.send(message);
// RETURNING NULL BECAUSE NO OPTION TYPE #ANGERY
return null;
}
// finds a machine to add a file to
// returns null if the file should be added on this machine
public MemberInfo findNewFileMachine(MyFile file) {
//File is in my ID space range
if((this.compareChordIds(file.chordID, predecessor.chordID) &&
(this.compareChordIds(this.myInfo.chordID, file.chordID) || file.chordID == this.myInfo.chordID))) {
return null;
}
//File should be stored by another machine
else{
int potentialStorer = -1;
//Find the machine with the lowest Chord ID that's still larger than the file's Chord ID
for(int i = 0; i < this.fingerTable.length; i++){
if( this.compareChordIds( this.fingerTable[i].chordID, file.chordID ) ){
potentialStorer = i;
}
}
return this.fingerTable[potentialStorer];
}
}
public void addFile(MyFile file, MemberInfo originator){
MemberInfo hostMachine = findNewFileMachine(file);
if(hostMachine == null){
files.add(file);
System.out.println("Added file " + file.fileName + " with Chord ID " + file.chordID + " to my records.");
}
else{
this.send(new AddFileMessage(file, originator, hostMachine));
System.out.println("Sent request to add file " + file.fileName + " with Chord ID " + file.chordID + " to "
+ hostMachine.IP.toString() + " with Chord ID " + hostMachine.chordID);
}
}
// Check that things have joined correctly but we're not implementing joining
public synchronized void stabilize(){
//Request predecessor's successor
RequestSuccessor m = new RequestSuccessor(myInfo, predecessor);
this.pool.execute(new SendingSocket(m));
//Check finger table entries
}
// Also part of join -- not implementing
public void notify(int chordID) {}
// Also part of join -- could implement but not a priority
public synchronized void fixFingers() {}
// we will implement this
public void checkPredecessor() {}
//helpers
// enables us to send messages from recieving socket
public void send(Message m) {
this.pool.execute(new SendingSocket(m));
}
/* Returns our finger table */
public synchronized MemberInfo[] getFingerTable() {
return this.fingerTable;
}
public synchronized void setFingerTable(MemberInfo[] ft) {
this.fingerTable = new MemberInfo[myInfo.chordIDLength];
System.arraycopy(ft, 0, this.fingerTable, 0, myInfo.chordIDLength);
}
// if a is to the right of b, return true, else false
public boolean compareChordIds(int a, int b) {
if (Math.abs(a - b) > Math.pow(2, this.myInfo.chordIDLength - 1)) {
return b > a;
}
else {
return a > b;
}
}
public int findFingerTableSlot(MemberInfo newEntry){
int slot = this.myInfo.chordIDLength;
for(int i = 0; i < this.myInfo.chordIDLength; i++){
int maxI = ( (this.myInfo.chordID + ((int) Math.pow(2, i)) ) % ((int) Math.pow(2, this.myInfo.chordIDLength)) );
if( this.compareChordIds(maxI, newEntry.chordID)){
slot = i;
}
}
return slot;
}
public void addFingerTableEntry(MemberInfo newEntry){
int slot = findFingerTableSlot(newEntry);
if(slot < this.myInfo.chordIDLength && ((this.fingerTable[slot] == null) || this.compareChordIds(newEntry.chordID, this.fingerTable[slot].chordID) )){
this.fingerTable[slot] = newEntry;
}
}
public void setPredecessor(MemberInfo neighbor){
//No null predecessor
if(this.predecessor == null){
this.predecessor = neighbor;
}
else if( this.compareChordIds( this.myInfo.chordID, neighbor.chordID ) && this.compareChordIds(neighbor.chordID, this.predecessor.chordID) ){
this.predecessor = neighbor;
}
}
public void setSuccessor(MemberInfo neighbor){
//No null successor
if(this.fingerTable[0] == null){
this.fingerTable[0] = neighbor;
}
else if( this.compareChordIds(this.fingerTable[0].chordID, neighbor.chordID) && this.compareChordIds(neighbor.chordID, this.myInfo.chordID) ){
this.fingerTable[0] = neighbor;
}
}
public synchronized void printFingerTable(){
System.out.println("------- Myself -------");
System.out.println("My Chord ID: " + this.myInfo.chordID);
System.out.println("My IP: " + this.myInfo.IP.toString());
System.out.println("-----Predecessor -----");
if(this.predecessor != null){
System.out.println("Predecessor Chord ID: " + this.predecessor.chordID);
System.out.println("Predecessor IP: " + this.predecessor.IP.toString());
}
else{
System.out.println("Predecessor is NULL");
}
System.out.println("-----Finger Table-----");
for(int i = 0; i < this.fingerTable.length; i++){
if(this.fingerTable[i] != null){
System.out.println("Entry :" + i);
System.out.println("Chord ID: " + this.fingerTable[i].chordID);
System.out.println("IP: " + this.fingerTable[i].IP.toString());
}
else{
System.out.println("Entry " + i + " is NULL");
}
}
}
public void help() {
System.out.println("Welcome to Chord.");
System.out.println("This member has id " + this.myInfo.chordID + " and max id length in bits (\"m\")" + this.myInfo.chordIDLength);
System.out.println("Enter \"add <filename>\" to add a file to the chord ring");
System.out.println("Enter \"search <filename>\" to retrieve a file in the chord ring");
System.out.println("Enter \"successor <key>\" to find the node following that key");
System.out.println("Enter \"help\" to view this menu again.");
}
/* Main workflow */
public static void main(String[] args){
//command line should take the format:
//java Member (My Receive Port) (Machine A IP) (Machine A Receive Port) (Machine B IP) ....
Member member = new Member(Integer.parseInt(args[0]));
for(int i = 1; i < args.length; i+=2){
MemberInfo newNeighbor = new MemberInfo(MemberInfo.parseIP(args[i]), Integer.parseInt(args[i+1]));
member.addFingerTableEntry(newNeighbor);
member.setPredecessor(newNeighbor);
member.setSuccessor(newNeighbor);
}
MemberInfo lastNonNull = member.fingerTable[0];
//No null finger table entries.
//If no members fit into the ID space (id + 2^i) mod 2^m,
//then the entry for (id + 2^(j)) mod 2^m, where j was the
//last non-null entry, will be duplicated
for(int i = 1; i < member.fingerTable.length; i++){
if(member.fingerTable[i] == null){
member.fingerTable[i] = lastNonNull;
}
else{
lastNonNull = member.fingerTable[i];
}
}
member.printFingerTable();
//We give the ReceivingSocket and Stabilizer handles on the invoking Member for when they
//need to invoke a process that alters the Member's connectivity info or open a SendingSocket
member.pool.execute(new ReceivingSocket(member));
member.pool.execute(new Stabilizer(member));
Scanner userInput = new Scanner(System.in);
String[] command;
// UI --
// Enter "add <filename>" to add a file to the chord ring
// Enter "search <filename>" to retrieve a file in the chord ring
// Enter "successor <key>" to find the node following that key
// Enter "help" to view this menu again.
while(true) {
System.out.println("Please enter a command");
command = userInput.nextLine().trim().split(" ");
if (command.length == 0) {
System.out.println("Please enter a valid command. Enter \"help\" to see commands");
}
if (command[0].equals("help")) {
member.help();
continue;
}
if (command[0].equals("quit")) {
break;
}
if (command.length != 2) {
System.out.println("Please enter a valid command. Enter \"help\" to see commands");
continue;
}
if (command[0].equals("add")) {
String filename = command[1];
int key = member.myInfo.generateChordID(filename);
member.addFile(new MyFile(filename, key), member.myInfo);
continue;
}
if (command[0].equals("search")) {
String filename = command[1];
int key = member.myInfo.generateChordID(filename);
try {
member.fileSearch(key, member.myInfo);
} catch (FileNotFoundException e){
System.out.println("Couldn't find the file " + filename + ". It should have been on this machine, but it isn't. Oops!");
}
continue;
}
if (command[0].equals("successor")) {
int key = Integer.parseInt(command[1]);
member.findSuccessor(key, member.myInfo);
continue;
}
}
userInput.close();
}
}