1 package org.simantics.databoard.example.old;
2 import java.io.IOException;
3 import java.net.Inet4Address;
4 import java.net.Socket;
5 import java.util.Arrays;
7 import org.simantics.databoard.Methods;
8 import org.simantics.databoard.binding.error.BindingConstructionException;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.method.Client;
11 import org.simantics.databoard.method.MethodInterface;
12 import org.simantics.databoard.method.Server;
13 import org.simantics.databoard.method.TcpConnection;
14 import org.simantics.databoard.method.TcpConnection.ConnectionListener;
15 import org.simantics.databoard.serialization.SerializationException;
17 /*******************************************************************************
18 * Copyright (c) 2010 Association for Decentralized Information Management in
20 * All rights reserved. This program and the accompanying materials
21 * are made available under the terms of the Eclipse Public License v1.0
22 * which accompanies this distribution, and is available at
23 * http://www.eclipse.org/legal/epl-v10.html
26 * VTT Technical Research Centre of Finland - initial API and implementation
27 *******************************************************************************/
30 * In this example we create CommandProcessor
33 public class RPCExample1 {
35 public interface CommandProcessor {
37 // void elProblem(int i);
39 String execute(String command, String...args) throws Error1;
42 public static class Error1 extends Exception {
44 private static final long serialVersionUID = 1L;
47 public Error1(String msg) {
51 public String toString() {
56 public static void main(String[] args) throws IOException, BindingConstructionException, SerializationException, BindingException {
59 // Create service handler
60 CommandProcessor myProcessor = new CommandProcessor() {
61 public int helloWorld() {
62 TcpConnection connection = TcpConnection.getCurrentConnection();
63 MethodInterface interfaceToClient = connection;
65 Socket socket = connection.getSocket();
66 System.out.println("Hello "+socket.getRemoteSocketAddress());
68 connection.addConnectionListener( new ConnectionListener() {
70 public void onError(Exception error) {
71 System.out.println("Error: "+error.getMessage());
75 public void onClosed() {
76 TcpConnection connection = TcpConnection.getCurrentConnection();
77 Socket socket = connection.getSocket();
78 System.out.println("Bye bye "+socket.getRemoteSocketAddress());
84 public String execute(String command, String...args) throws Error1
86 if (!command.equals("start")) throw new Error1("Unknown command "+command);
87 return "Program started "+command+" with args "+Arrays.toString(args);
90 // public void elProblem() {
93 // public void elProblem(int i) {
97 // Adapt the server to a MethodInterface
98 MethodInterface myProcessorBinding = Methods.bindInterface(CommandProcessor.class, myProcessor);
100 // Put the server on a socket
101 Server myServerSocket = new Server(8192, myProcessorBinding);
104 // Connect to the server socket
105 Client myClientSocket = new Client(Inet4Address.getByName("localhost"), 8192);
108 CommandProcessor myClient = Methods.createProxy(CommandProcessor.class, myClientSocket);
110 // myClient.elProblem();
113 myClient.helloWorld();
115 // Execute command "start"
117 String result = myClient.execute("start");
118 System.out.println(result);
120 System.err.println(e);
123 // Execute command "fault"
125 String result = myClient.execute("fault");
126 System.out.println(result);
128 System.err.println(e);
131 myClientSocket.close();
132 myServerSocket.close();