package uk.ac.vamsas.client.picking; import java.io.*; import java.net.*; import java.util.logging.*; class PickEndPoint extends Thread { private Logger logger = Logger.getLogger("uk.ac.vamsas.client.picking"); private Socket socket; private int rPort; private BufferedWriter os; private BufferedReader in; private SocketManager manager; PickEndPoint(SocketManager manager, Socket s) { this.manager = manager; socket = s; } boolean openConnection() { try { // Create the socket if it doesn't already exist if (socket == null) socket = new Socket(InetAddress.getLocalHost(), PickServer.PORT); rPort = socket.getPort(); socket.setKeepAlive(true); // Open the streams for reading/writing os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Start the thread to listen for incoming messages // logger.info("CLIENT: connection successful to port " // + socket.getPort() + " via port " + socket.getLocalPort()); start(); return true; } catch (Exception e) { //logger.info("CLIENT: connection failed: " + e); return false; } } void send(Message message) { try { String str = message.getRawMessage(); //logger.info("CLIENT: send " + str + " to " + rPort); os.write(str); // We use a newline to terminate the message os.newLine(); os.flush(); } catch (Exception e) { //logger.info("CLIENT: failed to send"); // TODO: terminate the connection on a failed send or retry? terminate(); } } // void receive() (threaded) public void run() { try { while (true) { String str = in.readLine(); //logger.info("CLIENT: recv " + str + " from " + rPort); // TODO: Spawn this off into the GUI Event-Dispatch thread... // Convert the string back into something API friendly Message message = strToMessage(str); // Ignore corrupted or unknown message types if (message != null) manager.processMessage(this, message); } } catch (Exception e) { // Means the other end of the connection has (probably died) so we need // terminate this endpoint (if this is server side) //logger.info("CLIENT: read failed: " + e); terminate(); } } private void terminate() { try { socket.close(); } catch (IOException e) {} //logger.info("CLIENT: closing connection to port " + socket.getPort()); manager.removeEndPoint(this); } private Message strToMessage(String str) { try { if (str.startsWith("CUSTOM")) return new CustomMessage(str.substring(6)); if (str.startsWith("MOUSEOVER")) return new MouseOverMessage(str); } catch (Exception e) { //logger.info("Unable to reconstruct message: " + e); } return null; } }