1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.method;
14 import java.io.IOException;
\r
15 import java.net.InetAddress;
\r
16 import java.net.InetSocketAddress;
\r
17 import java.net.Socket;
\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
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
\r
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
\r
51 * connection is established over TCP/IP channel.
\r
54 * @throws IOException connection error
\r
55 * @throws SerializationException handshake communication error
\r
56 * @throws BindingException handshake communication error
\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
68 * Create a new method interface client. On successful construction the
\r
69 * connection is established over TCP/IP channel.
\r
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
\r
88 * connection is established over TCP/IP channel.
\r
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) {