Repository navigation
Expand file tree
/
Copy pathReceivingSocket.java
More file actions
95 lines (80 loc) · 3.17 KB
/
Copy pathReceivingSocket.java
File metadata and controls
95 lines (80 loc) · 3.17 KB
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
import java.net.ServerSocket;
import java.net.Socket;
import java.io.ObjectInputStream;
import java.io.FileNotFoundException;
/* Receive all messages, process them, and handle lookup (file search or member search) logic. */
public class ReceivingSocket implements Runnable {
private Member myself;
public ReceivingSocket(Member me){
this.myself = me;
}
public void run() {
try {
ServerSocket listener = new ServerSocket(this.myself.myInfo.receivePort);
while(true) {
Socket newConnection = listener.accept();
ObjectInputStream in = new ObjectInputStream(newConnection.getInputStream());
Object inObject = in.readObject();
if (inObject instanceof RequestSuccessor) {
RequestSuccessor rs = (RequestSuccessor) inObject;
MemberInfo successor = myself.findSuccessor(rs.chordID, rs.sender);
if (successor == null) {
// do nothing -- findSuccessor already forwarded the message
}
else {
// send a requestSuccessorResponse to rs.sender with the successor
RequestSuccessorResponse message = new RequestSuccessorResponse(rs.chordID, successor, this.myself.myInfo, rs.sender);
this.myself.send(message);
}
}
else if (inObject instanceof RequestSuccessorResponse){
RequestSuccessorResponse rsr = (RequestSuccessorResponse) inObject;
System.out.println("The successor of " + rsr.chordID + " is " + rsr.successor.chordID);
}
else if (inObject instanceof RequestFile) {
RequestFile rf = (RequestFile) inObject;
try {
String file = myself.fileSearch(rf.key, rf.sender);
if(file == null) {
System.out.println("Forwarded..");
// message has been forwarded via finger table; do nothing
} else {
System.out.println("Sending requested file to " + rf.sender.chordID);
// send the file back
RequestFileResponse message = new RequestFileResponse(file, this.myself.myInfo, rf.sender);
this.myself.send(message);
}
}
catch (FileNotFoundException e) {
// no file is stored at the key --
// SEND A FILE NOT FOUND MESSAGE
}
}
else if (inObject instanceof RequestFileResponse) {
System.out.println(myself.myInfo.chordID + " got a request file response!");
//FileNotFoundResponse is a subclass of RequestFileResponse
if (inObject instanceof FileNotFoundResponse) {
FileNotFoundResponse fnfr = (FileNotFoundResponse) inObject;
System.out.println("File " + fnfr.filename + " could not be found. It should have been located at node " +
fnfr.sender.chordID + " , but it wasn't there. You can try again with a new file name, or add the file to the system. ");
}
else {
RequestFileResponse rfr = (RequestFileResponse) inObject;
System.out.println("File " + rfr.filename + " was found at " + rfr.sender.chordID + "!");
}
}
else if(inObject instanceof AddFileMessage){
AddFileMessage addRequest = (AddFileMessage) inObject;
this.myself.addFile(addRequest.file, addRequest.sender);
}
else {
System.err.println("Recieved an object of unknown message type");
}
}
}
catch(Exception e) {
System.err.println("Receive socket error");
e.printStackTrace();
}
}
}