Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/iot2
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index 0a8e5dd0f2e125f2d173af297aa2c4e352d0660e..a1144919e47a8f3e8e62be71e4fb692d9fe7c4f5 100644 (file)
@@ -61,6 +61,7 @@ public class IoTCompiler {
        private String subdir;
        private Map<String,Integer> mapPortCount;       // Counter for ports
        private static int portCount = 0;
+       private static int countObjId = 1;                      // Always increment object Id for a new stub/skeleton
        private String mainClass;
 
 
@@ -143,7 +144,7 @@ public class IoTCompiler {
                mapIntfacePTH.put(origInt, ptHandler);
                mapIntDeclHand.put(origInt, decHandler);
                // Set object Id counter to 0 for each interface
-               mapIntfaceObjId.put(origInt, new Integer(0));
+               mapIntfaceObjId.put(origInt, countObjId++);
        }
 
 
@@ -363,9 +364,9 @@ public class IoTCompiler {
         */
        private void updateIntfaceObjIdMap(String intface, String newIntface) {
 
+               // We are assuming that we only generate one stub per one skeleton at this point @Feb 2017
                Integer objId = mapIntfaceObjId.get(intface);
                mapNewIntfaceObjId.put(newIntface, objId);
-               mapIntfaceObjId.put(intface, objId++);
        }
 
 
@@ -416,8 +417,8 @@ public class IoTCompiler {
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
                        int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("private final static int object" + newObjectId + "Id = " + 
-                               newObjectId + ";\t//" + newIntface);
+                       //println("private final static int object" + newObjectId + "Id = " + 
+                       //      newObjectId + ";\t//" + newIntface);
                        Set<String> methodIds = intMeth.getValue();
                        print("private static Integer[] object" + newObjectId + "Permission = { ");
                        int i = 0;
@@ -479,7 +480,12 @@ public class IoTCompiler {
        private void writeConstructorJavaStub(String intface, String newStubClass, Set<String> methods, InterfaceDecl intDecl) {
 
                println("public " + newStubClass + "(int _localPortSend, int _localPortRecv, int _portSend, int _portRecv, String _skeletonAddress, int _rev) throws Exception {");
+               println("if (_localPortSend != 0 && _localPortRecv != 0) {");
                println("rmiComm = new IoTRMICommClient(_localPortSend, _localPortRecv, _portSend, _portRecv, _skeletonAddress, _rev);");
+               println("} else");
+               println("{");
+               println("rmiComm = new IoTRMICommClient(_portSend, _portRecv, _skeletonAddress, _rev);");
+               println("}");
                // Register the AtomicBoolean variables
                for (String method : methods) {
                        // Generate AtomicBooleans for methods that have return values
@@ -515,42 +521,6 @@ public class IoTCompiler {
        }
 
 
-       /**
-        * HELPER: writeJavaMethodCallbackPermission() writes permission checks in stub for callbacks
-        */
-       private void writeJavaMethodCallbackPermission(String intface) {
-
-               println("int methodId = IoTRMIObject.getMethodId(method);");
-               // Get all the different stubs
-               Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-               for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                       String newIntface = intMeth.getKey();
-                       int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
-                       println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);");
-                       println("}");
-               }
-       }
-
-
-       /**
-        * HELPER: writeJavaInitCallbackPermission() writes the permission for callback
-        */
-       private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) {
-
-               if (callbackExist) {
-                       String method = "___initCallBack()";
-                       int methodNumId = intDecl.getHelperMethodNumId(method);
-                       Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-                       for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                               String newIntface = intMeth.getKey();
-                               int newObjectId = getNewIntfaceObjectId(newIntface);
-                               println("set" + newObjectId + "Allowed.add(" + methodNumId + ");");
-                       }
-               }
-       }
-
-
        /**
         * HELPER: getPortCount() gets port count for different stubs and skeletons
         */
@@ -753,7 +723,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeStructParamClassJavaStub() writes parameters if struct is present
         */
-       private void writeStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+       private void writeStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes, Set<String> callbackType) {
 
                print("int paramLen = ");
                writeLengthStructParamClassJavaStub(methParams, methPrmTypes);
@@ -769,15 +739,8 @@ public class IoTCompiler {
                        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(";");
+                               println("paramCls[pos] = int[].class;");
+                               println("paramObj[pos++] = objIdSent" + i + ";");
                        } else {
                                String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
                                println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
@@ -896,7 +859,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, String callbackType) {
+                       List<String> methPrmTypes, String method, Set<String> callbackType) {
 
                checkAndWriteStructSetupJavaStub(methParams, methPrmTypes, intDecl, method);
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
@@ -927,7 +890,7 @@ public class IoTCompiler {
                        for (int i = 0; i < methParams.size(); i++) {
                                String paramType = methPrmTypes.get(i);
                                if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                                       print("objIdSent");
+                                       print("objIdSent" + i);
                                } else
                                        print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
                                // Check if this is the last element (don't print a comma)
@@ -976,6 +939,24 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: checkCallbackType() checks the callback type
+        */
+       private boolean checkCallbackType(String paramType, Set<String> callbackType) {
+
+               String prmType = returnGenericCallbackType(paramType);
+               if (callbackType == null)       // If there is no callbackType it means not a callback method
+                       return false;
+               else {
+                       for (String type : callbackType) {
+                               if (type.equals(prmType))
+                                       return true;    // Check callbackType one by one
+                       }
+                       return false;
+               }
+       }
+
+
        /**
         * HELPER: checkCallbackType() checks the callback type
         */
@@ -997,17 +978,17 @@ public class IoTCompiler {
                println("if (!IoTRMIUtil.mapSkel.containsKey(" + paramIdent + ")) {");
                println("int newObjIdSent = rmiComm.getObjectIdCounter();");
                if (isMultipleCallbacks)
-                       println("objIdSent[i++] = newObjIdSent;");
+                       println("objIdSent" + counter + "[cnt" + counter + "++] = newObjIdSent;");
                else
-                       println("objIdSent[0] = newObjIdSent;");
+                       println("objIdSent" + counter + "[0] = newObjIdSent;");
                println("rmiComm.decrementObjectIdCounter();");
                println(callbackType + "_Skeleton skel" + counter + " = new " + callbackType + "_Skeleton(" + paramIdent + ", rmiComm, newObjIdSent);");
-               println("IoTRMIUtil.mapSkel.put(" + paramIdent + ", skel0);");
+               println("IoTRMIUtil.mapSkel.put(" + paramIdent + ", skel" + counter + ");");
                println("IoTRMIUtil.mapSkelId.put(" + paramIdent + ", newObjIdSent);");
                println("Thread thread = new Thread() {");
                println("public void run() {");
                println("try {");
-               println("skel0.___waitRequestInvokeMethod();");
+               println("skel" + counter + ".___waitRequestInvokeMethod();");
                println("} catch (Exception ex) {");
                println("ex.printStackTrace();");
                println("throw new Error(\"Exception when trying to run ___waitRequestInvokeMethod() for " + 
@@ -1022,9 +1003,9 @@ public class IoTCompiler {
                println("{");
                println("int newObjIdSent = IoTRMIUtil.mapSkelId.get(" + paramIdent + ");");
                if (isMultipleCallbacks)
-                       println("objIdSent[i++] = newObjIdSent;");
+                       println("objIdSent" + counter + "[cnt" + counter + "++] = newObjIdSent;");
                else
-                       println("objIdSent[0] = newObjIdSent;");
+                       println("objIdSent" + counter + "[0] = newObjIdSent;");
                println("}");
        }
 
@@ -1033,13 +1014,13 @@ public class IoTCompiler {
         * HELPER: writeCallbackMethodBodyJavaStub() writes the callback method of the stub class
         */
        private void writeCallbackMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, String callbackType) {
+                       List<String> methPrmTypes, String method, Set<String> callbackType) {
 
                // Determine callback object counter type (List vs. single variable)
-               print("int[] objIdSent = ");
                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[] objIdSent" + i + " = ");
                                String param = methParams.get(i);
                                if (isArray(methParams.get(i)))
                                        println("new int[" + getSimpleIdentifier(methParams.get(i)) + ".length];");
@@ -1056,11 +1037,11 @@ public class IoTCompiler {
                        if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
                                String param = methParams.get(i);
                                if (isArrayOrList(paramType, param)) {  // Generate loop
-                                       println("int i = 0;");
+                                       println("int cnt" + i + " = 0;");
                                        println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {");
-                                       writeCallbackInstantiationMethodBodyJavaStub("cb", callbackType, i, true);
+                                       writeCallbackInstantiationMethodBodyJavaStub("cb", returnGenericCallbackType(paramType), i, true);
                                } else
-                                       writeCallbackInstantiationMethodBodyJavaStub(getSimpleIdentifier(param), callbackType, i, false);
+                                       writeCallbackInstantiationMethodBodyJavaStub(getSimpleIdentifier(param), returnGenericCallbackType(paramType), i, false);
                                if (isArrayOrList(paramType, param))
                                        println("}");
                        }
@@ -1085,14 +1066,16 @@ public class IoTCompiler {
                        print("public " + intDecl.getMethodType(method) + " " +
                                intDecl.getMethodId(method) + "(");
                        boolean isCallbackMethod = false;
-                       String callbackType = null;
+                       //String callbackType = null;
+                       Set<String> callbackType = new HashSet<String>();
                        for (int i = 0; i < methParams.size(); i++) {
 
                                String paramType = returnGenericCallbackType(methPrmTypes.get(i));
                                // Check if this has callback object
                                if (callbackClasses.contains(paramType)) {
                                        isCallbackMethod = true;
-                                       callbackType = paramType;       
+                                       //callbackType = paramType;
+                                       callbackType.add(paramType);
                                        // Even if there're 2 callback arguments, we expect them to be of the same interface
                                }
                                print(methPrmTypes.get(i) + " " + methParams.get(i));
@@ -1173,7 +1156,9 @@ public class IoTCompiler {
        private void writePropertiesJavaSkeleton(String intface, InterfaceDecl intDecl) {
 
                println("private " + intface + " mainObj;");
-               println("private int objectId = 0;");
+               //println("private int objectId = 0;");
+               Integer objId = mapIntfaceObjId.get(intface);
+               println("private int objectId = " + objId + ";");
                println("// Communications and synchronizations");
                println("private IoTRMIComm rmiComm;");
                println("private AtomicBoolean didAlreadyInitWaitInvoke;");
@@ -1227,7 +1212,6 @@ public class IoTCompiler {
                println("rmiComm = new IoTRMICommServer(_portSend, _portRecv);");
                // Generate permission control initialization
                writeConstructorJavaPermission(intface);
-               //writeJavaInitCallbackPermission(intface, intDecl, callbackExist);
                writeStructPermissionJavaSkeleton(methods, intDecl, intface);
                println("IoTRMIUtil.mapSkel.put(_mainObj, this);");
                println("IoTRMIUtil.mapSkelId.put(_mainObj, objectId);");
@@ -1294,8 +1278,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, String intface) {
+       private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
 
                for (String method : methods) {
 
@@ -1303,16 +1286,10 @@ public class IoTCompiler {
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
                        String methodId = intDecl.getMethodId(method);
                        print("public " + intDecl.getMethodType(method) + " " + methodId + "(");
-                       boolean isCallbackMethod = false;
-                       String callbackType = null;
                        for (int i = 0; i < methParams.size(); i++) {
 
                                String origParamType = methPrmTypes.get(i);
                                String paramType = checkAndGetParamClass(origParamType);
-                               if (callbackClasses.contains(origParamType)) { // Check if this has callback object
-                                       isCallbackMethod = true;
-                                       callbackType = origParamType;   
-                               }
                                print(paramType + " " + methParams.get(i));
                                // Check if this is the last element (don't print a comma)
                                if (i != methParams.size() - 1) {
@@ -1330,18 +1307,17 @@ public class IoTCompiler {
        /**
         * HELPER: writeCallbackInstantiationJavaStubGeneration() writes the instantiation of callback stubs
         */
-       private void writeCallbackInstantiationJavaStubGeneration(String exchParamType) {
+       private void writeCallbackInstantiationJavaStubGeneration(String exchParamType, int counter) {
 
-               //println("int objIdRecv = (int[]) paramObj[0];");
-               println(exchParamType + " newStub = null;");
-               println("if(!IoTRMIUtil.mapStub.containsKey(objIdRecv)) {");
-               println("newStub = new " + exchParamType + "_Stub(rmiComm, objIdRecv);");
-               println("IoTRMIUtil.mapStub.put(objIdRecv, newStub);");
-               println("rmiComm.setObjectIdCounter(objIdRecv);");
+               println(exchParamType + " newStub" + counter + " = null;");
+               println("if(!IoTRMIUtil.mapStub.containsKey(objIdRecv" + counter + ")) {");
+               println("newStub" + counter + " = new " + exchParamType + "_Stub(rmiComm, objIdRecv" + counter + ");");
+               println("IoTRMIUtil.mapStub.put(objIdRecv" + counter + ", newStub" + counter + ");");
+               println("rmiComm.setObjectIdCounter(objIdRecv" + counter + ");");
                println("rmiComm.decrementObjectIdCounter();");
                println("}");
                println("else {");
-               println("newStub = (" + exchParamType + "_Stub) IoTRMIUtil.mapStub.get(objIdRecv);");
+               println("newStub" + counter + " = (" + exchParamType + "_Stub) IoTRMIUtil.mapStub.get(objIdRecv" + counter + ");");
                println("}");
        }
 
@@ -1350,7 +1326,7 @@ public class IoTCompiler {
         * HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part
         */
        private Map<Integer,String> writeCallbackJavaStubGeneration(List<String> methParams, List<String> methPrmTypes, 
-                       String callbackType, boolean isStructMethod) {
+                       Set<String> callbackType, boolean isStructMethod) {
 
                Map<Integer,String> mapStubParam = new HashMap<Integer,String>();
                String offsetPfx = "";
@@ -1371,8 +1347,8 @@ public class IoTCompiler {
                                        println("int numStubs" + i + " = stubIdArray" + i + ".length;");
                                        println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();");
                                } else {
-                                       println("int objIdRecv = stubIdArray" + i + "[0];");
-                                       writeCallbackInstantiationJavaStubGeneration(exchParamType);
+                                       println("int objIdRecv" + i + " = stubIdArray" + i + "[0];");
+                                       writeCallbackInstantiationJavaStubGeneration(exchParamType, i);
                                }
                        }
                        // Generate a loop if needed
@@ -1380,18 +1356,18 @@ public class IoTCompiler {
                                String exchParamType = checkAndGetParamClass(getGenericType(paramType));
                                if (isArray(param)) {
                                        println("for (int i = 0; i < numStubs" + i + "; i++) {");
-                                       println("int objIdRecv = stubIdArray" + i + "[i];");
-                                       writeCallbackInstantiationJavaStubGeneration(exchParamType);
-                                       println("stub" + i + "[i] = newStub;");
+                                       println("int objIdRecv" + i + " = stubIdArray" + i + "[i];");
+                                       writeCallbackInstantiationJavaStubGeneration(exchParamType, i);
+                                       println("stub" + i + "[i] = newStub" + i + ";");
                                        println("}");
                                } else if (isList(paramType)) {
                                        println("for (int i = 0; i < numStubs" + i + "; i++) {");
-                                       println("int objIdRecv = stubIdArray" + i + "[i];");
-                                       writeCallbackInstantiationJavaStubGeneration(exchParamType);
-                                       println("stub" + i + ".add(newStub);");
+                                       println("int objIdRecv" + i + " = stubIdArray" + i + "[i];");
+                                       writeCallbackInstantiationJavaStubGeneration(exchParamType, i);
+                                       println("stub" + i + ".add(newStub" + i + ");");
                                        println("}");
                                } else
-                                       println(exchParamType + " stub" + i + " = newStub;");
+                                       println(exchParamType + " stub" + i + " = newStub" + i + ";");
                                mapStubParam.put(i, "stub" + i);        // List of all stub parameters
                        }
                }
@@ -1675,7 +1651,7 @@ public class IoTCompiler {
         * HELPER: writeMethodHelperReturnJavaSkeleton() writes return statement part in skeleton
         */
        private void writeMethodHelperReturnJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, boolean isCallbackMethod, String callbackType,
+                       List<String> methPrmTypes, String method, boolean isCallbackMethod, Set<String> callbackType,
                        boolean isStructMethod) {
 
                checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes, isStructMethod);
@@ -1743,7 +1719,8 @@ public class IoTCompiler {
 
                // Generate array of parameter objects
                boolean isCallbackMethod = false;
-               String callbackType = null;
+               //String callbackType = null;
+               Set<String> callbackType = new HashSet<String>();
                println("byte[] localMethodBytes = methodBytes;");
                println("rmiComm.setGetMethodBytes();");
                print("int paramLen = ");
@@ -1763,7 +1740,8 @@ public class IoTCompiler {
                                String prmType = returnGenericCallbackType(methPrmTypes.get(i));
                                if (callbackClasses.contains(prmType)) {
                                        isCallbackMethod = true;
-                                       callbackType = prmType;
+                                       //callbackType = prmType;
+                                       callbackType.add(prmType);
                                        println("paramCls[pos] = int[].class;");
                                        println("paramClsGen[pos++] = null;");
                                } else {        // Generate normal classes if it's not a callback object
@@ -1793,7 +1771,8 @@ public class IoTCompiler {
 
                // Generate array of parameter objects
                boolean isCallbackMethod = false;
-               String callbackType = null;
+               //String callbackType = null;
+               Set<String> callbackType = new HashSet<String>();
                println("byte[] localMethodBytes = methodBytes;");
                println("rmiComm.setGetMethodBytes();");
                print("Object[] paramObj = rmiComm.getMethodParams(new Class<?>[] { ");
@@ -1802,7 +1781,8 @@ public class IoTCompiler {
                        String paramType = returnGenericCallbackType(methPrmTypes.get(i));
                        if (callbackClasses.contains(paramType)) {
                                isCallbackMethod = true;
-                               callbackType = paramType;
+                               //callbackType = paramType;
+                               callbackType.add(paramType);
                                print("int[].class");
                        } else {        // Generate normal classes if it's not a callback object
                                String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
@@ -2280,7 +2260,7 @@ public class IoTCompiler {
                        // Write function to return didAlreadyInitWaitInvoke
                        writeReturnDidAlreadyInitWaitInvoke();
                        // Write methods
-                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface);
+                       writeMethodJavaSkeleton(methods, intDecl);
                        // Write method helper
                        writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
@@ -2556,7 +2536,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeMethodDeclCplusStub() writes the method declarations of the stub
         */
-       private void writeMethodDeclCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, String newStubClass) {
+       private void writeMethodDeclCplusStub(Collection<String> methods, InterfaceDecl intDecl) {
 
                for (String method : methods) {
 
@@ -2564,17 +2544,9 @@ public class IoTCompiler {
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
                        print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
                                intDecl.getMethodId(method) + "(");
-                       boolean isCallbackMethod = false;
-                       String callbackType = null;
                        for (int i = 0; i < methParams.size(); i++) {
 
                                String paramType = returnGenericCallbackType(methPrmTypes.get(i));
-                               // Check if this has callback object
-                               if (callbackClasses.contains(paramType)) {
-                                       isCallbackMethod = true;
-                                       callbackType = paramType;       
-                                       // Even if there're 2 callback arguments, we expect them to be of the same interface
-                               }
                                String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
                                String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
                                print(methParamComplete);
@@ -2604,14 +2576,15 @@ public class IoTCompiler {
                        print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " + newStubClass + "::" +
                                intDecl.getMethodId(method) + "(");
                        boolean isCallbackMethod = false;
-                       String callbackType = null;
+                       Set<String> callbackType = new HashSet<String>();
                        for (int i = 0; i < methParams.size(); i++) {
 
                                String paramType = returnGenericCallbackType(methPrmTypes.get(i));
                                // Check if this has callback object
                                if (callbackClasses.contains(paramType)) {
                                        isCallbackMethod = true;
-                                       callbackType = paramType;       
+                                       //callbackType = paramType;
+                                       callbackType.add(paramType);
                                        // Even if there're 2 callback arguments, we expect them to be of the same interface
                                }
                                String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
@@ -2638,23 +2611,23 @@ public class IoTCompiler {
         */
        private void writeCallbackInstantiationMethodBodyCplusStub(String paramIdent, String callbackType, int counter) {
 
-               println("auto it = IoTRMIUtil::mapSkel->find(" + paramIdent + ");");
-               println("if (it == IoTRMIUtil::mapSkel->end()) {");
+               println("auto it" + counter + " = IoTRMIUtil::mapSkel->find(" + paramIdent + ");");
+               println("if (it" + counter + " == IoTRMIUtil::mapSkel->end()) {");
                println("int newObjIdSent = rmiComm->getObjectIdCounter();");
-               println("objIdSent.push_back(newObjIdSent);");
+               println("objIdSent" + counter + ".push_back(newObjIdSent);");
                println("rmiComm->decrementObjectIdCounter();");
                println(callbackType + "_Skeleton* skel" + counter + " = new " + callbackType + "_Skeleton(" + paramIdent + ", rmiComm, newObjIdSent);");
-               println("vecCallbackObj.push_back(skel0);");
-               println("IoTRMIUtil::mapSkel->insert(make_pair(" + paramIdent + ", skel0));");
+               println("IoTRMIUtil::mapSkel->insert(make_pair(" + paramIdent + ", skel" + counter + "));");
                println("IoTRMIUtil::mapSkelId->insert(make_pair(" + paramIdent + ", newObjIdSent));");
-               println("thread th0 (&" + callbackType + "_Skeleton::___waitRequestInvokeMethod, std::ref(skel0), std::ref(skel0));");
-               println("th0.detach();");
+               println("thread th" + counter + " (&" + callbackType + "_Skeleton::___waitRequestInvokeMethod, std::ref(skel" + counter + 
+                       "), std::ref(skel" + counter +"));");
+               println("th" + counter + ".detach();");
                println("while(!skel" + counter + "->didInitWaitInvoke());");
                println("}");
                println("else");
                println("{");
                println("auto itId = IoTRMIUtil::mapSkelId->find(" + paramIdent + ");");
-               println("objIdSent.push_back(itId->second);");
+               println("objIdSent" + counter + ".push_back(itId->second);");
                println("}");
        }
 
@@ -2663,28 +2636,27 @@ public class IoTCompiler {
         * HELPER: writeCallbackMethodBodyCplusStub() writes the callback method of the stub class
         */
        private void writeCallbackMethodBodyCplusStub(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, String callbackType) {
+                       List<String> methPrmTypes, String method, Set<String> callbackType) {
 
                // Check if this is single object, array, or list of objects
                boolean isArrayOrList = false;
                String callbackParam = null;
-               println("vector<int> objIdSent;");
                for (int i = 0; i < methParams.size(); i++) {
-
                        String paramType = methPrmTypes.get(i);
                        if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                               println("vector<int> objIdSent" + i + ";");
                                String param = methParams.get(i);
                                if (isArrayOrList(paramType, param)) {  // Generate loop                                
                                        println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {");
-                                       writeCallbackInstantiationMethodBodyCplusStub("cb", callbackType, i);
+                                       writeCallbackInstantiationMethodBodyCplusStub("cb", returnGenericCallbackType(paramType), i);
                                        isArrayOrList = true;
                                        callbackParam = getSimpleIdentifier(param);
                                } else {
-                                       writeCallbackInstantiationMethodBodyCplusStub(getSimpleIdentifier(param), callbackType, i);
+                                       writeCallbackInstantiationMethodBodyCplusStub(getSimpleIdentifier(param), returnGenericCallbackType(paramType), i);
                                }
                                if (isArrayOrList)
                                        println("}");
-                               println("vector<int> ___paramCB" + i + " = objIdSent;");
+                               println("vector<int> ___paramCB" + i + " = objIdSent" + i + ";");
                        }
                }
        }
@@ -2843,7 +2815,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeStructParamClassCplusStub() writes member parameters of struct
         */
-       private void writeStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+       private void writeStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes, Set<String> callbackType) {
 
                print("int numParam = ");
                writeLengthStructParamClassCplusStub(methParams, methPrmTypes);
@@ -2980,7 +2952,7 @@ 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, String callbackType, boolean isCallbackMethod) {
+                       List<String> methPrmTypes, String method, Set<String> callbackType, boolean isCallbackMethod) {
 
                checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method);
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
@@ -3059,7 +3031,6 @@ public class IoTCompiler {
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
                        int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("int objectId = " + newObjectId + ";");
                        println("static set<int> set" + newObjectId + "Allowed;");
                }
        }       
@@ -3074,13 +3045,6 @@ public class IoTCompiler {
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("int objectId = " + objId + ";");
-               if (callbackExist) {
-               // We assume that each class only has one callback interface for now
-                       Iterator it = callbackClasses.iterator();
-                       String callbackType = (String) it.next();
-                       println("// Callback properties");
-                       println("vector<" + callbackType + "*> vecCallbackObj;");
-               }
                println("// Synchronization variables");
                for (String method : methods) {
                        // Generate AtomicBooleans for methods that have return values
@@ -3149,39 +3113,11 @@ public class IoTCompiler {
                println("delete rmiComm;");
                println("rmiComm = NULL;");
                println("}");
-               if (callbackExist) {
-               // We assume that each class only has one callback interface for now
-                       Iterator it = callbackClasses.iterator();
-                       String callbackType = (String) it.next();
-                       println("for(" + callbackType + "* cb : vecCallbackObj) {");
-                       println("delete cb;");
-                       println("cb = NULL;");
-                       println("}");
-               }
                println("}");
                println("");
        }
 
 
-       /**
-        * HELPER: writeCplusMethodCallbackPermission() writes permission checks in stub for callbacks
-        */
-       private void writeCplusMethodCallbackPermission(String intface) {
-
-               println("int methodId = IoTRMIObject::getMethodId(method);");
-               // Get all the different stubs
-               Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-               for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                       String newIntface = intMeth.getKey();
-                       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("return;");
-                       println("}");
-               }
-       }
-
-
        /**
         * generateCPlusStubClassesHpp() generate stubs based on the methods list in C++ (.hpp file)
         */
@@ -3225,7 +3161,7 @@ public class IoTCompiler {
                                println(newStubClass + "(IoTRMIComm* _rmiComm, int _objectId);");
                                println("~" + newStubClass + "();");
                                // Write methods
-                               writeMethodDeclCplusStub(methods, intDecl, callbackClasses, newStubClass);
+                               writeMethodDeclCplusStub(methods, intDecl);
                                print("}"); println(";");
                                println("#endif");
                                pw.close();
@@ -3249,11 +3185,11 @@ public class IoTCompiler {
                println(newStubClass + "* obj = (" + newStubClass + "*) t;");
                println("delete obj;");
                println("}\n");
-               //println("extern \"C\" void init" + newStubClass + "(void* t) {");
+               println("extern \"C\" void init" + newStubClass + "(void* t) {");
                //println(newStubClass + "* obj = (" + newStubClass + "*) t;");
                //println("obj->init();");
                //println("while(true);");
-               //println("}\n");
+               println("}\n");
        }
 
 
@@ -3317,17 +3253,11 @@ public class IoTCompiler {
        private void writePropertiesCplusSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
 
                println(intface + " *mainObj;");
-               // Callback
-               if (callbackExist) {
-                       Iterator it = callbackClasses.iterator();
-                       String callbackType = (String) it.next();
-                       String exchangeType = checkAndGetParamClass(callbackType);
-                       println("// Callback properties");
-                       println("vector<" + exchangeType + "*> vecCallbackObj;");
-               }
                println("IoTRMIComm *rmiComm;");
                println("char* methodBytes;");
                println("int methodLen;");
+               Integer objId = mapIntfaceObjId.get(intface);
+               println("int objectId = " + objId + ";");
                // Keep track of object Ids of all stubs registered to this interface
                writePropertiesCplusPermission(intface);
                println("// Synchronization variables");
@@ -3450,16 +3380,6 @@ public class IoTCompiler {
                println("delete rmiComm;");
                println("rmiComm = NULL;");
                println("}");
-               if (callbackExist) {
-               // We assume that each class only has one callback interface for now
-                       Iterator it = callbackClasses.iterator();
-                       String callbackType = (String) it.next();
-                       String exchangeType = checkAndGetParamClass(callbackType);
-                       println("for(" + exchangeType + "* cb : vecCallbackObj) {");
-                       println("delete cb;");
-                       println("cb = NULL;");
-                       println("}");
-               }
                println("}");
                println("");
        }
@@ -3525,8 +3445,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, String intface, String newSkelClass) {
+       private void writeMethodCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, String newSkelClass) {
 
                for (String method : methods) {
 
@@ -3535,15 +3454,9 @@ public class IoTCompiler {
                        String methodId = intDecl.getMethodId(method);
                        String methodType = checkAndGetCplusType(intDecl.getMethodType(method));
                        print(methodType + " " + newSkelClass + "::" + methodId + "(");
-                       boolean isCallbackMethod = false;
-                       String callbackType = null;
                        for (int i = 0; i < methParams.size(); i++) {
 
                                String origParamType = methPrmTypes.get(i);
-                               if (callbackClasses.contains(origParamType)) { // Check if this has callback object
-                                       isCallbackMethod = true;
-                                       callbackType = origParamType;   
-                               }
                                String paramType = checkAndGetParamClass(methPrmTypes.get(i));
                                String methPrmType = checkAndGetCplusType(paramType);
                                String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
@@ -3564,7 +3477,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeCallbackCplusNumStubs() writes the numStubs variable
         */
-       private void writeCallbackCplusNumStubs(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+       private void writeCallbackCplusNumStubs(List<String> methParams, List<String> methPrmTypes, Set<String> callbackType) {
 
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
@@ -3580,20 +3493,18 @@ public class IoTCompiler {
        /**
         * HELPER: writeCallbackInstantiationCplusStubGeneration() writes the instantiation of callback stubs
         */
-       private void writeCallbackInstantiationCplusStubGeneration(String exchParamType) {
+       private void writeCallbackInstantiationCplusStubGeneration(String exchParamType, int counter) {
 
-               //println("int objIdRecv = (int[]) paramObj[0];");
-               println(exchParamType + "* newStub = NULL;");
-               println("auto it = IoTRMIUtil::mapStub->find(objIdRecv);");
-               println("if (it == IoTRMIUtil::mapStub->end()) {");
-               println("newStub = new " + exchParamType + "_Stub(rmiComm, objIdRecv);");
-               println("IoTRMIUtil::mapStub->insert(make_pair(objIdRecv, newStub));");
-               println("rmiComm->setObjectIdCounter(objIdRecv);");
+               println(exchParamType + "* newStub" + counter + " = NULL;");
+               println("auto it" + counter + " = IoTRMIUtil::mapStub->find(objIdRecv" + counter + ");");
+               println("if (it" + counter + " == IoTRMIUtil::mapStub->end()) {");
+               println("newStub" + counter + " = new " + exchParamType + "_Stub(rmiComm, objIdRecv" + counter + ");");
+               println("IoTRMIUtil::mapStub->insert(make_pair(objIdRecv" + counter + ", newStub" + counter + "));");
+               println("rmiComm->setObjectIdCounter(objIdRecv" + counter + ");");
                println("rmiComm->decrementObjectIdCounter();");
-               println("skel->vecCallbackObj.push_back(newStub);");
                println("}");
                println("else {");
-               println("newStub = (" + exchParamType + "_Stub*) it->second;");
+               println("newStub" + counter + " = (" + exchParamType + "_Stub*) it" + counter + "->second;");
                println("}");
        }
 
@@ -3601,7 +3512,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeCallbackCplusStubGeneration() writes the callback stub generation part
         */
-       private void writeCallbackCplusStubGeneration(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+       private void writeCallbackCplusStubGeneration(List<String> methParams, List<String> methPrmTypes, Set<String> callbackType) {
 
                // Iterate over callback objects
                for (int i = 0; i < methParams.size(); i++) {
@@ -3613,14 +3524,14 @@ public class IoTCompiler {
                                if (isArrayOrList(paramType, param)) {
                                        println("vector<" + exchParamType + "*> stub" + i + ";");
                                        println("for (int i = 0; i < numStubIdArray" + i + ".size(); i++) {");
-                                       println("int objIdRecv = numStubIdArray" + i + "[i];");
-                                       writeCallbackInstantiationCplusStubGeneration(exchParamType);
-                                       println("stub" + i + ".push_back(newStub);");
+                                       println("int objIdRecv" + i + " = numStubIdArray" + i + "[i];");
+                                       writeCallbackInstantiationCplusStubGeneration(exchParamType, i);
+                                       println("stub" + i + ".push_back(newStub" + i + ");");
                                        println("}");
                                } else {
-                                       println("int objIdRecv = numStubIdArray" + i + "[0];");
-                                       writeCallbackInstantiationCplusStubGeneration(exchParamType);
-                                       println(exchParamType + "* stub" + i + " = newStub;");
+                                       println("int objIdRecv" + i + " = numStubIdArray" + i + "[0];");
+                                       writeCallbackInstantiationCplusStubGeneration(exchParamType, i);
+                                       println(exchParamType + "* stub" + i + " = newStub" + i + ";");
                                }
                        }
                }
@@ -3710,7 +3621,7 @@ public class IoTCompiler {
         * HELPER: writeMethodHelperReturnCplusSkeleton() writes the return statement part in skeleton
         */
        private void writeMethodHelperReturnCplusSkeleton(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, boolean isCallbackMethod, String callbackType,
+                       List<String> methPrmTypes, String method, boolean isCallbackMethod, Set<String> callbackType,
                        String methodId, Set<String> callbackClasses) {
 
                println("rmiComm->getMethodParams(paramCls, numParam, paramObj, localMethodBytes);");
@@ -3756,13 +3667,15 @@ public class IoTCompiler {
 
                // Generate array of parameter types
                boolean isCallbackMethod = false;
-               String callbackType = null;
+               //String callbackType = null;
+               Set<String> callbackType = new HashSet<String>();
                print("string paramCls[] = { ");
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = returnGenericCallbackType(methPrmTypes.get(i));
                        if (callbackClasses.contains(paramType)) {
                                isCallbackMethod = true;
-                               callbackType = paramType;
+                               //callbackType = paramType;
+                               callbackType.add(paramType);
                                print("\"int*\"");
                        } else {        // Generate normal classes if it's not a callback object
                                String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
@@ -3950,7 +3863,7 @@ public class IoTCompiler {
 
                // Generate array of parameter objects
                boolean isCallbackMethod = false;
-               String callbackType = null;
+               Set<String> callbackType = new HashSet<String>();
                print("int numParam = ");
                writeLengthStructParamClassSkeleton(methParams, methPrmTypes, method, intDecl);
                println(";");
@@ -3968,7 +3881,8 @@ public class IoTCompiler {
                                String prmType = returnGenericCallbackType(methPrmTypes.get(i));
                                if (callbackClasses.contains(prmType)) {
                                        isCallbackMethod = true;
-                                       callbackType = prmType;
+                                       //callbackType = prmType;
+                                       callbackType.add(prmType);
                                        //println("int numStubs" + i + " = 0;");
                                        println("vector<int> numStubIdArray" + i + ";");
                                        println("paramCls[pos] = \"int*\";");
@@ -3999,8 +3913,7 @@ public class IoTCompiler {
        /**
         * HELPER: writeMethodHelperDeclCplusSkeleton() writes the method helper declarations of the skeleton class
         */
-       private void writeMethodHelperDeclCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, 
-                       Set<String> callbackClasses, String newSkelClass) {
+       private void writeMethodHelperDeclCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, String newSkelClass) {
 
                // Use this set to handle two same methodIds
                Set<String> uniqueMethodIds = new HashSet<String>();
@@ -4344,7 +4257,7 @@ public class IoTCompiler {
                        println("bool didInitWaitInvoke();");
                        writeMethodDeclCplusSkeleton(methods, intDecl, callbackClasses);
                        // Write method helper declarations
-                       writeMethodHelperDeclCplusSkeleton(methods, intDecl, callbackClasses, newSkelClass);
+                       writeMethodHelperDeclCplusSkeleton(methods, intDecl, newSkelClass);
                        // Write waitRequestInvokeMethod() declaration - main loop
                        println("void ___waitRequestInvokeMethod(" + newSkelClass + "* skel);");
                        println("};");
@@ -4373,17 +4286,17 @@ public class IoTCompiler {
 
                println("extern \"C\" void* create" + newSkelClass + "(void** params) {");
                println("// Args: *_mainObj, int _portSend, int _portRecv");
-               println("return new " + newSkelClass + "((" + intface + "*) params[0], *((int*) params[0]), *((int*) params[1]));");
+               println("return new " + newSkelClass + "((" + intface + "*) params[0], *((int*) params[1]), *((int*) params[2]));");
                println("}\n");
                println("extern \"C\" void destroy" + newSkelClass + "(void* t) {");
                println(newSkelClass + "* obj = (" + newSkelClass + "*) t;");
                println("delete obj;");
                println("}\n");
-               //println("extern \"C\" void init" + newSkelClass + "(void* t) {");
+               println("extern \"C\" void init" + newSkelClass + "(void* t) {");
                //println(newSkelClass + "* obj = (" + newSkelClass + "*) t;");
                //println("obj->init();");
                //println("while(true);");
-               //println("}\n");
+               println("}\n");
        }
 
 
@@ -4425,7 +4338,7 @@ public class IoTCompiler {
                        // Write didInitWaitInvoke() to return bool
                        writeReturnDidAlreadyInitWaitInvoke(newSkelClass);
                        // Write methods
-                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false, intface, newSkelClass);
+                       writeMethodCplusSkeleton(methods, intDecl, newSkelClass);
                        // Write method helper
                        writeMethodHelperCplusSkeleton(methods, intDecl, callbackClasses, newSkelClass);
                        // Write waitRequestInvokeMethod() - main loop
@@ -5329,10 +5242,8 @@ public class IoTCompiler {
                                comp.generateCPlusInterfaces();
                                comp.generateCPlusStubClassesHpp();
                                comp.generateCPlusStubClassesCpp();
-                               //comp.generateCPlusCallbackStubClasses();
                                comp.generateCplusSkeletonClassHpp();
                                comp.generateCplusSkeletonClassCpp();
-                               //comp.generateCplusCallbackSkeletonClass();
                        } else {
                        // Check other options
                                while(i < args.length) {
@@ -5360,10 +5271,8 @@ public class IoTCompiler {
                                                        comp.generateCPlusInterfaces();
                                                        comp.generateCPlusStubClassesHpp();
                                                        comp.generateCPlusStubClassesCpp();
-                                                       //comp.generateCPlusCallbackStubClasses();
                                                        comp.generateCplusSkeletonClassHpp();
                                                        comp.generateCplusSkeletonClassCpp();
-                                                       //comp.generateCplusCallbackSkeletonClass();
                                                }
                                        }
                                        i = i + 2;