Porting and compiling SmartLightsController
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index 0f696bec03967f6d72355335e4c41cdaaa68d1fb..4cd54182312c25e37f22aca4b11b1910dddf0cfe 100644 (file)
@@ -342,6 +342,17 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: updateIntfaceObjIdMap() updates the mapping between new interface and object Id
+        */
+       private void updateIntfaceObjIdMap(String intface, String newIntface) {
+
+               Integer objId = mapIntfaceObjId.get(intface);
+               mapNewIntfaceObjId.put(newIntface, objId);
+               mapIntfaceObjId.put(intface, objId++);
+       }
+
+
        /**
         * generateJavaInterfaces() generate stub interfaces based on the methods list in Java
         */
@@ -369,6 +380,7 @@ public class IoTCompiler {
                                // Write interface header
                                println("");
                                println("public interface " + newIntface + " {\n");
+                               updateIntfaceObjIdMap(intface, newIntface);
                                // Write methods
                                writeMethodJavaInterface(methods, intDecl);
                                println("}");
@@ -419,8 +431,8 @@ public class IoTCompiler {
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("private final static int objectId = " + objId + ";");
-               mapNewIntfaceObjId.put(newIntface, objId);
-               mapIntfaceObjId.put(intface, objId++);
+               //mapNewIntfaceObjId.put(newIntface, objId);
+               //mapIntfaceObjId.put(intface, objId++);
                if (callbackExist) {
                // We assume that each class only has one callback interface for now
                        Iterator it = callbackClasses.iterator();
@@ -511,14 +523,17 @@ public class IoTCompiler {
        /**
         * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
         */
-       private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl) {
+       private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl, boolean isGenCallbackStub) {
 
                println("public void ___initCallBack() {");
                // Generate main thread for callbacks
                println("Thread thread = new Thread() {");
                println("public void run() {");
                println("try {");
-               println("rmiObj = new IoTRMIObject(ports[0]);");
+               if (isGenCallbackStub)  // Use the second port for a _CallbackStub class (callback in callback)
+                       println("rmiObj = new IoTRMIObject(ports[1]);");
+               else
+                       println("rmiObj = new IoTRMIObject(ports[0]);");
                println("while (true) {");
                println("byte[] method = rmiObj.getMethodBytes();");
                writeJavaMethodCallbackPermission(intface);
@@ -544,8 +559,8 @@ public class IoTCompiler {
                int methodNumId = intDecl.getHelperMethodNumId(method);
                println("int methodId = " + methodNumId + ";");
                println("Class<?> retType = void.class;");
-               println("Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };");
-               println("Object[] paramObj = new Object[] { ports[0], callbackAddress, 0 };");
+               println("Class<?>[] paramCls = new Class<?>[] { int[].class, String.class, int.class };");
+               println("Object[] paramObj = new Object[] { ports, callbackAddress, 0 };");
                println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
                println("}\n");
        }
@@ -982,10 +997,10 @@ public class IoTCompiler {
                                String param = methParams.get(i);
                                if (isArrayOrList(paramType, param)) {  // Generate loop
                                        println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {");
-                                       println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);");
+                                       println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);");
                                } else
                                        println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
-                                               getSimpleIdentifier(param) + ", objIdCnt++);");
+                                               getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
                                println("listCallbackObj.add(skel" + i + ");");
                                if (isArrayOrList(paramType, param))
                                        println("}");
@@ -1002,7 +1017,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeMethodJavaStub() writes the methods of the stub class
         */
-       private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+       private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, boolean isGenCallbackStub) {
 
                boolean isDefined = false;
                for (String method : methods) {
@@ -1037,7 +1052,7 @@ public class IoTCompiler {
                        println("}\n");
                        // Write the init callback helper method
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackJavaStub(callbackType, intDecl);
+                               writeInitCallbackJavaStub(callbackType, intDecl, isGenCallbackStub);
                                isDefined = true;
                        }
                }
@@ -1079,7 +1094,7 @@ public class IoTCompiler {
                                // Write constructor
                                writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, false);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java...");
@@ -1094,7 +1109,7 @@ public class IoTCompiler {
        private void writePropertiesJavaCallbackStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
 
                println("private IoTRMICall rmiCall;");
-               println("private String address;");
+               println("private String callbackAddress;");
                println("private int[] ports;\n");
                // Get the object Id
                println("private int objectId = 0;");
@@ -1121,16 +1136,17 @@ public class IoTCompiler {
        private void writeConstructorJavaCallbackStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
                // TODO: If we want callback in callback, then we need to add address and port initializations
-               println("public " + newStubClass + "(IoTRMICall _rmiCall, int _objectId) throws Exception {");
+               println("public " + newStubClass + "(IoTRMICall _rmiCall, String _callbackAddress, int _objectId, int[] _ports) throws Exception {");
+               println("callbackAddress = _callbackAddress;");
                println("objectId = _objectId;");
                println("rmiCall = _rmiCall;");
+               println("ports = _ports;");
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
                        writeConstructorJavaPermission(intface);
                        println("listCallbackObj = new ArrayList<" + callbackType + ">();");
                        println("___initCallBack();");
-                       println("// TODO: Add address and port initialization here if we want callback in callback!");
                }
                println("}\n");
        }
@@ -1176,7 +1192,7 @@ public class IoTCompiler {
                                writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
                                // Write methods
                                // TODO: perhaps need to generate callback for callback
-                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, true);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
@@ -1193,10 +1209,12 @@ public class IoTCompiler {
                println("private " + intface + " mainObj;");
                //println("private int ports;");
                println("private IoTRMIObject rmiObj;\n");
+               println("private String callbackAddress;");
                // Callback
                if (callbackExist) {
                        println("private static int objIdCnt = 0;");
                        println("private IoTRMICall rmiCall;");
+                       println("private int[] ports;\n");
                }
                writePropertiesJavaPermission(intface, intDecl);
                println("\n");
@@ -1239,8 +1257,9 @@ public class IoTCompiler {
         */
        private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods, boolean callbackExist) {
 
-               println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
+               println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _port) throws Exception {");
                println("mainObj = _mainObj;");
+               println("callbackAddress = _callbackAddress;");
                println("rmiObj = new IoTRMIObject(_port);");
                // Generate permission control initialization
                writeConstructorJavaPermission(intface);
@@ -1282,9 +1301,13 @@ public class IoTCompiler {
                        println("public void ___regCB(IoTRMIObject rmiObj) throws IOException {");
                else
                        println("public void ___regCB() throws IOException {");
-               print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },");
+               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("ports = (int[]) paramObj[0];");
+               if (callbackSkeleton)   // If this is a callback skeleton then use the other port
+                       println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], ports[1]);");
+               else
+                       println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], ports[0]);");
                println("}\n");
        }
 
