From: rtrimana Date: Mon, 5 Dec 2016 22:53:39 +0000 (-0800) Subject: Testing callbacks for Java and C++; fixing a few bugs; more bugs to tackle: 1) Need... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c57b798aa1d5fa09f97ab62940636d0aaeceb091;p=iot2.git Testing callbacks for Java and C++; fixing a few bugs; more bugs to tackle: 1) Need to retest C++ skeleton and Java stub, 2) Array of callbacks contains wrong references in Java --- diff --git a/config/iotpolicy/testclasspolicy.pol b/config/iotpolicy/testclasspolicy.pol index 1d9d47a..e562cfa 100644 --- a/config/iotpolicy/testclasspolicy.pol +++ b/config/iotpolicy/testclasspolicy.pol @@ -32,6 +32,11 @@ public interface TestClassInterface { public Struct[] handleStructArray(Struct str[]); public List handleStructList(List str); + public void registerCallback(CallBackInterface _cb); + public void registerCallbackArray(CallBackInterface _cb[]); + public void registerCallbackList(List _cb); + public int callBack(); + public int getA(); public void setA(int _int); public void setB(float _float); @@ -72,6 +77,11 @@ public interface TestClassInterface { method = "handleStructArray(Struct str[])"; method = "handleStructList(List str)"; + method = "registerCallback(CallBackInterface _cb)"; + method = "registerCallbackArray(CallBackInterface _cb[])"; + method = "registerCallbackList(List _cb)"; + method = "callBack()"; + method = "getA()"; method = "setA(int _int)"; method = "setB(float _float)"; diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index c23896f..e461706 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -924,7 +924,7 @@ public class IoTCompiler { private String returnGenericCallbackType(String paramType) { if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES) - return getTypeOfGeneric(paramType)[0]; + return getGenericType(paramType); else return paramType; } @@ -953,12 +953,12 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String param = methParams.get(i); if (isArrayOrList(paramType, param)) { // Generate loop - println("for (" + paramType + " cb : " + getSimpleIdentifier(param) + ") {"); - println(callbackType + "_CallbackSkeleton skel = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); + println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {"); + println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); } else - println(callbackType + "_CallbackSkeleton skel = new " + callbackType + "_CallbackSkeleton(" + + println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" + getSimpleIdentifier(param) + ", objIdCnt++);"); - println("listCallbackObj.add(skel);"); + println("listCallbackObj.add(skel" + i + ");"); if (isArrayOrList(paramType, param)) println("}"); } @@ -1026,6 +1026,7 @@ public class IoTCompiler { */ private void writeMethodJavaStub(Collection methods, InterfaceDecl intDecl, Set callbackClasses) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -1057,8 +1058,10 @@ public class IoTCompiler { writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method); println("}\n"); // Write the init callback helper method - if (isCallbackMethod) + if (isCallbackMethod && !isDefined) { writeInitCallbackJavaStub(callbackType, intDecl); + isDefined = true; + } } } @@ -1301,8 +1304,8 @@ public class IoTCompiler { println("public void ___regCB(IoTRMIObject rmiObj) throws IOException {"); else println("public void ___regCB() throws IOException {"); - println("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class, String.class, int.class },"); - println("\tnew Class[] { null, null, null });"); + print("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class, String.class, int.class },"); + println("new Class[] { null, null, null });"); println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);"); println("}\n"); } @@ -1314,6 +1317,7 @@ public class IoTCompiler { private void writeMethodJavaSkeleton(Collection methods, InterfaceDecl intDecl, Set callbackClasses, boolean callbackSkeleton) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -1340,8 +1344,10 @@ public class IoTCompiler { // Now, write the body of skeleton! writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method)); println("}\n"); - if (isCallbackMethod) + if (isCallbackMethod && !isDefined) { // Make sure that this function is only defined once! writeInitCallbackJavaSkeleton(callbackSkeleton); + isDefined = true; + } } } @@ -1360,7 +1366,7 @@ public class IoTCompiler { //if (callbackType.equals(paramType)) { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object println("try {"); - String exchParamType = checkAndGetParamClass(paramType); + String exchParamType = checkAndGetParamClass(getGenericType(paramType)); // Print array if this is array or list if this is a list of callback objects if (isArray(param)) { println("int numStubs" + i + " = (int) paramObj[" + i + "];"); @@ -1375,7 +1381,7 @@ public class IoTCompiler { } // Generate a loop if needed if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String exchParamType = checkAndGetParamClass(paramType); + String exchParamType = checkAndGetParamClass(getGenericType(paramType)); if (isArray(param)) { println("for (int objId = 0; objId < numStubs" + i + "; objId++) {"); println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);"); @@ -1798,7 +1804,8 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String prmType = methPrmTypes.get(i); if ((getParamCategory(prmType) == ParamCategory.NONPRIMITIVES) && - !isEnumClass(getGenericType(prmType))) + !isEnumClass(getGenericType(prmType)) && + !callbackClasses.contains(getGenericType(prmType))) print(getGenericType(prmType) + ".class"); else print("null"); @@ -2568,6 +2575,7 @@ public class IoTCompiler { */ private void writeMethodCplusStub(Collection methods, InterfaceDecl intDecl, Set callbackClasses) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -2578,7 +2586,7 @@ public class IoTCompiler { String callbackType = null; for (int i = 0; i < methParams.size(); i++) { - String paramType = methPrmTypes.get(i); + String paramType = returnGenericCallbackType(methPrmTypes.get(i)); // Check if this has callback object if (callbackClasses.contains(paramType)) { isCallbackMethod = true; @@ -2597,12 +2605,13 @@ public class IoTCompiler { if (isCallbackMethod) writeCallbackMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType); else - writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method); + writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackClasses); println("}\n"); // Write the init callback helper method - if (isCallbackMethod) { + if (isCallbackMethod && !isDefined) { writeInitCallbackCplusStub(callbackType, intDecl); writeInitCallbackSendInfoCplusStub(intDecl); + isDefined = true; } } } @@ -2623,7 +2632,7 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String param = methParams.get(i); if (isArrayOrList(paramType, param)) { // Generate loop - println("for (" + paramType + "* cb : " + getSimpleIdentifier(param) + ") {"); + println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {"); println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);"); isArrayOrList = true; callbackParam = getSimpleIdentifier(param); @@ -2631,7 +2640,7 @@ public class IoTCompiler { println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(" + getSimpleIdentifier(param) + ", objIdCnt++);"); println("vecCallbackObj.push_back(skel);"); - if (isArrayOrList(paramType, param)) + if (isArrayOrList) println("}"); } } @@ -2958,7 +2967,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) { + List methPrmTypes, String method, Set callbackClasses) { checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method); println("int methodId = " + intDecl.getMethodNumId(method) + ";"); @@ -2971,8 +2980,13 @@ public class IoTCompiler { println("int numParam = " + methParams.size() + ";"); print("string paramCls[] = { "); for (int i = 0; i < methParams.size(); i++) { - String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); - print("\"" + paramTypeC + "\""); + String paramType = returnGenericCallbackType(methPrmTypes.get(i)); + if (callbackClasses.contains(paramType)) { + print("\"int\""); + } else { + String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); + print("\"" + paramTypeC + "\""); + } // Check if this is the last element (don't print a comma) if (i != methParams.size() - 1) { print(", "); @@ -3188,7 +3202,7 @@ public class IoTCompiler { println("int methodId = " + methodNumId + ";"); //writeCplusCallbackPermission(intface, methodNumId); println("string retType = \"void\";"); - println("string paramCls[] = { \"int\", \"string\", \"int\" };"); + println("string paramCls[] = { \"int\", \"String\", \"int\" };"); println("int rev = 0;"); println("void* paramObj[] = { &ports[0], &address, &rev };"); println("void* retObj = NULL;"); @@ -3238,8 +3252,14 @@ public class IoTCompiler { // Write methods writeMethodCplusStub(methods, intDecl, callbackClasses); print("}"); println(";"); - if (callbackExist) - writePermissionInitializationCplus(intface, newStubClass, intDecl); + if (callbackExist) { + Iterator it = callbackClasses.iterator(); + String callbackType = (String) it.next(); + // Generate permission stuff for callback stubs + DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType); + InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType); + writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback); + } writeObjectIdCountInitializationCplus(newStubClass, callbackExist); println("#endif"); pw.close(); @@ -3334,8 +3354,14 @@ public class IoTCompiler { // Write methods writeMethodCplusStub(methods, intDecl, callbackClasses); println("};"); - if (callbackExist) - writePermissionInitializationCplus(intface, newStubClass, intDecl); + if (callbackExist) { + Iterator it = callbackClasses.iterator(); + String callbackType = (String) it.next(); + // Generate permission stuff for callback stubs + DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType); + InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType); + writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback); + } writeObjectIdCountInitializationCplus(newStubClass, callbackExist); println("#endif"); pw.close(); @@ -3388,7 +3414,7 @@ public class IoTCompiler { for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); - print("set " + newSkelClass + "::set" + newObjectId + "Allowed {"); + print("set " + newSkelClass + "::set" + newObjectId + "Allowed { "); Set methodIds = intMeth.getValue(); int i = 0; for (String methodId : methodIds) { @@ -3516,7 +3542,7 @@ public class IoTCompiler { println("int param1 = 0;"); println("string param2 = \"\";"); println("int param3 = 0;"); - println("string paramCls[] = { \"int\", \"string\", \"int\" };"); + println("string paramCls[] = { \"int\", \"String\", \"int\" };"); println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };"); println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); println("bool bResult = false;"); @@ -3531,6 +3557,7 @@ public class IoTCompiler { private void writeMethodCplusSkeleton(Collection methods, InterfaceDecl intDecl, Set callbackClasses, boolean callbackSkeleton) { + boolean isDefined = false; for (String method : methods) { List methParams = intDecl.getMethodParams(method); @@ -3560,8 +3587,10 @@ public class IoTCompiler { // Now, write the body of skeleton! writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method)); println("}\n"); - if (isCallbackMethod) + if (isCallbackMethod && !isDefined) { writeInitCallbackCplusSkeleton(callbackSkeleton); + isDefined = true; + } } } @@ -3575,11 +3604,8 @@ public class IoTCompiler { String paramType = methPrmTypes.get(i); String param = methParams.get(i); //if (callbackType.equals(paramType)) { - if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String exchParamType = checkAndGetParamClass(paramType); - // Print array if this is array or list if this is a list of callback objects + if (checkCallbackType(paramType, callbackType)) // Check if this has callback object println("int numStubs" + i + " = 0;"); - } } } @@ -3595,13 +3621,13 @@ public class IoTCompiler { String param = methParams.get(i); // Generate a loop if needed if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - String exchParamType = checkAndGetParamClass(paramType); + String exchParamType = checkAndGetParamClass(getGenericType(paramType)); if (isArrayOrList(paramType, param)) { - println("vector<" + exchParamType + "> stub;"); + println("vector<" + exchParamType + "*> stub" + i + ";"); println("for (int objId = 0; objId < numStubs" + i + "; objId++) {"); println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);"); - println("stub" + i + ".push_back(cb);"); - println("vecCallbackObj.push_back(cb);"); + println("stub" + i + ".push_back(cb" + i + ");"); + println("vecCallbackObj.push_back(cb" + i + ");"); println("objIdCnt++;"); println("}"); } else { @@ -5058,14 +5084,14 @@ public class IoTCompiler { if (paramType.contains("<") && paramType.contains(">")) { String genericClass = getSimpleType(paramType); - String[] genericType = getTypeOfGeneric(paramType); + String genericType = getGenericType(paramType); String cplusTemplate = null; - if (genericType.length == 1) // Generic/template with one type - cplusTemplate = getNonPrimitiveCplusClass(genericClass) + - "<" + convertType(genericType[0]) + ">"; - else // Generic/template with two types - cplusTemplate = getNonPrimitiveCplusClass(genericClass) + - "<" + convertType(genericType[0]) + "," + convertType(genericType[1]) + ">"; + cplusTemplate = getNonPrimitiveCplusClass(genericClass); + if(getParamCategory(getGenericType(paramType)) == ParamCategory.USERDEFINED) { + cplusTemplate = cplusTemplate + "<" + genericType + "*>"; + } else { + cplusTemplate = cplusTemplate + "<" + convertType(genericType) + ">"; + } return cplusTemplate; } else return getNonPrimitiveCplusClass(paramType); @@ -5077,7 +5103,6 @@ public class IoTCompiler { } else // Just return it as is if it's not non-primitives return paramType; - //return checkAndGetParamClass(paramType, true); } @@ -5242,6 +5267,9 @@ public class IoTCompiler { // Check if this is generics if(getParamCategory(paramType) == ParamCategory.USERDEFINED) { return exchangeParamType(paramType); + } else if (isList(paramType) && + (getParamCategory(getGenericType(paramType)) == ParamCategory.USERDEFINED)) { + return "List<" + exchangeParamType(getGenericType(paramType)) + ">"; } else return paramType; } diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index 58a6d2a..f2951ba 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -40,7 +40,8 @@ class TestClass : public TestClassInterface { // Callbacks void registerCallback(CallBackInterfaceWithCallBack* _cb); - //void registerCallback(vector _cb); + void registerCallbackArray(vector _cb); + void registerCallbackList(vector _cb); int callBack(); // Enum @@ -94,13 +95,22 @@ void TestClass::registerCallback(CallBackInterfaceWithCallBack* _cb) { } -/*void TestClass::registerCallback(vector _cb) { +void TestClass::registerCallbackArray(vector _cb) { - for (CallBackInterface* cb : _cb) { + for (CallBackInterfaceWithCallBack* cb : _cb) { cbvec.push_back(cb); - cout << "Registering callback object!" << endl; + cout << "Registering callback object in array!" << endl; } -}*/ +} + + +void TestClass::registerCallbackList(vector _cb) { + + for (CallBackInterfaceWithCallBack* cb : _cb) { + cbvec.push_back(cb); + cout << "Registering callback object in list!" << endl; + } +} int TestClass::callBack() { diff --git a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp index 96011d1..207eb8b 100644 --- a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp @@ -1,6 +1,7 @@ #include #include #include "TestClassComplete_Stub.hpp" +#include "CallBack.hpp" using namespace std; @@ -24,7 +25,7 @@ int main(int argc, char *argv[]) cout << "Return value: " << tcStub->getDouble(12345.678) << endl; cout << "Return value: " << tcStub->getBoolean(true) << endl; cout << "Return value: " << tcStub->getChar('c') << endl; - cout << "==== ARRAY ====" << endl; +/* cout << "==== ARRAY ====" << endl; vector in1; in1.push_back(68); in1.push_back(69); @@ -127,7 +128,29 @@ int main(int argc, char *argv[]) cout << "Name: " << st.name << endl; cout << "Value:" << st.value << endl; cout << "Year" << st.year << endl; - } + }*/ + cout << "==== CALLBACK ====" << endl; + CallBackInterface *cbSingle = new CallBack(2354); + tcStub->registerCallback(cbSingle); + cout << "Return value from callback: " << tcStub->callBack() << endl; + CallBackInterface *cb1 = new CallBack(23); + CallBackInterface *cb2 = new CallBack(33); + CallBackInterface *cb3 = new CallBack(43); + vector cb; + cb.push_back(cb1); + cb.push_back(cb2); + cb.push_back(cb3); + tcStub->registerCallbackArray(cb); + cout << "Return value from callback: " << tcStub->callBack() << endl; + CallBackInterface *cb4 = new CallBack(23); + CallBackInterface *cb5 = new CallBack(33); + CallBackInterface *cb6 = new CallBack(43); + vector cblist; + cblist.push_back(cb4); + cblist.push_back(cb5); + cblist.push_back(cb6); + tcStub->registerCallbackList(cblist); + cout << "Return value from callback: " << tcStub->callBack() << endl; cout << "==== OTHERS ====" << endl; cout << "Return value: " << tcStub->getA() << endl; diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java index a5c0c11..7031dc2 100644 --- a/iotjava/iotrmi/Java/basics/TestClass.java +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -20,6 +20,7 @@ public class TestClass implements TestClassInterface { intA = 1; floatB = 2; stringC = "345"; + cblist = new ArrayList(); } @@ -28,6 +29,7 @@ public class TestClass implements TestClassInterface { intA = _int; floatB = _float; stringC = _string; + cblist = new ArrayList(); } @@ -40,13 +42,22 @@ public class TestClass implements TestClassInterface { } - /*public void registerCallback(CallBackInterfaceWithCallBack[] _cb) { + public void registerCallbackArray(CallBackInterfaceWithCallBack _cb[]) { for (CallBackInterfaceWithCallBack cb : _cb) { cblist.add(cb); - System.out.println("Registering callback object!"); + System.out.println("Registering callback objects in array!"); } - }*/ + } + + + public void registerCallbackList(List _cb) { + + for (CallBackInterfaceWithCallBack cb : _cb) { + cblist.add(cb); + System.out.println("Registering callback objects in list!"); + } + } public int callBack() { diff --git a/iotjava/iotrmi/Java/basics/TestClass_Stub.java b/iotjava/iotrmi/Java/basics/TestClass_Stub.java index 01daf30..d82c905 100644 --- a/iotjava/iotrmi/Java/basics/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/basics/TestClass_Stub.java @@ -28,7 +28,7 @@ public class TestClass_Stub { System.out.println("Return value: " + tcstub.getBoolean(true)); System.out.println("Return value: " + tcstub.getChar('c')); - System.out.println("==== ARRAY ===="); +/* System.out.println("==== ARRAY ===="); byte[] in1 = { 68, 69 }; System.out.println("Return value: " + Arrays.toString(tcstub.getByteArray(in1))); short[] in2 = { (short)1234, (short)1235 }; @@ -98,6 +98,26 @@ public class TestClass_Stub { System.out.println("Value: " + st.value); System.out.println("Year: " + st.year); } +*/ + System.out.println("==== CALLBACKS ===="); + CallBackInterface cbSingle = new CallBack(2354); + tcstub.registerCallback(cbSingle); + System.out.println("Return value from callback: " + tcstub.callBack()); + //CallBackInterface cbSingle2 = new CallBack(2355); + //tcstub.registerCallback(cbSingle2); + //System.out.println("Return value from callback: " + tcstub.callBack()); + /*CallBackInterface cb1 = new CallBack(23); + CallBackInterface cb2 = new CallBack(33); + CallBackInterface cb3 = new CallBack(43); + CallBackInterface[] cb = { cb1, cb2, cb3 }; + tcstub.registerCallbackArray(cb); + System.out.println("Return value from callback: " + tcstub.callBack());*/ + List cblist = new ArrayList(); + CallBackInterface cb1 = new CallBack(23); cblist.add(cb1); + CallBackInterface cb2 = new CallBack(33); cblist.add(cb2); + CallBackInterface cb3 = new CallBack(43); cblist.add(cb3); + tcstub.registerCallbackList(cblist); + System.out.println("Return value from callback: " + tcstub.callBack()); System.out.println("==== OTHERS ===="); System.out.println("Return value: " + tcstub.getA());