X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fexamples%2Forg%2Fsimantics%2Fdataboard%2Fexample%2Fold%2FRPCExample1.java;fp=bundles%2Forg.simantics.databoard%2Fexamples%2Forg%2Fsimantics%2Fdataboard%2Fexample%2Fold%2FRPCExample1.java;h=de6f6a2d99f3b77fb4df44c9b25acbbae8a672c4;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample1.java b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample1.java new file mode 100644 index 000000000..de6f6a2d9 --- /dev/null +++ b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample1.java @@ -0,0 +1,138 @@ +package org.simantics.databoard.example.old; +import java.io.IOException; +import java.net.Inet4Address; +import java.net.Socket; +import java.util.Arrays; + +import org.simantics.databoard.Methods; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.method.Client; +import org.simantics.databoard.method.MethodInterface; +import org.simantics.databoard.method.Server; +import org.simantics.databoard.method.TcpConnection; +import org.simantics.databoard.method.TcpConnection.ConnectionListener; +import org.simantics.databoard.serialization.SerializationException; + +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ + +/** + * In this example we create CommandProcessor + * + */ +public class RPCExample1 { + + public interface CommandProcessor { + int helloWorld(); +// void elProblem(int i); +// void elProblem(); + String execute(String command, String...args) throws Error1; + } + + public static class Error1 extends Exception { + + private static final long serialVersionUID = 1L; + + public String msg; + public Error1(String msg) { + this.msg = msg; + } + @Override + public String toString() { + return msg; + } + } + + public static void main(String[] args) throws IOException, BindingConstructionException, SerializationException, BindingException { + + ////// Server ////// + // Create service handler + CommandProcessor myProcessor = new CommandProcessor() { + public int helloWorld() { + TcpConnection connection = TcpConnection.getCurrentConnection(); + MethodInterface interfaceToClient = connection; + + Socket socket = connection.getSocket(); + System.out.println("Hello "+socket.getRemoteSocketAddress()); + + connection.addConnectionListener( new ConnectionListener() { + @Override + public void onError(Exception error) { + System.out.println("Error: "+error.getMessage()); + } + + @Override + public void onClosed() { + TcpConnection connection = TcpConnection.getCurrentConnection(); + Socket socket = connection.getSocket(); + System.out.println("Bye bye "+socket.getRemoteSocketAddress()); + }} ); + + return 0; + } + + public String execute(String command, String...args) throws Error1 + { + if (!command.equals("start")) throw new Error1("Unknown command "+command); + return "Program started "+command+" with args "+Arrays.toString(args); + } + +// public void elProblem() { +// } +// +// public void elProblem(int i) { +// } + }; + + // Adapt the server to a MethodInterface + MethodInterface myProcessorBinding = Methods.bindInterface(CommandProcessor.class, myProcessor); + + // Put the server on a socket + Server myServerSocket = new Server(8192, myProcessorBinding); + + ////// Client ////// + // Connect to the server socket + Client myClientSocket = new Client(Inet4Address.getByName("localhost"), 8192); + + try { + CommandProcessor myClient = Methods.createProxy(CommandProcessor.class, myClientSocket); + +// myClient.elProblem(); + + // Hello World + myClient.helloWorld(); + + // Execute command "start" + try { + String result = myClient.execute("start"); + System.out.println(result); + } catch (Error1 e) { + System.err.println(e); + } + + // Execute command "fault" + try { + String result = myClient.execute("fault"); + System.out.println(result); + } catch (Error1 e) { + System.err.println(e); + } + } finally { + myClientSocket.close(); + myServerSocket.close(); + } + + } + +} +