@@ -1354,7 +1377,7 @@ public class IoTCompiler {
                                        println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
                                        println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();");
                                } else {
-                                       println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+                                       println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
                                        println("objIdCnt++;");
                                }
                        }
@@ -1363,12 +1386,12 @@ public class IoTCompiler {
                                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);");
+                                       println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
                                        println("objIdCnt++;");
                                        println("}");
                                } else if (isList(paramType)) {
                                        println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
-                                       println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt));");
+                                       println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt, ports));");
                                        println("objIdCnt++;");
                                        println("}");
                                }
@@ -2164,10 +2187,13 @@ public class IoTCompiler {
                println("private " + intface + " mainObj;");
                // For callback skeletons, this is its own object Id
                println("private int objectId = 0;");
+               println("private String callbackAddress;");
                // Callback
                if (callbackExist) {
+
                        println("private static int objIdCnt = 0;");
                        println("private IoTRMICall rmiCall;");
+                       println("private int[] ports;\n");
                }
                println("\n");
        }
@@ -2178,7 +2204,8 @@ public class IoTCompiler {
         */
        private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
 
-               println("public " + newSkelClass + "(" + intface + " _mainObj, int _objectId) throws Exception {");
+               println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _objectId) throws Exception {");
+               println("callbackAddress = _callbackAddress;");
                println("mainObj = _mainObj;");
                println("objectId = _objectId;");
                println("}\n");
@@ -2542,6 +2569,7 @@ public class IoTCompiler {
                                println("#ifndef _" + newIntface.toUpperCase() + "_HPP__");
                                println("#define _" + newIntface.toUpperCase() + "_HPP__");
                                println("#include <iostream>");
+                               updateIntfaceObjIdMap(intface, newIntface);
                                // Pass in set of methods and get import classes
                                Set<String> methods = intMeth.getValue();
                                Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, false);
@@ -2566,7 +2594,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeMethodCplusStub() writes the methods of the stub
         */
-       private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+       private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, boolean isGenCallbackStub) {
 
                boolean isDefined = false;
                for (String method : methods) {
@@ -2601,7 +2629,7 @@ public class IoTCompiler {
                        println("}\n");
                        // Write the init callback helper method
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackCplusStub(callbackType, intDecl);
+                               writeInitCallbackCplusStub(callbackType, intDecl, isGenCallbackStub);
                                writeInitCallbackSendInfoCplusStub(intDecl);
                                isDefined = true;
                        }
@@ -2625,12 +2653,12 @@ public class IoTCompiler {
                                String param = methParams.get(i);
                                if (isArrayOrList(paramType, param)) {  // Generate loop
                                        println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {");
-                                       println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);");
+                                       println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);");
                                        isArrayOrList = true;
                                        callbackParam = getSimpleIdentifier(param);
                                } else
                                        println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
-                                               getSimpleIdentifier(param) + ", objIdCnt++);");
+                                               getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
                                println("vecCallbackObj.push_back(skel" + i + ");");
                                if (isArrayOrList)
                                        println("}");
@@ -3012,8 +3040,8 @@ public class IoTCompiler {
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("const static int objectId = " + objId + ";");
-               mapNewIntfaceObjId.put(newIntface, objId);
-               mapIntfaceObjId.put(intface, objId++);
+               //mapNewIntfaceObjId.put(newIntface, objId);
+               //mapIntfaceObjId.put(intface, objId++);
                if (callbackExist) {
                // We assume that each class only has one callback interface for now
                        Iterator it = callbackClasses.iterator();
@@ -3100,11 +3128,14 @@ public class IoTCompiler {
        /**
         * HELPER: writeInitCallbackCplusStub() writes the initialization of callback
         */
-       private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl) {
+       private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, boolean isGenCallbackStub) {
 
                println("void ___initCallBack() {");
                println("bool bResult = false;");
-               println("rmiObj = new IoTRMIObject(ports[0], &bResult);");
+               if (isGenCallbackStub)  // Use the second port for a _CallbackStub class (callback in callback)
+                       println("rmiObj = new IoTRMIObject(ports[1], &bResult);");
+               else
+                       println("rmiObj = new IoTRMIObject(ports[0], &bResult);");
                println("while (true) {");
                println("char* method = rmiObj->getMethodBytes();");
                writeCplusMethodCallbackPermission(intface);
@@ -3154,9 +3185,9 @@ public class IoTCompiler {
                int methodNumId = intDecl.getHelperMethodNumId(method);
                println("int methodId = " + 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], &callbackAddress, &rev };");
+               println("void* paramObj[] = { &ports, &callbackAddress, &rev };");
                println("void* retObj = NULL;");
                println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
                println("}\n");
@@ -3202,7 +3233,7 @@ public class IoTCompiler {
                                writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodCplusStub(methods, intDecl, callbackClasses);
+                               writeMethodCplusStub(methods, intDecl, callbackClasses, false);
                                print("}"); println(";");
                                if (callbackExist) {
                                        Iterator it = callbackClasses.iterator();
@@ -3238,7 +3269,7 @@ public class IoTCompiler {
                        println("vector<" + callbackType + "*> vecCallbackObj;");
                        println("static int objIdCnt;");
                        // TODO: Need to initialize address and ports if we want to have callback-in-callback
-                       println("string address;");
+                       println("string callbackAddress;");
                        println("vector<int> ports;\n");
                        writePropertiesCplusPermission(callbackType);
                }
@@ -3251,9 +3282,10 @@ public class IoTCompiler {
         */
        private void writeConstructorCplusCallbackStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
-               println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId) {");
+               println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId, vector<int> _ports) {");
                println("objectId = _objectId;");
                println("rmiCall = _rmiCall;");
+               println("ports = _ports;");
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
@@ -3304,7 +3336,7 @@ public class IoTCompiler {
                                writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses);
                                writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodCplusStub(methods, intDecl, callbackClasses);
+                               writeMethodCplusStub(methods, intDecl, callbackClasses, true);
                                println("};");
                                if (callbackExist) {
                                        Iterator it = callbackClasses.iterator();
@@ -3338,6 +3370,7 @@ public class IoTCompiler {
                        println("static int objIdCnt;");
                        println("vector<" + exchangeType + "*> vecCallbackObj;");
                        println("IoTRMICall *rmiCall;");
+                       println("vector<int> ports;");
                }
                println("IoTRMIObject *rmiObj;\n");
                // Keep track of object Ids of all stubs registered to this interface
@@ -3491,14 +3524,17 @@ public class IoTCompiler {
                else
                        println("void ___regCB() {");
                println("int numParam = 3;");
-               println("int param1 = 0;");
+               println("vector<int> param1;");
                println("string param2 = \"\";");
                println("int param3 = 0;");
-               println("string paramCls[] = { \"int\", \"String\", \"int\" };");
+               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);");
+               if (callbackSkeleton)
+                       println("rmiCall = new IoTRMICall(param1[1], param2.c_str(), param3, &bResult);");
+               else
+                       println("rmiCall = new IoTRMICall(param1[0], param2.c_str(), param3, &bResult);");
                println("}\n");
        }
 
@@ -3577,13 +3613,13 @@ public class IoTCompiler {
                                if (isArrayOrList(paramType, param)) {
                                        println("vector<" + exchParamType + "*> stub" + i + ";");
                                        println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
-                                       println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+                                       println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
                                        println("stub" + i + ".push_back(cb" + i + ");");
                                        println("vecCallbackObj.push_back(cb" + i + ");");
                                        println("objIdCnt++;");
                                        println("}");
                                } else {
-                                       println(exchParamType + "* stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+                                       println(exchParamType + "* stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
                                        println("vecCallbackObj.push_back(stub" + i + ");");
                                        println("objIdCnt++;");
                                }
@@ -4232,6 +4268,7 @@ public class IoTCompiler {
                        println("IoTRMICall* rmiCall;");
                        println("vector<" + exchangeType + "*> vecCallbackObj;");
                        println("static int objIdCnt;");
+                       println("vector<int> ports;\n");
                }
                println("\n");
        }
@@ -4628,13 +4665,14 @@ public class IoTCompiler {
        // Basically the compiler needs to parse the policy (and requires) files for callback class first
        private int getNewIntfaceObjectId(String newIntface) {
 
-               if (!mapNewIntfaceObjId.containsKey(newIntface)) {
-                       throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
-                                                       "Please place the two files for callback class in front...\n");
-               } else {
+//             if (!mapNewIntfaceObjId.containsKey(newIntface)) {
+//                     throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
+//                                                     "Please place the two files for callback class in front...\n");
+//                     return -1;
+//             } else {
                        int retObjId = mapNewIntfaceObjId.get(newIntface);
                        return retObjId;
-               }
+//             }
        }