Minor fixes in LifxLightBulb driver (not fully tested yet)
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index a5a6781293882ba2fdf854085b8f8b6b87abd466..a46a20be8ffc9a63d42ee85d52289a400b1535b5 100644 (file)
@@ -48,6 +48,7 @@ public class IoTCompiler {
        private Map<String,ParseTreeHandler> mapIntfacePTH;
        private Map<String,DeclarationHandler> mapIntDeclHand;
        private Map<String,Map<String,Set<String>>> mapInt2NewInts;
+       private Map<String,String> mapInt2NewIntName;
        // Data structure to store our types (primitives and non-primitives) for compilation
        private Map<String,String> mapPrimitives;
        private Map<String,String> mapNonPrimitivesJava;
@@ -58,6 +59,8 @@ public class IoTCompiler {
        private PrintWriter pw;
        private String dir;
        private String subdir;
+       private Map<String,Integer> mapPortCount;       // Counter for ports
+       private static int portCount = 0;
 
 
        /**
@@ -83,6 +86,7 @@ public class IoTCompiler {
                mapIntfacePTH = new HashMap<String,ParseTreeHandler>();
                mapIntDeclHand = new HashMap<String,DeclarationHandler>();
                mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
+               mapInt2NewIntName = new HashMap<String,String>();
                mapIntfaceObjId = new HashMap<String,Integer>();
                mapNewIntfaceObjId = new HashMap<String,Integer>();
                mapPrimitives = new HashMap<String,String>();
@@ -91,6 +95,7 @@ public class IoTCompiler {
                        arraysToMap(mapNonPrimitivesJava, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitiveJavaLibs);
                mapNonPrimitivesCplus = new HashMap<String,String>();
                        arraysToMap(mapNonPrimitivesCplus, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
+               mapPortCount = new HashMap<String,Integer>();
                pw = null;
                dir = OUTPUT_DIRECTORY;
                subdir = null;
@@ -177,6 +182,9 @@ public class IoTCompiler {
                        }
                        // Add interface and methods information into map
                        mapNewIntMethods.put(strInt, setMethods);
+                       // Map new interface method name to the original interface
+                       // TODO: perhaps need to check in the future if we have more than 1 stub interface for one original interface
+                       mapInt2NewIntName.put(origInt, strInt);
                }
                // Map the map of interface-methods to the original interface
                mapInt2NewInts.put(origInt, mapNewIntMethods);
@@ -342,6 +350,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
         */
@@ -361,7 +380,7 @@ public class IoTCompiler {
                                DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                                InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                                // Pass in set of methods and get import classes
-                               List<String> methods = intDecl.getMethods();
+                               Set<String> methods = intMeth.getValue();
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
                                List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
@@ -369,6 +388,7 @@ public class IoTCompiler {
                                // Write interface header
                                println("");
                                println("public interface " + newIntface + " {\n");
+                               updateIntfaceObjIdMap(intface, newIntface);
                                // Write methods
                                writeMethodJavaInterface(methods, intDecl);
                                println("}");
@@ -419,8 +439,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();
@@ -428,7 +448,7 @@ public class IoTCompiler {
                        println("// Callback properties");
                        println("private IoTRMIObject rmiObj;");
                        println("List<" + callbackType + "> listCallbackObj;");
-                       println("private static int objIdCnt = 0;");
+                       println("private int objIdCnt = 0;");
                        // Generate permission stuff for callback stubs
                        DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
@@ -464,8 +484,11 @@ public class IoTCompiler {
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
-                       writeConstructorJavaPermission(intface);
+                       writeConstructorJavaPermission(callbackType);
                        println("listCallbackObj = new ArrayList<" + callbackType + ">();");
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeJavaInitCallbackPermission(callbackType, intDecl, callbackExist);
                        println("___initCallBack();");
                }
                println("}\n");
@@ -508,23 +531,35 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: getPortCount() gets port count for different stubs and skeletons
+        */
+       private int getPortCount(String intface) {
+
+               if (!mapPortCount.containsKey(intface))
+                       mapPortCount.put(intface, portCount++);
+               return mapPortCount.get(intface);
+       }
+
+
        /**
         * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
         */
-       private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl) {
+       private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl, String newStubClass) {
 
                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]);");
+               int port = getPortCount(newStubClass);
+               println("rmiObj = new IoTRMIObject(ports[" + port + "]);");
                println("while (true) {");
                println("byte[] method = rmiObj.getMethodBytes();");
-               writeJavaMethodCallbackPermission(intface);
                println("int objId = IoTRMIObject.getObjectId(method);");
                println(intface + "_CallbackSkeleton skel = (" + intface + "_CallbackSkeleton) listCallbackObj.get(objId);");
                println("if (skel != null) {");
+               writeJavaMethodCallbackPermission(intface);
                println("skel.invokeMethod(rmiObj);");
                print("}");
                println(" else {");
@@ -544,8 +579,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 +1017,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 +1037,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, String newStubClass) {
 
                boolean isDefined = false;
                for (String method : methods) {
@@ -1037,13 +1072,22 @@ public class IoTCompiler {
                        println("}\n");
                        // Write the init callback helper method
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackJavaStub(callbackType, intDecl);
+                               writeInitCallbackJavaStub(callbackType, intDecl, newStubClass);
                                isDefined = true;
                        }
                }
        }
 
 
+       /**
+        * HELPER: getStubInterface() gets stub interface name based on original interface
+        */
+       public String getStubInterface(String intface) {
+
+               return mapInt2NewIntName.get(intface);
+       }
+
+
        /**
         * generateJavaStubClasses() generate stubs based on the methods list in Java
         */
@@ -1079,7 +1123,7 @@ public class IoTCompiler {
                                // Write constructor
                                writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java...");
@@ -1094,7 +1138,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;");
@@ -1105,7 +1149,7 @@ public class IoTCompiler {
                        println("// Callback properties");
                        println("private IoTRMIObject rmiObj;");
                        println("List<" + callbackType + "> listCallbackObj;");
-                       println("private static int objIdCnt = 0;");
+                       println("private int objIdCnt = 0;");
                        // Generate permission stuff for callback stubs
                        DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
@@ -1121,16 +1165,20 @@ 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);
+                       writeConstructorJavaPermission(callbackType);
                        println("listCallbackObj = new ArrayList<" + callbackType + ">();");
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeJavaInitCallbackPermission(callbackType, intDecl, callbackExist);
                        println("___initCallBack();");
-                       println("// TODO: Add address and port initialization here if we want callback in callback!");
                }
                println("}\n");
        }
@@ -1176,7 +1224,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, newStubClass);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
@@ -1193,10 +1241,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 int objIdCnt = 0;");
                        println("private IoTRMICall rmiCall;");
