]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/old/RPCExample3.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / old / RPCExample3.java
1 package org.simantics.databoard.example.old;
2 import java.io.IOException;
3 import java.net.Inet4Address;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.simantics.databoard.Methods;
8 import org.simantics.databoard.annotations.Unit;
9 import org.simantics.databoard.binding.error.BindingConstructionException;
10 import org.simantics.databoard.binding.error.BindingException;
11 import org.simantics.databoard.method.Client;
12 import org.simantics.databoard.method.MethodInterface;
13 import org.simantics.databoard.method.Server;
14 import org.simantics.databoard.serialization.SerializationException;
15
16 /*******************************************************************************
17  *  Copyright (c) 2010 Association for Decentralized Information Management in
18  *  Industry THTH ry.
19  *  All rights reserved. This program and the accompanying materials
20  *  are made available under the terms of the Eclipse Public License v1.0
21  *  which accompanies this distribution, and is available at
22  *  http://www.eclipse.org/legal/epl-v10.html
23  *
24  *  Contributors:
25  *      VTT Technical Research Centre of Finland - initial API and implementation
26  *******************************************************************************/
27
28 public class RPCExample3 {
29
30         public interface SiCarProvider {
31                 SiCar findCar(String modelName) throws CarNotFoundError;
32         }
33         
34         static public class SiCar {
35                 public String modelName;                
36                 public @Unit("km/h") double maxVelocity;                
37                 public @Unit("kg") double mass;         
38                 public @Unit("cm") double length; 
39                 public @Unit("kW") double power;
40                 
41                 public SiCar(String modelName, double maxVelocity, double mass, double length, double power) 
42                 {
43                         this.modelName = modelName;
44                         this.maxVelocity = maxVelocity;
45                         this.mass = mass;
46                         this.length = length;
47                         this.power = power;
48                 }
49                 
50                 @Override
51                 public String toString() {
52                         return String.format("Name:%s, Mass: %2.0f kg, Power: %2.0f kW, Speed: %2.0f km/h, Length: %2.0f cm", modelName, mass, power, maxVelocity, length);
53                 }
54         }
55
56         
57         public interface ImCarProvider {
58                 ImCar findCar(String modelName) throws CarNotFoundError;
59         }       
60
61         static public class ImCar {
62                 public String modelName;                
63                 public @Unit("mph") float maxVelocity;          
64                 public @Unit("lbs") float mass;         
65                 public @Unit("ft") float length; 
66                 public @Unit("hp(M)") float power;
67                 
68                 public ImCar(String modelName, float maxVelocity, float mass, float length, float power) 
69                 {
70                         this.modelName = modelName;
71                         this.maxVelocity = maxVelocity;
72                         this.mass = mass;
73                         this.length = length;
74                         this.power = power;
75                 }
76                 
77                 @Override
78                 public String toString() {
79                         return String.format("Name:%s, Mass: %2.0f lbs, Power: %2.0f hp, Speed: %2.0f mph, Length: %2.0f ft", modelName, mass, power, maxVelocity, length);
80                 }
81         }
82         
83         
84         // Implementation
85         public static class CarCatalog implements SiCarProvider {
86                 Map<String, SiCar> cars = new HashMap<String, SiCar>();
87                 @Override
88                 public SiCar findCar(String modelName) 
89                 throws CarNotFoundError{
90                         if (cars.containsKey(modelName)) return cars.get(modelName);
91                         throw new CarNotFoundError(modelName);
92                 }
93                 
94                 public void addCar(SiCar car) {
95                         cars.put(car.modelName, car);
96                 }
97
98         }
99         
100         // Example Code         
101         public static void main(String[] args) throws IOException, CarNotFoundError, BindingConstructionException, SerializationException, BindingException {
102                 // Server
103                 CarCatalog catalog = new CarCatalog();
104                 catalog.addCar( new SiCar("Fiat 500", 205., 1034., 355., 75.) );
105                 
106                 MethodInterface serverMi = Methods.bindInterface(SiCarProvider.class, catalog);
107                 Server myServerSocket = new Server(8193, serverMi);
108                 
109                 // Client
110                 Client myClientSocket = new Client(Inet4Address.getByName("localhost"), 8193);
111                 
112                 try {
113                         SiCarProvider siCarProvider = Methods.createProxy(SiCarProvider.class, serverMi);
114                         ImCarProvider imCarProvider = Methods.createProxy(ImCarProvider.class, Methods.adaptMethods(serverMi, Methods.getInterfaceType(ImCarProvider.class).getMethodDefinitions()));
115                         
116                         System.out.println( siCarProvider.findCar("Fiat 500") );
117                         System.out.println( imCarProvider.findCar("Fiat 500") );
118                         
119                 } finally {
120                         myClientSocket.close();
121                         myServerSocket.close();
122                 }
123         }       
124
125         public static class CarNotFoundError extends Exception {
126                 public String modelName;
127                 public CarNotFoundError(String modelName) {
128                         this.modelName = modelName;
129                 }
130                 @Override
131                 public String toString() {
132                         return modelName+" is not in the catalog";
133                 }
134         }
135         
136         
137 }
138