-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionListener.java
More file actions
51 lines (45 loc) · 1.32 KB
/
ConnectionListener.java
File metadata and controls
51 lines (45 loc) · 1.32 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
package common;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;
import common.Message;
import common.MessageFactory;
import util.CommChannel;
public class ConnectionListener implements Runnable
{
ServerSocket listen;
LinkedBlockingQueue<Message> mailbox;
public ConnectionListener(int port, LinkedBlockingQueue<Message> queue)
{
try{
listen = new ServerSocket(port);
} catch(IOException e) {
e.printStackTrace();
}
mailbox = queue;
}
private void tellController(CommChannel incoming){
Message newConnection = MessageFactory.newConnection(incoming);
try{
mailbox.put(newConnection);
} catch(InterruptedException e){
e.printStackTrace();
}
}
public void run()
{
int i = 0;
while(true)
{
try{
CommChannel incoming = new CommChannel(listen.accept());
tellController(incoming);
ConnectionHandler connection = new ConnectionHandler(incoming, mailbox);
new Thread(connection, "ConnectionHandler " + i).start();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}