+                       println("private int[] ports;\n");
                }
                writePropertiesJavaPermission(intface, intDecl);
                println("\n");
@@ -1239,8 +1289,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);
@@ -1275,16 +1326,23 @@ public class IoTCompiler {
        /**
         * HELPER: writeInitCallbackJavaSkeleton() writes the init callback method for skeleton class
         */
-       private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton) {
+       private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton, String intface) {
 
                // This is a callback skeleton generation
                if (callbackSkeleton)
                        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];");
+               String stubInt = null;
+               if (callbackSkeleton)
+                       stubInt = getStubInterface(intface) + "_CallbackStub";
+               else
+                       stubInt = getStubInterface(intface) + "_Stub";
+               int port = getPortCount(stubInt);
+               println("rmiCall = new IoTRMICall(ports[" + port + "], (String) paramObj[1], (int) paramObj[2]);");
                println("}\n");
        }
 
@@ -1293,7 +1351,7 @@ public class IoTCompiler {
         * HELPER: writeMethodJavaSkeleton() writes the method of the skeleton class
         */
        private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, 
-                       boolean callbackSkeleton) {
+                       boolean callbackSkeleton, String intface) {
 
                boolean isDefined = false;
                for (String method : methods) {
@@ -1323,7 +1381,7 @@ public class IoTCompiler {
                        writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method));
                        println("}\n");
                        if (isCallbackMethod && !isDefined) {   // Make sure that this function is only defined once!
-                               writeInitCallbackJavaSkeleton(callbackSkeleton);
+                               writeInitCallbackJavaSkeleton(callbackSkeleton, intface);
                                isDefined = true;
                        }
                }
@@ -1354,7 +1412,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 +1421,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, callbackAddress, objIdCnt, ports));");
                                        println("objIdCnt++;");
                                        println("}");
                                }
@@ -2144,7 +2202,7 @@ public class IoTCompiler {
                        // Write constructor
                        writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
                        // Write methods
-                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false);
+                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface);
                        // Write method helper
                        writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
@@ -2164,10 +2222,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 int objIdCnt = 0;");
                        println("private IoTRMICall rmiCall;");
+                       println("private int[] ports;\n");
                }
                println("\n");
        }
