+
+
+ /**
+ * sendReturnObj() overloaded to send multiple return objects for structs
+ */
+ public void sendReturnObj(Class<?>[] retCls, Object[] retObj) throws IOException {
+
+ // Send back return value
+ byte[] retObjBytes = returnToBytes(retCls, retObj);
+ rmiServer.sendBytes(retObjBytes);
+ }
+
+
+ /**
+ * returnToBytes() takes array of objects and generates bytes
+ */
+ public byte[] returnToBytes(Class<?>[] retCls, Object[] retObj) {
+
+ // Get byte arrays and calculate method bytes length
+ int numbRet = retObj.length;
+ int retLen = 0;
+ byte[][] objBytesArr = new byte[numbRet][];
+ for (int i = 0; i < numbRet; i++) {
+ // Get byte arrays for the objects
+ objBytesArr[i] = IoTRMIUtil.getObjectBytes(retObj[i]);
+ String clsName = retCls[i].getSimpleName();
+ int retObjLen = rmiUtil.getTypeSize(clsName);
+ if (retObjLen == -1) { // indefinite length - store the length first
+ retLen = retLen + IoTRMIUtil.RETURN_LEN;
+ }
+ retLen = retLen + objBytesArr[i].length;
+ }
+ // Construct return in byte array
+ byte[] retBytes = new byte[retLen];
+ int pos = 0;
+ // Iteration for copying bytes
+ for (int i = 0; i < numbRet; i++) {
+
+ String clsName = retCls[i].getSimpleName();
+ int retObjLen = rmiUtil.getTypeSize(clsName);
+ if (retObjLen == -1) { // indefinite length
+ retObjLen = objBytesArr[i].length;
+ byte[] retLenBytes = IoTRMIUtil.intToByteArray(retObjLen);
+ System.arraycopy(retLenBytes, 0, retBytes, pos, IoTRMIUtil.RETURN_LEN);
+ pos = pos + IoTRMIUtil.RETURN_LEN;
+ }
+ System.arraycopy(objBytesArr[i], 0, retBytes, pos, retObjLen);
+ pos = pos + retObjLen;
+ }
+
+ return retBytes;
+ }
+
+
+ public static void main(String[] args) throws Exception {
+
+ int port = 5010;
+ IoTRMIObject rmiObj = new IoTRMIObject(port);
+
+ Class<?>[] retCls = new Class<?>[] { int.class, String.class, int.class };
+ Object[] retObj = new Object[] { 1234, "teststring", 5432 };
+
+ System.out.println("Bytes: " + Arrays.toString(rmiObj.returnToBytes(retCls, retObj)));
+ }