]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / RPCExample2.java
diff --git a/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample2.java b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample2.java
new file mode 100644 (file)
index 0000000..9f8862b
--- /dev/null
@@ -0,0 +1,115 @@
+package org.simantics.databoard.example.old;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.simantics.databoard.Methods;
+import org.simantics.databoard.binding.error.BindingConstructionException;
+import org.simantics.databoard.method.MethodInterface;
+
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+
+public class RPCExample2 {
+
+       public static class Server {
+       
+               // Server Code
+               public interface NetworkProtocol {
+                       void elProblem();
+                       String execute(String command, int[] args) throws Error1;
+               }
+
+               public static class Error1 extends Exception {
+                       
+                       private static final long serialVersionUID = 1L;
+                       
+                       public String errorMessage;
+                       public Error1(String msg) {
+                               this.errorMessage = msg;
+                       }
+                       @Override
+                       public String toString() {
+                               return errorMessage;
+                       }
+               }
+
+       }
+       
+       // Client Code
+       public static class Client {
+               public interface NetworkProtocol {
+                       String execute(String cmd, int[] args) throws Error1;
+               }
+       
+               public static class Error1 extends Exception {          
+                       
+                       private static final long serialVersionUID = 1L;
+                       
+                       public String errorMessage;
+                       public Error1(String errorMessage) {
+                               this.errorMessage = errorMessage;
+                       }
+                       @Override
+                       public String toString() {
+                               return "Error occured: "+errorMessage;
+                       }
+               }
+       }
+       
+       // Example Code 
+       
+       public static void main(String[] args) throws IOException, BindingConstructionException {
+
+               // Server
+               Server.NetworkProtocol myServer = new Server.NetworkProtocol() {
+                       @Override
+                       public String execute(String command, int[] args) throws Server.Error1 {
+                               if (!command.equals("start")) throw new Server.Error1("Unknown command "+command);
+                               return "Program started "+command+" with args "+Arrays.toString(args);
+                       }
+
+                       @Override
+                       public void elProblem() {
+                               // TODO Auto-generated method stub
+                               System.out.println("Works");
+                               
+                       }                                       
+               };
+               
+               // Adapt server to MethodInterface
+               MethodInterface myMI = Methods.bindInterface(Server.NetworkProtocol.class, myServer);
+               
+               
+               // Client               
+               // Adapt methodInterface to client
+               Client.NetworkProtocol myMiProxy = Methods.createProxy(Client.NetworkProtocol.class, myMI);
+               
+               // Execute command "start"
+               try {
+                       String result = myMiProxy.execute("start", new int[] {5,6,72,7423});
+                       System.out.println(result);
+               } catch (Client.Error1 e) {
+                       System.err.println(e);
+               }
+
+               // Execute command "fault"
+               try {
+                       String result = myMiProxy.execute("fault", new int[] {});
+                       System.out.println(result);
+               } catch (Client.Error1 e) {
+                       System.err.println(e);
+               }
+               
+       }
+       
+}
+