@@ -2178,7 +2239,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");
@@ -2326,7 +2388,7 @@ public class IoTCompiler {
                        // Write constructor
                        writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods);
                        // Write methods
-                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true);
+                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true, intface);
                        // Write method helper
                        writeMethodHelperJavaCallbackSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
@@ -2542,8 +2604,10 @@ 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> includeClasses = getIncludeClasses(intMeth.getValue(), intDecl, intface, false);
+                               Set<String> methods = intMeth.getValue();
+                               Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, false);
                                List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
                                List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
                                printIncludeStatements(allIncludeClasses); println("");                 
@@ -2552,7 +2616,7 @@ public class IoTCompiler {
                                println("{");
                                println("public:");
                                // Write methods
-                               writeMethodCplusInterface(intMeth.getValue(), intDecl);
+                               writeMethodCplusInterface(methods, intDecl);
                                println("};");
                                println("#endif");
                                pw.close();
@@ -2565,7 +2629,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, String newStubClass) {
 
                boolean isDefined = false;
                for (String method : methods) {
@@ -2600,7 +2664,7 @@ public class IoTCompiler {
                        println("}\n");
                        // Write the init callback helper method
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackCplusStub(callbackType, intDecl);
+                               writeInitCallbackCplusStub(callbackType, intDecl, newStubClass);
                                writeInitCallbackSendInfoCplusStub(intDecl);
                                isDefined = true;
                        }
@@ -2624,12 +2688,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("}");
@@ -3011,8 +3075,6 @@ 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++);
                if (callbackExist) {
                // We assume that each class only has one callback interface for now
                        Iterator it = callbackClasses.iterator();
@@ -3034,13 +3096,16 @@ public class IoTCompiler {
        private void writeConstructorCplusStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
                println(newStubClass + 
-                       "(int _port, const char* _skeletonAddress, const char* _callbackAddress, int _rev, bool* _bResult, vector<int> _ports) {");
+                       "(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _ports) {");
                println("callbackAddress = _callbackAddress;");
                println("ports = _ports;");
                println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev, _bResult);");
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist);
                        println("thread th1 (&" + newStubClass + "::___initCallBack, this);");
                        println("th1.detach();");
                        println("___regCB();");
