import java.net.*;\r
import java.util.logging.*;\r
\r
+import org.apache.commons.logging.Log;\r
+\r
class PickEndPoint extends Thread\r
{\r
- private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking");\r
+ private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.PickEndPoint.class);\r
\r
private Socket socket;\r
private int rPort;\r
terminate();\r
}\r
}\r
- \r
+ /**\r
+ * state of server thread - running or not running\r
+ */\r
+ private boolean running=false;\r
+ /**\r
+ * condition for server thread to stop\r
+ */\r
+ private boolean enabled=true;\r
+ public void haltEndpoint() {\r
+ // TODO: FIX THIS METHOD\r
+ if (!running || !enabled) {\r
+ logger.debug("PickEndpoint is not running or already requested to halt.");\r
+ return;\r
+ }\r
+ logger.debug("Disabling pick endpoint.");\r
+ enabled=false;\r
+ terminate();\r
+ while (running) {\r
+ try {\r
+ Thread.sleep(1);\r
+ } catch (Exception e) {\r
+ }\r
+ }\r
+ logger.debug("Pick endpoint has stopped.");\r
+ }\r
+\r
// void receive() (threaded)\r
public void run()\r
{\r
+ enabled=true;\r
+ running=true;\r
try\r
{\r
- while (true)\r
+ while (enabled)\r
{\r
String str = in.readLine(); \r
//logger.info("CLIENT: recv " + str + " from " + rPort);\r
// TODO: Spawn this off into the GUI Event-Dispatch thread...\r
\r
// Convert the string back into something API friendly\r
+ if (str!=null) {\r
Message message = strToMessage(str);\r
\r
// Ignore corrupted or unknown message types\r
if (message != null) \r
manager.processMessage(this, message);\r
+ }\r
}\r
}\r
catch (Exception e)\r
{\r
- // Means the other end of the connection has (probably died) so we need\r
- // terminate this endpoint (if this is server side)\r
- //logger.info("CLIENT: read failed: " + e);\r
- \r
- terminate();\r
- }\r
+ // Means the other end of the connection has (probably) died\r
+ logger.debug("Exception in receiver loop: "+e.getMessage());\r
+ }\r
+ // terminate this endpoint (if this is server side)\r
+ //logger.info("CLIENT: read failed: " + e);\r
+ \r
+ terminate();\r
+ \r
+ running=false;\r
}\r
\r
private void terminate()\r
{\r
- try { socket.close(); }\r
+ try { \r
+ if (socket.isConnected())\r
+ socket.close(); \r
+ }\r
catch (IOException e) {}\r
\r
//logger.info("CLIENT: closing connection to port " + socket.getPort());\r
\r
private Message strToMessage(String str)\r
{\r
+ // TODO: generalize message class \r
try\r
{\r
if (str.startsWith("CUSTOM"))\r
import java.net.*;\r
import java.util.logging.*;\r
\r
+import org.apache.commons.logging.Log;\r
+\r
/**\r
* Server class that listens for incoming connections on a predefined port.\r
*/\r
class PickServer extends Thread\r
{\r
- private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking");\r
+ private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.PickServer.class);\r
\r
// The port number we'll listen on\r
static final int PORT = 53782;\r
return false;\r
}\r
}\r
- \r
+ /**\r
+ * state of server thread - running or not running\r
+ */\r
+ private boolean running=false;\r
+ /**\r
+ * condition for server thread to stop\r
+ */\r
+ private boolean enabled=true;\r
+ public void haltServer() {\r
+ // TODO: FIX THIS METHOD\r
+ if (!running || !enabled) {\r
+ logger.debug("PickServer is not running or already requested to halt.");\r
+ return;\r
+ }\r
+ logger.debug("Disabling pick server.");\r
+ enabled=false;\r
+ while (running) {\r
+ try {\r
+ serverSocket.close();\r
+ } catch (Exception e) {\r
+ \r
+ }\r
+ try {\r
+ Thread.sleep(5);\r
+ } catch (Exception e) {\r
+ }\r
+ }\r
+ logger.debug("Pick server has stopped.");\r
+ }\r
/**\r
* Thread listening method - loops indefinitely listening for connections.\r
* When one is received, the socket object is passed to the manager so it\r
*/\r
public void run()\r
{\r
+ enabled=true;\r
+ running=true;\r
//logger.info("SERVER: listening on " + PORT + " - SERVER");\r
\r
// Loop forever, accepting connectons from other clients\r
- // TODO: add in the ability to terminate the server if a VAMSAS session\r
- // is ended\r
- while (true)\r
+ // TODO: terminate the server when haltServer is called (fails tests currently) \r
+ while (enabled)\r
{\r
try\r
{\r
}\r
catch (IOException e) {}\r
}\r
+ running=false;\r
+ manager.haltManager();\r
+ \r
}\r
}
\ No newline at end of file
import java.util.*;\r
import java.util.logging.*;\r
\r
+import org.apache.commons.logging.Log;\r
+\r
/**\r
* Concrete implementation of the IPickManager interface that uses sockets for\r
* message communication. An instance of this class attempts to run the central\r
*/\r
public class SocketManager implements IPickManager\r
{\r
- private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking");\r
+ private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.SocketManager.class);\r
\r
// Maintains a list of client communication objects - each object represents\r
// a way of talking to either:\r
}\r
}\r
}\r
+ public void haltManager() {\r
+ // TODO: FIX this method\r
+ if (server==null)\r
+ throw new Error("Client Implementation Error: haltManager called on uninitialized SocketManager.");\r
+ logger.debug("Halting PickManager threads...");\r
+ while (clients.size()>0) {\r
+ ((PickEndPoint)clients.getFirst()).haltEndpoint();\r
+ }\r
+ server.haltServer();\r
+ \r
+ logger.debug("Halted PickManager threads.");\r
+ \r
+ }\r
}
\ No newline at end of file