From c5bfacbc9c945f61d1ca38cfd6432e8e039fd6d5 Mon Sep 17 00:00:00 2001 From: imilne Date: Wed, 13 Dec 2006 17:03:37 +0000 Subject: [PATCH] First check-in of the picking api. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@253 be28352e-c001-0410-b1a7-c7978e42abec --- src/org/vamsas/client/picking/PickEndPoint.java | 77 +++++++++++++++++++++++ src/org/vamsas/client/picking/PickManager.java | 16 +++++ src/org/vamsas/client/picking/PickServer.java | 52 +++++++++++++++ src/org/vamsas/client/picking/TestApp.java | 26 ++++++++ 4 files changed, 171 insertions(+) create mode 100644 src/org/vamsas/client/picking/PickEndPoint.java create mode 100644 src/org/vamsas/client/picking/PickManager.java create mode 100644 src/org/vamsas/client/picking/PickServer.java create mode 100644 src/org/vamsas/client/picking/TestApp.java diff --git a/src/org/vamsas/client/picking/PickEndPoint.java b/src/org/vamsas/client/picking/PickEndPoint.java new file mode 100644 index 0000000..c81b744 --- /dev/null +++ b/src/org/vamsas/client/picking/PickEndPoint.java @@ -0,0 +1,77 @@ +package org.vamsas.client.picking; + +import java.io.*; +import java.net.*; + +class PickEndPoint extends Thread +{ + private Socket socket; + private BufferedWriter os; + private BufferedReader in; + + // Constructs a new client connection (client->server) + PickEndPoint() + throws Exception + { + // Attempt to connect... + // If we fail, wrapper has to attempt to make server instead?? + // Or keep trying until a server exists + socket = new Socket(InetAddress.getLocalHost(), PickServer.PORT); + + createStreams(); + } + + // Constructs a new client based on an existing connection (server->client) + PickEndPoint(Socket s) + throws IOException + { + socket = s; + createStreams(); + } + + private void createStreams() + throws IOException + { + os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); + + in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + start(); + } + + void send() + { + try + { + System.out.println("CLIENT: sending message..."); + os.write("test message: " + (System.currentTimeMillis())); + + // We use a newline to terminate the message + os.newLine(); + os.flush(); + System.out.println("CLIENT: message sent"); + } + catch (Exception e) + { + System.out.println(e); + } + } + + public void run() + { + try { + while (true) + { + String str = in.readLine(); + + System.out.println("CLIENT: READ: " + str); + } + } + catch (Exception e) + { + System.out.println("CLIENT: read fail: " + e); + // Means the other end of the connection has (probably died) so we need + // terminate this endpoint (if this is server side) + } + } +} \ No newline at end of file diff --git a/src/org/vamsas/client/picking/PickManager.java b/src/org/vamsas/client/picking/PickManager.java new file mode 100644 index 0000000..6e93955 --- /dev/null +++ b/src/org/vamsas/client/picking/PickManager.java @@ -0,0 +1,16 @@ +package org.vamsas.client.picking; + +import java.util.*; + +public class PickManager +{ + // Maintains a list of client communication objects - each object represents + // a way of talking to either: + // the server - if this is client side (and in which case, the list will only contain one element + // the other clients - if this is server side + private LinkedList clients = new LinkedList(); + + public PickManager() + { + } +} \ No newline at end of file diff --git a/src/org/vamsas/client/picking/PickServer.java b/src/org/vamsas/client/picking/PickServer.java new file mode 100644 index 0000000..f483b05 --- /dev/null +++ b/src/org/vamsas/client/picking/PickServer.java @@ -0,0 +1,52 @@ +package org.vamsas.client.picking; + +import java.io.*; +import java.net.*; + +class PickServer extends Thread +{ + static final int PORT = 53782; + + private ServerSocket serverSocket; + + PickServer() + { + } + + boolean createServer() + { + try + { + serverSocket = new ServerSocket(PORT); + start(); + + return true; + } + catch (IOException e) + { + // If we fail, just assume another app already has the port + System.out.println(e); + return false; + } + } + + public void run() + { + System.out.println("SERVER: listening..."); + + // Loop forever, accepting connectons from other clients + while (true) + { + try + { + Socket socket = serverSocket.accept(); + + System.out.println("SERVER: connection made, spawning pipe..."); + + // Do something with the socket... + new PickEndPoint(socket); + } + catch (IOException e) {} + } + } +} \ No newline at end of file diff --git a/src/org/vamsas/client/picking/TestApp.java b/src/org/vamsas/client/picking/TestApp.java new file mode 100644 index 0000000..3e89ee8 --- /dev/null +++ b/src/org/vamsas/client/picking/TestApp.java @@ -0,0 +1,26 @@ +package org.vamsas.client.picking; + +public class TestApp +{ + public static void main(String[] args) + throws Exception + { + TestApp app = new TestApp(); + + PickServer server = new PickServer(); + + + if (server.createServer() == false) + { + PickEndPoint client = new PickEndPoint(); + + for (int i = 0; i < 10; i++) + client.send(); +// client.send(); + } + } + + public TestApp() + { + } +} \ No newline at end of file -- 1.7.10.2