@@ -3099,18 +3164,20 @@ public class IoTCompiler {
        /**
         * HELPER: writeInitCallbackCplusStub() writes the initialization of callback
         */
-       private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl) {
+       private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, String newStubClass) {
 
                println("void ___initCallBack() {");
                println("bool bResult = false;");
-               println("rmiObj = new IoTRMIObject(ports[0], &bResult);");
+               int port = getPortCount(newStubClass);
+               println("rmiObj = new IoTRMIObject(ports[" + port + "], &bResult);");
                println("while (true) {");
                println("char* method = rmiObj->getMethodBytes();");
-               writeCplusMethodCallbackPermission(intface);
+               //writeCplusMethodCallbackPermission(intface);
                println("int objId = IoTRMIObject::getObjectId(method);");
                println("if (objId < vecCallbackObj.size()) {   // Check if still within range");
                println(intface + "_CallbackSkeleton* skel = dynamic_cast<" + intface + 
                        "_CallbackSkeleton*> (vecCallbackObj.at(objId));");
+               writeCplusMethodCallbackPermission(intface);
                println("skel->invokeMethod(rmiObj);");
                print("}");
                println(" else {");
@@ -3153,9 +3220,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");
@@ -3201,7 +3268,7 @@ public class IoTCompiler {
                                writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodCplusStub(methods, intDecl, callbackClasses);
+                               writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass);
                                print("}"); println(";");
                                if (callbackExist) {
                                        Iterator it = callbackClasses.iterator();
@@ -3228,6 +3295,8 @@ public class IoTCompiler {
                println("IoTRMICall *rmiCall;");
                // Get the object Id
                println("int objectId;");
+               println("vector<int> ports;\n");
+               println("string callbackAddress;");
                if (callbackExist) {
                // We assume that each class only has one callback interface for now
                        Iterator it = callbackClasses.iterator();
@@ -3237,8 +3306,6 @@ 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("vector<int> ports;\n");
                        writePropertiesCplusPermission(callbackType);
                }
                println("\n");
@@ -3250,12 +3317,17 @@ public class IoTCompiler {
         */
        private void writeConstructorCplusCallbackStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
-               println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId) {");
+               println(newStubClass + "(IoTRMICall* _rmiCall, string _callbackAddress, int _objectId, vector<int> _ports) {");
                println("objectId = _objectId;");
+               println("callbackAddress = _callbackAddress;");
                println("rmiCall = _rmiCall;");
+               println("ports = _ports;");
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist);
                        println("thread th1 (&" + newStubClass + "::___initCallBack, this);");
                        println("th1.detach();");
                        println("___regCB();");
@@ -3303,7 +3375,7 @@ public class IoTCompiler {
                                writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses);
                                writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodCplusStub(methods, intDecl, callbackClasses);
+                               writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass);
                                println("};");
                                if (callbackExist) {
                                        Iterator it = callbackClasses.iterator();
@@ -3328,6 +3400,8 @@ public class IoTCompiler {
        private void writePropertiesCplusSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
 
                println(intface + " *mainObj;");
+               println("vector<int> ports;");
+               println("string callbackAddress;");
                // Callback
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
@@ -3418,9 +3492,10 @@ public class IoTCompiler {
         */
        private void writeConstructorCplusSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
 
-               println(newSkelClass + "(" + intface + " *_mainObj, int _port) {");
+               println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _port) {");
                println("bool _bResult = false;");
                println("mainObj = _mainObj;");
+               println("callbackAddress = _callbackAddress;");
                println("rmiObj = new IoTRMIObject(_port, &_bResult);");
                writeCplusInitCallbackPermission(intface, intDecl, callbackExist);
                writeStructPermissionCplusSkeleton(methods, intDecl, intface);
@@ -3482,7 +3557,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeInitCallbackCplusSkeleton() writes the init callback method for skeleton class
         */
-       private void writeInitCallbackCplusSkeleton(boolean callbackSkeleton) {
+       private void writeInitCallbackCplusSkeleton(boolean callbackSkeleton, String intface) {
 
                // This is a callback skeleton generation
                if (callbackSkeleton)
@@ -3490,14 +3565,20 @@ 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);");
+               String stubInt = null;
+               if (callbackSkeleton)
+                       stubInt = getStubInterface(intface) + "_CallbackStub";
+               else
+                       stubInt = getStubInterface(intface) + "_Stub";
+               int port = getPortCount(stubInt);
+               println("rmiCall = new IoTRMICall(param1[" + port + "], param2.c_str(), param3, &bResult);");
                println("}\n");
        }
 
@@ -3506,7 +3587,7 @@ public class IoTCompiler {
         * HELPER: writeMethodCplusSkeleton() writes the method of the skeleton class
         */
        private void writeMethodCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, 
-                       Set<String> callbackClasses, boolean callbackSkeleton) {
+                       Set<String> callbackClasses, boolean callbackSkeleton, String intface) {
 
                boolean isDefined = false;
                for (String method : methods) {
@@ -3539,7 +3620,7 @@ public class IoTCompiler {
                        writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method));
                        println("}\n");
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackCplusSkeleton(callbackSkeleton);
+                               writeInitCallbackCplusSkeleton(callbackSkeleton, intface);
                                isDefined = true;
                        }
                }
@@ -3576,13 +3657,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++;");
                                }
@@ -4199,7 +4280,7 @@ public class IoTCompiler {
                        // Write deconstructor
                        writeDeconstructorCplusSkeleton(newSkelClass, callbackExist, callbackClasses);
                        // Write methods
-                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false);
+                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false, intface);
                        // Write method helper
                        writeMethodHelperCplusSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
@@ -4222,6 +4303,8 @@ public class IoTCompiler {
                println(intface + " *mainObj;");
                // Keep track of object Ids of all stubs registered to this interface
                println("int objectId;");
+               println("vector<int> ports;\n");
+               println("string callbackAddress;");
                // Callback
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
@@ -4241,8 +4324,9 @@ public class IoTCompiler {
         */
        private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
 
-               println(newSkelClass + "(" + intface + " *_mainObj, int _objectId) {");
+               println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _objectId) {");
                println("mainObj = _mainObj;");
+               println("callbackAddress = _callbackAddress;");
                println("objectId = _objectId;");
                println("}\n");
        }
@@ -4420,7 +4504,7 @@ public class IoTCompiler {
                        // Write deconstructor
                        writeDeconstructorCplusCallbackSkeleton(newSkelClass, callbackExist, callbackClasses);
                        // Write methods
-                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, true);
+                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, true, intface);
                        // Write method helper
                        writeMethodHelperCplusCallbackSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
@@ -4627,13 +4711,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;
-               }
+//             }
        }