First check-in of the picking api.
authorimilne <imilne@issues.jalview.org>
Wed, 13 Dec 2006 17:03:37 +0000 (17:03 +0000)
committerimilne <imilne@issues.jalview.org>
Wed, 13 Dec 2006 17:03:37 +0000 (17:03 +0000)
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 [new file with mode: 0644]
src/org/vamsas/client/picking/PickManager.java [new file with mode: 0644]
src/org/vamsas/client/picking/PickServer.java [new file with mode: 0644]
src/org/vamsas/client/picking/TestApp.java [new file with mode: 0644]

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