11import java .io .IOException ;
22import java .net .ServerSocket ;
33import java .net .Socket ;
4+ import java .util .concurrent .CopyOnWriteArrayList ;
45import java .util .concurrent .ExecutorService ;
56
67// From http://tutorials.jenkov.com/java-multithreaded-servers/thread-pooled-server.html
78
8- public class ThreadPoolServer implements Runnable {
9+ public class Server implements Runnable {
910
1011 protected ServerSocket serverSocket ;
1112 protected Thread runningThread ;
12- protected ExecutorService threadPool ;
13-
13+ protected CopyOnWriteArrayList < Client > clients ;
14+ private Client localClient ;
1415
1516 protected int serverPort = 9000 ;
1617 protected boolean isStopped = false ;
17-
18- public ThreadPoolServer (int port , ExecutorService threadPool ) {
18+ private ExecutorService threadPool ;
19+
20+ public Server (int port , ExecutorService threadPool ) {
1921 this .serverPort = port ;
2022 this .threadPool = threadPool ;
23+ this .clients = new CopyOnWriteArrayList <Client >();
24+ this .localClient = new Client (threadPool );
2125 }
2226
2327 @ Override
2428 public void run () {
25- synchronized (this ) {
29+ synchronized (this ) {
2630 this .runningThread = Thread .currentThread ();
2731 }
2832 this .openServerSocket ();
29- while (! this .isStopped ()) {
33+ while (!this .isStopped ()) {
3034 Socket clientSocket = null ;
3135 try {
3236 clientSocket = this .serverSocket .accept ();
@@ -36,21 +40,35 @@ public void run() {
3640 }
3741 throw new RuntimeException ("Error accepting client connection" + e );
3842 }
39- this .threadPool .execute (new WorkerRunnable (clientSocket , "Thread Pooled Server" ));
43+ Client client = new Client (clientSocket , this .localClient , this .threadPool );
44+ synchronized (this .clients ) {
45+ this .clients .add (client );
46+ }
47+ new Thread (client ).start ();
4048 }
41- this .threadPool .shutdown ();
4249 System .out .println ("Server Stopped" );
4350 }
4451
4552 public synchronized void stop () {
4653 this .isStopped = true ;
54+ for (Client client : clients ) {
55+ client .stop ();
56+ }
4757 try {
4858 this .serverSocket .close ();
4959 } catch (IOException e ) {
5060 throw new RuntimeException ("Error closing server" , e );
5161 }
5262 }
5363
64+ public Client getLocalClient () {
65+ return this .localClient ;
66+ }
67+
68+ public void updateClients () {
69+ this .threadPool .execute (new UpdateClients (this .clients , this .localClient ));
70+ }
71+
5472 private synchronized boolean isStopped () {
5573 return this .isStopped ;
5674 }
@@ -62,5 +80,27 @@ private void openServerSocket() {
6280 throw new RuntimeException ("Cannot open port " + this .serverPort + ":" + e );
6381 }
6482 }
83+
84+ /*
85+ * TODO: For now just updating all clients with numIter
86+ */
87+ private class UpdateClients implements Runnable {
88+
89+ private CopyOnWriteArrayList <Client > clients ;
90+ private Client localState ;
91+
92+ public UpdateClients (CopyOnWriteArrayList <Client > clients , Client localState ) {
93+ this .clients = clients ;
94+ this .localState = localState ;
95+ }
96+
97+ @ Override
98+ public void run () {
99+ for (Client client : this .clients ) {
100+ client .write (this .localState .getNumIter ());
101+ }
102+ }
103+
104+ }
65105
66106}
0 commit comments