]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.method;
13
14 import java.io.IOException;\r
15 import java.net.InetAddress;\r
16 import java.net.InetSocketAddress;\r
17 import java.net.Socket;\r
18 \r
19 import org.simantics.databoard.Methods;\r
20 import org.simantics.databoard.binding.error.BindingException;\r
21 import org.simantics.databoard.method.TcpConnection.ConnectionListener;\r
22 import org.simantics.databoard.serialization.SerializationException;\r
23
24 /**
25  * Proxy InterfaceBinding over a socket.
26  *
27  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28  */
29 public class Client implements MethodInterface {
30         
31         Socket s;
32         TcpConnection c;
33
34         /**
35          * Create a new method interface client. On successful construction the \r
36          * connection is established over TCP/IP channel.
37          * 
38          * @param addr
39          * @param port
40          * @throws IOException connection error
41          * @throws SerializationException handshake communication error
42          * @throws BindingException handshake communication error
43          */
44         public Client(String addr, int port) 
45         throws IOException, SerializationException, BindingException {
46                 this(InetAddress.getByName(addr), port);
47         }
48 \r
49         /**\r
50          * Create a new method interface client. On successful construction the \r
51          * connection is established over TCP/IP channel.\r
52          * \r
53          * @param sa\r
54          * @throws IOException connection error\r
55          * @throws SerializationException handshake communication error\r
56          * @throws BindingException handshake communication error\r
57          */\r
58         public Client(InetSocketAddress sa) \r
59         throws IOException, SerializationException, BindingException {\r
60                 s = new Socket(sa.getAddress(), sa.getPort());\r
61                 Handshake local = new Handshake();\r
62                 local.methods = Methods.noMethods().getInterface().getMethodDefinitions();\r
63                 Handshake remote = TcpConnection.handshake(s, local);\r
64                 c = new TcpConnection(s, Methods.noMethods(), local, remote);           \r
65         }\r
66
67         /**
68          * Create a new method interface client. On successful construction the \r
69          * connection is established over TCP/IP channel.\r
70          * 
71          * @param addr
72          * @param port
73          * @throws IOException connection error
74          * @throws SerializationException handshake communication error
75          * @throws BindingException handshake communication error
76          */
77         public Client(InetAddress addr, int port) 
78         throws IOException, SerializationException, BindingException {
79                 s = new Socket(addr, port);
80                 Handshake local = new Handshake();
81                 local.methods = Methods.noMethods().getInterface().getMethodDefinitions();
82                 Handshake remote = TcpConnection.handshake(s, local);
83                 c = new TcpConnection(s, Methods.noMethods(), local, remote);           
84         }
85         
86         /**
87          * Create a new method interface client. On successful construction the \r
88          * connection is established over TCP/IP channel.\r
89          * 
90          * @param addr
91          * @param port
92          * @param localMethodHandler handles requests sent by the server
93          * @throws IOException
94          * @throws BindingException 
95          * @throws SerializationException 
96          */
97         public Client(InetAddress addr, int port, MethodInterface localMethodHandler) 
98         throws IOException, SerializationException, BindingException {
99                 s = new Socket(addr, port);
100                 Handshake local = new Handshake();
101                 local.methods = localMethodHandler.getInterface().getMethodDefinitions();
102                 Handshake remote = TcpConnection.handshake(s, local);
103                 c = new TcpConnection(s, localMethodHandler, local, remote);            
104         }
105
106         public void setConnectListener(ConnectionListener listener) 
107         {
108                 c.addConnectionListener(listener);
109         }
110         
111         /**
112          * Get remote method descriptions
113          */
114         @Override
115         public Interface getInterface() {
116                 return c.getInterface();
117         }
118         
119         @Override
120         public Method getMethod(MethodTypeBinding binding)
121                         throws MethodNotSupportedException {
122                 return c.getMethod(binding);
123         }
124         
125         @Override
126         public Method getMethod(MethodTypeDefinition description)
127                         throws MethodNotSupportedException {
128                 return c.getMethod(description);
129         }\r
130         
131         public TcpConnection getConnection() 
132         {
133                 return c;
134         }
135         
136         public void close()
137         {
138                 c.close();
139                 try {
140                         s.close();
141                 } catch (IOException e) {
142                 }
143         }
144         
145 }
146