Cleaning up callback code generation; Fixing a few minor issues, e.g. indentation...
authorrtrimana <rtrimana@uci.edu>
Tue, 29 Nov 2016 18:40:16 +0000 (10:40 -0800)
committerrtrimana <rtrimana@uci.edu>
Tue, 29 Nov 2016 18:40:16 +0000 (10:40 -0800)
iotjava/iotpolicy/IoTCompiler.java
iotjava/iotrmi/C++/basics/TestClass.hpp
iotjava/iotrmi/Java/basics/TestClass.java
iotjava/iotrmi/Java/sample/TestClass_Stub.java

index 5af3c5e..8571998 100644 (file)
@@ -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<String,Set<String>> 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<int> 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[] = { &param1, &param2, &param3 };");
+               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...");
index e5c3898..492d3d9 100644 (file)
@@ -5,6 +5,7 @@
 #include <thread>
 #include <chrono>
 #include "TestClassInterface.hpp"
+#include "CallBackInterfaceWithCallBack.hpp"
 
 using namespace std;
 
@@ -38,8 +39,8 @@ class TestClass : public TestClassInterface {
                vector<char>            getCharList(vector<char> in);
 
                // Callbacks
-               void                            registerCallback(CallBackInterface* _cb);
-               //void                          registerCallback(vector<CallBackInterface*> _cb);
+               void                            registerCallback(CallBackInterfaceWithCallBack* _cb);
+               //void                          registerCallback(vector<CallBackInterfaceWithCallBack*> _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<CallBackInterface*>      cbvec;
+               int                                                                             intA;
+               float                                                                   floatB;
+               string                                                                  stringC;
+               vector<CallBackInterfaceWithCallBack*>  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<CallBackInterface*> _cb) {
+/*void TestClass::registerCallback(vector<CallBackInterfaceWithCallBack*> _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();
        }
 
index 616606d..2de5bb7 100644 (file)
@@ -10,7 +10,7 @@ public class TestClass implements TestClassInterface {
        private int intA;
        private float floatB;
        private String stringC;
-       private List<CallBackInterface> cblist;
+       private List<CallBackInterfaceWithCallBack> 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;
index 2a45396..a08ce62 100644 (file)
@@ -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