From 2fad50e1aa24179079a3b04904b7fd945e642d46 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 29 Nov 2016 10:40:16 -0800 Subject: [PATCH] Cleaning up callback code generation; Fixing a few minor issues, e.g. indentation, etc. --- iotjava/iotpolicy/IoTCompiler.java | 47 ++++++++++++------- iotjava/iotrmi/C++/basics/TestClass.hpp | 19 ++++---- iotjava/iotrmi/Java/basics/TestClass.java | 13 ++--- .../iotrmi/Java/sample/TestClass_Stub.java | 2 - 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 5af3c5e..8571998 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -508,11 +508,13 @@ public class IoTCompiler { println(intface + "_CallbackSkeleton skel = (" + intface + "_CallbackSkeleton) listCallbackObj.get(objId);"); println("if (skel != null) {"); println("skel.invokeMethod(rmiObj);"); - println("} else {"); + print("}"); + println(" else {"); println("throw new Error(\"" + intface + ": Object with Id \" + objId + \" not found!\");"); println("}"); println("}"); - println("} catch (Exception ex) {"); + print("}"); + println(" catch (Exception ex) {"); println("ex.printStackTrace();"); println("throw new Error(\"Error instantiating class " + intface + "_CallbackSkeleton!\");"); println("}"); @@ -936,7 +938,8 @@ public class IoTCompiler { println("}"); } } - println("} catch (Exception ex) {"); + print("}"); + println(" catch (Exception ex) {"); println("ex.printStackTrace();"); println("throw new Error(\"Exception when generating skeleton objects!\");"); println("}\n"); @@ -1642,7 +1645,8 @@ public class IoTCompiler { println("rmiObj.sendReturnObj(retObj);"); } if (isCallbackMethod) { // Catch exception if this is callback - println("} catch(Exception ex) {"); + print("}"); + println(" catch(Exception ex) {"); println("ex.printStackTrace();"); println("throw new Error(\"Exception from callback object instantiation!\");"); println("}"); @@ -2972,7 +2976,7 @@ public class IoTCompiler { for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); - println("const static int object" + newObjectId + "Id = " + newObjectId + ";"); + println("const static int object" + newObjectId + "Id = " + newObjectId + ";\t//" + newIntface); println("const static set set" + newObjectId + "Allowed;"); } } @@ -3017,7 +3021,6 @@ public class IoTCompiler { println("ports = _ports;"); println("rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);"); if (callbackExist) { - println("objIdCnt = 0;"); Iterator it = callbackClasses.iterator(); String callbackType = (String) it.next(); println("thread th1 (&" + newStubClass + "::___initCallBack, this);"); @@ -3091,7 +3094,8 @@ public class IoTCompiler { println(intface + "_CallbackSkeleton* skel = dynamic_cast<" + intface + "_CallbackSkeleton*> (vecCallbackObj.at(objId));"); println("skel->invokeMethod(rmiObj);"); - println("} else {"); + print("}"); + println(" else {"); println("cerr << \"Illegal object Id: \" << to_string(objId);"); // TODO: perhaps need to change this into "throw" to make it cleaner (allow stack unfolding) println("exit(-1);"); @@ -3164,6 +3168,7 @@ public class IoTCompiler { print("}"); println(";"); if (callbackExist) writePermissionInitializationCplus(intface, newStubClass, intDecl); + writeObjectIdCountInitializationCplus(newStubClass, callbackExist); println("#endif"); pw.close(); System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".hpp..."); @@ -3179,7 +3184,7 @@ public class IoTCompiler { println("IoTRMICall *rmiCall;"); // Get the object Id - println("static int objectId;"); + println("int objectId;"); if (callbackExist) { // We assume that each class only has one callback interface for now Iterator it = callbackClasses.iterator(); @@ -3259,6 +3264,7 @@ public class IoTCompiler { println("};"); if (callbackExist) writePermissionInitializationCplus(intface, newStubClass, intDecl); + writeObjectIdCountInitializationCplus(newStubClass, callbackExist); println("#endif"); pw.close(); System.out.println("IoTCompiler: Generated callback stub class " + newIntface + ".hpp..."); @@ -3290,6 +3296,16 @@ public class IoTCompiler { } + /** + * HELPER: writeObjectIdCountInitializationCplus() writes the initialization of objIdCnt variable + */ + private void writeObjectIdCountInitializationCplus(String newSkelClass, boolean callbackExist) { + + if (callbackExist) + println("int " + newSkelClass + "::objIdCnt = 0;"); + } + + /** * HELPER: writePermissionInitializationCplus() writes the initialization of permission set */ @@ -3326,11 +3342,6 @@ public class IoTCompiler { println("bool _bResult = false;"); println("mainObj = _mainObj;"); println("rmiObj = new IoTRMIObject(_port, &_bResult);"); - // Callback - if (callbackExist) { - println("objIdCnt = 0;"); - } - //println("set0Allowed = Arrays.asList(object0Permission);"); println("___waitRequestInvokeMethod();"); println("}\n"); } @@ -3400,7 +3411,9 @@ public class IoTCompiler { println("int param1 = 0;"); println("string param2 = \"\";"); println("int param3 = 0;"); + println("string paramCls[] = { \"int\", \"string\", \"int\" };"); println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };"); + println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); println("bool bResult = false;"); println("rmiCall = new IoTRMICall(param1, param2.c_str(), param3, &bResult);"); println("}\n"); @@ -4116,6 +4129,7 @@ public class IoTCompiler { writeCplusWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface); println("};"); writePermissionInitializationCplus(intface, newSkelClass, intDecl); + writeObjectIdCountInitializationCplus(newSkelClass, callbackExist); println("#endif"); pw.close(); System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".hpp..."); @@ -4130,7 +4144,7 @@ public class IoTCompiler { println(intface + " *mainObj;"); // Keep track of object Ids of all stubs registered to this interface - println("static int objectId;"); + println("int objectId;"); // Callback if (callbackExist) { Iterator it = callbackClasses.iterator(); @@ -4153,10 +4167,6 @@ public class IoTCompiler { println(newSkelClass + "(" + intface + " *_mainObj, int _objectId) {"); println("mainObj = _mainObj;"); println("objectId = _objectId;"); - // Callback - if (callbackExist) { - println("objIdCnt = 0;"); - } println("}\n"); } @@ -4338,6 +4348,7 @@ public class IoTCompiler { // Write waitRequestInvokeMethod() - main loop writeCplusCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist); println("};"); + writeObjectIdCountInitializationCplus(newSkelClass, callbackExist); println("#endif"); pw.close(); System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".hpp..."); diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index e5c3898..492d3d9 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -5,6 +5,7 @@ #include #include #include "TestClassInterface.hpp" +#include "CallBackInterfaceWithCallBack.hpp" using namespace std; @@ -38,8 +39,8 @@ class TestClass : public TestClassInterface { vector getCharList(vector in); // Callbacks - void registerCallback(CallBackInterface* _cb); - //void registerCallback(vector _cb); + void registerCallback(CallBackInterfaceWithCallBack* _cb); + //void registerCallback(vector _cb); int callBack(); int getA(); @@ -51,10 +52,10 @@ class TestClass : public TestClassInterface { int setACAndGetA(string newC, int newA); private: - int intA; - float floatB; - string stringC; - vector cbvec; + int intA; + float floatB; + string stringC; + vector cbvec; }; @@ -76,14 +77,14 @@ TestClass::TestClass(int _int, float _float, string _string) { } -void TestClass::registerCallback(CallBackInterface* _cb) { +void TestClass::registerCallback(CallBackInterfaceWithCallBack* _cb) { cbvec.push_back(_cb); cout << "Registering callback object!" << endl; } -/*void TestClass::registerCallback(vector _cb) { +/*void TestClass::registerCallback(vector _cb) { for (CallBackInterface* cb : _cb) { cbvec.push_back(cb); @@ -95,7 +96,7 @@ void TestClass::registerCallback(CallBackInterface* _cb) { int TestClass::callBack() { int sum = 0; - for (CallBackInterface* cb : cbvec) { + for (CallBackInterfaceWithCallBack* cb : cbvec) { sum = sum + cb->printInt(); } diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java index 616606d..2de5bb7 100644 --- a/iotjava/iotrmi/Java/basics/TestClass.java +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -10,7 +10,7 @@ public class TestClass implements TestClassInterface { private int intA; private float floatB; private String stringC; - private List cblist; + private List cblist; /** * Constructors @@ -32,16 +32,17 @@ public class TestClass implements TestClassInterface { // Callback - public void registerCallback(CallBackInterface _cb) { + //public void registerCallback(CallBackInterface _cb) { + public void registerCallback(CallBackInterfaceWithCallBack _cb) { - cblist.add(cb); + cblist.add(_cb); System.out.println("Registering callback object!"); } - /*public void registerCallback(CallBackInterface[] _cb) { + /*public void registerCallback(CallBackInterfaceWithCallBack[] _cb) { - for (CallBackInterface cb : _cb) { + for (CallBackInterfaceWithCallBack cb : _cb) { cblist.add(cb); System.out.println("Registering callback object!"); } @@ -51,7 +52,7 @@ public class TestClass implements TestClassInterface { public int callBack() { int sum = 0; - for (CallBackInterface cb : cblist) { + for (CallBackInterfaceWithCallBack cb : cblist) { sum = sum + cb.printInt(); } return sum; diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java index 2a45396..a08ce62 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Stub.java @@ -68,9 +68,7 @@ public class TestClass_Stub implements TestClassInterface { Thread thread = new Thread() { public void run() { try{ - System.out.println("Created server with port: " + ports[0]); rmiObj = new IoTRMIObject(ports[0]); - Object retObj = null; while (true) { byte[] method = rmiObj.getMethodBytes(); // Permission checking -- 2.34.1