1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.method;
14 import java.io.IOException;
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17 import java.net.Socket;
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;
25 * Proxy InterfaceBinding over a socket.
27 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
29 public class Client implements MethodInterface {
35 * Create a new method interface client. On successful construction the
36 * connection is established over TCP/IP channel.
40 * @throws IOException connection error
41 * @throws SerializationException handshake communication error
42 * @throws BindingException handshake communication error
44 public Client(String addr, int port)
45 throws IOException, SerializationException, BindingException {
46 this(InetAddress.getByName(addr), port);
50 * Create a new method interface client. On successful construction the
51 * connection is established over TCP/IP channel.
54 * @throws IOException connection error
55 * @throws SerializationException handshake communication error
56 * @throws BindingException handshake communication error
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);
68 * Create a new method interface client. On successful construction the
69 * connection is established over TCP/IP channel.
73 * @throws IOException connection error
74 * @throws SerializationException handshake communication error
75 * @throws BindingException handshake communication error
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);
87 * Create a new method interface client. On successful construction the
88 * connection is established over TCP/IP channel.
92 * @param localMethodHandler handles requests sent by the server
94 * @throws BindingException
95 * @throws SerializationException
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);
106 public void setConnectListener(ConnectionListener listener)
108 c.addConnectionListener(listener);
112 * Get remote method descriptions
115 public Interface getInterface() {
116 return c.getInterface();
120 public Method getMethod(MethodTypeBinding binding)
121 throws MethodNotSupportedException {
122 return c.getMethod(binding);
126 public Method getMethod(MethodTypeDefinition description)
127 throws MethodNotSupportedException {
128 return c.getMethod(description);
131 public TcpConnection getConnection()
141 } catch (IOException e) {