Early version of RMI system for Java part; supports primitives, one-dimensional array...
[iot2.git] / iotjava / iotrmi / Java / IoTRMIObject.java
index 9f40b821abc80f534cde557ecc6de45b3331fdc3..c8fefa41d8dbb080dae3dfce93efe9673da119d5 100644 (file)
@@ -28,109 +28,66 @@ public class IoTRMIObject {
        /**
         * Class Properties
         */
-       private Map<String,Method> mapSign2Method;      // Map from signature to method
        private Map<Integer,String> mapHash2Sign;       // Map from hashcode(method ID) to signature
        private IoTRMIUtil rmiUtil;
        private IoTSocketServer rmiServer;
-       private Object obj;
-       private Class<?> cls;
-       private Method[] methods;
+       private byte[] methodBytes;
 
 
        /**
         * Constructors
         */
-       public IoTRMIObject(String _clsName, int _port) throws 
-                       ClassNotFoundException, InstantiationException, 
-                               IllegalAccessException, IOException {
+       public IoTRMIObject(int _port, String[] _methodSign) throws  
+               ClassNotFoundException, InstantiationException, 
+                       IllegalAccessException, IOException {
 
                rmiUtil = new IoTRMIUtil();
-               cls = Class.forName(_clsName);
-               obj = cls.newInstance();
-               methods = cls.getDeclaredMethods();
-               mapSign2Method = new HashMap<String,Method>();
                mapHash2Sign = new HashMap<Integer,String>();
-               getMethodSignatures();  // Initialize the signature map
-               getMethodIds();                 // Initialize the method ID map
+               methodBytes = null;
+               getMethodIds(_methodSign);      // Initialize the method ID map
                rmiServer = new IoTSocketServer(_port);
                rmiServer.connect();
        }
 
 
        /**
-        * getName() gets class name
+        * sendReturnObj() sends back return Object to client
         */
-       public String getName() {
+       public void sendReturnObj(Object retObj) throws IOException {
 
-               return cls.getName();
+               // Send back return value
+               byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
+               rmiServer.sendBytes(retObjBytes);
        }
 
 
        /**
-        * getSignatures() gets method signatures
-        */
-       public Set<String> getSignatures() {
-
-               return mapSign2Method.keySet();
-       }
-
-       
-       /**
-        * getMethodParamTypes() gets method parameter types
+        * getMethodBytes() waits for method transmission in bytes
         */
-       public Class<?>[] getMethodParamTypes(String signature) {
+       public void getMethodBytes() throws IOException {
 
-               Method method = mapSign2Method.get(signature);
-               return method.getParameterTypes();
+               // Receive method info
+               methodBytes = rmiServer.receiveBytes(methodBytes);
        }
 
 
        /**
-        * getMethodRetType() gets method return type
-        */
-       public Class<?> getMethodRetType(String signature) {
-
-               Method method = mapSign2Method.get(signature);
-               return method.getReturnType();
-       }
-       
-
-       /**
-        * invokeMethod() invokes a method based on signature and params
+        * getSignature() gets method signature from bytes
         */
-       public Object invokeMethod(String signature, Object[] params) {
-
-               Method method = mapSign2Method.get(signature);
-               Object retVal = null;
-               try {
-                       retVal = method.invoke(obj, params);
-               } catch (IllegalAccessException |
-                                InvocationTargetException ex) {
-                       ex.printStackTrace();
-                       throw new Error("IoTRMICall: Error invoking method: " + signature);
-               }
+       public String getSignature() {
 
-               return retVal;
+               // Get method ID
+               byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
+               System.arraycopy(methodBytes, 0, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
+               // Get Method object to handle method
+               int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
+               // Get method signature from the Map
+               return mapHash2Sign.get(methodId);
        }
 
 
        /**
-        * recvAndInvoke() waits for method transmission and invoke the method
-        */
-       private void recvAndInvoke() throws IOException {
-
-               // Receive method info and invoke
-               byte[] method = null;
-               method = rmiServer.receiveBytes(method);
-               Object retObj = invokeMethod(method);
-               // Send back return value
-               byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
-               rmiServer.sendBytes(retObjBytes);
-       }
-       
-
-       /**
-        * invokeMethod() invokes a method based on byte array received
+        * getMethodParams() gets method params based on byte array received
         * <p>
         * Basically this is the format of a method in bytes:
         * 1) 32-bit value of method ID (hash code)
@@ -146,20 +103,10 @@ public class IoTRMIObject {
         * | 32-bit method ID | 32-bit length | n-bit actual data | ...
         * 
         */
-       public Object invokeMethod(byte[] methodBytes) {
+       public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenKeyCls, Class<?>[] arrGenValCls) {
 
                // Byte scanning position
-               int pos = 0;
-               // Get method ID
-               byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
-               System.arraycopy(methodBytes, pos, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
-               pos = pos + IoTRMIUtil.METHOD_ID_LEN;
-               // Get Method object to handle method
-               int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
-               String signature = mapHash2Sign.get(methodId);
-               Method method = mapSign2Method.get(signature);
-               // Get class name and then param length
-               Class<?>[] arrCls = method.getParameterTypes();
+               int pos = IoTRMIUtil.METHOD_ID_LEN;
                Object[] paramObj = new Object[arrCls.length];
                for (int i=0; i < arrCls.length; i++) {
 
@@ -176,56 +123,23 @@ public class IoTRMIObject {
                        byte[] paramBytes = new byte[paramSize];
                        System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize);
                        pos = pos + paramSize;
-                       paramObj[i] = IoTRMIUtil.getParamObject(paramType, paramBytes);
+                       paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenKeyCls[i], 
+                               arrGenValCls[i], paramBytes);
                }
-               Object retObj = null;
-               try {
-                       retObj = method.invoke(obj, paramObj);
-               } catch (IllegalAccessException |
-                                InvocationTargetException ex) {
-                       ex.printStackTrace();
-                       throw new Error("IoTRMICall: Error invoking method: " + signature);
-               }
-
-               return retObj;
-       }
 
-
-       /**================
-        * Helper methods
-        **================
-        */
-       /**
-        * getMethodSignatures() gets methods signatures and store them in the Map
-        */
-       private void getMethodSignatures() {
-
-               for (Method m : methods) {
-                       String sign = rmiUtil.getSignature(m);
-                       //System.out.println("Signature: " + sign);
-                       mapSign2Method.put(sign, m);
-               }
+               return paramObj;
        }
        
-       
+
        /**
         * getMethodIds() gets methods identifiers (hash code) and store them in the Map
         */
-       private void getMethodIds() {
+       private void getMethodIds(String[] methodSign) {
 
-               Set<String> setSignatures = getSignatures();
-               for (String sign : setSignatures) {
+               for (String sign : methodSign) {
                        byte[] hashCode = IoTRMIUtil.getHashCodeBytes(sign);
                        int methodId = IoTRMIUtil.byteArrayToInt(hashCode);
                        mapHash2Sign.put(methodId, sign);
                }
        }
-
-
-       public static void main(String[] args) throws Exception {
-
-               int port = 5010;
-               IoTRMIObject rmiObj = new IoTRMIObject("TestClass", port);
-               rmiObj.recvAndInvoke();
-       }
 }