From ac75576ae0fffb9f1c277cca6ecd285548006137 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 29 Nov 2016 12:59:57 -0800 Subject: [PATCH] Adding permission inclusion for callback helper function; some testcases on enum type --- config/iotpolicy/testclasspolicy.pol | 14 +++- iotjava/Makefile | 1 + iotjava/iotpolicy/IoTCompiler.java | 73 +++++++++++++++---- iotjava/iotrmi/C++/basics/TestClass.hpp | 22 ++++++ iotjava/iotrmi/C++/basics/TestClass_Stub.cpp | 6 ++ iotjava/iotrmi/Java/basics/TestClass.java | 19 +++++ .../iotrmi/Java/basics/TestClass_Stub.java | 5 ++ 7 files changed, 125 insertions(+), 15 deletions(-) diff --git a/config/iotpolicy/testclasspolicy.pol b/config/iotpolicy/testclasspolicy.pol index ebe2533..481a284 100644 --- a/config/iotpolicy/testclasspolicy.pol +++ b/config/iotpolicy/testclasspolicy.pol @@ -27,6 +27,8 @@ public interface TestClassInterface { public void registerCallback(CallBackInterface _cb); public int callBack(); + public Enum handleEnum(Enum en); + public int getA(); public void setA(int _int); public void setB(float _float); @@ -60,6 +62,7 @@ public interface TestClassInterface { method = "getCharList(List in)"; method = "registerCallback(CallBackInterface _cb)"; method = "callBack()"; + method = "handleEnum(Enum en)"; method = "getA()"; method = "setA(int _int)"; method = "setB(float _float)"; @@ -73,12 +76,19 @@ public interface TestClassInterface { method = "setACAndGetA(String newC, int newA)"; } - struct StructJ { + enum Enum { + + APPLE, + ORANGE, + GRAPE + } + + struct Struct { string name; float value; int year; - } + } } diff --git a/iotjava/Makefile b/iotjava/Makefile index c00b34d..e1cd731 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -66,6 +66,7 @@ clean: cd $(BIN_DIR)/iotpolicy/output_files; rm -rf *.class *.gch cd $(BIN_DIR)/iotpolicy/output_files/Java; rm -rf *.class cd $(BIN_DIR)/iotpolicy/output_files/Cplus; rm -rf *.gch + cd $(BIN_DIR)/iotpolicy/output_files/Cplus; rm -rf *.out # RMI compilation and run PHONY += rmi diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 8571998..f2d8397 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -403,7 +403,7 @@ public class IoTCompiler { i++; } println(" };"); - println("private List set" + newObjectId + "Allowed;"); + println("private static List set" + newObjectId + "Allowed;"); } } @@ -490,6 +490,20 @@ public class IoTCompiler { } + /** + * HELPER: writeJavaCallbackPermission() writes the permission for callback + */ + private void writeJavaCallbackPermission(String intface, int methodNumId) { + + Map> mapNewIntMethods = mapInt2NewInts.get(intface); + for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { + String newIntface = intMeth.getKey(); + int newObjectId = getNewIntfaceObjectId(newIntface); + println("set" + newObjectId + "Allowed.add(" + methodNumId + ");"); + } + } + + /** * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub */ @@ -523,7 +537,9 @@ public class IoTCompiler { println("thread.start();\n"); // Generate info sending part String method = "___initCallBack()"; - println("int methodId = " + intDecl.getHelperMethodNumId(method) + ";"); + int methodNumId = intDecl.getHelperMethodNumId(method); + println("int methodId = " + methodNumId + ";"); + writeJavaCallbackPermission(intface, methodNumId); println("Class retType = void.class;"); println("Class[] paramCls = new Class[] { int.class, String.class, int.class };"); println("Object[] paramObj = new Object[] { ports[0], address, 0 };"); @@ -2497,7 +2513,7 @@ public class IoTCompiler { /** * HELPER: writeMethodCplusStub() writes the methods of the stub */ - private void writeMethodCplusStub(Collection methods, InterfaceDecl intDecl, Set callbackClasses) { + private void writeMethodCplusStub(String intface, Collection methods, InterfaceDecl intDecl, Set callbackClasses) { for (String method : methods) { @@ -2536,7 +2552,7 @@ public class IoTCompiler { // Write the init callback helper method if (isCallbackMethod) { writeInitCallbackCplusStub(callbackType, intDecl); - writeInitCallbackSendInfoCplusStub(intDecl); + writeInitCallbackSendInfoCplusStub(intface, intDecl); } } } @@ -2585,7 +2601,7 @@ public class IoTCompiler { //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); //String prmType = getSimpleType(getEnumType(paramTypeC)); String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); - String prmType = getEnumType(paramTypeC); + String prmType = getEnumCplusClsType(paramTypeC); print("\"" + prmType + "\""); } if (i != methParams.size() - 1) // Check if this is the last element @@ -2919,7 +2935,7 @@ public class IoTCompiler { //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); //String prmType = getSimpleType(getEnumType(paramTypeC)); String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); - String prmType = getEnumType(paramTypeC); + String prmType = getEnumCplusClsType(paramTypeC); print("\"" + prmType + "\""); // Check if this is the last element (don't print a comma) if (i != methParams.size() - 1) { @@ -2977,7 +2993,7 @@ public class IoTCompiler { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); println("const static int object" + newObjectId + "Id = " + newObjectId + ";\t//" + newIntface); - println("const static set set" + newObjectId + "Allowed;"); + println("static set set" + newObjectId + "Allowed;"); } } @@ -3105,16 +3121,32 @@ public class IoTCompiler { } + /** + * HELPER: writeCplusCallbackPermission() writes the permission for callback + */ + private void writeCplusCallbackPermission(String intface, int methodNumId) { + + Map> mapNewIntMethods = mapInt2NewInts.get(intface); + for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { + String newIntface = intMeth.getKey(); + int newObjectId = getNewIntfaceObjectId(newIntface); + println("set" + newObjectId + "Allowed.insert(" + methodNumId + ");"); + } + } + + /** * HELPER: writeInitCallbackSendInfoCplusStub() writes the initialization (send info part) of callback */ - private void writeInitCallbackSendInfoCplusStub(InterfaceDecl intDecl) { + private void writeInitCallbackSendInfoCplusStub(String intface, InterfaceDecl intDecl) { // Generate info sending part println("void ___regCB() {"); println("int numParam = 3;"); String method = "___initCallBack()"; - println("int methodId = " + intDecl.getHelperMethodNumId(method) + ";"); + int methodNumId = intDecl.getHelperMethodNumId(method); + println("int methodId = " + methodNumId + ";"); + writeCplusCallbackPermission(intface, methodNumId); println("string retType = \"void\";"); println("string paramCls[] = { \"int\", \"string\", \"int\" };"); println("int rev = 0;"); @@ -3164,7 +3196,7 @@ public class IoTCompiler { writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses); writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses); // Write methods - writeMethodCplusStub(methods, intDecl, callbackClasses); + writeMethodCplusStub(intface, methods, intDecl, callbackClasses); print("}"); println(";"); if (callbackExist) writePermissionInitializationCplus(intface, newStubClass, intDecl); @@ -3260,7 +3292,7 @@ public class IoTCompiler { writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses); writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses); // Write methods - writeMethodCplusStub(methods, intDecl, callbackClasses); + writeMethodCplusStub(intface, methods, intDecl, callbackClasses); println("};"); if (callbackExist) writePermissionInitializationCplus(intface, newStubClass, intDecl); @@ -3316,7 +3348,7 @@ public class IoTCompiler { for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); - print("const set " + newSkelClass + "::set" + newObjectId + "Allowed {"); + print("set " + newSkelClass + "::set" + newObjectId + "Allowed {"); Set methodIds = intMeth.getValue(); int i = 0; for (String methodId : methodIds) { @@ -3650,7 +3682,7 @@ public class IoTCompiler { //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); //String prmType = getSimpleType(getEnumType(paramTypeC)); String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); - String prmType = getEnumType(paramTypeC); + String prmType = getEnumCplusClsType(paramTypeC); print("\"" + prmType + "\""); } if (i != methParams.size() - 1) { @@ -4725,6 +4757,21 @@ public class IoTCompiler { return type; } + // Handle and return the correct enum declaration translate into int* for C + private String getEnumCplusClsType(String type) { + + // Strips off array "[]" for return type + String pureType = getSimpleArrayType(type); + // Take the inner type of generic + if (getParamCategory(type) == ParamCategory.NONPRIMITIVES) + pureType = getTypeOfGeneric(type)[0]; + if (isEnumClass(pureType)) { + String enumType = "int*"; + return enumType; + } else + return type; + } + // Handle and return the correct struct declaration private String getStructType(String type) { diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index 492d3d9..6350712 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -43,6 +43,9 @@ class TestClass : public TestClassInterface { //void registerCallback(vector _cb); int callBack(); + // Enum + Enum handleEnum(Enum en); + int getA(); void setA(int _int); void setB(float _float); @@ -256,6 +259,25 @@ void TestClass::setC(string _string) { } +// Enum +Enum TestClass::handleEnum(Enum en) { + + cout << "Enum: " << en << endl; + + return en; +} + + +/*vector TestClass::handleEnum(vector vecEn) { + + for (EnumC en : vecEn) { + cout << "Enum: " << en << endl; + } + + return vecEn; +}*/ + + string TestClass::sumArray(vector newA) { string sum = ""; diff --git a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp index bac2b0e..a1c35c0 100644 --- a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp @@ -82,6 +82,12 @@ int main(int argc, char *argv[]) inl7.push_back('c'); inl7.push_back('e'); cout << "Return value: " << tcStub->getCharList(inl7)[0] << ", " << tcStub->getCharList(inl7)[1] << endl; + cout << "==== ENUM ====" << endl; + Enum en; + en = APPLE; + Enum res = tcStub->handleEnum(en); + cout << "Return value: " << res << endl; + cout << "==== OTHERS ====" << endl; cout << "Return value: " << tcStub->getA() << endl; cout << "Return value: " << tcStub->setAndGetA(123) << endl; diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java index 2de5bb7..01924a5 100644 --- a/iotjava/iotrmi/Java/basics/TestClass.java +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -213,6 +213,25 @@ public class TestClass implements TestClassInterface { } + public Enum handleEnum(Enum en) { + + System.out.println("Enum: " + en); + + return en; + } + + + // Enum + /*public EnumJ[] handleEnum(EnumJ[] en) { + + for (EnumJ e : en) { + System.out.println("Enum: " + e); + } + + return en; + }*/ + + // Getters public String sumArray(String[] newA) { diff --git a/iotjava/iotrmi/Java/basics/TestClass_Stub.java b/iotjava/iotrmi/Java/basics/TestClass_Stub.java index 67b69dc..26ecd43 100644 --- a/iotjava/iotrmi/Java/basics/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/basics/TestClass_Stub.java @@ -60,6 +60,11 @@ public class TestClass_Stub { List inl7 = Arrays.asList(new Character[] { 'c', 'e' }); System.out.println("Return value: " + tcstub.getCharList(inl7)); + System.out.println("==== ENUM ===="); + Enum en = Enum.APPLE; + Enum res = tcstub.handleEnum(en); + System.out.println("Enum member: " + res); + System.out.println("==== OTHERS ===="); System.out.println("Return value: " + tcstub.getA()); System.out.println("Return value: " + tcstub.setAndGetA(123)); -- 2.34.1