1 package org.simantics.databoard.example.old;
2 import java.io.IOException;
3 import java.net.Inet4Address;
4 import java.util.HashMap;
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;
16 /*******************************************************************************
\r
17 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
19 * All rights reserved. This program and the accompanying materials
\r
20 * are made available under the terms of the Eclipse Public License v1.0
\r
21 * which accompanies this distribution, and is available at
\r
22 * http://www.eclipse.org/legal/epl-v10.html
\r
25 * VTT Technical Research Centre of Finland - initial API and implementation
\r
26 *******************************************************************************/
\r
28 public class RPCExample3 {
30 public interface SiCarProvider {
31 SiCar findCar(String modelName) throws CarNotFoundError;
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;
41 public SiCar(String modelName, double maxVelocity, double mass, double length, double power)
43 this.modelName = modelName;
44 this.maxVelocity = maxVelocity;
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);
57 public interface ImCarProvider {
58 ImCar findCar(String modelName) throws CarNotFoundError;
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;
68 public ImCar(String modelName, float maxVelocity, float mass, float length, float power)
70 this.modelName = modelName;
71 this.maxVelocity = maxVelocity;
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);
85 public static class CarCatalog implements SiCarProvider {
86 Map<String, SiCar> cars = new HashMap<String, SiCar>();
88 public SiCar findCar(String modelName)
89 throws CarNotFoundError{
90 if (cars.containsKey(modelName)) return cars.get(modelName);
91 throw new CarNotFoundError(modelName);
94 public void addCar(SiCar car) {
95 cars.put(car.modelName, car);
101 public static void main(String[] args) throws IOException, CarNotFoundError, BindingConstructionException, SerializationException, BindingException {
103 CarCatalog catalog = new CarCatalog();
104 catalog.addCar( new SiCar("Fiat 500", 205., 1034., 355., 75.) );
106 MethodInterface serverMi = Methods.bindInterface(SiCarProvider.class, catalog);
107 Server myServerSocket = new Server(8193, serverMi);
110 Client myClientSocket = new Client(Inet4Address.getByName("localhost"), 8193);
113 SiCarProvider siCarProvider = Methods.createProxy(SiCarProvider.class, serverMi);
114 ImCarProvider imCarProvider = Methods.createProxy(ImCarProvider.class, Methods.adaptMethods(serverMi, Methods.getInterfaceType(ImCarProvider.class).getMethodDefinitions()));
116 System.out.println( siCarProvider.findCar("Fiat 500") );
117 System.out.println( imCarProvider.findCar("Fiat 500") );
120 myClientSocket.close();
121 myServerSocket.close();
125 public static class CarNotFoundError extends Exception {
126 public String modelName;
127 public CarNotFoundError(String modelName) {
128 this.modelName = modelName;
131 public String toString() {
132 return modelName+" is not in the catalog";