Making sure that sample stub and skeleton classes inherit from a common interface
[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         /**
24          * Constructors
25          */
26         public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws IOException {
27
28                 address = _address;
29                 ports = _ports;
30                 rmiCall = new IoTRMICall(_port, _address, _rev);
31         }
32
33
34         /**
35          * Instantiation of callback objects
36          */
37         public static int numCallbackObjects() {
38
39                 return NUM_CB_OBJ;      // Generated by the IoTCompiler
40         }
41
42
43         public void registerCallback(CallBackInterface _cb) {
44
45                 //int port = 5011;      // Send this info to the other end to start the stub
46                 //String address = "localhost";
47
48                 Thread thread = new Thread() {
49                         public void run() {
50                     try{
51                                         CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
52                                         cbskel.waitRequestInvokeMethod();
53                                 } catch (Exception ex){
54                                         ex.printStackTrace();
55                                         throw new Error("Error instantiating class CallBack_Skeleton!");
56                     }
57                 }
58             };
59                 thread.start();
60
61                 String sign = "voidregisterCallBack(CallBackInterface)";
62                 Class<?> retType = void.class;
63                 // port, address, and rev
64                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
65                 Object[] paramObj = new Object[] { ports[0], address, 0 };
66                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
67         }
68
69
70         public void setA(int _int) {
71
72                 String sign = "voidsetA(int)";
73                 Class<?> retType = void.class;
74                 Class<?>[] paramCls = new Class<?>[] { int.class };
75                 Object[] paramObj = new Object[] { _int };
76                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
77         }
78
79
80         public void setB(float _float) {
81
82                 String sign = "voidsetB(float)";
83                 Class<?> retType = void.class;
84                 Class<?>[] paramCls = new Class<?>[] { float.class };
85                 Object[] paramObj = new Object[] { _float };
86                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
87         }
88
89
90         public void setC(String _string) {
91
92                 String sign = "voidsetC(string)";
93                 Class<?> retType = void.class;
94                 Class<?>[] paramCls = new Class<?>[] { String.class };
95                 Object[] paramObj = new Object[] { _string };
96                 rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
97         }
98
99
100         // Getters
101         public String sumArray(String[] newA) {
102
103                 String sign = "sumArray(string[])";
104                 Class<?> retType = String.class;
105                 Class<?>[] paramCls = new Class<?>[] { String[].class };
106                 Object[] paramObj = new Object[] { newA };
107                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
108                 return (String)retObj;
109         }
110
111
112         public int setAndGetA(int newA) {
113                 String sign = "intsetAndGetA(int)";
114                 Class<?> retType = int.class;
115                 Class<?>[] paramCls = new Class<?>[] { int.class };
116                 Object[] paramObj = new Object[] { newA };
117                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
118                 return (int)retObj;
119         }
120
121
122         public int setACAndGetA(String newC, int newA) {
123
124                 String sign = "intsetACAndGetA(string,int)";
125                 Class<?> retType = int.class;
126                 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
127                 Object[] paramObj = new Object[] { newC, newA };
128                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
129                 return (int)retObj;
130         }
131
132
133         public int callBack() {
134
135                 String sign = "intcallBack()";
136                 Class<?> retType = int.class;
137                 Class<?>[] paramCls = new Class<?>[] { };
138                 Object[] paramObj = new Object[] { };
139                 Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
140                 return (int)retObj;
141
142         }
143
144
145         public static void main(String[] args) throws Exception {
146
147                 CommunicationHandler comHan = new CommunicationHandler(true);
148                 int numOfPorts = TestClass_Stub.numCallbackObjects();
149                 int[] ports = comHan.getCallbackPorts(numOfPorts);
150
151                 System.out.println("Allocated ports: " + Arrays.toString(ports));
152
153                 int port = 5010;
154                 String address = "localhost";
155                 int rev = 0;
156
157                 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
158                 System.out.println("Return value: " + tcstub.setAndGetA(123));
159                 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
160                 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
161
162                 /*CallBack cb = new CallBack(23);
163                 tcstub.registerCallback(cb);
164                 System.out.println("Return value from callback: " + tcstub.callBack());*/
165                 //System.out.println("Return value: " + tcstub.setAndGetA(1234));
166         }
167 }