Minor fixes in LifxLightBulb driver (not fully tested yet)
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index d056fe473e4c13377445fb38b21389c1e65fc5dd..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("}");
@@ -414,13 +434,13 @@ public class IoTCompiler {
        private void writePropertiesJavaStub(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
                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);
@@ -457,15 +477,18 @@ public class IoTCompiler {
         */
        private void writeConstructorJavaStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
-               println("public " + newStubClass + "(int _port, String _address, int _rev, int[] _ports) throws Exception {");
-               println("address = _address;");
+               println("public " + newStubClass + "(int _port, String _skeletonAddress, String _callbackAddress, int _rev, int[] _ports) throws Exception {");
+               println("callbackAddress = _callbackAddress;");
                println("ports = _ports;");
-               println("rmiCall = new IoTRMICall(_port, _address, _rev);");
+               println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev);");
                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], address, 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");
        }
@@ -742,7 +777,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeStructParamClassJavaStub() writes parameters if struct is present
         */
-       private void writeStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes) {
+       private void writeStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
 
                print("int paramLen = ");
                writeLengthStructParamClassJavaStub(methParams, methPrmTypes);
@@ -757,6 +792,16 @@ public class IoTCompiler {
                        String simpleType = getGenericType(paramType);
                        if (isStructClass(simpleType)) {
                                writeStructMembersJavaStub(simpleType, paramType, param);
+                       } else if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                               println("paramCls[pos] = int.class;");
+                               print("paramObj[pos++] = ");
+                               if (isArray(methParams.get(i)))
+                                       print(getSimpleIdentifier(methParams.get(i)) + ".length");
+                               else if (isList(methPrmTypes.get(i)))
+                                       print(getSimpleIdentifier(methParams.get(i)) + ".size()");
+                               else
+                                       print("new Integer(1)");
+                               println(";");
                        } else {
                                String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
                                println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
@@ -859,7 +904,7 @@ public class IoTCompiler {
         * HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class
         */
        private void writeStdMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method) {
+                       List<String> methPrmTypes, String method, String callbackType) {
 
                checkAndWriteStructSetupJavaStub(methParams, methPrmTypes, intDecl, method);
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
@@ -868,12 +913,17 @@ public class IoTCompiler {
                checkAndWriteEnumTypeJavaStub(methParams, methPrmTypes);
                // Generate array of parameter types
                if (isStructPresent(methParams, methPrmTypes)) {
-                       writeStructParamClassJavaStub(methParams, methPrmTypes);
+                       writeStructParamClassJavaStub(methParams, methPrmTypes, callbackType);
                } else {
                        print("Class<?>[] paramCls = new Class<?>[] { ");
                        for (int i = 0; i < methParams.size(); i++) {
-                               String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                               print(getSimpleType(getEnumType(paramType)) + ".class");
+                               String prmType = methPrmTypes.get(i);
+                               if (checkCallbackType(prmType, callbackType)) { // Check if this has callback object
+                                       print("int.class");
+                               } else { // Generate normal classes if it's not a callback object
+                                       String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                                       print(getSimpleType(getEnumType(paramType)) + ".class");
+                               }
                                // Check if this is the last element (don't print a comma)
                                if (i != methParams.size() - 1) {
                                        print(", ");
@@ -883,7 +933,17 @@ public class IoTCompiler {
                        // Generate array of parameter objects
                        print("Object[] paramObj = new Object[] { ");
                        for (int i = 0; i < methParams.size(); i++) {
-                               print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
+                               String paramType = methPrmTypes.get(i);
+                               if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                                       //if (isArray(methPrmTypes.get(i), methParams.get(i)))
+                                       if (isArray(methParams.get(i)))
+                                               print(getSimpleIdentifier(methParams.get(i)) + ".length");
+                                       else if (isList(methPrmTypes.get(i)))
+                                               print(getSimpleIdentifier(methParams.get(i)) + ".size()");
+                                       else
+                                               print("new Integer(1)");
+                               } else
+                                       print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
                                // Check if this is the last element (don't print a comma)
                                if (i != methParams.size() - 1) {
                                        print(", ");
@@ -936,7 +996,10 @@ public class IoTCompiler {
        private boolean checkCallbackType(String paramType, String callbackType) {
 
                String prmType = returnGenericCallbackType(paramType);
-               return callbackType.equals(prmType);
+               if (callbackType == null)       // If there is no callbackType it means not a callback method
+                       return false;
+               else
+                       return callbackType.equals(prmType);
        }
 
 
@@ -954,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("}");
@@ -968,63 +1031,13 @@ public class IoTCompiler {
                println("ex.printStackTrace();");
                println("throw new Error(\"Exception when generating skeleton objects!\");");
                println("}\n");
-               println("int methodId = " + intDecl.getMethodNumId(method) + ";");
-               String retType = intDecl.getMethodType(method);
-               println("Class<?> retType = " + getSimpleType(getEnumType(retType)) + ".class;");
-               // Generate array of parameter types
-               print("Class<?>[] paramCls = new Class<?>[] { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       String paramType = methPrmTypes.get(i);
-                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               print("int.class");
-                       } else { // Generate normal classes if it's not a callback object
-                               String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                               print(getSimpleType(prmType) + ".class");
-                       }
-                       if (i != methParams.size() - 1) // Check if this is the last element
-                               print(", ");
-               }
-               println(" };");
-               // Generate array of parameter objects
-               print("Object[] paramObj = new Object[] { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       String paramType = methPrmTypes.get(i);
-                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               //if (isArray(methPrmTypes.get(i), methParams.get(i)))
-                               if (isArray(methParams.get(i)))
-                                       print(getSimpleIdentifier(methParams.get(i)) + ".length");
-                               else if (isList(methPrmTypes.get(i)))
-                                       print(getSimpleIdentifier(methParams.get(i)) + ".size()");
-                               else
-                                       print("new Integer(1)");
-                       } else
-                               print(getSimpleIdentifier(methParams.get(i)));
-                       if (i != methParams.size() - 1)
-                               print(", ");
-               }
-               println(" };");
-               // Check if this is "void"
-               if (retType.equals("void")) {
-                       println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
-               } else { // We do have a return value
-               // Check if the return value NONPRIMITIVES
-                       if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) {
-                               String[] retGenValType = getTypeOfGeneric(retType);
-                               println("Class<?> retGenValType = " + retGenValType[0] + ".class;");
-                               println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);");
-                               println("return (" + retType + ")retObj;");
-                       } else {
-                               println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
-                               println("return (" + retType + ")retObj;");
-                       }
-               }
        }
 
 
        /**
         * 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) {
@@ -1054,18 +1067,27 @@ public class IoTCompiler {
                        // Now, write the body of stub!
                        if (isCallbackMethod)
                                writeCallbackMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
-                       else
-                               writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method);
+                       //else
+                       writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
                        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
         */
@@ -1101,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...");
@@ -1116,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;");
@@ -1127,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);
@@ -1143,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");
        }
@@ -1198,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...");
@@ -1215,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");
@@ -1261,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);
@@ -1297,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");
        }
 
@@ -1315,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) {
@@ -1345,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;
                        }
                }
