Early version of RMI system for Java part; supports primitives, one-dimensional array...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java
new file mode 100644 (file)
index 0000000..a57f036
--- /dev/null
@@ -0,0 +1,167 @@
+package iotrmi.Java.sample;
+
+import java.io.IOException;
+import iotrmi.Java.IoTRMICall;
+import iotruntime.master.CommunicationHandler;
+
+import java.util.Arrays;
+
+public class TestClass_Stub {
+
+       /**
+        * Class Properties
+        */
+       private IoTRMICall rmiCall;
+       private String address;
+       private int[] ports;
+
+       /**
+        * Class Constants
+        */
+       private final static int NUM_CB_OBJ = 1;
+
+       /**
+        * Constructors
+        */
+       public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws IOException {
+
+               address = _address;
+               ports = _ports;
+               rmiCall = new IoTRMICall(_port, _address, _rev);
+       }
+
+
+       /**
+        * Instantiation of callback objects
+        */
+       public static int numCallbackObjects() {
+
+               return NUM_CB_OBJ;      // Generated by the IoTCompiler
+       }
+
+
+       private void registerCallback(CallBackInterface _cb) {
+
+               //int port = 5011;      // Send this info to the other end to start the stub
+               //String address = "localhost";
+
+               Thread thread = new Thread() {
+                       public void run() {
+                   try{
+                                       CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
+                                       cbskel.waitRequestInvokeMethod();
+                               } catch (Exception ex){
+                                       ex.printStackTrace();
+                                       throw new Error("Error instantiating class CallBack_Skeleton!");
+                   }
+               }
+           };
+               thread.start();
+
+               String sign = "voidregisterCallBack(CallBackInterface)";
+               Class<?> retType = void.class;
+               // port, address, and rev
+               Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
+               Object[] paramObj = new Object[] { ports[0], address, 0 };
+               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+       }
+
+
+       public void setA(int _int) {
+
+               String sign = "voidsetA(int)";
+               Class<?> retType = void.class;
+               Class<?>[] paramCls = new Class<?>[] { int.class };
+               Object[] paramObj = new Object[] { _int };
+               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+       }
+
+
+       public void setB(float _float) {
+
+               String sign = "voidsetB(float)";
+               Class<?> retType = void.class;
+               Class<?>[] paramCls = new Class<?>[] { float.class };
+               Object[] paramObj = new Object[] { _float };
+               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+       }
+
+
+       public void setC(String _string) {
+
+               String sign = "voidsetC(string)";
+               Class<?> retType = void.class;
+               Class<?>[] paramCls = new Class<?>[] { String.class };
+               Object[] paramObj = new Object[] { _string };
+               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+       }
+
+
+       // Getters
+       public String sumArray(String[] newA) {
+
+               String sign = "sumArray(string[])";
+               Class<?> retType = String.class;
+               Class<?>[] paramCls = new Class<?>[] { String[].class };
+               Object[] paramObj = new Object[] { newA };
+               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               return (String)retObj;
+       }
+
+
+       public int setAndGetA(int newA) {
+               String sign = "intsetAndGetA(int)";
+               Class<?> retType = int.class;
+               Class<?>[] paramCls = new Class<?>[] { int.class };
+               Object[] paramObj = new Object[] { newA };
+               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               return (int)retObj;
+       }
+
+
+       public int setACAndGetA(String newC, int newA) {
+
+               String sign = "intsetACAndGetA(string,int)";
+               Class<?> retType = int.class;
+               Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
+               Object[] paramObj = new Object[] { newC, newA };
+               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               return (int)retObj;
+       }
+
+
+       public int callBack() {
+
+               String sign = "intcallBack()";
+               Class<?> retType = int.class;
+               Class<?>[] paramCls = new Class<?>[] { };
+               Object[] paramObj = new Object[] { };
+               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               return (int)retObj;
+
+       }
+
+
+       public static void main(String[] args) throws Exception {
+
+               CommunicationHandler comHan = new CommunicationHandler(true);
+               int numOfPorts = TestClass_Stub.numCallbackObjects();
+               int[] ports = comHan.getCallbackPorts(numOfPorts);
+
+               System.out.println("Allocated ports: " + Arrays.toString(ports));
+
+               int port = 5010;
+               String address = "localhost";
+               int rev = 0;
+
+               TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
+               System.out.println("Return value: " + tcstub.setAndGetA(123));
+               System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
+               System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
+
+               CallBack cb = new CallBack(23);
+               tcstub.registerCallback(cb);
+               System.out.println("Return value from callback: " + tcstub.callBack());
+               //System.out.println("Return value: " + tcstub.setAndGetA(1234));
+       }
+}