Using sorted integer as method Id instead of hash values
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
1 package iotrmi.Java.sample;
2
3 import java.io.IOException;
4 import iotrmi.Java.IoTRMICall;
5 import iotruntime.master.CommunicationHandler;
6
7 import java.util.Arrays;
8
9 public class TestClass_Stub implements TestClassInterface {
10
11         /**
12          * Class Properties
13          */
14         private IoTRMICall rmiCall;
15         private String address;
16         private int[] ports;
17
18         /**
19          * Class Constants
20          */
21         private final static int NUM_CB_OBJ = 1;
22
23         private String[] methodSignatures = {
24
25                 "voidsetA(int)",
26                 "voidsetB(float)",
27                 "voidsetC(string)",
28                 "sumArray(string[])",
29                 "intsetAndGetA(int)",
30                 "intsetACAndGetA(string,int)",
31                 "intcallBack()",
32                 "voidregisterCallBack(CallBackInterface)"
33         };
34
35         /**
36          * Constructors
37          */
38         public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws IOException {
39
40                 address = _address;
41                 ports = _ports;
42                 rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
43         }
44
45
46         /**
47          * Instantiation of callback objects
48          */
49         public static int numCallbackObjects() {
50
51                 return NUM_CB_OBJ;      // Generated by the IoTCompiler
52         }
53
54
55         public void registerCallback(CallBackInterface _cb) {
56
57                 //int port = 5011;      // Send this info to the other end to start the stub
58                 //String address = "localhost";
59
60                 Thread thread = new Thread() {
61                         public void run() {
62                     try{
63                                         CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
64                                         cbskel.waitRequestInvokeMethod();
65                                 } catch (Exception ex){
66                                         ex.printStackTrace();
67                                         throw new Error("Error instantiating class CallBack_Skeleton!");
68                     }
69                 }
70             };
71                 thread.start();
72
73                 String sign = "voidregisterCallBack(CallBackInterface)";
74                 Class<?> retType = void.class;
75                 // port, address, and rev
76                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
77                 Object[] paramObj = new Object[] { ports[0], address, 0 };
78                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
79         }
80
81
82         public void setA(int _int) {
83
84                 String sign = "voidsetA(int)";
85                 Class<?> retType = void.class;
86                 Class<?>[] paramCls = new Class<?>[] { int.class };
87                 Object[] paramObj = new Object[] { _int };
88                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
89         }
90
91
92         public void setB(float _float) {
93
94                 String sign = "voidsetB(float)";
95                 Class<?> retType = void.class;
96                 Class<?>[] paramCls = new Class<?>[] { float.class };
97                 Object[] paramObj = new Object[] { _float };
98                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
99         }
100
101
102         public void setC(String _string) {
103
104                 String sign = "voidsetC(string)";
105                 Class<?> retType = void.class;
106                 Class<?>[] paramCls = new Class<?>[] { String.class };
107                 Object[] paramObj = new Object[] { _string };
108                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
109         }
110
111
112         // Getters
113         public String sumArray(String[] newA) {
114
115                 String sign = "sumArray(string[])";
116                 Class<?> retType = String.class;
117                 Class<?>[] paramCls = new Class<?>[] { String[].class };
118                 Object[] paramObj = new Object[] { newA };
119                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
120                 return (String)retObj;
121         }
122
123
124         public int setAndGetA(int newA) {
125                 String sign = "intsetAndGetA(int)";
126                 Class<?> retType = int.class;
127                 Class<?>[] paramCls = new Class<?>[] { int.class };
128                 Object[] paramObj = new Object[] { newA };
129                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
130                 return (int)retObj;
131         }
132
133
134         public int setACAndGetA(String newC, int newA) {
135
136                 String sign = "intsetACAndGetA(string,int)";
137                 Class<?> retType = int.class;
138                 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
139                 Object[] paramObj = new Object[] { newC, newA };
140                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
141                 return (int)retObj;
142         }
143
144
145         public int callBack() {
146
147                 String sign = "intcallBack()";
148                 Class<?> retType = int.class;
149                 Class<?>[] paramCls = new Class<?>[] { };
150                 Object[] paramObj = new Object[] { };
151                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
152                 return (int)retObj;
153
154         }
155
156
157         public static void main(String[] args) throws Exception {
158
159                 CommunicationHandler comHan = new CommunicationHandler(true);
160                 int numOfPorts = TestClass_Stub.numCallbackObjects();
161                 int[] ports = comHan.getCallbackPorts(numOfPorts);
162
163                 System.out.println("Allocated ports: " + Arrays.toString(ports));
164
165                 int port = 5010;
166                 String address = "localhost";
167                 int rev = 0;
168
169                 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
170                 System.out.println("Return value: " + tcstub.setAndGetA(123));
171                 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
172                 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
173
174                 /*CallBack cb = new CallBack(23);
175                 tcstub.registerCallback(cb);
176                 System.out.println("Return value from callback: " + tcstub.callBack());*/
177                 //System.out.println("Return value: " + tcstub.setAndGetA(1234));
178         }
179 }