]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/method/Client.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / method / Client.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.method;
13
14 import java.io.IOException;
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17 import java.net.Socket;
18
19 import org.simantics.databoard.Methods;
20 import org.simantics.databoard.binding.error.BindingException;
21 import org.simantics.databoard.method.TcpConnection.ConnectionListener;
22 import org.simantics.databoard.serialization.SerializationException;
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 
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
49         /**
50          * Create a new method interface client. On successful construction the 
51          * connection is established over TCP/IP channel.
52          * 
53          * @param sa
54          * @throws IOException connection error
55          * @throws SerializationException handshake communication error
56          * @throws BindingException handshake communication error
57          */
58         public Client(InetSocketAddress sa) 
59         throws IOException, SerializationException, BindingException {
60                 s = new Socket(sa.getAddress(), sa.getPort());
61                 Handshake local = new Handshake();
62                 local.methods = Methods.noMethods().getInterface().getMethodDefinitions();
63                 Handshake remote = TcpConnection.handshake(s, local);
64                 c = new TcpConnection(s, Methods.noMethods(), local, remote);           
65         }
66
67         /**
68          * Create a new method interface client. On successful construction the 
69          * connection is established over TCP/IP channel.
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 
88          * connection is established over TCP/IP channel.
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         }
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