]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / RPCExample2.java
1 package org.simantics.databoard.example.old;
2 import java.io.IOException;
3 import java.util.Arrays;
4
5 import org.simantics.databoard.Methods;
6 import org.simantics.databoard.binding.error.BindingConstructionException;
7 import org.simantics.databoard.method.MethodInterface;
8
9 /*******************************************************************************
10  *  Copyright (c) 2010 Association for Decentralized Information Management in
11  *  Industry THTH ry.
12  *  All rights reserved. This program and the accompanying materials
13  *  are made available under the terms of the Eclipse Public License v1.0
14  *  which accompanies this distribution, and is available at
15  *  http://www.eclipse.org/legal/epl-v10.html
16  *
17  *  Contributors:
18  *      VTT Technical Research Centre of Finland - initial API and implementation
19  *******************************************************************************/
20
21 public class RPCExample2 {
22
23         public static class Server {
24         
25                 // Server Code
26                 public interface NetworkProtocol {
27                         void elProblem();
28                         String execute(String command, int[] args) throws Error1;
29                 }
30
31                 public static class Error1 extends Exception {
32                         
33                         private static final long serialVersionUID = 1L;
34                         
35                         public String errorMessage;
36                         public Error1(String msg) {
37                                 this.errorMessage = msg;
38                         }
39                         @Override
40                         public String toString() {
41                                 return errorMessage;
42                         }
43                 }
44
45         }
46         
47         // Client Code
48         public static class Client {
49                 public interface NetworkProtocol {
50                         String execute(String cmd, int[] args) throws Error1;
51                 }
52         
53                 public static class Error1 extends Exception {          
54                         
55                         private static final long serialVersionUID = 1L;
56                         
57                         public String errorMessage;
58                         public Error1(String errorMessage) {
59                                 this.errorMessage = errorMessage;
60                         }
61                         @Override
62                         public String toString() {
63                                 return "Error occured: "+errorMessage;
64                         }
65                 }
66         }
67         
68         // Example Code 
69         
70         public static void main(String[] args) throws IOException, BindingConstructionException {
71
72                 // Server
73                 Server.NetworkProtocol myServer = new Server.NetworkProtocol() {
74                         @Override
75                         public String execute(String command, int[] args) throws Server.Error1 {
76                                 if (!command.equals("start")) throw new Server.Error1("Unknown command "+command);
77                                 return "Program started "+command+" with args "+Arrays.toString(args);
78                         }
79
80                         @Override
81                         public void elProblem() {
82                                 // TODO Auto-generated method stub
83                                 System.out.println("Works");
84                                 
85                         }                                       
86                 };
87                 
88                 // Adapt server to MethodInterface
89                 MethodInterface myMI = Methods.bindInterface(Server.NetworkProtocol.class, myServer);
90                 
91                 
92                 // Client               
93                 // Adapt methodInterface to client
94                 Client.NetworkProtocol myMiProxy = Methods.createProxy(Client.NetworkProtocol.class, myMI);
95                 
96                 // Execute command "start"
97                 try {
98                         String result = myMiProxy.execute("start", new int[] {5,6,72,7423});
99                         System.out.println(result);
100                 } catch (Client.Error1 e) {
101                         System.err.println(e);
102                 }
103
104                 // Execute command "fault"
105                 try {
106                         String result = myMiProxy.execute("fault", new int[] {});
107                         System.out.println(result);
108                 } catch (Client.Error1 e) {
109                         System.err.println(e);
110                 }
111                 
112         }
113         
114 }
115