e4f44454a481c0ef2bebd30a6c0fc904978f6bae
[vamsas.git] / src / org / vamsas / client / picking / PickManager.java
1 package org.vamsas.client.picking;\r
2 \r
3 import java.net.*;\r
4 import java.util.*;\r
5 \r
6 public class PickManager\r
7 {\r
8         // Maintains a list of client communication objects - each object represents\r
9         // a way of talking to either:\r
10         //  the server - if this is client side (and in which case, the list will only contain one element\r
11         //  the other clients - if this is server side\r
12         private LinkedList clients;\r
13         \r
14         private PickServer server;\r
15         \r
16         public PickManager()\r
17         {\r
18                 server = new PickServer(this);\r
19                 clients = new LinkedList();\r
20                 \r
21                 new InitializeThread().start();\r
22         }\r
23         \r
24         // Called whenver we try to make a new endpoint<->endpoint connection\r
25         boolean addClientConnection(Socket socket)\r
26         {\r
27                 PickEndPoint client = new PickEndPoint(this, socket);\r
28                 \r
29                 if (client.openConnection())\r
30                 {\r
31                         clients.add(client);\r
32                         System.out.println("List now contains " + clients.size() + " client(s)");\r
33                         return true;\r
34                 }\r
35                 \r
36                 return false;\r
37         }\r
38         \r
39         public void sendMessage(String str)\r
40         {\r
41                 forwardMessage(null, str);\r
42         }\r
43         \r
44         // Forwards (or sends) a message\r
45         // When the server receives a message, it will be resent to all clients,\r
46         // but *not* to the client that sent it out in the first place!\r
47         private void forwardMessage(PickEndPoint origin, String str)\r
48         {\r
49                 ListIterator itor = clients.listIterator();\r
50                 while (itor.hasNext())\r
51                 {\r
52                         PickEndPoint client = (PickEndPoint) itor.next();\r
53                         \r
54                         if (client != origin)\r
55                                 client.send(str);\r
56                 }\r
57         }\r
58         \r
59         // Called by the endpoint code when a message is received\r
60         // The manager has to:\r
61         //  a) hand message to VAMSAS app\r
62         //  b) forward it to other clients if we're the server\r
63         void handleMessage(PickEndPoint origin, String str)\r
64         {\r
65                 if (server.isServer())\r
66                         forwardMessage(origin, str);\r
67                 \r
68                 // TODO: pass message to VAMSAS API\r
69         }\r
70         \r
71         void removeEndPoint(PickEndPoint client)\r
72         {\r
73                 clients.remove(client);\r
74                 System.out.println("List now contains " + clients.size() + " client(s)");\r
75                 \r
76                 // If there's no endpoints left, then we've lost all connections and\r
77                 // need to reinitialize\r
78                 if (clients.size() == 0)\r
79                         new InitializeThread().start();\r
80         }\r
81         \r
82         private class InitializeThread extends Thread\r
83         {\r
84                 public void run()\r
85                 {\r
86                         System.out.println("Initializing connection...");\r
87                         boolean connected = false;\r
88                         \r
89                         // Loop until we can get a connection (one way or the other)\r
90                         while (!connected)\r
91                         {\r
92                                 // Sleep for a rnd time so we don't end up with all the VAMSAS\r
93                                 // apps trying to initialize servers at the same time\r
94                                 try { Thread.sleep((int)Math.random()); }\r
95                                 catch (InterruptedException e) {}\r
96                                         \r
97                                 // Attempt to open the server port...\r
98                                 if (server.isServer() || server.createServer())\r
99                                         connected = true;\r
100 \r
101                                 // If it fails, then attempt to make a client connection...\r
102                                 else if (addClientConnection(null))\r
103                                         connected = true;\r
104                         }\r
105                 }\r
106         }\r
107 }