Preparing Makefiles, stub, skeleton, config files, etc. for porting LifxLightBulb...
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index 9eafffbef295bb01b287b4d6075600f2863f0d92..0f696bec03967f6d72355335e4c41cdaaa68d1fb 100644 (file)
@@ -361,7 +361,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);
@@ -414,7 +414,7 @@ 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);
@@ -457,10 +457,10 @@ 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();
@@ -545,7 +545,7 @@ public class IoTCompiler {
                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("Object[] paramObj = new Object[] { ports[0], callbackAddress, 0 };");
                println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
                println("}\n");
        }
@@ -742,7 +742,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 +757,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 +869,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 +878,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 +898,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 +961,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);
        }
 
 
@@ -968,56 +996,6 @@ 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;");
-                       }
-               }
        }
 
 
@@ -1054,8 +1032,8 @@ 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) {
@@ -1356,23 +1334,24 @@ 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);");
@@ -1403,17 +1382,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 + "];");
@@ -1672,10 +1658,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")) {
@@ -1689,14 +1677,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
@@ -1845,9 +1834,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 +1967,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);
                        }
@@ -2221,9 +2212,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);
                                        }
@@ -2551,7 +2543,8 @@ public class IoTCompiler {
                                println("#define _" + newIntface.toUpperCase() + "_HPP__");
                                println("#include <iostream>");
                                // 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 +2553,7 @@ public class IoTCompiler {
                                println("{");
                                println("public:");
                                // Write methods
-                               writeMethodCplusInterface(intMeth.getValue(), intDecl);
+                               writeMethodCplusInterface(methods, intDecl);
                                println("};");
                                println("#endif");
                                pw.close();
@@ -2604,8 +2597,7 @@ 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) {
@@ -2633,67 +2625,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, objIdCnt++);");
                                        isArrayOrList = true;
                                        callbackParam = getSimpleIdentifier(param);
                                } else
-                                       println(callbackType + "_CallbackSkeleton* skel = new " + callbackType + "_CallbackSkeleton(" +
+                                       println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
                                                getSimpleIdentifier(param) + ", objIdCnt++);");
-                               println("vecCallbackObj.push_back(skel);");
+                               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 +2796,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 +2811,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 +2917,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 +2944,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,8 +3007,7 @@ 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);
@@ -3082,10 +3035,10 @@ 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, const char* _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();
@@ -3200,11 +3153,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("int rev = 0;");
-               println("void* paramObj[] = { &ports[0], &address, &rev };");
+               println("void* paramObj[] = { &ports[0], &callbackAddress, &rev };");
                println("void* retObj = NULL;");
                println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
                println("}\n");
@@ -3695,7 +3647,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) {
@@ -3842,13 +3794,13 @@ public class IoTCompiler {
                        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
@@ -3858,14 +3810,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 + ";");
                        }
                }
        }
@@ -3897,13 +3849,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 + ";");
                                        }
                                }
                        }
@@ -3981,8 +3933,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 +3948,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 +3987,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);
                                        }
@@ -4350,9 +4303,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);
                                        }