1 package uk.ac.vamsas.client.picking;
\r
5 import java.util.logging.*;
\r
7 import org.apache.commons.logging.Log;
\r
9 class PickEndPoint extends Thread
\r
11 private Log logger = org.apache.commons.logging.LogFactory.getLog(uk.ac.vamsas.client.picking.PickEndPoint.class);
\r
13 private Socket socket;
\r
15 private BufferedWriter os;
\r
16 private BufferedReader in;
\r
18 * reference to server or null if no comms session active
\r
20 private SocketManager manager;
\r
22 PickEndPoint(SocketManager manager, Socket s)
\r
24 this.manager = manager;
\r
28 boolean openConnection()
\r
32 // Create the socket if it doesn't already exist
\r
34 socket = new Socket(InetAddress.getLocalHost(), PickServer.PORT);
\r
36 rPort = socket.getPort();
\r
37 socket.setKeepAlive(true);
\r
39 // Open the streams for reading/writing
\r
40 os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
\r
41 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
\r
43 // Start the thread to listen for incoming messages
\r
44 logger.debug("CLIENT: connection successful to port "
\r
45 + socket.getPort() + " via port " + socket.getLocalPort());
\r
52 logger.info("CLIENT: connection failed: " + e);
\r
57 void send(Message message)
\r
61 String str = message.getRawMessage();
\r
63 //logger.info("CLIENT: send " + str + " to " + rPort);
\r
66 // We use a newline to terminate the message
\r
72 //logger.info("CLIENT: failed to send");
\r
74 // TODO: terminate the connection on a failed send or retry? (Has this been done? JBP)
\r
79 // void receive() (threaded)
\r
84 while (manager!=null)
\r
86 String str = in.readLine();
\r
87 //logger.info("CLIENT: recv " + str + " from " + rPort);
\r
89 // TODO: Spawn this off into the GUI Event-Dispatch thread...
\r
91 // Convert the string back into something API friendly
\r
92 Message message = strToMessage(str);
\r
94 // Ignore corrupted or unknown message types
\r
95 if (message != null)
\r
96 manager.processMessage(this, message);
\r
101 // Means the other end of the connection has (probably died) so we need
\r
102 // terminate this endpoint (if this is server side)
\r
103 //logger.info("CLIENT: read failed: " + e);
\r
105 terminate(); // this may not be necessary - probably caused infinite loop on terminate() without null checks
\r
111 SocketManager mgr=manager;
\r
112 manager=null; // stops receive thread
\r
113 if (socket!=null) {
\r
114 try { socket.close(); }
\r
115 catch (IOException e) {}
\r
118 //logger.info("CLIENT: closing connection to port " + socket.getPort());
\r
119 // terminate() can be called by the socket manager to shutdown the connection,
\r
120 // or the receive thread after an exception has been received
\r
121 // (which includes the above case when the socket is closed.
\r
122 // so we only want to removeEndPoint once - for the initial call to terminate()
\r
124 mgr.removeEndPoint(this);
\r
127 private Message strToMessage(String str)
\r
131 if (str==null || str.length()==1)
\r
133 if (str.startsWith("CUSTOM"))
\r
134 return new CustomMessage(str.substring(6));
\r
136 if (str.startsWith("MOUSEOVER"))
\r
137 return new MouseOverMessage(str);
\r
139 catch (Exception e)
\r
141 //logger.info("Unable to reconstruct message: " + e);
\r