From fa20c8e87f7a52d03ce3a71647b22107ef0e9710 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Sun, 6 Nov 2016 08:24:35 -0800 Subject: [PATCH] Adding return value support for enumeration (both Java and C++ --- iotjava/Makefile | 8 +++--- iotjava/iotrmi/C++/sample/TestClass.hpp | 6 ++-- .../iotrmi/C++/sample/TestClassInterface.hpp | 2 +- .../iotrmi/C++/sample/TestClass_Skeleton.hpp | 20 ++++++++++--- iotjava/iotrmi/C++/sample/TestClass_Stub.cpp | 5 +++- iotjava/iotrmi/C++/sample/TestClass_Stub.hpp | 20 +++++++++---- iotjava/iotrmi/Java/IoTRMIUtil.java | 3 +- iotjava/iotrmi/Java/sample/TestClass.java | 4 ++- .../Java/sample/TestClassInterface.java | 2 +- .../Java/sample/TestClass_Skeleton.java | 19 ++++++++++--- .../iotrmi/Java/sample/TestClass_Stub.java | 28 +++++++++++++------ 11 files changed, 85 insertions(+), 32 deletions(-) diff --git a/iotjava/Makefile b/iotjava/Makefile index 8df7142..fdc86d8 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 @@ -46,8 +46,8 @@ rmi: # $(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 diff --git a/iotjava/iotrmi/C++/sample/TestClass.hpp b/iotjava/iotrmi/C++/sample/TestClass.hpp index 3bb8f93..71bd4bd 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass.hpp @@ -24,7 +24,7 @@ class TestClass : public TestClassInterface { void registerCallback(vector _cb); int callBack(); void handleStruct(vector vecData); - void handleEnum(vector vecEn); + vector handleEnum(vector vecEn); private: int intA; @@ -137,11 +137,13 @@ void TestClass::handleStruct(vector vecData) { } -void TestClass::handleEnum(vector vecEn) { +vector TestClass::handleEnum(vector vecEn) { for (EnumC en : vecEn) { cout << "Enum: " << en << endl; } + + return vecEn; } diff --git a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp index 86dd37b..c947662 100644 --- a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp +++ b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp @@ -22,7 +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; + virtual vector handleEnum(vector vecEn) = 0; }; #endif diff --git a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp index 28bf157..015cda4 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp @@ -26,7 +26,7 @@ class TestClass_Skeleton : public TestClassInterface { void registerCallback(vector _cb); int callBack(); void handleStruct(vector vecData); - void handleEnum(vector vecEn); + vector handleEnum(vector vecEn); void ___setA(); void ___setB(); @@ -336,9 +336,9 @@ void TestClass_Skeleton::___handleStruct(int structsize1) { } -void TestClass_Skeleton::handleEnum(vector vecEn) { +vector TestClass_Skeleton::handleEnum(vector vecEn) { - tc->handleEnum(vecEn); + return tc->handleEnum(vecEn); } @@ -369,7 +369,19 @@ void TestClass_Skeleton::___handleEnum(int enumsize1) { for (int i=0; i < enumsize1; i++) { en[i] = (EnumC) param1[i]; } - handleEnum(en); + //handleEnum(en); + // if void, just "handleEnum(en)" + // this is when we have return value vector + vector retEnum = handleEnum(en); + // Get length first + int enumSize2 = retEnum.size(); + // Now send the array of integers + vector retEnumInt(enumSize2); + for (int i=0; i < enumSize2; i++) { + retEnumInt[i] = (int) retEnum[i]; + } + void* retObj = &retEnumInt; + rmiObj->sendReturnObj(retObj, "int[]"); } diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp index 1d5049b..201f8c0 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp @@ -67,7 +67,10 @@ int main(int argc, char *argv[]) vecEn.push_back(ORANGE); vecEn.push_back(APPLE); vecEn.push_back(GRAPE); - tcStub->handleEnum(vecEn); + vector vecRes = tcStub->handleEnum(vecEn); + for (EnumC en : vecRes) { + cout << "EnumC: " << en << endl; + } return 0; } diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp index 63457fe..8e470ef 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp @@ -28,7 +28,7 @@ class TestClass_Stub : public TestClassInterface { void registerCallback(vector_cb); int callBack(); void handleStruct(vector vecData); - void handleEnum(vector vecEn); + vector handleEnum(vector vecEn); void ____init_CallBack(); // thread void ____registerCallBack(); // tell the other side that we are ready @@ -303,7 +303,7 @@ void TestClass_Stub::handleStruct(vector vecData) { } -void TestClass_Stub::handleEnum(vector vecEn) { +vector TestClass_Stub::handleEnum(vector vecEn) { int numParam = 1; int methodId = 13; @@ -316,18 +316,28 @@ void TestClass_Stub::handleEnum(vector vecEn) { int numParam2 = vecEn.size(); int methodId2 = 12; - string retType2 = "void"; + string retType2 = "int[]"; 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++) { + for(int i = 0; i < numParam2; i++) { paramCls2[i] = "int"; paramEnum[i] = (int) vecEn[i]; // cast enum to integer paramObj2[i] = ¶mEnum[i]; } - void* retObj2 = NULL; + vector retEnumInt(numParam2); + // if no return value just + // void* retObj2 = NULL; + // This is with return value: + void* retObj2 = &retEnumInt; rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2); + int enumsize1 = retEnumInt.size(); + vector retVal(enumsize1); + for (int i=0; i < enumsize1; i++) { + retVal[i] = (EnumC) retEnumInt[i]; + } + return retVal; } diff --git a/iotjava/iotrmi/Java/IoTRMIUtil.java b/iotjava/iotrmi/Java/IoTRMIUtil.java index bcb2975..b187bcd 100644 --- a/iotjava/iotrmi/Java/IoTRMIUtil.java +++ b/iotjava/iotrmi/Java/IoTRMIUtil.java @@ -210,6 +210,7 @@ public class IoTRMIUtil { public static Object getParamObjectArray(Class type, byte[] paramBytes) { Object retObj = null; + System.out.println("Got here!!!"); if ((type == byte[].class) || (type == byte.class)) { retObj = (Object) paramBytes; @@ -261,7 +262,7 @@ public class IoTRMIUtil { } else if ( (type == String[].class) || (type == String.class)) { retObj = (Object) byteArrayToStringArray(paramBytes); - } else if (type.isArray()) { + //} else if (type.isArray()) { // This is an array but it's more than 1 dimension, e.g. 2-dimensional, // 3-dimensional, etc. // for loop to check inner array perhaps using object diff --git a/iotjava/iotrmi/Java/sample/TestClass.java b/iotjava/iotrmi/Java/sample/TestClass.java index 95dbb1b..d8d78bc 100644 --- a/iotjava/iotrmi/Java/sample/TestClass.java +++ b/iotjava/iotrmi/Java/sample/TestClass.java @@ -155,11 +155,13 @@ public class TestClass implements TestClassInterface { } - public void handleEnum(EnumJ[] en) { + public EnumJ[] handleEnum(EnumJ[] en) { for (EnumJ e : en) { System.out.println("Enum: " + e); } + + return en; } diff --git a/iotjava/iotrmi/Java/sample/TestClassInterface.java b/iotjava/iotrmi/Java/sample/TestClassInterface.java index aa76ea0..5cc98d1 100644 --- a/iotjava/iotrmi/Java/sample/TestClassInterface.java +++ b/iotjava/iotrmi/Java/sample/TestClassInterface.java @@ -14,5 +14,5 @@ public interface TestClassInterface { public void registerCallback(CallBackInterface[] _cb); public int callBack(); public void handleStruct(StructJ[] data); - public void handleEnum(EnumJ[] en); + public EnumJ[] handleEnum(EnumJ[] en); } diff --git a/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java b/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java index 9ff3b18..73580a2 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Skeleton.java @@ -226,9 +226,9 @@ public class TestClass_Skeleton implements TestClassInterface { } - public void handleEnum(EnumJ[] en) { + public EnumJ[] handleEnum(EnumJ[] en) { - tc.handleEnum(en); + return tc.handleEnum(en); } @@ -240,7 +240,7 @@ public class TestClass_Skeleton implements TestClassInterface { } - public void ___handleEnum(int enumSize1) { + public void ___handleEnum(int enumSize1) throws IOException { Class[] paramCls = new Class[enumSize1]; Class[] paramClsVal = new Class[enumSize1]; @@ -256,7 +256,18 @@ public class TestClass_Skeleton implements TestClassInterface { for (int i=0; i < enumSize1; i++) { data[i] = enumJ[(int) paramObj[i]]; } - tc.handleEnum(data); + // if void, just "handleEnum(data)" + // this is when we have return value EnumJ[] + EnumJ[] retEnum = handleEnum(data); + // Get length first + int enumSize2 = retEnum.length; + // Now send the array of integers + int[] retEnumInt = new int[enumSize2]; + for (int i=0; i < enumSize2; i++) { + retEnumInt[i] = retEnum[i].ordinal(); + } + Object retObj = retEnumInt; + rmiObj.sendReturnObj(retObj); } diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java index a907ff7..2e64275 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Stub.java @@ -241,7 +241,7 @@ public class TestClass_Stub implements TestClassInterface { } - public void handleEnum(EnumJ[] en) { + public EnumJ[] handleEnum(EnumJ[] en) { // Send length info first int methodId = 13; @@ -251,7 +251,7 @@ public class TestClass_Stub implements TestClassInterface { rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj); int methodId2 = 12; - Class retType2 = void.class; + Class retType2 = int[].class; // Calculate the size of the array Class[] paramCls2 = new Class[en.length]; Object[] paramObj2 = new Object[en.length]; @@ -260,8 +260,20 @@ public class TestClass_Stub implements TestClassInterface { paramCls2[i] = int.class; paramObj2[i] = en[i].ordinal(); } - System.out.println(Arrays.toString(paramObj2)); - rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2); + // if no return value just + // rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2); + // This is with return value: + Object retObj = + rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2); + int[] retEnumInt = (int[]) retObj; + int enumsize1 = retEnumInt.length; + // Encoder/decoder + EnumJ[] enumJ = EnumJ.values(); + EnumJ[] enRetVal = new EnumJ[enumsize1]; + for(int i = 0; i < enumsize1; i++) { + enRetVal[i] = enumJ[retEnumInt[i]]; + } + return enRetVal; } @@ -282,7 +294,7 @@ public class TestClass_Stub implements TestClassInterface { System.out.println("Return value: " + tcstub.setACAndGetA("string", 123)); System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" })); - CallBackInterface cb1 = new CallBack(23); + /*CallBackInterface cb1 = new CallBack(23); CallBackInterface cb2 = new CallBack(33); CallBackInterface cb3 = new CallBack(43); CallBackInterface[] cb = { cb1, cb2, cb3 }; @@ -311,11 +323,11 @@ public class TestClass_Stub implements TestClassInterface { System.out.println("Value: " + str.value); System.out.println("Year: " + str.year); } - tcstub.handleStruct(data); + tcstub.handleStruct(data);*/ EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE }; - tcstub.handleEnum(en); - + EnumJ[] res = tcstub.handleEnum(en); + System.out.println("Enum members: " + Arrays.toString(res)); } } -- 2.34.1