]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample1.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / RPCExample1.java
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;
6
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;
16
17 /*******************************************************************************
18  *  Copyright (c) 2010 Association for Decentralized Information Management in
19  *  Industry THTH ry.
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
24  *
25  *  Contributors:
26  *      VTT Technical Research Centre of Finland - initial API and implementation
27  *******************************************************************************/
28
29 /**
30  * In this example we create CommandProcessor
31  * 
32  */
33 public class RPCExample1 {
34
35         public interface CommandProcessor {
36                 int helloWorld();
37 //              void elProblem(int i);
38 //              void elProblem();
39                 String execute(String command, String...args) throws Error1;
40         }
41
42         public static class Error1 extends Exception {
43                 
44                 private static final long serialVersionUID = 1L;
45                 
46                 public String msg;
47                 public Error1(String msg) {
48                         this.msg = msg;
49                 }
50                 @Override
51                 public String toString() {
52                         return msg;
53                 }
54         }
55         
56         public static void main(String[] args) throws IOException, BindingConstructionException, SerializationException, BindingException {
57
58                 ////// Server //////
59                 // Create service handler
60                 CommandProcessor myProcessor = new CommandProcessor() {
61                         public int helloWorld() {
62                                 TcpConnection connection = TcpConnection.getCurrentConnection();
63                                 MethodInterface interfaceToClient = connection;
64                                 
65                                 Socket socket = connection.getSocket();
66                                 System.out.println("Hello "+socket.getRemoteSocketAddress());
67                                 
68                                 connection.addConnectionListener( new ConnectionListener() {
69                                         @Override
70                                         public void onError(Exception error) {
71                                                 System.out.println("Error: "+error.getMessage());
72                                         }
73
74                                         @Override
75                                         public void onClosed() {
76                                                 TcpConnection connection = TcpConnection.getCurrentConnection();
77                                                 Socket socket = connection.getSocket();
78                                                 System.out.println("Bye bye "+socket.getRemoteSocketAddress());
79                                         }} );
80                                         
81                                 return 0;
82                         }
83                         
84                         public String execute(String command, String...args) throws Error1 
85                         {
86                                 if (!command.equals("start")) throw new Error1("Unknown command "+command);
87                                 return "Program started "+command+" with args "+Arrays.toString(args);
88                         }
89
90 //                      public void elProblem() {
91 //                      }
92 //
93 //                      public void elProblem(int i) {
94 //                      }                                       
95                 };
96                 
97                 // Adapt the server to a MethodInterface
98                 MethodInterface myProcessorBinding = Methods.bindInterface(CommandProcessor.class, myProcessor);
99                 
100                 // Put the server on a socket
101                 Server myServerSocket = new Server(8192, myProcessorBinding);
102
103                 ////// Client //////
104                 // Connect to the server socket 
105                 Client myClientSocket = new Client(Inet4Address.getByName("localhost"), 8192);
106                 
107                 try {
108                         CommandProcessor myClient = Methods.createProxy(CommandProcessor.class, myClientSocket);
109                 
110 //                      myClient.elProblem();
111                         
112                         // Hello World
113                         myClient.helloWorld();
114                         
115                         // Execute command "start"
116                         try {
117                                 String result = myClient.execute("start");
118                                 System.out.println(result);
119                         } catch (Error1 e) {
120                                 System.err.println(e);
121                         }
122
123                         // Execute command "fault"
124                         try {
125                                 String result = myClient.execute("fault");
126                                 System.out.println(result);
127                         } catch (Error1 e) {
128                                 System.err.println(e);
129                         }
130                 } finally {
131                         myClientSocket.close();
132                         myServerSocket.close();
133                 }               
134                 
135         }
136         
137 }
138