-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionHandler.java
More file actions
62 lines (55 loc) · 1.67 KB
/
ConnectionHandler.java
File metadata and controls
62 lines (55 loc) · 1.67 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
package common;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;
import common.Message;
import common.MessageFactory;
import util.CommChannel;
public class ConnectionHandler implements Runnable
{
CommChannel channel;
LinkedBlockingQueue<Message> mailbox;
String remoteAddr;
public ConnectionHandler(Socket sock, LinkedBlockingQueue<Message> queue) {
channel = new CommChannel(sock);
mailbox = queue;
remoteAddr = sock.getRemoteSocketAddress().toString();
}
public ConnectionHandler(CommChannel chan, LinkedBlockingQueue<Message> queue) {
channel = chan;
mailbox = queue;
remoteAddr = chan.getRemoteAddr();
}
public void run(){
Message msg;
Object rcved;
int count = 0;
while(true) {
rcved = channel.read();
if(rcved instanceof Message) {
count = 0;
msg = (Message) rcved;
msg.setReply(channel);
try{
mailbox.put(msg);
}catch(InterruptedException e){
e.printStackTrace();
}
}
else{
count++;
}
if(count == 16) {
//Since the connection is most likely broken at this point
//a flag should probably be set.
msg = MessageFactory.droppedConnection(remoteAddr);
try{
mailbox.put(msg);
}catch(InterruptedException e){
e.printStackTrace();
}
return;
}
msg = null;
}
}
}