-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
48 lines (38 loc) · 1.17 KB
/
Controller.java
File metadata and controls
48 lines (38 loc) · 1.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
package common;
import java.io.*;
import java.lang.Thread;
import java.net.*;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import util.CommChannel;
public abstract class Controller
{
private ServerSocket listener;
protected LinkedList<CommChannel> children;
protected LinkedBlockingQueue<Message> mailbox;
protected MessageFactory msgFactory;
abstract public void handleMessage(Message msg);
public Controller(int port){
mailbox = new LinkedBlockingQueue<Message>();
children = new LinkedList<CommChannel>();
ConnectionListener listenLoop = new ConnectionListener(port, mailbox);
new Thread(listenLoop, "Socket Listener").start();
}
public void checkMessages(){
Message msg;
while(true){
try{
msg = mailbox.take();
handleMessage(msg);
} catch(InterruptedException e) {
//Don't thhink this needs to be worried about;
e.printStackTrace();
}
}
}
protected void broadcast(Message msg){
for( CommChannel s: children ){
s.write(msg);
}
}
}