* @version 1.0
* @since 2016-10-04
*/
-public class IoTRMICall {
+public final class IoTRMICall {
/**
*/
private IoTRMIUtil rmiUtil;
private IoTSocketClient rmiClient;
- //private List<String> listMethodId; // Map from method ID to signature
/**
ex.printStackTrace();
throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
}
- System.out.println("Return object bytes: " + Arrays.toString(retObjBytes));
retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes);
}
return retObj;
}
- public static void main(String[] args) throws Exception {
-
- String[] test = { "123", "456", "789" };
- byte[] b = IoTRMIUtil.getObjectBytes(test);
-
- Boolean[] test2 = new Boolean[] { true, false, false };
- byte[] b2 = IoTRMIUtil.getObjectBytes(test2);
-
- System.out.println(Arrays.toString(b));
- System.out.println(Arrays.toString(b2));
-
- String[] c = (String[]) IoTRMIUtil.getParamObjectArray(String[].class, b);
- System.out.println(Arrays.toString(c));
+ /**
+ * remoteCall() calls a method remotely by passing in parameters and getting a return Object
+ */
+ public synchronized Object[] getStructObjects(Class<?>[] retType, Class<?>[] retGenTypeVal) {
- Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2);
- System.out.println(Arrays.toString(c2));
+ // Receive return value and return it to caller
+ Object[] retObj = null;
+ byte[] retObjBytes = null;
+ try {
+ retObjBytes = rmiClient.receiveBytes(retObjBytes);
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
+ }
+ retObj = getReturnObjects(retObjBytes, retType, retGenTypeVal);
- // List
- /*List<Long> list = new ArrayList<Long>();
- list.add(12345678l);
- list.add(23455432l);
- list.add(34566543l);
+ return retObj;
+ }
- byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
- System.out.println(Arrays.toString(objBytes));
- Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
- @SuppressWarnings("unchecked")
- List<Long> listStr = (List<Long>) obj;
- System.out.println("List: " + listStr.toString());*/
+ public Object[] getReturnObjects(byte[] retBytes, Class<?>[] arrCls, Class<?>[] arrGenValCls) {
- //@SuppressWarnings("unchecked")
- //List<Long> listStr = (List<Long>) obj;
- //System.out.println("List: " + listStr.toString());
+ // Byte scanning position
+ int pos = 0;
+ Object[] retObj = new Object[arrCls.length];
+ for (int i=0; i < arrCls.length; i++) {
+
+ String retType = arrCls[i].getSimpleName();
+ int retSize = rmiUtil.getTypeSize(retType);
+ // Get the 32-bit field in the byte array to get the actual
+ // length (this is a param with indefinite length)
+ if (retSize == -1) {
+ byte[] bytRetLen = new byte[IoTRMIUtil.RETURN_LEN];
+ System.arraycopy(retBytes, pos, bytRetLen, 0, IoTRMIUtil.RETURN_LEN);
+ pos = pos + IoTRMIUtil.RETURN_LEN;
+ retSize = IoTRMIUtil.byteArrayToInt(bytRetLen);
+ }
+ byte[] retObjBytes = new byte[retSize];
+ System.arraycopy(retBytes, pos, retObjBytes, 0, retSize);
+ pos = pos + retSize;
+ retObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], retObjBytes);
+ }
+ return retObj;
}
}