@@ -1356,26 +1392,27 @@ public class IoTCompiler {
         * HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part
         */
        private Map<Integer,String> writeCallbackJavaStubGeneration(List<String> methParams, List<String> methPrmTypes, 
-                       String callbackType) {
+                       String callbackType, boolean isStructMethod) {
 
                Map<Integer,String> mapStubParam = new HashMap<Integer,String>();
+               String offsetPfx = "";
+               if (isStructMethod)
+                       offsetPfx = "offset";
                // Iterate over callback objects
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
                        String param = methParams.get(i);
-                       //if (callbackType.equals(paramType)) {
                        if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               println("try {");
                                String exchParamType = checkAndGetParamClass(getGenericType(paramType));
                                // Print array if this is array or list if this is a list of callback objects
                                if (isArray(param)) {
-                                       println("int numStubs" + i + " = (int) paramObj[" + i + "];");
+                                       println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
                                        println(exchParamType + "[] stub" + i + " = new " + exchParamType + "[numStubs" + i + "];");
                                } else if (isList(paramType)) {
-                                       println("int numStubs" + i + " = (int) paramObj[" + i + "];");
+                                       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++;");
                                }
                        }
@@ -1384,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("}");
                                }
@@ -1403,17 +1440,24 @@ public class IoTCompiler {
        /**
         * HELPER: checkAndWriteEnumTypeJavaSkeleton() writes the enum type (convert from enum to int)
         */
-       private void checkAndWriteEnumTypeJavaSkeleton(List<String> methParams, List<String> methPrmTypes) {
+       private void checkAndWriteEnumTypeJavaSkeleton(List<String> methParams, List<String> methPrmTypes, boolean isStructMethod) {
 
+               String offsetPfx = "";
+               if (isStructMethod)
+                       offsetPfx = "offset";
                // Iterate and find enum declarations
+               boolean printed = false;
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
                        String param = methParams.get(i);
                        String simpleType = getGenericType(paramType);
                        if (isEnumClass(simpleType)) {
                        // Check if this is enum type
-                               println("int paramInt" + i + "[] = (int[]) paramObj[" + i + "];");
-                               println(simpleType + "[] enumVals = " + simpleType + ".values();");
+                               println("int paramInt" + i + "[] = (int[]) paramObj[" + offsetPfx + i + "];");
+                               if (!printed) {
+                                       println(simpleType + "[] enumVals = " + simpleType + ".values();");
+                                       printed = true;
+                               }
                                if (isArray(param)) {   // An array
                                        println("int len" + i + " = paramInt" + i + ".length;");
                                        println(simpleType + "[] paramEnum" + i + " = new " + simpleType + "[len" + i + "];");
@@ -1526,7 +1570,6 @@ public class IoTCompiler {
                StructDecl structDecl = getStructDecl(simpleType);
                List<String> memTypes = structDecl.getMemberTypes(simpleType);
                List<String> members = structDecl.getMembers(simpleType);
-               println("int pos = 0;");
                if (isArrayOrList(paramType, param)) {  // An array or list
                        int methodNumId = intDecl.getMethodNumId(method);
                        String counter = "struct" + methodNumId + "Size" + iVar;
@@ -1555,6 +1598,7 @@ public class IoTCompiler {
        private void writeStructMembersInitJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
                        List<String> methPrmTypes, String method) {
 
+               println("int objPos = 0;");
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
                        String param = methParams.get(i);
@@ -1572,7 +1616,6 @@ public class IoTCompiler {
                                        println("List<" + simpleType + "> paramStruct" + i + " = new ArrayList<" + simpleType + ">();");
                                } else
                                        println(simpleType + " paramStruct" + i + " = new " + simpleType + "();");
-                               println("int objPos = 0;");
                                // Initialize members
                                StructDecl structDecl = getStructDecl(simpleType);
                                List<String> members = structDecl.getMembers(simpleType);
@@ -1605,7 +1648,7 @@ public class IoTCompiler {
                                }
                        } else {
                                // Take offsets of parameters
-                               println("int offset" + i +" = objPos;");
+                               println("int offset" + i +" = objPos++;");
                        }
                }
        }
@@ -1673,10 +1716,12 @@ public class IoTCompiler {
                        List<String> methPrmTypes, String method, boolean isCallbackMethod, String callbackType,
                        boolean isStructMethod) {
 
-               checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes);
+               checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes, isStructMethod);
                Map<Integer,String> mapStubParam = null;
-               if (isCallbackMethod)
-                       mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType);
+               if (isCallbackMethod) {
+                       println("try {");
+                       mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType, isStructMethod);
+               }
                // Check if this is "void"
                String retType = intDecl.getMethodType(method);
                if (retType.equals("void")) {
@@ -1690,14 +1735,15 @@ public class IoTCompiler {
                }
                for (int i = 0; i < methParams.size(); i++) {
 
-                       if (isCallbackMethod) {
+                       String paramType = methPrmTypes.get(i);
+                       if (isCallbackMethod && checkCallbackType(paramType, callbackType)) {
                                print(mapStubParam.get(i));     // Get the callback parameter
-                       } else if (isEnumClass(getGenericType(methPrmTypes.get(i)))) { // Enum class
-                               print(getEnumParam(methPrmTypes.get(i), methParams.get(i), i));
-                       } else if (isStructClass(getGenericType(methPrmTypes.get(i)))) {
+                       } else if (isEnumClass(getGenericType(paramType))) { // Enum class
+                               print(getEnumParam(paramType, methParams.get(i), i));
+                       } else if (isStructClass(getGenericType(paramType))) {
                                print("paramStruct" + i);
                        } else {
-                               String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                               String prmType = checkAndGetArray(paramType, methParams.get(i));
                                if (isStructMethod)
                                        print("(" + prmType + ") paramObj[offset" + i + "]");
                                else
@@ -1741,6 +1787,7 @@ public class IoTCompiler {
                println(";");
                println("Class<?>[] paramCls = new Class<?>[paramLen];");
                println("Class<?>[] paramClsGen = new Class<?>[paramLen];");
+               println("int pos = 0;");
                // Iterate again over the parameters
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
@@ -1845,9 +1892,10 @@ public class IoTCompiler {
                                        String param = methParams.get(i);
                                        String simpleType = getGenericType(paramType);
                                        if (isStructClass(simpleType)) {
-                                               if (!begin) {   // Generate comma for not the beginning variable
-                                                       print(", "); begin = false;
-                                               }
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
                                                int methodNumId = intDecl.getMethodNumId(method);
                                                print("int struct" + methodNumId + "Size" + i);
                                        }
@@ -1977,17 +2025,18 @@ public class IoTCompiler {
                List<String> methParams = intDecl.getMethodParams(method);
                List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
                boolean structExist = false;
+               boolean begin = true;
                // Check for params with structs
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
                        String param = methParams.get(i);
                        String simpleType = getGenericType(paramType);
-                       boolean begin = true;
                        if (isStructClass(simpleType)) {
                                structExist = true;
-                               if (!begin) {
-                                       print(", "); begin = false;
-                               }
+                               if (!begin)
+                                       print(", ");
+                               else
+                                       begin = false;
                                int methodNumId = intDecl.getMethodNumId(method);
                                print("struct" + methodNumId + "Size" + i);
                        }
@@ -2153,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
@@ -2173,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");
        }
@@ -2187,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");
@@ -2221,9 +2274,10 @@ public class IoTCompiler {
                                        String param = methParams.get(i);
                                        String simpleType = getGenericType(paramType);
                                        if (isStructClass(simpleType)) {
-                                               if (!begin) {   // Generate comma for not the beginning variable
-                                                       print(", "); begin = false;
-                                               }
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
                                                int methodNumId = intDecl.getMethodNumId(method);
                                                print("int struct" + methodNumId + "Size" + i);
                                        }
@@ -2334,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
@@ -2550,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("");                 
@@ -2560,7 +2616,7 @@ public class IoTCompiler {
                                println("{");
                                println("public:");
                                // Write methods
-                               writeMethodCplusInterface(intMeth.getValue(), intDecl);
+                               writeMethodCplusInterface(methods, intDecl);
                                println("};");
                                println("#endif");
                                pw.close();
@@ -2573,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) {
@@ -2604,12 +2660,11 @@ public class IoTCompiler {
                        println(") { ");
                        if (isCallbackMethod)
                                writeCallbackMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType);
-                       else
-                               writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackClasses);
+                       writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType, isCallbackMethod);
                        println("}\n");
                        // Write the init callback helper method
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackCplusStub(callbackType, intDecl);
+                               writeInitCallbackCplusStub(callbackType, intDecl, newStubClass);
                                writeInitCallbackSendInfoCplusStub(intDecl);
                                isDefined = true;
                        }
@@ -2633,67 +2688,22 @@ 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 = 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 = new " + callbackType + "_CallbackSkeleton(" +
-                                               getSimpleIdentifier(param) + ", objIdCnt++);");
-                               println("vecCallbackObj.push_back(skel);");
+                                       println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
+                                               getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
+                               println("vecCallbackObj.push_back(skel" + i + ");");
                                if (isArrayOrList)
                                        println("}");
+                               print("int ___paramCB" + i + " = ");
+                               if (isArrayOrList)
+                                       println(callbackParam + ".size();");
+                               else
+                                       println("1;");
                        }
                }
-               println("int numParam = " + methParams.size() + ";");
-               println("int methodId = " + intDecl.getMethodNumId(method) + ";");
-               String retType = intDecl.getMethodType(method);
-               //String retTypeC = checkAndGetCplusType(retType);
-               //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";");
-               println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
-               // Generate array of parameter types
-               print("string paramCls[] = { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       String paramType = methPrmTypes.get(i);
-                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               print("\"int\"");
-                       } else { // Generate normal classes if it's not a callback object
-                               String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
-                               print("\"" + paramTypeC + "\"");
-                       }
-                       if (i != methParams.size() - 1) // Check if this is the last element
-                               print(", ");
-               }
-               println(" };");
-               print("int ___paramCB = ");
-               if (isArrayOrList)
-                       println(callbackParam + ".size();");
-               else
-                       println("1;");
-               // Generate array of parameter objects
-               print("void* paramObj[] = { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       String paramType = methPrmTypes.get(i);
-                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               print("&___paramCB");
-                       } else
-                               print(getSimpleIdentifier(methParams.get(i)));
-                       if (i != methParams.size() - 1)
-                               print(", ");
-               }
-               println(" };");
-               // Check if this is "void"
-               if (retType.equals("void")) {
-                       println("void* retObj = NULL;");
-                       println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
-               } else { // We do have a return value
-                       if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
-                               println(checkAndGetCplusType(retType) + " retVal;");
-                       else
-                               println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";");
-                       println("void* retObj = &retVal;");
-                       println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
-                       println("return retVal;");
-               }
        }
 
 
@@ -2849,7 +2859,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeStructParamClassCplusStub() writes member parameters of struct
         */
-       private void writeStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes) {
+       private void writeStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
 
                print("int numParam = ");
                writeLengthStructParamClassCplusStub(methParams, methPrmTypes);
@@ -2864,6 +2874,9 @@ public class IoTCompiler {
                        String simpleType = getGenericType(paramType);
                        if (isStructClass(simpleType)) {
                                writeStructMembersCplusStub(simpleType, paramType, param);
+                       } else if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                               println("paramCls[pos] = \"int\";");
+                               println("paramObj[pos++] = &___paramCB" + i + ";");
                        } else {
                                String prmTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
                                println("paramCls[pos] = \"" + prmTypeC + "\";");
@@ -2967,21 +2980,22 @@ public class IoTCompiler {
         * HELPER: writeStdMethodBodyCplusStub() writes the standard method body in the stub class
         */
        private void writeStdMethodBodyCplusStub(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, Set<String> callbackClasses) {
+                       List<String> methPrmTypes, String method, String callbackType, boolean isCallbackMethod) {
 
                checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method);
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
                String retType = intDecl.getMethodType(method);
                println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
+               checkAndWriteEnumTypeCplusStub(methParams, methPrmTypes);
                // Generate array of parameter types
                if (isStructPresent(methParams, methPrmTypes)) {
-                       writeStructParamClassCplusStub(methParams, methPrmTypes);
+                       writeStructParamClassCplusStub(methParams, methPrmTypes, callbackType);
                } else {
                        println("int numParam = " + methParams.size() + ";");
                        print("string paramCls[] = { ");
                        for (int i = 0; i < methParams.size(); i++) {
                                String paramType = returnGenericCallbackType(methPrmTypes.get(i));
-                               if (callbackClasses.contains(paramType)) {
+                               if (checkCallbackType(paramType, callbackType)) {
                                        print("\"int\"");
                                } else {
                                        String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
@@ -2993,11 +3007,14 @@ public class IoTCompiler {
                                }
                        }
                        println(" };");
-                       checkAndWriteEnumTypeCplusStub(methParams, methPrmTypes);
                        // Generate array of parameter objects
                        print("void* paramObj[] = { ");
                        for (int i = 0; i < methParams.size(); i++) {
-                               print("&" + getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
+                               String paramType = returnGenericCallbackType(methPrmTypes.get(i));
+                               if (checkCallbackType(paramType, callbackType)) // Check if this has callback object
+                                       print("&___paramCB" + i);
+                               else
+                                       print("&" + getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
                                // Check if this is the last element (don't print a comma)
                                if (i != methParams.size() - 1) {
                                        print(", ");
@@ -3053,14 +3070,11 @@ public class IoTCompiler {
        private void writePropertiesCplusStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
 
                println("IoTRMICall *rmiCall;");
-               //println("IoTRMIObject\t\t\t*rmiObj;");
-               println("string address;");
+               println("string callbackAddress;");
                println("vector<int> ports;\n");
                // 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();
@@ -3082,13 +3096,16 @@ public class IoTCompiler {
        private void writeConstructorCplusStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
                println(newStubClass + 
-                       "(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports) {");
-               println("address = _address;");
+                       "(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, _address, _rev, _bResult);");
+               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();");
@@ -3138,7 +3155,7 @@ public class IoTCompiler {
                        int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
                        println("cerr << \"Callback object for " + intface + " is not allowed to access method: \" << methodId;");
-                       println("exit(-1);");
+                       println("return;");
                        println("}");
                }
        }
@@ -3147,24 +3164,26 @@ 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 {");
                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);");
+               println("return;");
                println("}");
                println("}");
                println("}\n");
@@ -3200,11 +3219,10 @@ public class IoTCompiler {
                String method = "___initCallBack()";
                int methodNumId = intDecl.getHelperMethodNumId(method);
                println("int methodId = " + methodNumId + ";");
-               //writeCplusCallbackPermission(intface, methodNumId);
                println("string retType = \"void\";");
-               println("string paramCls[] = { \"int\", \"String\", \"int\" };");
+               println("string paramCls[] = { \"int*\", \"String\", \"int\" };");
                println("int rev = 0;");
-               println("void* paramObj[] = { &ports[0], &address, &rev };");
+               println("void* paramObj[] = { &ports, &callbackAddress, &rev };");
                println("void* retObj = NULL;");
                println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
                println("}\n");
@@ -3250,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();
@@ -3277,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();
@@ -3286,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");
@@ -3299,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();");
@@ -3352,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();
@@ -3377,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();
@@ -3467,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);
@@ -3531,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)
@@ -3539,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");
        }
 
@@ -3555,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) {
@@ -3588,7 +3620,7 @@ public class IoTCompiler {
                        writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method));
                        println("}\n");
                        if (isCallbackMethod && !isDefined) {
-                               writeInitCallbackCplusSkeleton(callbackSkeleton);
+                               writeInitCallbackCplusSkeleton(callbackSkeleton, intface);
                                isDefined = true;
                        }
                }
@@ -3625,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++;");
                                }
@@ -3695,7 +3727,7 @@ public class IoTCompiler {
 
 
        /**
-        * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return statement part in skeleton
+        * HELPER: writeMethodInputParameters() writes the parameter variables for C++ skeleton
         */
        private void writeMethodInputParameters(List<String> methParams, List<String> methPrmTypes, 
                        Set<String> callbackClasses, String methodId) {
@@ -3837,19 +3869,18 @@ public class IoTCompiler {
                List<String> members = structDecl.getMembers(simpleType);
                int methodNumId = intDecl.getMethodNumId(method);
                String counter = "struct" + methodNumId + "Size" + iVar;
-               println("int pos = 0;");
                // Set up variables
                if (isArrayOrList(paramType, param)) {  // An array or list
                        for (int i = 0; i < members.size(); i++) {
                                String prmTypeC = checkAndGetCplusType(memTypes.get(i));
                                String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
-                               println(getSimpleType(getEnumType(prmType)) + " param" + i + "[" + counter + "];");
+                               println(getSimpleType(getEnumType(prmType)) + " param" + iVar + i + "[" + counter + "];");
                        }
                } else {        // Just one struct element
                        for (int i = 0; i < members.size(); i++) {
                                String prmTypeC = checkAndGetCplusType(memTypes.get(i));
                                String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
-                               println(getSimpleType(getEnumType(prmType)) + " param" + i + ";");
+                               println(getSimpleType(getEnumType(prmType)) + " param" + iVar + i + ";");
                        }
                }
                if (isArrayOrList(paramType, param)) {  // An array or list
@@ -3859,14 +3890,14 @@ public class IoTCompiler {
                        for (int i = 0; i < members.size(); i++) {
                                String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
                                println("paramCls[pos] = \"" + prmTypeC + "\";");
-                               println("paramObj[pos++] = &param" + i + "[i];");
+                               println("paramObj[pos++] = &param" + iVar + i + "[i];");
                        }
                        println("}");
                } else {        // Just one struct element
                        for (int i = 0; i < members.size(); i++) {
                                String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
                                println("paramCls[pos] = \"" + prmTypeC + "\";");
-                               println("paramObj[pos++] = &param" + i + ";");
+                               println("paramObj[pos++] = &param" + iVar + i + ";");
                        }
                }
        }
@@ -3898,13 +3929,13 @@ public class IoTCompiler {
                                        println("for(int i = 0; i < " + counter + "; i++) {");
                                        for (int j = 0; j < members.size(); j++) {
                                                print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j)));
-                                               println(" = param" + j + "[i];");
+                                               println(" = param" + i + j + "[i];");
                                        }
                                        println("}");
                                } else {        // Just one struct element
                                        for (int j = 0; j < members.size(); j++) {
                                                print("paramStruct" + i + "." + getSimpleIdentifier(members.get(j)));
-                                               println(" = param" + j + ";");
+                                               println(" = param" + i + j + ";");
                                        }
                                }
                        }
@@ -3970,6 +4001,7 @@ public class IoTCompiler {
                println(";");
                println("string paramCls[numParam];");
                println("void* paramObj[numParam];");
+               println("int pos = 0;");
                // Iterate again over the parameters
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
@@ -3981,8 +4013,8 @@ public class IoTCompiler {
                                String prmType = returnGenericCallbackType(methPrmTypes.get(i));
                                if (callbackClasses.contains(prmType)) {
                                        isCallbackMethod = true;
-                                       callbackType = paramType;
-                                       writeCallbackCplusNumStubs(methParams, methPrmTypes, callbackType);
+                                       callbackType = prmType;
+                                       println("int numStubs" + i + " = 0;");
                                        println("paramCls[pos] = \"int\";");
                                        println("paramObj[pos++] = &numStubs" + i + ";");
                                } else {        // Generate normal classes if it's not a callback object
@@ -3996,7 +4028,7 @@ public class IoTCompiler {
                                        String prmTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
                                        println("paramCls[pos] = \"" + prmTypeC + "\";");
                                        if (isEnumClass(getGenericType(paramType)))     // Check if this is enum type
-                                               println("paramObj[pos++] = &paramEnumInt" + i);
+                                               println("paramObj[pos++] = &paramEnumInt" + i + ";");
                                        else
                                                println("paramObj[pos++] = &" + getSimpleIdentifier(methParams.get(i)) + ";");
                                }
@@ -4035,9 +4067,10 @@ public class IoTCompiler {
                                        String param = methParams.get(i);
                                        String simpleType = getGenericType(paramType);
                                        if (isStructClass(simpleType)) {
-                                               if (!begin) {   // Generate comma for not the beginning variable
-                                                       print(", "); begin = false;
-                                               }
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
                                                int methodNumId = intDecl.getMethodNumId(method);
                                                print("int struct" + methodNumId + "Size" + i);
                                        }
@@ -4149,12 +4182,12 @@ public class IoTCompiler {
                        println("if (_objectId == object" + newObjectId + "Id) {");
                        println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
                        println("cerr << \"Object with object Id: \" << _objectId << \"  is not allowed to access method: \" << methodId << endl;");
-                       println("exit(-1);");
+                       println("return;");
                        println("}");
                        println("}");
                        println("else {");
                        println("cerr << \"Object Id: \" << _objectId << \" not recognized!\" << endl;");
-                       println("exit(-1);");
+                       println("return;");
                        println("}");
                }
        }
@@ -4247,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
@@ -4270,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();
@@ -4289,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");
        }
@@ -4350,9 +4386,10 @@ public class IoTCompiler {
                                        String param = methParams.get(i);
                                        String simpleType = getGenericType(paramType);
                                        if (isStructClass(simpleType)) {
-                                               if (!begin) {   // Generate comma for not the beginning variable
-                                                       print(", "); begin = false;
-                                               }
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
                                                int methodNumId = intDecl.getMethodNumId(method);
                                                print("int struct" + methodNumId + "Size" + i);
                                        }
@@ -4467,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
@@ -4674,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;
-               }
+//             }
        }