From 87b736a65257f4497b0b28543e758d7b09db2820 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Sat, 5 Nov 2016 15:56:32 -0700 Subject: [PATCH] Adding enum support for method argument; need to emulate the same functionality for return value as well (together with struct) --- iotjava/Makefile | 17 ++-- iotjava/iotrmi/C++/sample/EnumC.cpp | 24 ++++++ iotjava/iotrmi/C++/sample/EnumC.hpp | 13 ++++ iotjava/iotrmi/C++/sample/TestClass.hpp | 9 +++ .../iotrmi/C++/sample/TestClassInterface.hpp | 2 + .../iotrmi/C++/sample/TestClass_Skeleton.hpp | 55 +++++++++++-- iotjava/iotrmi/C++/sample/TestClass_Stub.cpp | 11 ++- iotjava/iotrmi/C++/sample/TestClass_Stub.hpp | 31 +++++++- iotjava/iotrmi/Java/IoTRMICall.java | 30 +------- iotjava/iotrmi/Java/IoTRMIObject.java | 5 +- iotjava/iotrmi/Java/IoTRMIUtil.java | 10 +-- .../Java/sample/CallBack_CBSkeleton.java | 2 +- .../iotrmi/Java/sample/CallBack_CBStub.java | 4 +- .../iotrmi/Java/sample/CallBack_Skeleton.java | 2 +- iotjava/iotrmi/Java/sample/CallBack_Stub.java | 4 +- iotjava/iotrmi/Java/sample/EnumJ.java | 8 ++ iotjava/iotrmi/Java/sample/EnumMain.java | 21 +++++ iotjava/iotrmi/Java/sample/TestClass.java | 8 ++ .../Java/sample/TestClassInterface.java | 1 + .../Java/sample/TestClass_Skeleton.java | 77 +++++++++++++------ .../iotrmi/Java/sample/TestClass_Stub.java | 51 +++++++++--- 21 files changed, 288 insertions(+), 97 deletions(-) create mode 100644 iotjava/iotrmi/C++/sample/EnumC.cpp create mode 100644 iotjava/iotrmi/C++/sample/EnumC.hpp create mode 100644 iotjava/iotrmi/Java/sample/EnumJ.java create mode 100644 iotjava/iotrmi/Java/sample/EnumMain.java diff --git a/iotjava/Makefile b/iotjava/Makefile index 267c006..8df7142 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -31,8 +31,8 @@ runtime: PHONY += rmi rmi: mkdir -p $(BIN_DIR) -# $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/*.java -# $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/sample/*.java + $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/*.java + $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/sample/*.java # mkdir -p $(BIN_DIR)/iotrmi/C++ #$(G++) iotrmi/C++/IoTSocketServer.cpp -o $(BIN_DIR)/iotrmi/C++/IoTSocketServer.out #$(G++) iotrmi/C++/IoTSocketClient.cpp -o $(BIN_DIR)/iotrmi/C++/IoTSocketClient.out @@ -41,16 +41,17 @@ rmi: mkdir -p $(BIN_DIR)/iotrmi/C++/sample #$(G++) iotrmi/C++/sample/CallBackInterface.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBackInterface.out --std=c++11 #$(G++) iotrmi/C++/sample/CallBack.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack.out --std=c++11 - $(G++) iotrmi/C++/sample/CallBack_CBStub.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_CBStub.out --std=c++11 - $(G++) iotrmi/C++/sample/CallBack_CBSkeleton.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_CBSkeleton.out --std=c++11 - $(G++) iotrmi/C++/sample/CallBack_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Stub.out --std=c++11 - $(G++) iotrmi/C++/sample/CallBack_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Skeleton.out --std=c++11 +# $(G++) iotrmi/C++/sample/CallBack_CBStub.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_CBStub.out --std=c++11 +# $(G++) iotrmi/C++/sample/CallBack_CBSkeleton.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_CBSkeleton.out --std=c++11 +# $(G++) iotrmi/C++/sample/CallBack_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Stub.out --std=c++11 +# $(G++) iotrmi/C++/sample/CallBack_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Skeleton.out --std=c++11 #$(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11 - $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 -pthread - $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 -pthread +# $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 -pthread +# $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 -pthread #$(G++) iotrmi/C++/sample/Test.cpp -o ../bin/iotrmi/C++/sample/Test.out --std=c++11 -lpthread #$(G++) iotrmi/C++/sample/Test2.cpp -o ../bin/iotrmi/C++/sample/Test2.out --std=c++11 -pthread -pg # $(G++) iotrmi/C++/sample/StructC.cpp -o ../bin/iotrmi/C++/sample/StructC.out --std=c++11 +# $(G++) iotrmi/C++/sample/EnumC.cpp -o ../bin/iotrmi/C++/sample/EnumC.out --std=c++11 PHONY += run-rmiserver diff --git a/iotjava/iotrmi/C++/sample/EnumC.cpp b/iotjava/iotrmi/C++/sample/EnumC.cpp new file mode 100644 index 0000000..089a145 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/EnumC.cpp @@ -0,0 +1,24 @@ +#include +#include +#include "EnumC.hpp" + +int main(int argc, char *argv[]) { + + // Enum to integer + EnumC en = APPLE; + int enum1 = (int) en; + cout << "Enum 1: " << enum1 << endl; + EnumC en2 = ORANGE; + int enum2 = (int) en2; + cout << "Enum 2: " << enum2 << endl; + EnumC en3 = GRAPE; + int enum3 = (int) en3; + cout << "Enum 3: " << enum3 << endl; + + // Integer to enum + cout << "Enum 1: " << (EnumC) enum1 << endl; + cout << "Enum 2: " << (EnumC) enum2 << endl; + cout << "Enum 3: " << (EnumC) enum3 << endl; + + return 0; +} diff --git a/iotjava/iotrmi/C++/sample/EnumC.hpp b/iotjava/iotrmi/C++/sample/EnumC.hpp new file mode 100644 index 0000000..529ae73 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/EnumC.hpp @@ -0,0 +1,13 @@ +#ifndef _ENUMC_HPP__ +#define _ENUMC_HPP__ + +using namespace std; + +enum EnumC { + APPLE, + ORANGE, + GRAPE +} enumC; + +#endif + diff --git a/iotjava/iotrmi/C++/sample/TestClass.hpp b/iotjava/iotrmi/C++/sample/TestClass.hpp index f8b9ffb..3bb8f93 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass.hpp @@ -24,6 +24,7 @@ class TestClass : public TestClassInterface { void registerCallback(vector _cb); int callBack(); void handleStruct(vector vecData); + void handleEnum(vector vecEn); private: int intA; @@ -136,6 +137,14 @@ void TestClass::handleStruct(vector vecData) { } +void TestClass::handleEnum(vector vecEn) { + + for (EnumC en : vecEn) { + cout << "Enum: " << en << endl; + } +} + + //int TestClass::callBack() { // return cb.printInt(); //} diff --git a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp index ed73f1e..86dd37b 100644 --- a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp +++ b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp @@ -5,6 +5,7 @@ #include #include "CallBackInterface.hpp" #include "StructC.hpp" +#include "EnumC.hpp" using namespace std; @@ -21,6 +22,7 @@ class TestClassInterface { virtual void registerCallback(vector _cb) = 0; virtual int callBack() = 0; virtual void handleStruct(vector vecData) = 0; + virtual void handleEnum(vector vecEn) = 0; }; #endif diff --git a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp index f8bf9bf..28bf157 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp @@ -26,6 +26,7 @@ class TestClass_Skeleton : public TestClassInterface { void registerCallback(vector _cb); int callBack(); void handleStruct(vector vecData); + void handleEnum(vector vecEn); void ___setA(); void ___setB(); @@ -42,6 +43,8 @@ class TestClass_Skeleton : public TestClassInterface { // For array of structs int ___structSize(); void ___handleStruct(int structsize1); + int ___enumSize(); + void ___handleEnum(int enumsize1); const static int size = 12; const static string methodSignatures[size]; @@ -286,6 +289,12 @@ void TestClass_Skeleton::___callBack() { } +void TestClass_Skeleton::handleStruct(vector vecData) { + + tc->handleStruct(vecData); +} + + int TestClass_Skeleton::___structSize() { string paramCls[] = { "int" }; @@ -297,12 +306,6 @@ int TestClass_Skeleton::___structSize() { } -void TestClass_Skeleton::handleStruct(vector vecData) { - - tc->handleStruct(vecData); -} - - void TestClass_Skeleton::___handleStruct(int structsize1) { string paramCls[3*structsize1]; @@ -333,9 +336,47 @@ void TestClass_Skeleton::___handleStruct(int structsize1) { } +void TestClass_Skeleton::handleEnum(vector vecEn) { + + tc->handleEnum(vecEn); +} + + +int TestClass_Skeleton::___enumSize() { + + string paramCls[] = { "int" }; + int numParam = 1; + int param1 = 0; + void* paramObj[] = { ¶m1 }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + return param1; +} + + +void TestClass_Skeleton::___handleEnum(int enumsize1) { + + string paramCls[enumsize1]; + void* paramObj[enumsize1]; + int numParam = enumsize1; + // define array of everything + int param1[enumsize1]; + for(int i=0; i < enumsize1; i++) { + paramCls[i] = "int"; + paramObj[i] = ¶m1[i]; + } + rmiObj->getMethodParams(paramCls, numParam, paramObj); + vector en(enumsize1); + for (int i=0; i < enumsize1; i++) { + en[i] = (EnumC) param1[i]; + } + handleEnum(en); +} + + void TestClass_Skeleton::___waitRequestInvokeMethod() { int structsize1 = 0; + int enumsize1 = 0; // Loop continuously waiting for incoming bytes while (true) { @@ -357,6 +398,8 @@ void TestClass_Skeleton::___waitRequestInvokeMethod() { // Handle struct case 10: ___handleStruct(structsize1); break; case 11: structsize1 = ___structSize(); break; + case 12: ___handleEnum(enumsize1); break; + case 13: enumsize1 = ___enumSize(); break; default: string error = "Method Id not recognized!"; throw error; diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp index 4067dcb..1d5049b 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) cout << "Return value: " << tcStub->sumArray(input) << endl; - CallBackInterface *cb1 = new CallBack(23); + /*CallBackInterface *cb1 = new CallBack(23); CallBackInterface *cb2 = new CallBack(33); CallBackInterface *cb3 = new CallBack(43); vector cb; @@ -60,7 +60,14 @@ int main(int argc, char *argv[]) delete tcStub; delete cb1; delete cb2; - delete cb3; + delete cb3;*/ + + vector vecEn; + vecEn.push_back(APPLE); + vecEn.push_back(ORANGE); + vecEn.push_back(APPLE); + vecEn.push_back(GRAPE); + tcStub->handleEnum(vecEn); return 0; } diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp index 1a623f1..63457fe 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp @@ -28,6 +28,7 @@ class TestClass_Stub : public TestClassInterface { void registerCallback(vector_cb); int callBack(); void handleStruct(vector vecData); + void handleEnum(vector vecEn); void ____init_CallBack(); // thread void ____registerCallBack(); // tell the other side that we are ready @@ -298,9 +299,37 @@ void TestClass_Stub::handleStruct(vector vecData) { paramObj2[pos] = &vecData[i].year; pos++; } void* retObj2 = NULL; - cout << "In handle struct 3!" << endl; rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2); } +void TestClass_Stub::handleEnum(vector vecEn) { + + int numParam = 1; + int methodId = 13; + string retType = "void"; + string paramCls[] = { "int" }; + int enumsize = vecEn.size(); + void* paramObj[] = { &enumsize }; + void* retObj = NULL; + rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + + int numParam2 = vecEn.size(); + int methodId2 = 12; + string retType2 = "void"; + string paramCls2[numParam2]; + // Need to define this container for integer version of enum + int paramEnum[numParam2]; + void* paramObj2[numParam2]; + for(int i = 0; i < vecEn.size(); i++) { + paramCls2[i] = "int"; + paramEnum[i] = (int) vecEn[i]; // cast enum to integer + paramObj2[i] = ¶mEnum[i]; + } + void* retObj2 = NULL; + rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2); +} + + + #endif diff --git a/iotjava/iotrmi/Java/IoTRMICall.java b/iotjava/iotrmi/Java/IoTRMICall.java index 842547d..b84b10a 100644 --- a/iotjava/iotrmi/Java/IoTRMICall.java +++ b/iotjava/iotrmi/Java/IoTRMICall.java @@ -47,7 +47,7 @@ public class IoTRMICall { /** * remoteCall() calls a method remotely by passing in parameters and getting a return Object */ - public synchronized Object remoteCall(int objectId, int methodId, Class retType, Class retGenTypeKey, + public synchronized Object remoteCall(int objectId, int methodId, Class retType, Class retGenTypeVal, Class[] paramCls, Object[] paramObj) { // Send method info @@ -69,7 +69,7 @@ public class IoTRMICall { throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()"); } System.out.println("Return object bytes: " + Arrays.toString(retObjBytes)); - retObj = IoTRMIUtil.getParamObject(retType, retGenTypeKey, retGenTypeVal, retObjBytes); + retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes); } return retObj; } @@ -142,19 +142,6 @@ public class IoTRMICall { Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2); System.out.println(Arrays.toString(c2)); - // Set - /*Set set = new HashSet(); - set.add("1234"); - set.add("5678"); - - byte[] objBytes = IoTRMIUtil.getObjectBytes(set); - System.out.println(Arrays.toString(objBytes)); - Object obj = IoTRMIUtil.getParamObject(Set.class, null, String.class, objBytes); - - @SuppressWarnings("unchecked") - Set setStr = (Set) obj; - System.out.println("Set: " + setStr.toString());*/ - // List /*List list = new ArrayList(); list.add(12345678l); @@ -169,19 +156,6 @@ public class IoTRMICall { List listStr = (List) obj; System.out.println("List: " + listStr.toString());*/ - // Map - Map map = new HashMap(); - map.put(12345678l, 1234); - map.put(23455432l, 5678); - map.put(34566543l, 4321); - - byte[] objBytes = IoTRMIUtil.getObjectBytes(map); - System.out.println(Arrays.toString(objBytes)); - Object obj = IoTRMIUtil.getParamObject(Map.class, Long.class, Integer.class, objBytes); - - map = (Map) obj; - System.out.println("Received map: " + map.toString()); - //@SuppressWarnings("unchecked") //List listStr = (List) obj; //System.out.println("List: " + listStr.toString()); diff --git a/iotjava/iotrmi/Java/IoTRMIObject.java b/iotjava/iotrmi/Java/IoTRMIObject.java index e801e44..42e778b 100644 --- a/iotjava/iotrmi/Java/IoTRMIObject.java +++ b/iotjava/iotrmi/Java/IoTRMIObject.java @@ -132,7 +132,7 @@ public class IoTRMIObject { * | 32-bit object ID | 32-bit method ID | 32-bit length | n-bit actual data | ... * */ - public Object[] getMethodParams(Class[] arrCls, Class[] arrGenKeyCls, Class[] arrGenValCls) { + public Object[] getMethodParams(Class[] arrCls, Class[] arrGenValCls) { // Byte scanning position int pos = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN; @@ -152,8 +152,7 @@ public class IoTRMIObject { byte[] paramBytes = new byte[paramSize]; System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize); pos = pos + paramSize; - paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenKeyCls[i], - arrGenValCls[i], paramBytes); + paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], paramBytes); } return paramObj; diff --git a/iotjava/iotrmi/Java/IoTRMIUtil.java b/iotjava/iotrmi/Java/IoTRMIUtil.java index e1c1be8..bcb2975 100644 --- a/iotjava/iotrmi/Java/IoTRMIUtil.java +++ b/iotjava/iotrmi/Java/IoTRMIUtil.java @@ -162,7 +162,7 @@ public class IoTRMIUtil { /** * getParamObject() converts byte array of certain object type into Object */ - public static Object getParamObject(Class type, Class genTypeKey, Class genTypeVal, byte[] paramBytes) { + public static Object getParamObject(Class type, Class genTypeVal, byte[] paramBytes) { Object retObj = null; if (type == byte.class || @@ -194,17 +194,9 @@ public class IoTRMIUtil { // Array } else if (type.isArray()) { retObj = getParamObjectArray(type, paramBytes); - // Set - // e.g. Set - type = Set.class, genTypeVal = String.class - /*} else if (type == Set.class) { - retObj = getParamSetObject(genTypeVal, paramBytes);*/ // List } else if (type == List.class) { retObj = getParamListObject(genTypeVal, paramBytes); - // Map - // e.g. Map - type = Map.class, genTypeKey = String.class, genTypeVal = Integer.class - /*} else if (type == Map.class) { - retObj = getParamMapObject(genTypeKey, genTypeVal, paramBytes);*/ } else throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName()); diff --git a/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java b/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java index cb6b9d8..0e01eba 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java +++ b/iotjava/iotrmi/Java/sample/CallBack_CBSkeleton.java @@ -47,7 +47,7 @@ public class CallBack_CBSkeleton implements CallBackInterface { public void ___setInt(IoTRMIObject rmiObj) { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); setInt((int) paramObj[0]); } diff --git a/iotjava/iotrmi/Java/sample/CallBack_CBStub.java b/iotjava/iotrmi/Java/sample/CallBack_CBStub.java index 9c733e5..1278d6e 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_CBStub.java +++ b/iotjava/iotrmi/Java/sample/CallBack_CBStub.java @@ -40,7 +40,7 @@ public class CallBack_CBStub implements CallBackInterface { Class retType = int.class; Class[] paramCls = new Class[] { }; Object[] paramObj = new Object[] { }; - Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); return (int)retObj; } @@ -51,7 +51,7 @@ public class CallBack_CBStub implements CallBackInterface { Class retType = void.class; Class[] paramCls = new Class[] { int.class }; Object[] paramObj = new Object[] { _i }; - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } diff --git a/iotjava/iotrmi/Java/sample/CallBack_Skeleton.java b/iotjava/iotrmi/Java/sample/CallBack_Skeleton.java index 3d540f1..77a3c81 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_Skeleton.java +++ b/iotjava/iotrmi/Java/sample/CallBack_Skeleton.java @@ -44,7 +44,7 @@ public class CallBack_Skeleton implements CallBackInterface { public void ___setInt() { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); setInt((int) paramObj[0]); } diff --git a/iotjava/iotrmi/Java/sample/CallBack_Stub.java b/iotjava/iotrmi/Java/sample/CallBack_Stub.java index 04dc0d9..8eb2acb 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_Stub.java +++ b/iotjava/iotrmi/Java/sample/CallBack_Stub.java @@ -27,7 +27,7 @@ public class CallBack_Stub implements CallBackInterface { Class retType = int.class; Class[] paramCls = new Class[] { }; Object[] paramObj = new Object[] { }; - Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); return (int)retObj; } @@ -38,7 +38,7 @@ public class CallBack_Stub implements CallBackInterface { Class retType = void.class; Class[] paramCls = new Class[] { int.class }; Object[] paramObj = new Object[] { _i }; - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } diff --git a/iotjava/iotrmi/Java/sample/EnumJ.java b/iotjava/iotrmi/Java/sample/EnumJ.java new file mode 100644 index 0000000..2601c3d --- /dev/null +++ b/iotjava/iotrmi/Java/sample/EnumJ.java @@ -0,0 +1,8 @@ +package iotrmi.Java.sample; + +public enum EnumJ { + + APPLE, + ORANGE, + GRAPE +} diff --git a/iotjava/iotrmi/Java/sample/EnumMain.java b/iotjava/iotrmi/Java/sample/EnumMain.java new file mode 100644 index 0000000..7fcdc49 --- /dev/null +++ b/iotjava/iotrmi/Java/sample/EnumMain.java @@ -0,0 +1,21 @@ +package iotrmi.Java.sample; + +public class EnumMain { + + public static void main (String[] args) { + + // Enum to int + int enum1 = EnumJ.APPLE.ordinal(); + System.out.println("Enum 1: " + enum1); + int enum2 = EnumJ.ORANGE.ordinal(); + System.out.println("Enum 2: " + enum2); + int enum3 = EnumJ.GRAPE.ordinal(); + System.out.println("Enum 3: " + enum3); + + // Int to enum + EnumJ[] enumJ = EnumJ.values(); + System.out.println("Enum 1: " + enumJ[enum1]); + System.out.println("Enum 1: " + enumJ[enum2]); + System.out.println("Enum 1: " + enumJ[enum3]); + } +} diff --git a/iotjava/iotrmi/Java/sample/TestClass.java b/iotjava/iotrmi/Java/sample/TestClass.java index c1c397f..95dbb1b 100644 --- a/iotjava/iotrmi/Java/sample/TestClass.java +++ b/iotjava/iotrmi/Java/sample/TestClass.java @@ -155,6 +155,14 @@ public class TestClass implements TestClassInterface { } + public void handleEnum(EnumJ[] en) { + + for (EnumJ e : en) { + System.out.println("Enum: " + e); + } + } + + public static void main(String[] args) { //TestClass tc = new TestClass(); diff --git a/iotjava/iotrmi/Java/sample/TestClassInterface.java b/iotjava/iotrmi/Java/sample/TestClassInterface.java index 9d46df5..aa76ea0 100644 --- a/iotjava/iotrmi/Java/sample/TestClassInterface.java +++ b/iotjava/iotrmi/Java/sample/TestClassInterface.java @@ -14,4 +14,5 @@ public interface TestClassInterface { public void registerCallback(CallBackInterface[] _cb); public int callBack(); public void handleStruct(StructJ[] data); + public void handleEnum(EnumJ[] en); } diff --git a/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java b/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java index ce2b1b7..9ff3b18 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java @@ -46,7 +46,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___setA() { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); setA((int) paramObj[0]); } @@ -60,7 +60,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___setB() { Object[] paramObj = rmiObj.getMethodParams(new Class[] { float.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); setB((float) paramObj[0]); } @@ -74,7 +74,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___setC() { Object[] paramObj = rmiObj.getMethodParams(new Class[] { String.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); setC((String) paramObj[0]); } @@ -88,7 +88,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___sumArray() throws IOException { Object[] paramObj = rmiObj.getMethodParams(new Class[] { String[].class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); Object retObj = sumArray((String[]) paramObj[0]); rmiObj.sendReturnObj(retObj); } @@ -103,7 +103,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___setAndGetA() throws IOException { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); Object retObj = setAndGetA((int) paramObj[0]); rmiObj.sendReturnObj(retObj); } @@ -118,7 +118,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___setACAndGetA() throws IOException { Object[] paramObj = rmiObj.getMethodParams(new Class[] { String.class, int.class }, - new Class[] { null, null }, new Class[] { null, null }); + new Class[] { null, null }); Object retObj = setACAndGetA((String) paramObj[0], (int) paramObj[1]); rmiObj.sendReturnObj(retObj); } @@ -133,7 +133,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___registerCallback() throws IOException { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class, String.class, int.class }, - new Class[] { null, null, null }, new Class[] { null, null, null }); + new Class[] { null, null, null }); CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]); registerCallback((CallBackInterface) cbstub); } @@ -149,7 +149,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ____registerCallback() throws IOException { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); int numStubs = (int) paramObj[0]; CallBackInterface[] stub = new CallBackInterface[numStubs]; for (int objId = 0; objId < numStubs; objId++) { @@ -163,7 +163,7 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___regCB() throws IOException { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class, String.class, int.class }, - new Class[] { null, null, null }, new Class[] { null, null, null }); + new Class[] { null, null, null }); //String[] methodSignatures = CallBack_CBStub.getMethodSignatures(); rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]); System.out.println("Creating a new IoTRMICall object"); @@ -192,7 +192,7 @@ public class TestClass_Skeleton implements TestClassInterface { public int ___structSize() { Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, - new Class[] { null }, new Class[] { null }); + new Class[] { null }); return (int) paramObj[0]; } @@ -200,22 +200,18 @@ public class TestClass_Skeleton implements TestClassInterface { public void ___handleStruct(int structsize1) { Class[] paramCls = new Class[3*structsize1]; - Class[] paramClsTyp1 = new Class[3*structsize1]; - Class[] paramClsTyp2 = new Class[3*structsize1]; + Class[] paramClsVal = new Class[3*structsize1]; int pos = 0; for(int i=0; i < structsize1; i++) { paramCls[pos] = String.class; - paramClsTyp1[pos] = null; - paramClsTyp2[pos++] = null; + paramClsVal[pos++] = null; paramCls[pos] = float.class; - paramClsTyp1[pos] = null; - paramClsTyp2[pos++] = null; + paramClsVal[pos++] = null; paramCls[pos] = int.class; - paramClsTyp1[pos] = null; - paramClsTyp2[pos++] = null; + paramClsVal[pos++] = null; } Object[] paramObj = rmiObj.getMethodParams(paramCls, - paramClsTyp1, paramClsTyp2); + paramClsVal); StructJ[] data = new StructJ[structsize1]; for (int i=0; i < structsize1; i++) { data[i] = new StructJ(); @@ -228,12 +224,47 @@ public class TestClass_Skeleton implements TestClassInterface { } tc.handleStruct(data); } + + + public void handleEnum(EnumJ[] en) { + + tc.handleEnum(en); + } + + + public int ___enumSize() { + + Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, + new Class[] { null }); + return (int) paramObj[0]; + } + + + public void ___handleEnum(int enumSize1) { + + Class[] paramCls = new Class[enumSize1]; + Class[] paramClsVal = new Class[enumSize1]; + for(int i=0; i < enumSize1; i++) { + paramCls[i] = int.class; + paramClsVal[i] = null; + } + Object[] paramObj = rmiObj.getMethodParams(paramCls, + paramClsVal); + // Encoder/decoder + EnumJ[] enumJ = EnumJ.values(); + EnumJ[] data = new EnumJ[enumSize1]; + for (int i=0; i < enumSize1; i++) { + data[i] = enumJ[(int) paramObj[i]]; + } + tc.handleEnum(data); + } private void ___waitRequestInvokeMethod() throws IOException { // Struct size - int structsize1 = 0; + int structSize1 = 0; + int enumSize1 = 0; // Loop continuously waiting for incoming bytes while (true) { @@ -256,8 +287,10 @@ public class TestClass_Skeleton implements TestClassInterface { // Special option to register callback case 9: ___regCB(); break; // Struct handling (3 is the size of the struct) - case 10: ___handleStruct(structsize1); break; - case 11: structsize1 = ___structSize(); break; + case 10: ___handleStruct(structSize1); break; + case 11: structSize1 = ___structSize(); break; + case 12: ___handleEnum(enumSize1); break; + case 13: enumSize1 = ___enumSize(); break; default: throw new Error("Method Id not recognized!"); } diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java index c1f03c6..a907ff7 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Stub.java @@ -85,7 +85,7 @@ public class TestClass_Stub implements TestClassInterface { Class[] paramCls = new Class[] { int.class, String.class, int.class }; Object[] paramObj = new Object[] { ports[0], address, 0 }; //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj); - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } @@ -133,7 +133,7 @@ public class TestClass_Stub implements TestClassInterface { Class[] paramCls = new Class[] { int.class }; Object[] paramObj = new Object[] { _cb.length }; //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj); - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } @@ -144,7 +144,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = void.class; Class[] paramCls = new Class[] { int.class }; Object[] paramObj = new Object[] { _int }; - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } @@ -154,7 +154,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = void.class; Class[] paramCls = new Class[] { float.class }; Object[] paramObj = new Object[] { _float }; - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } @@ -164,7 +164,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = void.class; Class[] paramCls = new Class[] { String.class }; Object[] paramObj = new Object[] { _string }; - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); } @@ -175,7 +175,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = String.class; Class[] paramCls = new Class[] { String[].class }; Object[] paramObj = new Object[] { newA }; - Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); return (String)retObj; } @@ -185,7 +185,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = int.class; Class[] paramCls = new Class[] { int.class }; Object[] paramObj = new Object[] { newA }; - Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); return (int)retObj; } @@ -196,7 +196,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = int.class; Class[] paramCls = new Class[] { String.class, int.class }; Object[] paramObj = new Object[] { newC, newA }; - Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); return (int)retObj; } @@ -207,7 +207,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = int.class; Class[] paramCls = new Class[] { }; Object[] paramObj = new Object[] { }; - Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); return (int)retObj; } @@ -219,7 +219,7 @@ public class TestClass_Stub implements TestClassInterface { Class retType = void.class; Class[] paramCls = new Class[] { int.class }; Object[] paramObj = new Object[] { data.length }; - rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj); + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); int methodId2 = 10; Class retType2 = void.class; @@ -237,7 +237,31 @@ public class TestClass_Stub implements TestClassInterface { paramObj2[pos++] = data[i].year; } System.out.println(Arrays.toString(paramObj2)); - rmiCall.remoteCall(objectId, methodId2, retType2, null, null, paramCls2, paramObj2); + rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2); + } + + + public void handleEnum(EnumJ[] en) { + + // Send length info first + int methodId = 13; + Class retType = void.class; + Class[] paramCls = new Class[] { int.class }; + Object[] paramObj = new Object[] { en.length }; + rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); + + int methodId2 = 12; + Class retType2 = void.class; + // Calculate the size of the array + Class[] paramCls2 = new Class[en.length]; + Object[] paramObj2 = new Object[en.length]; + // Handle with for loop + for(int i = 0; i < en.length; i++) { + paramCls2[i] = int.class; + paramObj2[i] = en[i].ordinal(); + } + System.out.println(Arrays.toString(paramObj2)); + rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2); } @@ -287,8 +311,11 @@ public class TestClass_Stub implements TestClassInterface { System.out.println("Value: " + str.value); System.out.println("Year: " + str.year); } - tcstub.handleStruct(data); + + EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE }; + tcstub.handleEnum(en); + } } -- 2.34.1