-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisconnectThread.java
More file actions
49 lines (43 loc) · 1.65 KB
/
DisconnectThread.java
File metadata and controls
49 lines (43 loc) · 1.65 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
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
public class DisconnectThread implements Runnable {
private Socket socket;
private final ChordNameServiceImpl cns;
private final DistributedTextEditor dte;
public DisconnectThread(DistributedTextEditor dte, ChordNameServiceImpl cns, Socket predecessor) {
socket = predecessor;
this.cns = cns;
this.dte = dte;
}
@Override
public void run() {
try {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(true) {
ObjectInputStream disconnectStream = new ObjectInputStream(socket.getInputStream());
Object de;
while ((de = disconnectStream.readObject()) != null) {
if (de instanceof DisconnectEvent) {
InetAddress newSuccessor = ((DisconnectEvent) de).getNewSuccessor();
cns.setSucSocket(new Socket(newSuccessor, cns.getChordName().getPort()));
dte.newEventPlayer(cns.getSucSocket(), cns.keyOfName(cns.getChordName()));
// New successor
socket = cns.getSucSocket();
} else if (de instanceof ConnectEvent) {
cns.setSucSocket(cns.getPreSocket());
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}