From 3b467e9892ac26b0f2ea2f1dd13760f310ba801c Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 7 Feb 2017 15:31:07 -0800 Subject: [PATCH] Making sure that compiler can generate methods with multiple callbacks with different types --- iotjava/Makefile | 5 +- iotjava/iotpolicy/IoTCompiler.java | 231 +++++++----------- .../development/callbackpolicy_two.pol | 14 ++ .../development/callbackrequires_two.pol | 3 + .../testclasspolicy_callbacks_three.pol | 31 +++ .../testclassrequires_callbacks_three.pol | 3 + 6 files changed, 149 insertions(+), 138 deletions(-) create mode 100644 localconfig/iotpolicy/development/callbackpolicy_two.pol create mode 100644 localconfig/iotpolicy/development/callbackrequires_two.pol create mode 100644 localconfig/iotpolicy/development/testclasspolicy_callbacks_three.pol create mode 100644 localconfig/iotpolicy/development/testclassrequires_callbacks_three.pol diff --git a/iotjava/Makefile b/iotjava/Makefile index 4aa6ad3..da48eaa 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -26,6 +26,7 @@ run-compiler-dev: #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler callbackpolicy.pol callbackrequires.pol testclasspolicy_advanced.pol testclassrequires_advanced.pol -cplus Cplus -java Java #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler callbackpolicy.pol callbackrequires.pol testclasspolicy_callbacks.pol testclassrequires_callbacks.pol -cplus Cplus -java Java cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_callbacks.pol testclassrequires_callbacks.pol callbackpolicy.pol callbackrequires.pol -cplus Cplus -java Java + #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_callbacks_three.pol testclassrequires_callbacks_three.pol callbackpolicy.pol callbackrequires.pol callbackpolicy_two.pol callbackrequires_two.pol -cplus Cplus -java Java PHONY += run-compiler-lbtest run-compiler-lbtest: @@ -113,9 +114,9 @@ compile: #cp ./iotrmi/C++/basics/* $(BIN_DIR)/iotpolicy/output_files/Cplus cd $(BIN_DIR)/iotpolicy/output_files; cp *.java ./Java cd $(BIN_DIR)/iotpolicy/output_files; cp *.hpp ./Cplus - cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClass_Skeleton.java + #cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClass_Skeleton.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassAdvanced_Stub.java - cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassCallbacks_Stub.java + #cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassCallbacks_Stub.java cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassInterface_Skeleton.java -Xlint:unchecked cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassComplete_Stub.java -Xlint:unchecked cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterfaceWithCallBack_Stub.java -Xlint:unchecked diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 8848d2c..f127cda 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -758,7 +758,7 @@ public class IoTCompiler { /** * HELPER: writeStructParamClassJavaStub() writes parameters if struct is present */ - private void writeStructParamClassJavaStub(List methParams, List methPrmTypes, String callbackType) { + private void writeStructParamClassJavaStub(List methParams, List methPrmTypes, Set callbackType) { print("int paramLen = "); writeLengthStructParamClassJavaStub(methParams, methPrmTypes); @@ -894,7 +894,7 @@ public class IoTCompiler { * HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class */ private void writeStdMethodBodyJavaStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, String callbackType) { + List methPrmTypes, String method, Set callbackType) { checkAndWriteStructSetupJavaStub(methParams, methPrmTypes, intDecl, method); println("int methodId = " + intDecl.getMethodNumId(method) + ";"); @@ -974,6 +974,24 @@ public class IoTCompiler { } + /** + * HELPER: checkCallbackType() checks the callback type + */ + private boolean checkCallbackType(String paramType, Set callbackType) { + + String prmType = returnGenericCallbackType(paramType); + if (callbackType == null) // If there is no callbackType it means not a callback method + return false; + else { + for (String type : callbackType) { + if (type.equals(prmType)) + return true; // Check callbackType one by one + } + return false; + } + } + + /** * HELPER: checkCallbackType() checks the callback type */ @@ -1031,7 +1049,7 @@ public class IoTCompiler { * HELPER: writeCallbackMethodBodyJavaStub() writes the callback method of the stub class */ private void writeCallbackMethodBodyJavaStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, String callbackType) { + List methPrmTypes, String method, Set callbackType) { // Determine callback object counter type (List vs. single variable) for (int i = 0; i < methParams.size(); i++) { @@ -1056,9 +1074,9 @@ public class IoTCompiler { if (isArrayOrList(paramType, param)) { // Generate loop println("int cnt" + i + " = 0;"); println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {"); - writeCallbackInstantiationMethodBodyJavaStub("cb", callbackType, i, true); + writeCallbackInstantiationMethodBodyJavaStub("cb", returnGenericCallbackType(paramType), i, true); } else - writeCallbackInstantiationMethodBodyJavaStub(getSimpleIdentifier(param), callbackType, i, false); + writeCallbackInstantiationMethodBodyJavaStub(getSimpleIdentifier(param), returnGenericCallbackType(paramType), i, false); if (isArrayOrList(paramType, param)) println("}"); } @@ -1083,14 +1101,16 @@ public class IoTCompiler { print("public " + intDecl.getMethodType(method) + " " + intDecl.getMethodId(method) + "("); boolean isCallbackMethod = false; - String callbackType = null; + //String callbackType = null; + Set callbackType = new HashSet(); for (int i = 0; i < methParams.size(); i++) { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); // Check if this has callback object if (callbackClasses.contains(paramType)) { isCallbackMethod = true; - callbackType = paramType; + //callbackType = paramType; + callbackType.add(paramType); // Even if there're 2 callback arguments, we expect them to be of the same interface } print(methPrmTypes.get(i) + " " + methParams.get(i)); @@ -1292,8 +1312,7 @@ public class IoTCompiler { /** * HELPER: writeMethodJavaSkeleton() writes the method of the skeleton class */ - private void writeMethodJavaSkeleton(Collection methods, InterfaceDecl intDecl, Set callbackClasses, - boolean callbackSkeleton, String intface) { + private void writeMethodJavaSkeleton(Collection methods, InterfaceDecl intDecl) { for (String method : methods) { @@ -1301,16 +1320,10 @@ public class IoTCompiler { List methPrmTypes = intDecl.getMethodParamTypes(method); String methodId = intDecl.getMethodId(method); print("public " + intDecl.getMethodType(method) + " " + methodId + "("); - boolean isCallbackMethod = false; - String callbackType = null; for (int i = 0; i < methParams.size(); i++) { String origParamType = methPrmTypes.get(i); String paramType = checkAndGetParamClass(origParamType); - if (callbackClasses.contains(origParamType)) { // Check if this has callback object - isCallbackMethod = true; - callbackType = origParamType; - } print(paramType + " " + methParams.get(i)); // Check if this is the last element (don't print a comma) if (i != methParams.size() - 1) { @@ -1328,18 +1341,17 @@ public class IoTCompiler { /** * HELPER: writeCallbackInstantiationJavaStubGeneration() writes the instantiation of callback stubs */ - private void writeCallbackInstantiationJavaStubGeneration(String exchParamType) { + private void writeCallbackInstantiationJavaStubGeneration(String exchParamType, int counter) { - //println("int objIdRecv = (int[]) paramObj[0];"); - println(exchParamType + " newStub = null;"); - println("if(!IoTRMIUtil.mapStub.containsKey(objIdRecv)) {"); - println("newStub = new " + exchParamType + "_Stub(rmiComm, objIdRecv);"); - println("IoTRMIUtil.mapStub.put(objIdRecv, newStub);"); - println("rmiComm.setObjectIdCounter(objIdRecv);"); + println(exchParamType + " newStub" + counter + " = null;"); + println("if(!IoTRMIUtil.mapStub.containsKey(objIdRecv" + counter + ")) {"); + println("newStub" + counter + " = new " + exchParamType + "_Stub(rmiComm, objIdRecv" + counter + ");"); + println("IoTRMIUtil.mapStub.put(objIdRecv" + counter + ", newStub" + counter + ");"); + println("rmiComm.setObjectIdCounter(objIdRecv" + counter + ");"); println("rmiComm.decrementObjectIdCounter();"); println("}"); println("else {"); - println("newStub = (" + exchParamType + "_Stub) IoTRMIUtil.mapStub.get(objIdRecv);"); + println("newStub" + counter + " = (" + exchParamType + "_Stub) IoTRMIUtil.mapStub.get(objIdRecv" + counter + ");"); println("}"); } @@ -1348,7 +1360,7 @@ public class IoTCompiler { * HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part */ private Map writeCallbackJavaStubGeneration(List methParams, List methPrmTypes, - String callbackType, boolean isStructMethod) { + Set callbackType, boolean isStructMethod) { Map mapStubParam = new HashMap(); String offsetPfx = ""; @@ -1369,8 +1381,8 @@ public class IoTCompiler { println("int numStubs" + i + " = stubIdArray" + i + ".length;"); println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();"); } else { - println("int objIdRecv = stubIdArray" + i + "[0];"); - writeCallbackInstantiationJavaStubGeneration(exchParamType); + println("int objIdRecv" + i + " = stubIdArray" + i + "[0];"); + writeCallbackInstantiationJavaStubGeneration(exchParamType, i); } } // Generate a loop if needed @@ -1378,18 +1390,18 @@ public class IoTCompiler { String exchParamType = checkAndGetParamClass(getGenericType(paramType)); if (isArray(param)) { println("for (int i = 0; i < numStubs" + i + "; i++) {"); - println("int objIdRecv = stubIdArray" + i + "[i];"); - writeCallbackInstantiationJavaStubGeneration(exchParamType); - println("stub" + i + "[i] = newStub;"); + println("int objIdRecv" + i + " = stubIdArray" + i + "[i];"); + writeCallbackInstantiationJavaStubGeneration(exchParamType, i); + println("stub" + i + "[i] = newStub" + i + ";"); println("}"); } else if (isList(paramType)) { println("for (int i = 0; i < numStubs" + i + "; i++) {"); - println("int objIdRecv = stubIdArray" + i + "[i];"); - writeCallbackInstantiationJavaStubGeneration(exchParamType); - println("stub" + i + ".add(newStub);"); + println("int objIdRecv" + i + " = stubIdArray" + i + "[i];"); + writeCallbackInstantiationJavaStubGeneration(exchParamType, i); + println("stub" + i + ".add(newStub" + i + ");"); println("}"); } else - println(exchParamType + " stub" + i + " = newStub;"); + println(exchParamType + " stub" + i + " = newStub" + i + ";"); mapStubParam.put(i, "stub" + i); // List of all stub parameters } } @@ -1673,7 +1685,7 @@ public class IoTCompiler { * HELPER: writeMethodHelperReturnJavaSkeleton() writes return statement part in skeleton */ private void writeMethodHelperReturnJavaSkeleton(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, boolean isCallbackMethod, String callbackType, + List methPrmTypes, String method, boolean isCallbackMethod, Set callbackType, boolean isStructMethod) { checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes, isStructMethod); @@ -1741,7 +1753,8 @@ public class IoTCompiler { // Generate array of parameter objects boolean isCallbackMethod = false; - String callbackType = null; + //String callbackType = null; + Set callbackType = new HashSet(); println("byte[] localMethodBytes = methodBytes;"); println("rmiComm.setGetMethodBytes();"); print("int paramLen = "); @@ -1761,7 +1774,8 @@ public class IoTCompiler { String prmType = returnGenericCallbackType(methPrmTypes.get(i)); if (callbackClasses.contains(prmType)) { isCallbackMethod = true; - callbackType = prmType; + //callbackType = prmType; + callbackType.add(prmType); println("paramCls[pos] = int[].class;"); println("paramClsGen[pos++] = null;"); } else { // Generate normal classes if it's not a callback object @@ -1791,7 +1805,8 @@ public class IoTCompiler { // Generate array of parameter objects boolean isCallbackMethod = false; - String callbackType = null; + //String callbackType = null; + Set callbackType = new HashSet(); println("byte[] localMethodBytes = methodBytes;"); println("rmiComm.setGetMethodBytes();"); print("Object[] paramObj = rmiComm.getMethodParams(new Class[] { "); @@ -1800,7 +1815,8 @@ public class IoTCompiler { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); if (callbackClasses.contains(paramType)) { isCallbackMethod = true; - callbackType = paramType; + //callbackType = paramType; + callbackType.add(paramType); print("int[].class"); } else { // Generate normal classes if it's not a callback object String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); @@ -2278,7 +2294,7 @@ public class IoTCompiler { // Write function to return didAlreadyInitWaitInvoke writeReturnDidAlreadyInitWaitInvoke(); // Write methods - writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface); + writeMethodJavaSkeleton(methods, intDecl); // Write method helper writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses); // Write waitRequestInvokeMethod() - main loop @@ -2554,7 +2570,7 @@ public class IoTCompiler { /** * HELPER: writeMethodDeclCplusStub() writes the method declarations of the stub */ - private void writeMethodDeclCplusStub(Collection methods, InterfaceDecl intDecl, Set callbackClasses, String newStubClass) { + private void writeMethodDeclCplusStub(Collection methods, InterfaceDecl intDecl) { for (String method : methods) { @@ -2562,17 +2578,9 @@ public class IoTCompiler { List methPrmTypes = intDecl.getMethodParamTypes(method); print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " + intDecl.getMethodId(method) + "("); - boolean isCallbackMethod = false; - String callbackType = null; for (int i = 0; i < methParams.size(); i++) { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); - // Check if this has callback object - if (callbackClasses.contains(paramType)) { - isCallbackMethod = true; - callbackType = paramType; - // Even if there're 2 callback arguments, we expect them to be of the same interface - } String methPrmType = checkAndGetCplusType(methPrmTypes.get(i)); String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); print(methParamComplete); @@ -2602,14 +2610,15 @@ public class IoTCompiler { print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " + newStubClass + "::" + intDecl.getMethodId(method) + "("); boolean isCallbackMethod = false; - String callbackType = null; + Set callbackType = new HashSet(); for (int i = 0; i < methParams.size(); i++) { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); // Check if this has callback object if (callbackClasses.contains(paramType)) { isCallbackMethod = true; - callbackType = paramType; + //callbackType = paramType; + callbackType.add(paramType); // Even if there're 2 callback arguments, we expect them to be of the same interface } String methPrmType = checkAndGetCplusType(methPrmTypes.get(i)); @@ -2636,13 +2645,12 @@ public class IoTCompiler { */ private void writeCallbackInstantiationMethodBodyCplusStub(String paramIdent, String callbackType, int counter) { - println("auto it = IoTRMIUtil::mapSkel->find(" + paramIdent + ");"); - println("if (it == IoTRMIUtil::mapSkel->end()) {"); + println("auto it" + counter + " = IoTRMIUtil::mapSkel->find(" + paramIdent + ");"); + println("if (it" + counter + " == IoTRMIUtil::mapSkel->end()) {"); println("int newObjIdSent = rmiComm->getObjectIdCounter();"); println("objIdSent" + counter + ".push_back(newObjIdSent);"); println("rmiComm->decrementObjectIdCounter();"); println(callbackType + "_Skeleton* skel" + counter + " = new " + callbackType + "_Skeleton(" + paramIdent + ", rmiComm, newObjIdSent);"); - println("vecCallbackObj.push_back(skel" + counter + ");"); println("IoTRMIUtil::mapSkel->insert(make_pair(" + paramIdent + ", skel" + counter + "));"); println("IoTRMIUtil::mapSkelId->insert(make_pair(" + paramIdent + ", newObjIdSent));"); println("thread th" + counter + " (&" + callbackType + "_Skeleton::___waitRequestInvokeMethod, std::ref(skel" + counter + @@ -2662,7 +2670,7 @@ public class IoTCompiler { * HELPER: writeCallbackMethodBodyCplusStub() writes the callback method of the stub class */ private void writeCallbackMethodBodyCplusStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, String callbackType) { + List methPrmTypes, String method, Set callbackType) { // Check if this is single object, array, or list of objects boolean isArrayOrList = false; @@ -2674,11 +2682,11 @@ public class IoTCompiler { String param = methParams.get(i); if (isArrayOrList(paramType, param)) { // Generate loop println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {"); - writeCallbackInstantiationMethodBodyCplusStub("cb", callbackType, i); + writeCallbackInstantiationMethodBodyCplusStub("cb", returnGenericCallbackType(paramType), i); isArrayOrList = true; callbackParam = getSimpleIdentifier(param); } else { - writeCallbackInstantiationMethodBodyCplusStub(getSimpleIdentifier(param), callbackType, i); + writeCallbackInstantiationMethodBodyCplusStub(getSimpleIdentifier(param), returnGenericCallbackType(paramType), i); } if (isArrayOrList) println("}"); @@ -2841,7 +2849,7 @@ public class IoTCompiler { /** * HELPER: writeStructParamClassCplusStub() writes member parameters of struct */ - private void writeStructParamClassCplusStub(List methParams, List methPrmTypes, String callbackType) { + private void writeStructParamClassCplusStub(List methParams, List methPrmTypes, Set callbackType) { print("int numParam = "); writeLengthStructParamClassCplusStub(methParams, methPrmTypes); @@ -2978,7 +2986,7 @@ public class IoTCompiler { * HELPER: writeStdMethodBodyCplusStub() writes the standard method body in the stub class */ private void writeStdMethodBodyCplusStub(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, String callbackType, boolean isCallbackMethod) { + List methPrmTypes, String method, Set callbackType, boolean isCallbackMethod) { checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method); println("int methodId = " + intDecl.getMethodNumId(method) + ";"); @@ -3072,13 +3080,6 @@ public class IoTCompiler { // Get the object Id Integer objId = mapIntfaceObjId.get(intface); println("int objectId = " + objId + ";"); - if (callbackExist) { - // We assume that each class only has one callback interface for now - Iterator it = callbackClasses.iterator(); - String callbackType = (String) it.next(); - println("// Callback properties"); - println("vector<" + callbackType + "*> vecCallbackObj;"); - } println("// Synchronization variables"); for (String method : methods) { // Generate AtomicBooleans for methods that have return values @@ -3147,17 +3148,6 @@ public class IoTCompiler { println("delete rmiComm;"); println("rmiComm = NULL;"); println("}"); - if (callbackExist) { - // We assume that each class only has one callback interface for now - //Iterator it = callbackClasses.iterator(); - //String callbackType = (String) it.next(); - for (String callbackType : callbackClasses) { - println("for(" + callbackType + "* cb : vecCallbackObj) {"); - println("delete cb;"); - println("cb = NULL;"); - println("}"); - } - } println("}"); println(""); } @@ -3225,7 +3215,7 @@ public class IoTCompiler { println(newStubClass + "(IoTRMIComm* _rmiComm, int _objectId);"); println("~" + newStubClass + "();"); // Write methods - writeMethodDeclCplusStub(methods, intDecl, callbackClasses, newStubClass); + writeMethodDeclCplusStub(methods, intDecl); print("}"); println(";"); println("#endif"); pw.close(); @@ -3317,14 +3307,6 @@ public class IoTCompiler { private void writePropertiesCplusSkeleton(String intface, boolean callbackExist, Set callbackClasses) { println(intface + " *mainObj;"); - // Callback - if (callbackExist) { - Iterator it = callbackClasses.iterator(); - String callbackType = (String) it.next(); - String exchangeType = checkAndGetParamClass(callbackType); - println("// Callback properties"); - println("vector<" + exchangeType + "*> vecCallbackObj;"); - } println("IoTRMIComm *rmiComm;"); println("char* methodBytes;"); println("int methodLen;"); @@ -3450,18 +3432,6 @@ public class IoTCompiler { println("delete rmiComm;"); println("rmiComm = NULL;"); println("}"); - if (callbackExist) { - // We assume that each class only has one callback interface for now - //Iterator it = callbackClasses.iterator(); - //String callbackType = (String) it.next(); - for (String callbackType : callbackClasses) { - String exchangeType = checkAndGetParamClass(callbackType); - println("for(" + exchangeType + "* cb : vecCallbackObj) {"); - println("delete cb;"); - println("cb = NULL;"); - println("}"); - } - } println("}"); println(""); } @@ -3527,8 +3497,7 @@ public class IoTCompiler { /** * HELPER: writeMethodCplusSkeleton() writes the method of the skeleton class */ - private void writeMethodCplusSkeleton(Collection methods, InterfaceDecl intDecl, - Set callbackClasses, boolean callbackSkeleton, String intface, String newSkelClass) { + private void writeMethodCplusSkeleton(Collection methods, InterfaceDecl intDecl, String newSkelClass) { for (String method : methods) { @@ -3537,15 +3506,9 @@ public class IoTCompiler { String methodId = intDecl.getMethodId(method); String methodType = checkAndGetCplusType(intDecl.getMethodType(method)); print(methodType + " " + newSkelClass + "::" + methodId + "("); - boolean isCallbackMethod = false; - String callbackType = null; for (int i = 0; i < methParams.size(); i++) { String origParamType = methPrmTypes.get(i); - if (callbackClasses.contains(origParamType)) { // Check if this has callback object - isCallbackMethod = true; - callbackType = origParamType; - } String paramType = checkAndGetParamClass(methPrmTypes.get(i)); String methPrmType = checkAndGetCplusType(paramType); String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); @@ -3566,7 +3529,7 @@ public class IoTCompiler { /** * HELPER: writeCallbackCplusNumStubs() writes the numStubs variable */ - private void writeCallbackCplusNumStubs(List methParams, List methPrmTypes, String callbackType) { + private void writeCallbackCplusNumStubs(List methParams, List methPrmTypes, Set callbackType) { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); @@ -3582,20 +3545,18 @@ public class IoTCompiler { /** * HELPER: writeCallbackInstantiationCplusStubGeneration() writes the instantiation of callback stubs */ - private void writeCallbackInstantiationCplusStubGeneration(String exchParamType) { + private void writeCallbackInstantiationCplusStubGeneration(String exchParamType, int counter) { - //println("int objIdRecv = (int[]) paramObj[0];"); - println(exchParamType + "* newStub = NULL;"); - println("auto it = IoTRMIUtil::mapStub->find(objIdRecv);"); - println("if (it == IoTRMIUtil::mapStub->end()) {"); - println("newStub = new " + exchParamType + "_Stub(rmiComm, objIdRecv);"); - println("IoTRMIUtil::mapStub->insert(make_pair(objIdRecv, newStub));"); - println("rmiComm->setObjectIdCounter(objIdRecv);"); + println(exchParamType + "* newStub" + counter + " = NULL;"); + println("auto it" + counter + " = IoTRMIUtil::mapStub->find(objIdRecv" + counter + ");"); + println("if (it" + counter + " == IoTRMIUtil::mapStub->end()) {"); + println("newStub" + counter + " = new " + exchParamType + "_Stub(rmiComm, objIdRecv" + counter + ");"); + println("IoTRMIUtil::mapStub->insert(make_pair(objIdRecv" + counter + ", newStub" + counter + "));"); + println("rmiComm->setObjectIdCounter(objIdRecv" + counter + ");"); println("rmiComm->decrementObjectIdCounter();"); - println("skel->vecCallbackObj.push_back(newStub);"); println("}"); println("else {"); - println("newStub = (" + exchParamType + "_Stub*) it->second;"); + println("newStub" + counter + " = (" + exchParamType + "_Stub*) it" + counter + "->second;"); println("}"); } @@ -3603,7 +3564,7 @@ public class IoTCompiler { /** * HELPER: writeCallbackCplusStubGeneration() writes the callback stub generation part */ - private void writeCallbackCplusStubGeneration(List methParams, List methPrmTypes, String callbackType) { + private void writeCallbackCplusStubGeneration(List methParams, List methPrmTypes, Set callbackType) { // Iterate over callback objects for (int i = 0; i < methParams.size(); i++) { @@ -3615,14 +3576,14 @@ public class IoTCompiler { if (isArrayOrList(paramType, param)) { println("vector<" + exchParamType + "*> stub" + i + ";"); println("for (int i = 0; i < numStubIdArray" + i + ".size(); i++) {"); - println("int objIdRecv = numStubIdArray" + i + "[i];"); - writeCallbackInstantiationCplusStubGeneration(exchParamType); - println("stub" + i + ".push_back(newStub);"); + println("int objIdRecv" + i + " = numStubIdArray" + i + "[i];"); + writeCallbackInstantiationCplusStubGeneration(exchParamType, i); + println("stub" + i + ".push_back(newStub" + i + ");"); println("}"); } else { - println("int objIdRecv = numStubIdArray" + i + "[0];"); - writeCallbackInstantiationCplusStubGeneration(exchParamType); - println(exchParamType + "* stub" + i + " = newStub;"); + println("int objIdRecv" + i + " = numStubIdArray" + i + "[0];"); + writeCallbackInstantiationCplusStubGeneration(exchParamType, i); + println(exchParamType + "* stub" + i + " = newStub" + i + ";"); } } } @@ -3712,7 +3673,7 @@ public class IoTCompiler { * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return statement part in skeleton */ private void writeMethodHelperReturnCplusSkeleton(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, boolean isCallbackMethod, String callbackType, + List methPrmTypes, String method, boolean isCallbackMethod, Set callbackType, String methodId, Set callbackClasses) { println("rmiComm->getMethodParams(paramCls, numParam, paramObj, localMethodBytes);"); @@ -3758,13 +3719,15 @@ public class IoTCompiler { // Generate array of parameter types boolean isCallbackMethod = false; - String callbackType = null; + //String callbackType = null; + Set callbackType = new HashSet(); print("string paramCls[] = { "); for (int i = 0; i < methParams.size(); i++) { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); if (callbackClasses.contains(paramType)) { isCallbackMethod = true; - callbackType = paramType; + //callbackType = paramType; + callbackType.add(paramType); print("\"int*\""); } else { // Generate normal classes if it's not a callback object String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); @@ -3952,7 +3915,7 @@ public class IoTCompiler { // Generate array of parameter objects boolean isCallbackMethod = false; - String callbackType = null; + Set callbackType = new HashSet(); print("int numParam = "); writeLengthStructParamClassSkeleton(methParams, methPrmTypes, method, intDecl); println(";"); @@ -3970,7 +3933,8 @@ public class IoTCompiler { String prmType = returnGenericCallbackType(methPrmTypes.get(i)); if (callbackClasses.contains(prmType)) { isCallbackMethod = true; - callbackType = prmType; + //callbackType = prmType; + callbackType.add(prmType); //println("int numStubs" + i + " = 0;"); println("vector numStubIdArray" + i + ";"); println("paramCls[pos] = \"int*\";"); @@ -4001,8 +3965,7 @@ public class IoTCompiler { /** * HELPER: writeMethodHelperDeclCplusSkeleton() writes the method helper declarations of the skeleton class */ - private void writeMethodHelperDeclCplusSkeleton(Collection methods, InterfaceDecl intDecl, - Set callbackClasses, String newSkelClass) { + private void writeMethodHelperDeclCplusSkeleton(Collection methods, InterfaceDecl intDecl, String newSkelClass) { // Use this set to handle two same methodIds Set uniqueMethodIds = new HashSet(); @@ -4346,7 +4309,7 @@ public class IoTCompiler { println("bool didInitWaitInvoke();"); writeMethodDeclCplusSkeleton(methods, intDecl, callbackClasses); // Write method helper declarations - writeMethodHelperDeclCplusSkeleton(methods, intDecl, callbackClasses, newSkelClass); + writeMethodHelperDeclCplusSkeleton(methods, intDecl, newSkelClass); // Write waitRequestInvokeMethod() declaration - main loop println("void ___waitRequestInvokeMethod(" + newSkelClass + "* skel);"); println("};"); @@ -4427,7 +4390,7 @@ public class IoTCompiler { // Write didInitWaitInvoke() to return bool writeReturnDidAlreadyInitWaitInvoke(newSkelClass); // Write methods - writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false, intface, newSkelClass); + writeMethodCplusSkeleton(methods, intDecl, newSkelClass); // Write method helper writeMethodHelperCplusSkeleton(methods, intDecl, callbackClasses, newSkelClass); // Write waitRequestInvokeMethod() - main loop @@ -5331,10 +5294,8 @@ public class IoTCompiler { comp.generateCPlusInterfaces(); comp.generateCPlusStubClassesHpp(); comp.generateCPlusStubClassesCpp(); - //comp.generateCPlusCallbackStubClasses(); comp.generateCplusSkeletonClassHpp(); comp.generateCplusSkeletonClassCpp(); - //comp.generateCplusCallbackSkeletonClass(); } else { // Check other options while(i < args.length) { @@ -5362,10 +5323,8 @@ public class IoTCompiler { comp.generateCPlusInterfaces(); comp.generateCPlusStubClassesHpp(); comp.generateCPlusStubClassesCpp(); - //comp.generateCPlusCallbackStubClasses(); comp.generateCplusSkeletonClassHpp(); comp.generateCplusSkeletonClassCpp(); - //comp.generateCplusCallbackSkeletonClass(); } } i = i + 2; diff --git a/localconfig/iotpolicy/development/callbackpolicy_two.pol b/localconfig/iotpolicy/development/callbackpolicy_two.pol new file mode 100644 index 0000000..3bf4ad2 --- /dev/null +++ b/localconfig/iotpolicy/development/callbackpolicy_two.pol @@ -0,0 +1,14 @@ +public interface CallBackInterfaceSecond { + + public int printInt(); + public void needCallbackToo(TestClassInterface tc); + + capability CallBack { + description = "The quick brown fox jumps over the smart dog"; + description = "Another description"; + method = "printInt()"; + method = "needCallbackToo(TestClassInterface tc)"; + } +} + + diff --git a/localconfig/iotpolicy/development/callbackrequires_two.pol b/localconfig/iotpolicy/development/callbackrequires_two.pol new file mode 100644 index 0000000..e0a798d --- /dev/null +++ b/localconfig/iotpolicy/development/callbackrequires_two.pol @@ -0,0 +1,3 @@ + +requires CallBackInterfaceSecond with CallBack as interface CallBackInterfaceWithCallBackSecond; + diff --git a/localconfig/iotpolicy/development/testclasspolicy_callbacks_three.pol b/localconfig/iotpolicy/development/testclasspolicy_callbacks_three.pol new file mode 100644 index 0000000..f332961 --- /dev/null +++ b/localconfig/iotpolicy/development/testclasspolicy_callbacks_three.pol @@ -0,0 +1,31 @@ +public interface TestClassInterface { + + public short getShort(short in); + public void registerCallback(CallBackInterface _cb); + public void registerCallback(CallBackInterfaceSecond _cb, CallBackInterface _cb2); + public int callBack(); + + capability Callbacks { + description = "All the set-and-get methods"; + method = "getShort(short in)"; + method = "registerCallback(CallBackInterface _cb)"; + method = "registerCallback(CallBackInterfaceSecond _cb, CallBackInterface _cb2)"; + method = "callBack()"; + } + + enum Enum { + + APPLE, + ORANGE, + GRAPE + } + + struct Struct { + + string name; + float value; + int year; + } +} + + diff --git a/localconfig/iotpolicy/development/testclassrequires_callbacks_three.pol b/localconfig/iotpolicy/development/testclassrequires_callbacks_three.pol new file mode 100644 index 0000000..4ae575b --- /dev/null +++ b/localconfig/iotpolicy/development/testclassrequires_callbacks_three.pol @@ -0,0 +1,3 @@ + +requires TestClassInterface with Callbacks as interface TestClassComplete; + -- 2.34.1