]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/method/Client.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / method / Client.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/method/Client.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/method/Client.java
new file mode 100644 (file)
index 0000000..2393337
--- /dev/null
@@ -0,0 +1,146 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.method;
+
+import java.io.IOException;\r
+import java.net.InetAddress;\r
+import java.net.InetSocketAddress;\r
+import java.net.Socket;\r
+\r
+import org.simantics.databoard.Methods;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.method.TcpConnection.ConnectionListener;\r
+import org.simantics.databoard.serialization.SerializationException;\r
+
+/**
+ * Proxy InterfaceBinding over a socket.
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class Client implements MethodInterface {
+       
+       Socket s;
+       TcpConnection c;
+
+       /**
+        * Create a new method interface client. On successful construction the \r
+        * connection is established over TCP/IP channel.
+        * 
+        * @param addr
+        * @param port
+        * @throws IOException connection error
+        * @throws SerializationException handshake communication error
+        * @throws BindingException handshake communication error
+        */
+       public Client(String addr, int port) 
+       throws IOException, SerializationException, BindingException {
+               this(InetAddress.getByName(addr), port);
+       }
+\r
+       /**\r
+        * Create a new method interface client. On successful construction the \r
+        * connection is established over TCP/IP channel.\r
+        * \r
+        * @param sa\r
+        * @throws IOException connection error\r
+        * @throws SerializationException handshake communication error\r
+        * @throws BindingException handshake communication error\r
+        */\r
+       public Client(InetSocketAddress sa) \r
+       throws IOException, SerializationException, BindingException {\r
+               s = new Socket(sa.getAddress(), sa.getPort());\r
+               Handshake local = new Handshake();\r
+               local.methods = Methods.noMethods().getInterface().getMethodDefinitions();\r
+               Handshake remote = TcpConnection.handshake(s, local);\r
+               c = new TcpConnection(s, Methods.noMethods(), local, remote);           \r
+       }\r
+
+       /**
+        * Create a new method interface client. On successful construction the \r
+        * connection is established over TCP/IP channel.\r
+        * 
+        * @param addr
+        * @param port
+        * @throws IOException connection error
+        * @throws SerializationException handshake communication error
+        * @throws BindingException handshake communication error
+        */
+       public Client(InetAddress addr, int port) 
+       throws IOException, SerializationException, BindingException {
+               s = new Socket(addr, port);
+               Handshake local = new Handshake();
+               local.methods = Methods.noMethods().getInterface().getMethodDefinitions();
+               Handshake remote = TcpConnection.handshake(s, local);
+               c = new TcpConnection(s, Methods.noMethods(), local, remote);           
+       }
+       
+       /**
+        * Create a new method interface client. On successful construction the \r
+        * connection is established over TCP/IP channel.\r
+        * 
+        * @param addr
+        * @param port
+        * @param localMethodHandler handles requests sent by the server
+        * @throws IOException
+        * @throws BindingException 
+        * @throws SerializationException 
+        */
+       public Client(InetAddress addr, int port, MethodInterface localMethodHandler) 
+       throws IOException, SerializationException, BindingException {
+               s = new Socket(addr, port);
+               Handshake local = new Handshake();
+               local.methods = localMethodHandler.getInterface().getMethodDefinitions();
+               Handshake remote = TcpConnection.handshake(s, local);
+               c = new TcpConnection(s, localMethodHandler, local, remote);            
+       }
+
+       public void setConnectListener(ConnectionListener listener) 
+       {
+               c.addConnectionListener(listener);
+       }
+       
+       /**
+        * Get remote method descriptions
+        */
+       @Override
+       public Interface getInterface() {
+               return c.getInterface();
+       }
+       
+       @Override
+       public Method getMethod(MethodTypeBinding binding)
+                       throws MethodNotSupportedException {
+               return c.getMethod(binding);
+       }
+       
+       @Override
+       public Method getMethod(MethodTypeDefinition description)
+                       throws MethodNotSupportedException {
+               return c.getMethod(description);
+       }\r
+       
+       public TcpConnection getConnection() 
+       {
+               return c;
+       }
+       
+       public void close()
+       {
+               c.close();
+               try {
+                       s.close();
+               } catch (IOException e) {
+               }
+       }
+       
+}
+