Minor fixes in LifxLightBulb driver (not fully tested yet)
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index 9c6a16aad378302e185fecc20240f6fa0d3d8039..a46a20be8ffc9a63d42ee85d52289a400b1535b5 100644 (file)
@@ -48,6 +48,7 @@ public class IoTCompiler {
        private Map<String,ParseTreeHandler> mapIntfacePTH;
        private Map<String,DeclarationHandler> mapIntDeclHand;
        private Map<String,Map<String,Set<String>>> mapInt2NewInts;
+       private Map<String,String> mapInt2NewIntName;
        // Data structure to store our types (primitives and non-primitives) for compilation
        private Map<String,String> mapPrimitives;
        private Map<String,String> mapNonPrimitivesJava;
@@ -58,6 +59,9 @@ public class IoTCompiler {
        private PrintWriter pw;
        private String dir;
        private String subdir;
+       private Map<String,Integer> mapPortCount;       // Counter for ports
+       private static int portCount = 0;
+
 
        /**
         * Class constants
@@ -68,9 +72,12 @@ public class IoTCompiler {
 
                PRIMITIVES,             // All the primitive types, e.g. byte, short, int, long, etc.
                NONPRIMITIVES,  // Non-primitive types, e.g. Set, Map, List, etc.
-               USERDEFINED             // Non-supported type by default; assumed as driver classes
+               ENUM,                   // Enum type
+               STRUCT,                 // Struct type
+               USERDEFINED             // Assumed as driver classes
        }
 
+
        /**
         * Class constructors
         */
@@ -79,6 +86,7 @@ public class IoTCompiler {
                mapIntfacePTH = new HashMap<String,ParseTreeHandler>();
                mapIntDeclHand = new HashMap<String,DeclarationHandler>();
                mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
+               mapInt2NewIntName = new HashMap<String,String>();
                mapIntfaceObjId = new HashMap<String,Integer>();
                mapNewIntfaceObjId = new HashMap<String,Integer>();
                mapPrimitives = new HashMap<String,String>();
@@ -87,6 +95,7 @@ public class IoTCompiler {
                        arraysToMap(mapNonPrimitivesJava, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitiveJavaLibs);
                mapNonPrimitivesCplus = new HashMap<String,String>();
                        arraysToMap(mapNonPrimitivesCplus, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
+               mapPortCount = new HashMap<String,Integer>();
                pw = null;
                dir = OUTPUT_DIRECTORY;
                subdir = null;
@@ -102,7 +111,6 @@ public class IoTCompiler {
         * data structures.
         * Additionally, the data structure handles are
         * returned from tree-parsing for further process.
-        *
         */
        public void setDataStructures(String origInt, ParseNode pnPol, ParseNode pnReq) {
 
@@ -174,6 +182,9 @@ public class IoTCompiler {
                        }
                        // Add interface and methods information into map
                        mapNewIntMethods.put(strInt, setMethods);
+                       // Map new interface method name to the original interface
+                       // TODO: perhaps need to check in the future if we have more than 1 stub interface for one original interface
+                       mapInt2NewIntName.put(origInt, strInt);
                }
                // Map the map of interface-methods to the original interface
                mapInt2NewInts.put(origInt, mapNewIntMethods);
@@ -181,7 +192,7 @@ public class IoTCompiler {
 
 
        /**
-        * HELPER: writeMethodJavaLocalInterface() writes the method of the interface
+        * HELPER: writeMethodJavaLocalInterface() writes the method of the local interface
         */
        private void writeMethodJavaLocalInterface(Collection<String> methods, InterfaceDecl intDecl) {
 
@@ -194,7 +205,7 @@ public class IoTCompiler {
                        for (int i = 0; i < methParams.size(); i++) {
                                // Check for params with driver class types and exchange it 
                                //              with its remote interface
-                               String paramType = checkAndGetParamClass(methPrmTypes.get(i), false);
+                               String paramType = checkAndGetParamClass(methPrmTypes.get(i));
                                print(paramType + " " + methParams.get(i));
                                // Check if this is the last element (don't print a comma)
                                if (i != methParams.size() - 1) {
@@ -233,50 +244,72 @@ public class IoTCompiler {
 
 
        /**
-        * HELPER: writeEnumJava() writes the enumeration declaration
+        * HELPER: generateEnumJava() writes the enumeration declaration
         */
-       private void writeEnumJava(EnumDecl enumDecl) {
-
-               Set<String> enumTypes = enumDecl.getEnumDeclarations();
-               // Iterate over enum declarations
-               for (String enType : enumTypes) {
-
-                       println("public enum " + enType + " {");
-                       List<String> enumMembers = enumDecl.getMembers(enType);
-                       for (int i = 0; i < enumMembers.size(); i++) {
+       private void generateEnumJava() throws IOException {
 
-                               String member = enumMembers.get(i);
-                               print(member);
-                               // Check if this is the last element (don't print a comma)
-                               if (i != enumMembers.size() - 1)
-                                       println(",");
-                               else
-                                       println("");
+               // Create a new directory
+               createDirectory(dir);
+               for (String intface : mapIntfacePTH.keySet()) {
+                       // Get the right EnumDecl
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       Set<String> enumTypes = enumDecl.getEnumDeclarations();
+                       // Iterate over enum declarations
+                       for (String enType : enumTypes) {
+                               // Open a new file to write into
+                               FileWriter fw = new FileWriter(dir + "/" + enType + ".java");
+                               pw = new PrintWriter(new BufferedWriter(fw));
+                               println("public enum " + enType + " {");
+                               List<String> enumMembers = enumDecl.getMembers(enType);
+                               for (int i = 0; i < enumMembers.size(); i++) {
+
+                                       String member = enumMembers.get(i);
+                                       print(member);
+                                       // Check if this is the last element (don't print a comma)
+                                       if (i != enumMembers.size() - 1)
+                                               println(",");
+                                       else
+                                               println("");
+                               }
+                               println("}\n");
+                               pw.close();
+                               System.out.println("IoTCompiler: Generated enum class " + enType + ".java...");
                        }
-                       println("}\n");
                }
        }
 
 
        /**
-        * HELPER: writeStructJava() writes the struct declaration
+        * HELPER: generateStructJava() writes the struct declaration
         */
-       private void writeStructJava(StructDecl structDecl) {
+       private void generateStructJava() throws IOException {
 
-               List<String> structTypes = structDecl.getStructTypes();
-               // Iterate over enum declarations
-               for (String stType : structTypes) {
-
-                       println("public class " + stType + " {");
-                       List<String> structMemberTypes = structDecl.getMemberTypes(stType);
-                       List<String> structMembers = structDecl.getMembers(stType);
-                       for (int i = 0; i < structMembers.size(); i++) {
-
-                               String memberType = structMemberTypes.get(i);
-                               String member = structMembers.get(i);
-                               println("public static " + memberType + " " + member + ";");
+               // Create a new directory
+               createDirectory(dir);
+               for (String intface : mapIntfacePTH.keySet()) {
+                       // Get the right StructDecl
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       List<String> structTypes = structDecl.getStructTypes();
+                       // Iterate over enum declarations
+                       for (String stType : structTypes) {
+                               // Open a new file to write into
+                               FileWriter fw = new FileWriter(dir + "/" + stType + ".java");
+                               pw = new PrintWriter(new BufferedWriter(fw));
+                               println("public class " + stType + " {");
+                               List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+                               List<String> structMembers = structDecl.getMembers(stType);
+                               for (int i = 0; i < structMembers.size(); i++) {
+
+                                       String memberType = structMemberTypes.get(i);
+                                       String member = structMembers.get(i);
+                                       println("public static " + memberType + " " + member + ";");
+                               }
+                               println("}\n");
+                               pw.close();
+                               System.out.println("IoTCompiler: Generated struct class " + stType + ".java...");
                        }
-                       println("}\n");
                }
        }
 
@@ -302,16 +335,12 @@ public class IoTCompiler {
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                        List<String> methods = intDecl.getMethods();
                        Set<String> importClasses = getImportClasses(methods, intDecl);
-                       printImportStatements(importClasses);
+                       List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
+                       List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+                       printImportStatements(allImportClasses);
                        // Write interface header
                        println("");
                        println("public interface " + intface + " {");
-                       // Write enum if any...
-                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
-                       writeEnumJava(enumDecl);
-                       // Write struct if any...
-                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
-                       writeStructJava(structDecl);
                        // Write methods
                        writeMethodJavaLocalInterface(methods, intDecl);
                        println("}");
@@ -321,6 +350,17 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: updateIntfaceObjIdMap() updates the mapping between new interface and object Id
+        */
+       private void updateIntfaceObjIdMap(String intface, String newIntface) {
+
+               Integer objId = mapIntfaceObjId.get(intface);
+               mapNewIntfaceObjId.put(newIntface, objId);
+               mapIntfaceObjId.put(intface, objId++);
+       }
+
+
        /**
         * generateJavaInterfaces() generate stub interfaces based on the methods list in Java
         */
@@ -340,13 +380,17 @@ public class IoTCompiler {
                                DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                                InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                                // Pass in set of methods and get import classes
-                               Set<String> importClasses = getImportClasses(intMeth.getValue(), intDecl);
-                               printImportStatements(importClasses);
+                               Set<String> methods = intMeth.getValue();
+                               Set<String> importClasses = getImportClasses(methods, intDecl);
+                               List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
+                               List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+                               printImportStatements(allImportClasses);
                                // Write interface header
                                println("");
                                println("public interface " + newIntface + " {\n");
+                               updateIntfaceObjIdMap(intface, newIntface);
                                // Write methods
-                               writeMethodJavaInterface(intMeth.getValue(), intDecl);
+                               writeMethodJavaInterface(methods, intDecl);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated interface " + newIntface + ".java...");
@@ -355,19 +399,48 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writePropertiesJavaPermission() writes the permission in properties
+        */
+       private void writePropertiesJavaPermission(String intface, InterfaceDecl intDecl) {
+
+               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("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;
+                       for (String methodId : methodIds) {
+                               int methodNumId = intDecl.getMethodNumId(methodId);
+                               print(Integer.toString(methodNumId));
+                               // Check if this is the last element (don't print a comma)
+                               if (i != methodIds.size() - 1) {
+                                       print(", ");
+                               }
+                               i++;
+                       }
+                       println(" };");
+                       println("private static List<Integer> set" + newObjectId + "Allowed;");
+               }
+       }
+
+
        /**
         * HELPER: writePropertiesJavaStub() writes the properties of the stub class
         */
        private void writePropertiesJavaStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
 
                println("private IoTRMICall rmiCall;");
-               println("private String address;");
+               println("private String callbackAddress;");
                println("private int[] ports;\n");
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("private final static int objectId = " + objId + ";");
-               mapNewIntfaceObjId.put(newIntface, objId);
-               mapIntfaceObjId.put(intface, objId++);
+               //mapNewIntfaceObjId.put(newIntface, objId);
+               //mapIntfaceObjId.put(intface, objId++);
                if (callbackExist) {
                // We assume that each class only has one callback interface for now
                        Iterator it = callbackClasses.iterator();
@@ -375,25 +448,47 @@ public class IoTCompiler {
                        println("// Callback properties");
                        println("private IoTRMIObject rmiObj;");
                        println("List<" + callbackType + "> listCallbackObj;");
-                       println("private static int objIdCnt = 0;");
+                       println("private int objIdCnt = 0;");
+                       // Generate permission stuff for callback stubs
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writePropertiesJavaPermission(callbackType, intDecl);
                }
                println("\n");
        }
 
 
+       /**
+        * HELPER: writeConstructorJavaPermission() writes the permission in constructor
+        */
+       private void writeConstructorJavaPermission(String intface) {
+
+               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 = new ArrayList<Integer>(Arrays.asList(object" + newObjectId +"Permission));");
+               }
+       }
+
+
        /**
         * HELPER: writeConstructorJavaStub() writes the constructor of the stub class
         */
-       private void writeConstructorJavaStub(String intface, boolean callbackExist, Set<String> callbackClasses) {
+       private void writeConstructorJavaStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
-               println("public " + intface + "(int _port, String _address, int _rev, int[] _ports) throws Exception {");
-               println("address = _address;");
+               println("public " + newStubClass + "(int _port, String _skeletonAddress, String _callbackAddress, int _rev, int[] _ports) throws Exception {");
+               println("callbackAddress = _callbackAddress;");
                println("ports = _ports;");
-               println("rmiCall = new IoTRMICall(_port, _address, _rev);");
+               println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev);");
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
+                       writeConstructorJavaPermission(callbackType);
                        println("listCallbackObj = new ArrayList<" + callbackType + ">();");
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeJavaInitCallbackPermission(callbackType, intDecl, callbackExist);
                        println("___initCallBack();");
                }
                println("}\n");
@@ -401,27 +496,78 @@ public class IoTCompiler {
 
 
        /**
-        * HELPER: writeConstructorJavaStub() writes the constructor of the stub class
+        * 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
+        */
+       private int getPortCount(String intface) {
+
+               if (!mapPortCount.containsKey(intface))
+                       mapPortCount.put(intface, portCount++);
+               return mapPortCount.get(intface);
+       }
+
+
+       /**
+        * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
         */
-       private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl) {
+       private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl, String newStubClass) {
 
                println("public void ___initCallBack() {");
                // Generate main thread for callbacks
                println("Thread thread = new Thread() {");
                println("public void run() {");
                println("try {");
-               println("rmiObj = new IoTRMIObject(ports[0]);");
+               int port = getPortCount(newStubClass);
+               println("rmiObj = new IoTRMIObject(ports[" + port + "]);");
                println("while (true) {");
                println("byte[] method = rmiObj.getMethodBytes();");
                println("int objId = IoTRMIObject.getObjectId(method);");
                println(intface + "_CallbackSkeleton skel = (" + intface + "_CallbackSkeleton) listCallbackObj.get(objId);");
                println("if (skel != null) {");
+               writeJavaMethodCallbackPermission(intface);
                println("skel.invokeMethod(rmiObj);");
-               println("} else {");
+               print("}");
+               println(" else {");
                println("throw new Error(\"" + intface + ": Object with Id \" + objId + \" not found!\");");
                println("}");
                println("}");
-               println("} catch (Exception ex) {");
+               print("}");
+               println(" catch (Exception ex) {");
                println("ex.printStackTrace();");
                println("throw new Error(\"Error instantiating class " + intface + "_CallbackSkeleton!\");");
                println("}");
@@ -430,211 +576,529 @@ public class IoTCompiler {
                println("thread.start();\n");
                // Generate info sending part
                String method = "___initCallBack()";
-               println("int methodId = " + intDecl.getHelperMethodNumId(method) + ";");
+               int methodNumId = intDecl.getHelperMethodNumId(method);
+               println("int methodId = " + methodNumId + ";");
                println("Class<?> retType = void.class;");
-               println("Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };");
-               println("Object[] paramObj = new Object[] { ports[0], address, 0 };");
+               println("Class<?>[] paramCls = new Class<?>[] { int[].class, String.class, int.class };");
+               println("Object[] paramObj = new Object[] { ports, callbackAddress, 0 };");
                println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
                println("}\n");
        }
 
 
        /**
-        * HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class
+        * HELPER: checkAndWriteEnumTypeJavaStub() writes the enum type (convert from enum to int)
         */
-       private void writeStdMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method) {
+       private void checkAndWriteEnumTypeJavaStub(List<String> methParams, List<String> methPrmTypes) {
 
-               println("int methodId = " + intDecl.getMethodNumId(method) + ";");
-               String retType = intDecl.getMethodType(method);
-               println("Class<?> retType = " + getSimpleType(retType) + ".class;");
-               // Generate array of parameter types
-               print("Class<?>[] paramCls = new Class<?>[] { ");
+               // Iterate and find enum declarations
                for (int i = 0; i < methParams.size(); i++) {
-                       String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                       print(getSimpleType(paramType) + ".class");
-                       // Check if this is the last element (don't print a comma)
-                       if (i != methParams.size() - 1) {
-                               print(", ");
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isEnumClass(simpleType)) {
+                       // Check if this is enum type
+                               if (isArray(param)) {   // An array
+                                       println("int len" + i + " = " + getSimpleIdentifier(param) + ".length;");
+                                       println("int paramEnum" + i + "[] = new int[len" + i + "];");
+                                       println("for (int i = 0; i < len" + i + "; i++) {");
+                                       println("paramEnum" + i + "[i] = " + getSimpleIdentifier(param) + "[i].ordinal();");
+                                       println("}");
+                               } else if (isList(paramType)) { // A list
+                                       println("int len" + i + " = " + getSimpleIdentifier(param) + ".size();");
+                                       println("int paramEnum" + i + "[] = new int[len" + i + "];");
+                                       println("for (int i = 0; i < len" + i + "; i++) {");
+                                       println("paramEnum" + i + "[i] = " + getSimpleIdentifier(param) + ".get(i).ordinal();");
+                                       println("}");
+                               } else {        // Just one element
+                                       println("int paramEnum" + i + "[] = new int[1];");
+                                       println("paramEnum" + i + "[0] = " + param + ".ordinal();");
+                               }
                        }
                }
-               println(" };");
-               // Generate array of parameter objects
-               print("Object[] paramObj = new Object[] { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       print(getSimpleIdentifier(methParams.get(i)));
-                       // Check if this is the last element (don't print a comma)
-                       if (i != methParams.size() - 1) {
-                               print(", ");
+       }
+
+
+       /**
+        * HELPER: checkAndWriteEnumRetTypeJavaStub() writes the enum return type (convert from enum to int)
+        */
+       private void checkAndWriteEnumRetTypeJavaStub(String retType) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(getGenericType(retType));
+               // Take the inner type of generic
+               if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(retType);
+               if (isEnumClass(pureType)) {
+               // Check if this is enum type
+                       // Enum decoder
+                       println("int[] retEnum = (int[]) retObj;");
+                       println(pureType + "[] enumVals = " + pureType + ".values();");
+                       if (isArray(retType)) {                 // An array
+                               println("int retLen = retEnum.length;");
+                               println(pureType + "[] enumRetVal = new " + pureType + "[retLen];");
+                               println("for (int i = 0; i < retLen; i++) {");
+                               println("enumRetVal[i] = enumVals[retEnum[i]];");
+                               println("}");
+                       } else if (isList(retType)) {   // A list
+                               println("int retLen = retEnum.length;");
+                               println("List<" + pureType + "> enumRetVal = new ArrayList<" + pureType + ">();");
+                               println("for (int i = 0; i < retLen; i++) {");
+                               println("enumRetVal.add(enumVals[retEnum[i]]);");
+                               println("}");
+                       } else {        // Just one element
+                               println(pureType + " enumRetVal = enumVals[retEnum[0]];");
                        }
+                       println("return enumRetVal;");
                }
-               println(" };");
-               // Check if this is "void"
-               if (retType.equals("void")) {
-                       println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
-               } else { // We do have a return value
-               // Check if the return value NONPRIMITIVES
-                       if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) {
-                               String[] retGenValType = getTypeOfGeneric(retType);
-                               println("Class<?> retGenValType = " + retGenValType[0] + ".class;");
-                               println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);");
-                               println("return (" + retType + ")retObj;");
-                       } else {
-                               println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
-                               println("return (" + retType + ")retObj;");
+       }
+
+
+       /**
+        * HELPER: checkAndWriteStructSetupJavaStub() writes the struct type setup
+        */
+       private void checkAndWriteStructSetupJavaStub(List<String> methParams, List<String> methPrmTypes, 
+                       InterfaceDecl intDecl, String method) {
+               
+               // Iterate and find struct declarations
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                       // Check if this is enum type
+                               int methodNumId = intDecl.getMethodNumId(method);
+                               String helperMethod = methodNumId + "struct" + i;
+                               println("int methodIdStruct" + i + " = " + intDecl.getHelperMethodNumId(helperMethod) + ";");
+                               println("Class<?> retTypeStruct" + i + " = void.class;");
+                               println("Class<?>[] paramClsStruct" + i + " = new Class<?>[] { int.class };");
+                               if (isArray(param)) {   // An array
+                                       println("Object[] paramObjStruct" + i + " = new Object[] { " + getSimpleArrayType(param) + ".length };");
+                               } else if (isList(paramType)) { // A list
+                                       println("Object[] paramObjStruct" + i + " = new Object[] { " + getSimpleArrayType(param) + ".size() };");
+                               } else {        // Just one element
+                                       println("Object[] paramObjStruct" + i + " = new Object[] { new Integer(1) };");
+                               }
+                               println("rmiCall.remoteCall(objectId, methodIdStruct" + i + 
+                                               ", retTypeStruct" + i + ", null, paramClsStruct" + i + 
+                                               ", paramObjStruct" + i + ");\n");
                        }
                }
        }
 
 
        /**
-        * HELPER: checkCallbackType() checks the callback type
+        * HELPER: isStructPresent() checks presence of struct
         */
-       private boolean checkCallbackType(String paramType, String callbackType) {
+       private boolean isStructPresent(List<String> methParams, List<String> methPrmTypes) {
 
-               if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES)
-                       paramType = getTypeOfGeneric(paramType)[0];
-               return callbackType.equals(paramType);
+               // Iterate and find enum declarations
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType))
+                               return true;
+               }
+               return false;
        }
 
 
        /**
-        * HELPER: writeCallbackMethodBodyJavaStub() writes the callback method of the stub class
+        * HELPER: writeLengthStructParamClassJavaStub() writes lengths of parameters
         */
-       private void writeCallbackMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, String callbackType) {
+       private void writeLengthStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes) {
 
-               println("try {");
-               // Check if this is single object, array, or list of objects
+               // Iterate and find struct declarations - count number of params
                for (int i = 0; i < methParams.size(); i++) {
-
                        String paramType = methPrmTypes.get(i);
-                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               String param = methParams.get(i);
-                               if (isArrayOrList(paramType, param)) {  // Generate loop
-                                       println("for (" + paramType + " cb : " + getSimpleIdentifier(param) + ") {");
-                                       println(callbackType + "_CallbackSkeleton skel = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);");
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               int members = getNumOfMembers(simpleType);
+                               if (isArray(param)) {                   // An array
+                                       String structLen = getSimpleArrayType(param) + ".length";
+                                       print(members + "*" + structLen);
+                               } else if (isList(paramType)) { // A list
+                                       String structLen = getSimpleArrayType(param) + ".size()";
+                                       print(members + "*" + structLen);
                                } else
-                                       println(callbackType + "_CallbackSkeleton skel = new " + callbackType + "_CallbackSkeleton(" +
-                                               getSimpleIdentifier(param) + ", objIdCnt++);");
-                               println("listCallbackObj.add(skel);");
-                               if (isArrayOrList(paramType, param))
-                                       println("}");
+                                       print(Integer.toString(members));
+                       } else
+                               print("1");
+                       if (i != methParams.size() - 1) {
+                               print("+");
                        }
                }
-               println("} catch (Exception ex) {");
-               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(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");
+       }
+
+
+       /**
+        * HELPER: writeStructMembersJavaStub() writes parameters of struct
+        */
+       private void writeStructMembersJavaStub(String simpleType, String paramType, String param) {
+
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArray(param)) {                   // An array
+                       println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".length; i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("paramObj[pos++] = " + getSimpleIdentifier(param) + "[i].");
+                               print(getSimpleIdentifier(members.get(i)));
+                               println(";");
+                       }
+                       println("}");
+               } else if (isList(paramType)) { // A list
+                       println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".get(i).");
+                               print(getSimpleIdentifier(members.get(i)));
+                               println(";");
+                       }
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".");
+                               print(getSimpleIdentifier(members.get(i)));
+                               println(";");
                        }
-                       if (i != methParams.size() - 1) // Check if this is the last element
-                               print(", ");
                }
-               println(" };");
-               // Generate array of parameter objects
-               print("Object[] paramObj = new Object[] { ");
+       }
+
+
+       /**
+        * HELPER: writeStructParamClassJavaStub() writes parameters if struct is present
+        */
+       private void writeStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+
+               print("int paramLen = ");
+               writeLengthStructParamClassJavaStub(methParams, methPrmTypes);
+               println(";");
+               println("Object[] paramObj = new Object[paramLen];");
+               println("Class<?>[] paramCls = new Class<?>[paramLen];");
+               println("int pos = 0;");
+               // Iterate again over the parameters
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = methPrmTypes.get(i);
-                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               if (isArray(methPrmTypes.get(i), methParams.get(i)))
+                       String param = methParams.get(i);
+                       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), methParams.get(i)))
+                               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;");
+                               println(";");
                        } else {
-                               println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
-                               println("return (" + retType + ")retObj;");
+                               String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                               println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("paramObj[pos++] = ");
+                               print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
+                               println(";");
                        }
                }
+               
        }
 
 
        /**
-        * HELPER: writeMethodJavaStub() writes the method of the stub class
+        * HELPER: writeStructRetMembersJavaStub() writes parameters of struct for return statement
         */
-       private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
-
-               for (String method : methods) {
+       private void writeStructRetMembersJavaStub(String simpleType, String retType) {
+
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+               }
+               if (isArray(retType)) { // An array
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               print("structRet[i]." + getSimpleIdentifier(members.get(i)));
+                               println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];");
+                       }
+                       println("}");
+               } else if (isList(retType)) {   // A list
+                       println(simpleType + " structRetMem = new " + simpleType + "();");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               print("structRetMem." + getSimpleIdentifier(members.get(i)));
+                               println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];");
+                       }
+                       println("structRet.add(structRetMem);");
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               print("structRet." + getSimpleIdentifier(members.get(i)));
+                               println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];");
+                       }
+               }
+               println("return structRet;");
+       }
 
-                       List<String> methParams = intDecl.getMethodParams(method);
-                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-                       print("public " + intDecl.getMethodType(method) + " " +
-                               intDecl.getMethodId(method) + "(");
-                       boolean isCallbackMethod = false;
-                       String callbackType = null;
-                       for (int i = 0; i < methParams.size(); i++) {
 
-                               String paramType = methPrmTypes.get(i);
-                               if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES)
-                                       paramType = getTypeOfGeneric(paramType)[0];
-                               // Check if this has callback object
-                               if (callbackClasses != null) {
-                                       if (callbackClasses.contains(paramType)) {
-                                               isCallbackMethod = true;
-                                               callbackType = paramType;       
-                                               // Even if there're 2 callback arguments, we expect them to be of the same interface
-                                       }
-                               }
-                               print(methPrmTypes.get(i) + " " + methParams.get(i));
-                               // Check if this is the last element (don't print a comma)
-                               if (i != methParams.size() - 1) {
-                                       print(", ");
-                               }
+       /**
+        * HELPER: writeStructReturnJavaStub() writes parameters if struct is present for return statement
+        */
+       private void writeStructReturnJavaStub(String simpleType, String retType) {
+
+               // Handle the returned struct!!!
+               println("Object retLenObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
+               // Minimum retLen is 1 if this is a single struct object
+               println("int retLen = (int) retLenObj;");
+               int numMem = getNumOfMembers(simpleType);
+               println("Class<?>[] retCls = new Class<?>[" + numMem + "*retLen];");
+               println("Class<?>[] retClsVal = new Class<?>[" + numMem + "*retLen];");
+               println("int retPos = 0;");
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               println("retClsVal[retPos++] = null;");
+                       }
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               println("retClsVal[retPos++] = null;");
                        }
-                       println(") {");
-                       // Now, write the body of stub!
-                       if (isCallbackMethod)
-                               writeCallbackMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
-                       else
-                               writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method);
-                       println("}\n");
-                       // Write the init callback helper method
-                       if (isCallbackMethod)
-                               writeInitCallbackJavaStub(callbackType, intDecl);
                }
+               println("Object[] retObj = rmiCall.getStructObjects(retCls, retClsVal);");
+               if (isArray(retType)) {                 // An array
+                       println(simpleType + "[] structRet = new " + simpleType + "[retLen];");
+                       println("for(int i = 0; i < retLen; i++) {");
+                       println("structRet[i] = new " + simpleType + "();");
+                       println("}");
+               } else if (isList(retType)) {   // A list
+                       println("List<" + simpleType + "> structRet = new ArrayList<" + simpleType + ">();");
+               } else
+                       println(simpleType + " structRet = new " + simpleType + "();");
+               println("int retObjPos = 0;");
+               writeStructRetMembersJavaStub(simpleType, retType);
        }
 
 
        /**
-        * generateJavaStubClasses() generate stubs based on the methods list in Java
+        * HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class
         */
-       public void generateJavaStubClasses() throws IOException {
-
-               // Create a new directory
-               String path = createDirectories(dir, subdir);
-               for (String intface : mapIntfacePTH.keySet()) {
+       private void writeStdMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
+                       List<String> methPrmTypes, String method, String callbackType) {
 
-                       Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-                       for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+               checkAndWriteStructSetupJavaStub(methParams, methPrmTypes, intDecl, method);
+               println("int methodId = " + intDecl.getMethodNumId(method) + ";");
+               String retType = intDecl.getMethodType(method);
+               println("Class<?> retType = " + getSimpleType(getStructType(getEnumType(retType))) + ".class;");
+               checkAndWriteEnumTypeJavaStub(methParams, methPrmTypes);
+               // Generate array of parameter types
+               if (isStructPresent(methParams, methPrmTypes)) {
+                       writeStructParamClassJavaStub(methParams, methPrmTypes, callbackType);
+               } else {
+                       print("Class<?>[] paramCls = new Class<?>[] { ");
+                       for (int i = 0; i < methParams.size(); i++) {
+                               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(", ");
+                               }
+                       }
+                       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(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(", ");
+                               }
+                       }
+                       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
+                       // Generate array of parameter types
+                       if (isStructClass(getGenericType(getSimpleArrayType(retType)))) {
+                               writeStructReturnJavaStub(getGenericType(getSimpleArrayType(retType)), retType);
+                       } else {
+                               // This is an enum type
+                               if (getParamCategory(getGenericType(getSimpleArrayType(retType))) == ParamCategory.ENUM) {
+                                       println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
+                                       checkAndWriteEnumRetTypeJavaStub(retType);
+                               } else if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) {
+                               // Check if the return value NONPRIMITIVES
+                                       String retGenValType = getGenericType(retType);
+                                       println("Class<?> retGenValType = " + retGenValType + ".class;");
+                                       println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);");
+                                       println("return (" + retType + ")retObj;");
+                               } else {
+                                       println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
+                                       println("return (" + retType + ")retObj;");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: returnGenericCallbackType() returns the callback type
+        */
+       private String returnGenericCallbackType(String paramType) {
+
+               if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES)
+                       return getGenericType(paramType);
+               else
+                       return paramType;
+       }
+
+
+       /**
+        * HELPER: checkCallbackType() checks the callback type
+        */
+       private boolean checkCallbackType(String paramType, String callbackType) {
+
+               String prmType = returnGenericCallbackType(paramType);
+               if (callbackType == null)       // If there is no callbackType it means not a callback method
+                       return false;
+               else
+                       return callbackType.equals(prmType);
+       }
+
+
+       /**
+        * 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) {
+
+               println("try {");
+               // Check if this is single object, array, or list of objects
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                               String param = methParams.get(i);
+                               if (isArrayOrList(paramType, param)) {  // Generate loop
+                                       println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {");
+                                       println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);");
+                               } else
+                                       println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
+                                               getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
+                               println("listCallbackObj.add(skel" + i + ");");
+                               if (isArrayOrList(paramType, param))
+                                       println("}");
+                       }
+               }
+               print("}");
+               println(" catch (Exception ex) {");
+               println("ex.printStackTrace();");
+               println("throw new Error(\"Exception when generating skeleton objects!\");");
+               println("}\n");
+       }
+
+
+       /**
+        * HELPER: writeMethodJavaStub() writes the methods of the stub class
+        */
+       private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, String newStubClass) {
+
+               boolean isDefined = false;
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       print("public " + 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
+                               }
+                               print(methPrmTypes.get(i) + " " + methParams.get(i));
+                               // Check if this is the last element (don't print a comma)
+                               if (i != methParams.size() - 1) {
+                                       print(", ");
+                               }
+                       }
+                       println(") {");
+                       // Now, write the body of stub!
+                       if (isCallbackMethod)
+                               writeCallbackMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
+                       //else
+                       writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
+                       println("}\n");
+                       // Write the init callback helper method
+                       if (isCallbackMethod && !isDefined) {
+                               writeInitCallbackJavaStub(callbackType, intDecl, newStubClass);
+                               isDefined = true;
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: getStubInterface() gets stub interface name based on original interface
+        */
+       public String getStubInterface(String intface) {
+
+               return mapInt2NewIntName.get(intface);
+       }
+
+
+       /**
+        * generateJavaStubClasses() generate stubs based on the methods list in Java
+        */
+       public void generateJavaStubClasses() throws IOException {
+
+               // Create a new directory
+               String path = createDirectories(dir, subdir);
+               for (String intface : mapIntfacePTH.keySet()) {
+
+                       Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+                       for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
 
                                // Open a new file to write into
                                String newIntface = intMeth.getKey();
@@ -647,7 +1111,7 @@ public class IoTCompiler {
                                Set<String> methods = intMeth.getValue();
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaImportClasses();
-                               List<String> allImportClasses = getAllImportClasses(stdImportClasses, importClasses);
+                               List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                                printImportStatements(allImportClasses); println("");
                                // Find out if there are callback objects
                                Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
@@ -657,9 +1121,9 @@ public class IoTCompiler {
                                // Write properties
                                writePropertiesJavaStub(intface, newIntface, callbackExist, callbackClasses);
                                // Write constructor
-                               writeConstructorJavaStub(newStubClass, callbackExist, callbackClasses);
+                               writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java...");
@@ -674,8 +1138,10 @@ public class IoTCompiler {
        private void writePropertiesJavaCallbackStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
 
                println("private IoTRMICall rmiCall;");
+               println("private String callbackAddress;");
+               println("private int[] ports;\n");
                // Get the object Id
-               println("private static int objectId = 0;");
+               println("private int objectId = 0;");
                if (callbackExist) {
                // We assume that each class only has one callback interface for now
                        Iterator it = callbackClasses.iterator();
@@ -683,7 +1149,11 @@ public class IoTCompiler {
                        println("// Callback properties");
                        println("private IoTRMIObject rmiObj;");
                        println("List<" + callbackType + "> listCallbackObj;");
-                       println("private static int objIdCnt = 0;");
+                       println("private int objIdCnt = 0;");
+                       // Generate permission stuff for callback stubs
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writePropertiesJavaPermission(callbackType, intDecl);
                }
                println("\n");
        }
@@ -692,15 +1162,22 @@ public class IoTCompiler {
        /**
         * HELPER: writeConstructorJavaCallbackStub() writes the constructor of the callback stub class
         */
-       private void writeConstructorJavaCallbackStub(String intface, boolean callbackExist, Set<String> callbackClasses) {
+       private void writeConstructorJavaCallbackStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
-               println("public " + intface + "(IoTRMICall _rmiCall, int _objectId) throws Exception {");
+               // TODO: If we want callback in callback, then we need to add address and port initializations
+               println("public " + newStubClass + "(IoTRMICall _rmiCall, String _callbackAddress, int _objectId, int[] _ports) throws Exception {");
+               println("callbackAddress = _callbackAddress;");
                println("objectId = _objectId;");
                println("rmiCall = _rmiCall;");
+               println("ports = _ports;");
                if (callbackExist) {
                        Iterator it = callbackClasses.iterator();
                        String callbackType = (String) it.next();
+                       writeConstructorJavaPermission(callbackType);
                        println("listCallbackObj = new ArrayList<" + callbackType + ">();");
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeJavaInitCallbackPermission(callbackType, intDecl, callbackExist);
                        println("___initCallBack();");
                }
                println("}\n");
@@ -734,7 +1211,7 @@ public class IoTCompiler {
                                Set<String> methods = intMeth.getValue();
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaImportClasses();
-                               List<String> allImportClasses = getAllImportClasses(stdImportClasses, importClasses);
+                               List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                                printImportStatements(allImportClasses); println("");
                                // Find out if there are callback objects
                                Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
@@ -744,10 +1221,10 @@ public class IoTCompiler {
                                // Write properties
                                writePropertiesJavaCallbackStub(intface, newIntface, callbackExist, callbackClasses);
                                // Write constructor
-                               writeConstructorJavaCallbackStub(newStubClass, callbackExist, callbackClasses);
+                               writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
                                // Write methods
                                // TODO: perhaps need to generate callback for callback
-                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+                               writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
@@ -759,37 +1236,67 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesJavaSkeleton() writes the properties of the skeleton class
         */
-       private void writePropertiesJavaSkeleton(String intface, boolean callbackExist) {
+       private void writePropertiesJavaSkeleton(String intface, boolean callbackExist, InterfaceDecl intDecl) {
 
                println("private " + intface + " mainObj;");
                //println("private int ports;");
                println("private IoTRMIObject rmiObj;\n");
+               println("private String callbackAddress;");
                // Callback
                if (callbackExist) {
-                       println("private static int objIdCnt = 0;");
+                       println("private int objIdCnt = 0;");
                        println("private IoTRMICall rmiCall;");
+                       println("private int[] ports;\n");
                }
-               // Keep track of object Ids of all stubs registered to this interface
-               Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-               for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                       String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
-                       println("private final static int object" + newObjectId + "Id = " + 
-                               newObjectId + ";\t//" + newIntface);
-               }
+               writePropertiesJavaPermission(intface, intDecl);
                println("\n");
        }
 
 
+       /**
+        * HELPER: writeStructPermissionJavaSkeleton() writes permission for struct helper
+        */
+       private void writeStructPermissionJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, String intface) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       int methodHelperNumId = intDecl.getHelperMethodNumId(helperMethod);
+                                       // Iterate over interfaces to give permissions to
+                                       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(" + methodHelperNumId + ");");
+                                       }
+                               }
+                       }
+               }
+       }
+
+
        /**
         * HELPER: writeConstructorJavaSkeleton() writes the constructor of the skeleton class
         */
-       private void writeConstructorJavaSkeleton(String newSkelClass, String intface) {
+       private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods, boolean callbackExist) {
 
-               println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
+               println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _port) throws Exception {");
                println("mainObj = _mainObj;");
+               println("callbackAddress = _callbackAddress;");
                println("rmiObj = new IoTRMIObject(_port);");
-               //println("set0Allowed = Arrays.asList(object0Permission);");
+               // Generate permission control initialization
+               writeConstructorJavaPermission(intface);
+               writeJavaInitCallbackPermission(intface, intDecl, callbackExist);
+               writeStructPermissionJavaSkeleton(methods, intDecl, intface);
                println("___waitRequestInvokeMethod();");
                println("}\n");
        }
@@ -819,12 +1326,23 @@ public class IoTCompiler {
        /**
         * HELPER: writeInitCallbackJavaSkeleton() writes the init callback method for skeleton class
         */
-       private void writeInitCallbackJavaSkeleton() {
+       private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton, String intface) {
 
-               println("public void ___regCB() throws IOException {");
-               println("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },");
-               println("\tnew Class<?>[] { null, null, null });");
-               println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);");
+               // This is a callback skeleton generation
+               if (callbackSkeleton)
+                       println("public void ___regCB(IoTRMIObject rmiObj) throws IOException {");
+               else
+                       println("public void ___regCB() throws IOException {");
+               print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class, String.class, int.class },");
+               println("new Class<?>[] { null, null, null });");
+               println("ports = (int[]) paramObj[0];");
+               String stubInt = null;
+               if (callbackSkeleton)
+                       stubInt = getStubInterface(intface) + "_CallbackStub";
+               else
+                       stubInt = getStubInterface(intface) + "_Stub";
+               int port = getPortCount(stubInt);
+               println("rmiCall = new IoTRMICall(ports[" + port + "], (String) paramObj[1], (int) paramObj[2]);");
                println("}\n");
        }
 
@@ -832,8 +1350,10 @@ public class IoTCompiler {
        /**
         * HELPER: writeMethodJavaSkeleton() writes the method of the skeleton class
         */
-       private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+       private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, 
+                       boolean callbackSkeleton, String intface) {
 
+               boolean isDefined = false;
                for (String method : methods) {
 
                        List<String> methParams = intDecl.getMethodParams(method);
@@ -845,12 +1365,10 @@ public class IoTCompiler {
                        for (int i = 0; i < methParams.size(); i++) {
 
                                String origParamType = methPrmTypes.get(i);
-                               String paramType = checkAndGetParamClass(origParamType, false);
-                               if (callbackClasses != null) {  // Check if this has callback object
-                                       if (callbackClasses.contains(origParamType)) {
-                                               isCallbackMethod = true;
-                                               callbackType = origParamType;   
-                                       }
+                               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)
@@ -862,179 +1380,767 @@ public class IoTCompiler {
                        // Now, write the body of skeleton!
                        writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method));
                        println("}\n");
-                       if (isCallbackMethod)
-                               writeInitCallbackJavaSkeleton();
+                       if (isCallbackMethod && !isDefined) {   // Make sure that this function is only defined once!
+                               writeInitCallbackJavaSkeleton(callbackSkeleton, intface);
+                               isDefined = true;
+                       }
                }
        }
 
 
        /**
-        * HELPER: writeCallbackStubGeneration() writes the callback stub generation part
+        * HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part
         */
-       private Map<Integer,String> writeCallbackStubGeneration(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+       private Map<Integer,String> writeCallbackJavaStubGeneration(List<String> methParams, List<String> methPrmTypes, 
+                       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(paramType, false);
+                               String exchParamType = checkAndGetParamClass(getGenericType(paramType));
                                // Print array if this is array or list if this is a list of callback objects
-                               if (isArray(paramType, param)) {
-                                       println("int numStubs" + i + " = (int) paramObj[" + i + "];");
+                               if (isArray(param)) {
+                                       println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
                                        println(exchParamType + "[] stub" + i + " = new " + exchParamType + "[numStubs" + i + "];");
-                               } else if (isList(paramType, param)) {
-                                       println("int numStubs" + i + " = (int) paramObj[" + i + "];");
+                               } else if (isList(paramType)) {
+                                       println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
                                        println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();");
                                } else {
-                                       println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+                                       println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
                                        println("objIdCnt++;");
                                }
                        }
                        // Generate a loop if needed
                        if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
-                               String exchParamType = checkAndGetParamClass(paramType, false);
-                               if (isArray(paramType, param)) {
+                               String exchParamType = checkAndGetParamClass(getGenericType(paramType));
+                               if (isArray(param)) {
                                        println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
-                                       println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+                                       println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
                                        println("objIdCnt++;");
                                        println("}");
-                               } else if (isList(paramType, param)) {
+                               } else if (isList(paramType)) {
                                        println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
-                                       println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt));");
+                                       println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports));");
                                        println("objIdCnt++;");
                                        println("}");
                                }
+                               mapStubParam.put(i, "stub" + i);        // List of all stub parameters
                        }
-                       mapStubParam.put(i, "stub" + i);        // List of all stub parameters
                }
                return mapStubParam;
        }
 
 
        /**
-        * HELPER: writeStdMethodHelperBodyJavaSkeleton() writes the standard method body helper in the skeleton class
+        * HELPER: checkAndWriteEnumTypeJavaSkeleton() writes the enum type (convert from enum to int)
         */
-       private void writeStdMethodHelperBodyJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, Set<String> callbackClasses) {
-               // Generate array of parameter objects
-               boolean isCallbackMethod = false;
-               String callbackType = null;
-               print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { ");
+       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);
-                       if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES)
-                               paramType = getTypeOfGeneric(paramType)[0];
-                       if (callbackClasses != null) {
-                               if (callbackClasses.contains(paramType)) {
-                                       isCallbackMethod = true;
-                                       callbackType = paramType;
-                                       print("int.class");
-                               } else {
-                                       String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                                       print(getSimpleType(prmType) + ".class");
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isEnumClass(simpleType)) {
+                       // Check if this is enum type
+                               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 + "];");
+                                       println("for (int i = 0; i < len" + i + "; i++) {");
+                                       println("paramEnum" + i + "[i] = enumVals[paramInt" + i + "[i]];");
+                                       println("}");
+                               } else if (isList(paramType)) { // A list
+                                       println("int len" + i + " = paramInt" + i + ".length;");
+                                       println("List<" + simpleType + "> paramEnum" + i + " = new ArrayList<" + simpleType + ">();");
+                                       println("for (int i = 0; i < len" + i + "; i++) {");
+                                       println("paramEnum" + i + ".add(enumVals[paramInt" + i + "[i]]);");
+                                       println("}");
+                               } else {        // Just one element
+                                       println(simpleType + " paramEnum" + i + " = enumVals[paramInt" + i + "[0]];");
                                }
-                       } 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)
-                               print(", ");
-               }
-               println(" }, ");
-               // Generate generic class if it's a generic type.. null otherwise
-               print("new Class<?>[] { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       String prmType = methPrmTypes.get(i);
-                       if (getParamCategory(prmType) == ParamCategory.NONPRIMITIVES)
-                               print(getTypeOfGeneric(prmType)[0] + ".class");
-                       else
-                               print("null");
-                       if (i != methParams.size() - 1)
-                               print(", ");
-               }
-               println(" });");
-               Map<Integer,String> mapStubParam = null;
-               if (isCallbackMethod)
-                       mapStubParam = writeCallbackStubGeneration(methParams, methPrmTypes, callbackType);
-               // Check if this is "void"
-               String retType = intDecl.getMethodType(method);
-               if (retType.equals("void")) {
-                       print(intDecl.getMethodId(method) + "(");
-               } else { // We do have a return value
-                       print("Object retObj = " + intDecl.getMethodId(method) + "(");
                }
-               for (int i = 0; i < methParams.size(); i++) {
+       }
 
-                       if (isCallbackMethod) {
-                               print(mapStubParam.get(i));     // Get the callback parameter
-                       } else {
-                               String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                               print("(" + prmType + ") paramObj[" + i + "]");
+
+       /**
+        * HELPER: checkAndWriteEnumRetTypeJavaSkeleton() writes the enum return type (convert from enum to int)
+        */
+       private void checkAndWriteEnumRetTypeJavaSkeleton(String retType, String methodId) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(getGenericType(retType));
+               // Take the inner type of generic
+               if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(retType);
+               if (isEnumClass(pureType)) {
+               // Check if this is enum type
+                       // Enum decoder
+                       if (isArray(retType)) {                 // An array
+                               print(pureType + "[] retEnum = " + methodId + "(");
+                       } else if (isList(retType)) {   // A list
+                               print("List<" + pureType + "> retEnum = " + methodId + "(");
+                       } else {        // Just one element
+                               print(pureType + " retEnum = " + methodId + "(");
                        }
-                       if (i != methParams.size() - 1)
-                               print(", ");
-               }
-               println(");");
-               if (!retType.equals("void"))
-                       println("rmiObj.sendReturnObj(retObj);");
-               if (isCallbackMethod) { // Catch exception if this is callback
-                       println("} catch(Exception ex) {");
-                       println("ex.printStackTrace();");
-                       println("throw new Error(\"Exception from callback object instantiation!\");");
-                       println("}");
                }
        }
 
 
        /**
-        * HELPER: writeMethodHelperJavaSkeleton() writes the method helper of the skeleton class
+        * HELPER: checkAndWriteEnumRetConvJavaSkeleton() writes the enum return type (convert from enum to int)
         */
-       private void writeMethodHelperJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+       private void checkAndWriteEnumRetConvJavaSkeleton(String retType) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(getGenericType(retType));
+               // Take the inner type of generic
+               if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(retType);
+               if (isEnumClass(pureType)) {
+               // Check if this is enum type
+                       if (isArray(retType)) { // An array
+                               println("int retLen = retEnum.length;");
+                               println("int[] retEnumVal = new int[retLen];");
+                               println("for (int i = 0; i < retLen; i++) {");
+                               println("retEnumVal[i] = retEnum[i].ordinal();");
+                               println("}");
+                       } else if (isList(retType)) {   // A list
+                               println("int retLen = retEnum.size();");
+                               println("int[] retEnumVal = new int[retLen];");
+                               println("for (int i = 0; i < retLen; i++) {");
+                               println("retEnumVal[i] = retEnum.get(i).ordinal();");
+                               println("}");
+                       } else {        // Just one element
+                               println("int[] retEnumVal = new int[1];");
+                               println("retEnumVal[0] = retEnum.ordinal();");
+                       }
+                       println("Object retObj = retEnumVal;");
+               }
+       }
+       
+       
+       /**
+        * HELPER: writeLengthStructParamClassSkeleton() writes lengths of params
+        */
+       private void writeLengthStructParamClassSkeleton(List<String> methParams, List<String> methPrmTypes, 
+                       String method, InterfaceDecl intDecl) {
 
-               // Use this set to handle two same methodIds
-               Set<String> uniqueMethodIds = new HashSet<String>();
-               for (String method : methods) {
+               // Iterate and find struct declarations - count number of params
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               int members = getNumOfMembers(simpleType);
+                               print(Integer.toString(members) + "*");
+                               int methodNumId = intDecl.getMethodNumId(method);
+                               print("struct" + methodNumId + "Size" + i);
+                       } else
+                               print("1");
+                       if (i != methParams.size() - 1) {
+                               print("+");
+                       }
+               }
+       }
 
-                       List<String> methParams = intDecl.getMethodParams(method);
-                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-                       String methodId = intDecl.getMethodId(method);
-                       print("public void ___");
-                       String helperMethod = methodId;
-                       if (uniqueMethodIds.contains(methodId))
-                               helperMethod = helperMethod + intDecl.getMethodNumId(method);
-                       else
-                               uniqueMethodIds.add(methodId);
-                       // Check if this is "void"
-                       String retType = intDecl.getMethodType(method);
-                       if (retType.equals("void"))
-                               println(helperMethod + "() {");
-                       else
-                               println(helperMethod + "() throws IOException {");
-                       // Now, write the helper body of skeleton!
-                       writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
-                       println("}\n");
+       
+       /**
+        * HELPER: writeStructMembersJavaSkeleton() writes member parameters of struct
+        */
+       private void writeStructMembersJavaSkeleton(String simpleType, String paramType, 
+                       String param, String method, InterfaceDecl intDecl, int iVar) {
+
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       int methodNumId = intDecl.getMethodNumId(method);
+                       String counter = "struct" + methodNumId + "Size" + iVar;
+                       println("for(int i = 0; i < " + counter + "; i++) {");
+               }
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               println("paramClsGen[pos++] = null;");
+                       }
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               println("paramClsGen[pos++] = null;");
+                       }
                }
        }
 
 
        /**
-        * HELPER: writeJavaWaitRequestInvokeMethod() writes the main loop of the skeleton class
+        * HELPER: writeStructMembersInitJavaSkeleton() writes member parameters initialization of struct
         */
-       private void writeJavaWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
+       private void writeStructMembersInitJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
+                       List<String> methPrmTypes, String method) {
+
+               println("int objPos = 0;");
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               int methodNumId = intDecl.getMethodNumId(method);
+                               String counter = "struct" + methodNumId + "Size" + i;
+                               // Declaration
+                               if (isArray(param)) {                   // An array
+                                       println(simpleType + "[] paramStruct" + i + " = new " + simpleType + "[" + counter + "];");
+                                       println("for(int i = 0; i < " + counter + "; i++) {");
+                                       println("paramStruct" + i + "[i] = new " + simpleType + "();");
+                                       println("}");
+                               } else if (isList(paramType)) { // A list
+                                       println("List<" + simpleType + "> paramStruct" + i + " = new ArrayList<" + simpleType + ">();");
+                               } else
+                                       println(simpleType + " paramStruct" + i + " = new " + simpleType + "();");
+                               // Initialize members
+                               StructDecl structDecl = getStructDecl(simpleType);
+                               List<String> members = structDecl.getMembers(simpleType);
+                               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+                               if (isArrayOrList(paramType, param)) {  // An array or list
+                                       println("for(int i = 0; i < " + counter + "; i++) {");
+                               }
+                               if (isArray(param)) {   // An array
+                                       for (int j = 0; j < members.size(); j++) {
+                                               String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
+                                               print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j)));
+                                               println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
+                                       }
+                                       println("}");
+                               } else if (isList(paramType)) { // A list
+                                       println(simpleType + " paramStructMem = new " + simpleType + "();");
+                                       for (int j = 0; j < members.size(); j++) {
+                                               String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
+                                               print("paramStructMem." + getSimpleIdentifier(members.get(j)));
+                                               println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
+                                       }
+                                       println("paramStruct" + i + ".add(paramStructMem);");
+                                       println("}");
+                               } else {        // Just one struct element
+                                       for (int j = 0; j < members.size(); j++) {
+                                               String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
+                                               print("paramStruct" + i + "." + getSimpleIdentifier(members.get(j)));
+                                               println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
+                                       }
+                               }
+                       } else {
+                               // Take offsets of parameters
+                               println("int offset" + i +" = objPos++;");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructReturnJavaSkeleton() writes struct for return statement
+        */
+       private void writeStructReturnJavaSkeleton(String simpleType, String retType) {
+
+               // Minimum retLen is 1 if this is a single struct object
+               if (isArray(retType))
+                       println("int retLen = retStruct.length;");
+               else if (isList(retType))
+                       println("int retLen = retStruct.size();");
+               else    // Just single struct object
+                       println("int retLen = 1;");
+               println("Object retLenObj = retLen;");
+               println("rmiObj.sendReturnObj(retLenObj);");
+               int numMem = getNumOfMembers(simpleType);
+               println("Class<?>[] retCls = new Class<?>[" + numMem + "*retLen];");
+               println("Object[] retObj = new Object[" + numMem + "*retLen];");
+               println("int retPos = 0;");
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArray(retType)) { // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("retObj[retPos++] = retStruct[i].");
+                               print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+                               println(";");
+                       }
+                       println("}");
+               } else if (isList(retType)) {   // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("retObj[retPos++] = retStruct.get(i).");
+                               print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+                               println(";");
+                       }
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
+                               print("retObj[retPos++] = retStruct.");
+                               print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+                               println(";");
+                       }
+               }
+
+       }
+
+
+       /**
+        * 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,
+                       boolean isStructMethod) {
+
+               checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes, isStructMethod);
+               Map<Integer,String> mapStubParam = null;
+               if (isCallbackMethod) {
+                       println("try {");
+                       mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType, isStructMethod);
+               }
+               // Check if this is "void"
+               String retType = intDecl.getMethodType(method);
+               if (retType.equals("void")) {
+                       print(intDecl.getMethodId(method) + "(");
+               } else if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) {  // Enum type
+                       checkAndWriteEnumRetTypeJavaSkeleton(retType, intDecl.getMethodId(method));
+               } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) {        // Struct type
+                       print(retType + " retStruct = " + intDecl.getMethodId(method) + "(");
+               } else { // We do have a return value
+                       print("Object retObj = " + intDecl.getMethodId(method) + "(");
+               }
+               for (int i = 0; i < methParams.size(); i++) {
+
+                       String paramType = methPrmTypes.get(i);
+                       if (isCallbackMethod && checkCallbackType(paramType, callbackType)) {
+                               print(mapStubParam.get(i));     // Get the callback parameter
+                       } 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(paramType, methParams.get(i));
+                               if (isStructMethod)
+                                       print("(" + prmType + ") paramObj[offset" + i + "]");
+                               else
+                                       print("(" + prmType + ") paramObj[" + i + "]");
+                       }
+                       if (i != methParams.size() - 1)
+                               print(", ");
+               }
+               println(");");
+               if (!retType.equals("void")) {
+                       if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type
+                               checkAndWriteEnumRetConvJavaSkeleton(retType);
+                               println("rmiObj.sendReturnObj(retObj);");
+                       } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type
+                               writeStructReturnJavaSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
+                               println("rmiObj.sendReturnObj(retCls, retObj);");
+                       } else
+                               println("rmiObj.sendReturnObj(retObj);");
+               }
+               if (isCallbackMethod) { // Catch exception if this is callback
+                       print("}");
+                       println(" catch(Exception ex) {");
+                       println("ex.printStackTrace();");
+                       println("throw new Error(\"Exception from callback object instantiation!\");");
+                       println("}");
+               }
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperStructJavaSkeleton() writes the struct in skeleton
+        */
+       private void writeMethodHelperStructJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
+                       List<String> methPrmTypes, String method, Set<String> callbackClasses) {
+
+               // Generate array of parameter objects
+               boolean isCallbackMethod = false;
+               String callbackType = null;
+               print("int paramLen = ");
+               writeLengthStructParamClassSkeleton(methParams, methPrmTypes, method, intDecl);
+               println(";");
+               println("Class<?>[] paramCls = new Class<?>[paramLen];");
+               println("Class<?>[] paramClsGen = new Class<?>[paramLen];");
+               println("int pos = 0;");
+               // Iterate again over the parameters
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               writeStructMembersJavaSkeleton(simpleType, paramType, param, method, intDecl, i);
+                       } else {
+                               String prmType = returnGenericCallbackType(methPrmTypes.get(i));
+                               if (callbackClasses.contains(prmType)) {
+                                       isCallbackMethod = true;
+                                       callbackType = prmType;
+                                       println("paramCls[pos] = int.class;");
+                                       println("paramClsGen[pos++] = null;");
+                               } else {        // Generate normal classes if it's not a callback object
+                                       String paramTypeOth = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                                       println("paramCls[pos] = " + getSimpleType(getEnumType(paramTypeOth)) + ".class;");
+                                       print("paramClsGen[pos++] = ");
+                                       String prmTypeOth = methPrmTypes.get(i);
+                                       if (getParamCategory(prmTypeOth) == ParamCategory.NONPRIMITIVES)
+                                               println(getTypeOfGeneric(prmType)[0] + ".class;");
+                                       else
+                                               println("null;");
+                               }
+                       }
+               }
+               println("Object[] paramObj = rmiObj.getMethodParams(paramCls, paramClsGen);");
+               writeStructMembersInitJavaSkeleton(intDecl, methParams, methPrmTypes, method);
+               // Write the return value part
+               writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, true);
+       }
+
+
+       /**
+        * HELPER: writeStdMethodHelperBodyJavaSkeleton() writes the standard method body helper in the skeleton class
+        */
+       private void writeStdMethodHelperBodyJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
+                       List<String> methPrmTypes, String method, Set<String> callbackClasses) {
+
+               // Generate array of parameter objects
+               boolean isCallbackMethod = false;
+               String callbackType = null;
+               print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { ");
+               for (int i = 0; i < methParams.size(); i++) {
+
+                       String paramType = returnGenericCallbackType(methPrmTypes.get(i));
+                       if (callbackClasses.contains(paramType)) {
+                               isCallbackMethod = true;
+                               callbackType = paramType;
+                               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(getEnumType(prmType)) + ".class");
+                       }
+                       if (i != methParams.size() - 1)
+                               print(", ");
+               }
+               println(" }, ");
+               // Generate generic class if it's a generic type.. null otherwise
+               print("new Class<?>[] { ");
+               for (int i = 0; i < methParams.size(); i++) {
+                       String prmType = methPrmTypes.get(i);
+                       if ((getParamCategory(prmType) == ParamCategory.NONPRIMITIVES) &&
+                               !isEnumClass(getGenericType(prmType)) &&
+                               !callbackClasses.contains(getGenericType(prmType)))
+                                       print(getGenericType(prmType) + ".class");
+                       else
+                               print("null");
+                       if (i != methParams.size() - 1)
+                               print(", ");
+               }
+               println(" });");
+               // Write the return value part
+               writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, false);
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperJavaSkeleton() writes the method helper of the skeleton class
+        */
+       private void writeMethodHelperJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+
+               // Use this set to handle two same methodIds
+               Set<String> uniqueMethodIds = new HashSet<String>();
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       if (isStructPresent(methParams, methPrmTypes)) {        // Treat struct differently
+                               String methodId = intDecl.getMethodId(method);
+                               print("public void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               String retType = intDecl.getMethodType(method);
+                               print(helperMethod + "(");
+                               boolean begin = true;
+                               for (int i = 0; i < methParams.size(); i++) { // Print size variables
+                                       String paramType = methPrmTypes.get(i);
+                                       String param = methParams.get(i);
+                                       String simpleType = getGenericType(paramType);
+                                       if (isStructClass(simpleType)) {
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
+                                               int methodNumId = intDecl.getMethodNumId(method);
+                                               print("int struct" + methodNumId + "Size" + i);
+                                       }
+                               }
+                               // Check if this is "void"
+                               if (retType.equals("void"))
+                                       println(") {");
+                               else
+                                       println(") throws IOException {");
+                               writeMethodHelperStructJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
+                               println("}\n");
+                       } else {
+                               String methodId = intDecl.getMethodId(method);
+                               print("public void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               // Check if this is "void"
+                               String retType = intDecl.getMethodType(method);
+                               if (retType.equals("void"))
+                                       println(helperMethod + "() {");
+                               else
+                                       println(helperMethod + "() throws IOException {");
+                               // Now, write the helper body of skeleton!
+                               writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
+                               println("}\n");
+                       }
+               }
+               // Write method helper for structs
+               writeMethodHelperStructSetupJavaSkeleton(methods, intDecl);
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperStructSetupJavaSkeleton() writes the method helper of struct setup in skeleton class
+        */
+       private void writeMethodHelperStructSetupJavaSkeleton(Collection<String> methods, 
+                       InterfaceDecl intDecl) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       print("public int ___");
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       println(helperMethod + "() {");
+                                       // Now, write the helper body of skeleton!
+                                       println("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null });");
+                                       println("return (int) paramObj[0];");
+                                       println("}\n");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperStructSetupJavaCallbackSkeleton() writes the method helper of struct setup in callback skeleton class
+        */
+       private void writeMethodHelperStructSetupJavaCallbackSkeleton(Collection<String> methods, 
+                       InterfaceDecl intDecl) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       print("public int ___");
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       println(helperMethod + "(IoTRMIObject rmiObj) {");
+                                       // Now, write the helper body of skeleton!
+                                       println("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null });");
+                                       println("return (int) paramObj[0];");
+                                       println("}\n");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeCountVarStructSkeleton() writes counter variable of struct for skeleton
+        */
+       private void writeCountVarStructSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       println("int struct" + methodNumId + "Size" + i + " = 0;");
+                               }
+                       }
+               }
+       }
+       
+       
+       /**
+        * HELPER: writeInputCountVarStructSkeleton() writes input counter variable of struct for skeleton
+        */
+       private boolean writeInputCountVarStructSkeleton(String method, InterfaceDecl intDecl) {
+
+               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);
+                       if (isStructClass(simpleType)) {
+                               structExist = true;
+                               if (!begin)
+                                       print(", ");
+                               else
+                                       begin = false;
+                               int methodNumId = intDecl.getMethodNumId(method);
+                               print("struct" + methodNumId + "Size" + i);
+                       }
+               }
+               return structExist;
+       }
+
+
+       /**
+        * HELPER: writeMethodCallStructSkeleton() writes method call for wait invoke in skeleton
+        */
+       private void writeMethodCallStructSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       print("case ");
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       String tempVar = "struct" + methodNumId + "Size" + i;
+                                       print(intDecl.getHelperMethodNumId(helperMethod) + ": ");
+                                       print(tempVar + " = ___");
+                                       println(helperMethod + "(); break;");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeMethodCallStructCallbackSkeleton() writes method call for wait invoke in skeleton
+        */
+       private void writeMethodCallStructCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       print("case ");
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       String tempVar = "struct" + methodNumId + "Size" + i;
+                                       print(intDecl.getHelperMethodNumId(helperMethod) + ": ");
+                                       print(tempVar + " = ___");
+                                       println(helperMethod + "(rmiObj); break;");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeJavaMethodPermission() writes permission checks in skeleton
+        */
+       private void writeJavaMethodPermission(String intface) {
+
+               // 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 (_objectId == object" + newObjectId + "Id) {");
+                       println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
+                       println("throw new Error(\"Object with object Id: \" + _objectId + \"  is not allowed to access method: \" + methodId);");
+                       println("}");
+                       println("}");
+                       println("else {");
+                       println("throw new Error(\"Object Id: \" + _objectId + \" not recognized!\");");
+                       println("}");
+               }
+       }
+
+
+       /**
+        * HELPER: writeJavaWaitRequestInvokeMethod() writes the main loop of the skeleton class
+        */
+       private void writeJavaWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist, String intface) {
 
                // Use this set to handle two same methodIds
                Set<String> uniqueMethodIds = new HashSet<String>();
                println("private void ___waitRequestInvokeMethod() throws IOException {");
                // Write variables here if we have callbacks or enums or structs
+               writeCountVarStructSkeleton(methods, intDecl);
                println("while (true) {");
                println("rmiObj.getMethodBytes();");
                println("int _objectId = rmiObj.getObjectId();");
                println("int methodId = rmiObj.getMethodId();");
-               // TODO: code the permission check here!
+               // Generate permission check
+               writeJavaMethodPermission(intface);
                println("switch (methodId) {");
                // Print methods and method Ids
                for (String method : methods) {
@@ -1046,7 +2152,9 @@ public class IoTCompiler {
                                helperMethod = helperMethod + methodNumId;
                        else
                                uniqueMethodIds.add(methodId);
-                       println(helperMethod + "(); break;");
+                       print(helperMethod + "(");
+                       writeInputCountVarStructSkeleton(method, intDecl);
+                       println("); break;");
                }
                String method = "___initCallBack()";
                // Print case -9999 (callback handler) if callback exists
@@ -1054,6 +2162,7 @@ public class IoTCompiler {
                        int methodId = intDecl.getHelperMethodNumId(method);
                        println("case " + methodId + ": ___regCB(); break;");
                }
+               writeMethodCallStructSkeleton(methods, intDecl);
                println("default: ");
                println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
                println("}");
@@ -1080,7 +2189,7 @@ public class IoTCompiler {
                        List<String> methods = intDecl.getMethods();
                        Set<String> importClasses = getImportClasses(methods, intDecl);
                        List<String> stdImportClasses = getStandardJavaImportClasses();
-                       List<String> allImportClasses = getAllImportClasses(stdImportClasses, importClasses);
+                       List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                        printImportStatements(allImportClasses);
                        // Find out if there are callback objects
                        Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
@@ -1089,15 +2198,15 @@ public class IoTCompiler {
                        println("");
                        println("public class " + newSkelClass  + " implements " + intface + " {\n");
                        // Write properties
-                       writePropertiesJavaSkeleton(intface, callbackExist);
+                       writePropertiesJavaSkeleton(intface, callbackExist, intDecl);
                        // Write constructor
-                       writeConstructorJavaSkeleton(newSkelClass, intface);
+                       writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
                        // Write methods
-                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses);
+                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface);
                        // Write method helper
                        writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
-                       writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist);
+                       writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
                        println("}");
                        pw.close();
                        System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java...");
@@ -1112,11 +2221,14 @@ public class IoTCompiler {
 
                println("private " + intface + " mainObj;");
                // For callback skeletons, this is its own object Id
-               println("private static int objectId = 0;");
+               println("private int objectId = 0;");
+               println("private String callbackAddress;");
                // Callback
                if (callbackExist) {
-                       println("private static int objIdCnt = 0;");
+
+                       println("private int objIdCnt = 0;");
                        println("private IoTRMICall rmiCall;");
+                       println("private int[] ports;\n");
                }
                println("\n");
        }
@@ -1125,9 +2237,10 @@ public class IoTCompiler {
        /**
         * HELPER: writeConstructorJavaCallbackSkeleton() writes the constructor of the skeleton class
         */
-       private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface) {
+       private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
 
-               println("public " + newSkelClass + "(" + intface + " _mainObj, int _objectId) throws Exception {");
+               println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _objectId) throws Exception {");
+               println("callbackAddress = _callbackAddress;");
                println("mainObj = _mainObj;");
                println("objectId = _objectId;");
                println("}\n");
@@ -1145,28 +2258,63 @@ public class IoTCompiler {
 
                        List<String> methParams = intDecl.getMethodParams(method);
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-                       String methodId = intDecl.getMethodId(method);
-                       print("public void ___");
-                       String helperMethod = methodId;
-                       if (uniqueMethodIds.contains(methodId))
-                               helperMethod = helperMethod + intDecl.getMethodNumId(method);
-                       else
-                               uniqueMethodIds.add(methodId);
-                       // Check if this is "void"
-                       String retType = intDecl.getMethodType(method);
-                       if (retType.equals("void"))
-                               println(helperMethod + "(IoTRMIObject rmiObj) {");
-                       else
-                               println(helperMethod + "(IoTRMIObject rmiObj) throws IOException {");
-                       // Now, write the helper body of skeleton!
-                       writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
-                       println("}\n");
+                       if (isStructPresent(methParams, methPrmTypes)) {        // Treat struct differently
+                               String methodId = intDecl.getMethodId(method);
+                               print("public void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               String retType = intDecl.getMethodType(method);
+                               print(helperMethod + "(");
+                               boolean begin = true;
+                               for (int i = 0; i < methParams.size(); i++) { // Print size variables
+                                       String paramType = methPrmTypes.get(i);
+                                       String param = methParams.get(i);
+                                       String simpleType = getGenericType(paramType);
+                                       if (isStructClass(simpleType)) {
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
+                                               int methodNumId = intDecl.getMethodNumId(method);
+                                               print("int struct" + methodNumId + "Size" + i);
+                                       }
+                               }
+                               // Check if this is "void"
+                               if (retType.equals("void"))
+                                       println(", IoTRMIObject rmiObj) {");
+                               else
+                                       println(", IoTRMIObject rmiObj) throws IOException {");
+                               writeMethodHelperStructJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
+                               println("}\n");
+                       } else {
+                               String methodId = intDecl.getMethodId(method);
+                               print("public void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               // Check if this is "void"
+                               String retType = intDecl.getMethodType(method);
+                               if (retType.equals("void"))
+                                       println(helperMethod + "(IoTRMIObject rmiObj) {");
+                               else
+                                       println(helperMethod + "(IoTRMIObject rmiObj) throws IOException {");
+                               // Now, write the helper body of skeleton!
+                               writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
+                               println("}\n");
+                       }
                }
+               // Write method helper for structs
+               writeMethodHelperStructSetupJavaCallbackSkeleton(methods, intDecl);
        }
 
 
        /**
-        * HELPER: writeJavaCallbackWaitRequestInvokeMethod() writes the main loop of the skeleton class
+        * HELPER: writeJavaCallbackWaitRequestInvokeMethod() writes the request invoke method of the callback skeleton class
         */
        private void writeJavaCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
 
@@ -1174,6 +2322,8 @@ public class IoTCompiler {
                Set<String> uniqueMethodIds = new HashSet<String>();
                println("public void invokeMethod(IoTRMIObject rmiObj) throws IOException {");
                // Write variables here if we have callbacks or enums or structs
+               writeCountVarStructSkeleton(methods, intDecl);
+               // Write variables here if we have callbacks or enums or structs
                println("int methodId = rmiObj.getMethodId();");
                // TODO: code the permission check here!
                println("switch (methodId) {");
@@ -1187,14 +2337,19 @@ public class IoTCompiler {
                                helperMethod = helperMethod + methodNumId;
                        else
                                uniqueMethodIds.add(methodId);
-                       println(helperMethod + "(rmiObj); break;");
+                       print(helperMethod + "(");
+                       if (writeInputCountVarStructSkeleton(method, intDecl))
+                               println(", rmiObj); break;");
+                       else
+                               println("rmiObj); break;");
                }
                String method = "___initCallBack()";
                // Print case -9999 (callback handler) if callback exists
                if (callbackExist) {
                        int methodId = intDecl.getHelperMethodNumId(method);
-                       println("case " + methodId + ": ___regCB(); break;");
+                       println("case " + methodId + ": ___regCB(rmiObj); break;");
                }
+               writeMethodCallStructCallbackSkeleton(methods, intDecl);
                println("default: ");
                println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
                println("}");
@@ -1220,7 +2375,7 @@ public class IoTCompiler {
                        List<String> methods = intDecl.getMethods();
                        Set<String> importClasses = getImportClasses(methods, intDecl);
                        List<String> stdImportClasses = getStandardJavaImportClasses();
-                       List<String> allImportClasses = getAllImportClasses(stdImportClasses, importClasses);
+                       List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                        printImportStatements(allImportClasses);
                        // Find out if there are callback objects
                        Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
@@ -1231,9 +2386,9 @@ public class IoTCompiler {
                        // Write properties
                        writePropertiesJavaCallbackSkeleton(intface, callbackExist);
                        // Write constructor
-                       writeConstructorJavaCallbackSkeleton(newSkelClass, intface);
+                       writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods);
                        // Write methods
-                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses);
+                       writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true, intface);
                        // Write method helper
                        writeMethodHelperJavaCallbackSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
@@ -1245,6 +2400,35 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeMethodCplusLocalInterface() writes the method of the local interface
+        */
+       private void writeMethodCplusLocalInterface(Collection<String> methods, InterfaceDecl intDecl) {
+
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       print("virtual " + checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
+                               intDecl.getMethodId(method) + "(");
+                       for (int i = 0; i < methParams.size(); i++) {
+                               // Check for params with driver class types and exchange it 
+                               //              with its remote interface
+                               String paramType = checkAndGetParamClass(methPrmTypes.get(i));
+                               paramType = checkAndGetCplusType(paramType);
+                               // Check for arrays - translate into vector in C++
+                               String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
+                               print(paramComplete);
+                               // Check if this is the last element (don't print a comma)
+                               if (i != methParams.size() - 1) {
+                                       print(", ");
+                               }
+                       }
+                       println(") = 0;");
+               }
+       }
+
+
        /**
         * HELPER: writeMethodCplusInterface() writes the method of the interface
         */
@@ -1259,7 +2443,6 @@ public class IoTCompiler {
                        for (int i = 0; i < methParams.size(); i++) {
                                // Check for params with driver class types and exchange it 
                                //              with its remote interface
-                               //String paramType = checkAndGetParamClass(methPrmTypes.get(i), true);
                                String paramType = methPrmTypes.get(i);
                                paramType = checkAndGetCplusType(paramType);
                                // Check for arrays - translate into vector in C++
@@ -1276,52 +2459,83 @@ public class IoTCompiler {
 
 
        /**
-        * HELPER: writeEnumCplus() writes the enumeration declaration
+        * HELPER: generateEnumCplus() writes the enumeration declaration
         */
-       private void writeEnumCplus(EnumDecl enumDecl) {
-
-               Set<String> enumTypes = enumDecl.getEnumDeclarations();
-               // Iterate over enum declarations
-               for (String enType : enumTypes) {
+       public void generateEnumCplus() throws IOException {
 
-                       println("enum " + enType + " {");
-                       List<String> enumMembers = enumDecl.getMembers(enType);
-                       for (int i = 0; i < enumMembers.size(); i++) {
-
-                               String member = enumMembers.get(i);
-                               print(member);
-                               // Check if this is the last element (don't print a comma)
-                               if (i != enumMembers.size() - 1)
-                                       println(",");
-                               else
-                                       println("");
+               // Create a new directory
+               createDirectory(dir);
+               for (String intface : mapIntfacePTH.keySet()) {
+                       // Get the right StructDecl
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       Set<String> enumTypes = enumDecl.getEnumDeclarations();
+                       // Iterate over enum declarations
+                       for (String enType : enumTypes) {
+                               // Open a new file to write into
+                               FileWriter fw = new FileWriter(dir + "/" + enType + ".hpp");
+                               pw = new PrintWriter(new BufferedWriter(fw));
+                               // Write file headers
+                               println("#ifndef _" + enType.toUpperCase() + "_HPP__");
+                               println("#define _" + enType.toUpperCase() + "_HPP__");
+                               println("enum " + enType + " {");
+                               List<String> enumMembers = enumDecl.getMembers(enType);
+                               for (int i = 0; i < enumMembers.size(); i++) {
+
+                                       String member = enumMembers.get(i);
+                                       print(member);
+                                       // Check if this is the last element (don't print a comma)
+                                       if (i != enumMembers.size() - 1)
+                                               println(",");
+                                       else
+                                               println("");
+                               }
+                               println("};\n");
+                               println("#endif");
+                               pw.close();
+                               System.out.println("IoTCompiler: Generated enum " + enType + ".hpp...");
                        }
-                       println("};\n");
                }
        }
 
 
        /**
-        * HELPER: writeStructCplus() writes the struct declaration
+        * HELPER: generateStructCplus() writes the struct declaration
         */
-       private void writeStructCplus(StructDecl structDecl) {
-
-               List<String> structTypes = structDecl.getStructTypes();
-               // Iterate over enum declarations
-               for (String stType : structTypes) {
+       public void generateStructCplus() throws IOException {
 
-                       println("struct " + stType + " {");
-                       List<String> structMemberTypes = structDecl.getMemberTypes(stType);
-                       List<String> structMembers = structDecl.getMembers(stType);
-                       for (int i = 0; i < structMembers.size(); i++) {
-
-                               String memberType = structMemberTypes.get(i);
-                               String member = structMembers.get(i);
-                               String structTypeC = checkAndGetCplusType(memberType);
-                               String structComplete = checkAndGetCplusArray(structTypeC, member);
-                               println(structComplete + ";");
+               // Create a new directory
+               createDirectory(dir);
+               for (String intface : mapIntfacePTH.keySet()) {
+                       // Get the right StructDecl
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       List<String> structTypes = structDecl.getStructTypes();
+                       // Iterate over enum declarations
+                       for (String stType : structTypes) {
+                               // Open a new file to write into
+                               FileWriter fw = new FileWriter(dir + "/" + stType + ".hpp");
+                               pw = new PrintWriter(new BufferedWriter(fw));
+                               // Write file headers
+                               println("#ifndef _" + stType.toUpperCase() + "_HPP__");
+                               println("#define _" + stType.toUpperCase() + "_HPP__");
+                               println("using namespace std;");
+                               println("struct " + stType + " {");
+                               List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+                               List<String> structMembers = structDecl.getMembers(stType);
+                               for (int i = 0; i < structMembers.size(); i++) {
+
+                                       String memberType = structMemberTypes.get(i);
+                                       String member = structMembers.get(i);
+                                       String structTypeC = checkAndGetCplusType(memberType);
+                                       String structComplete = checkAndGetCplusArray(structTypeC, member);
+                                       println(structComplete + ";");
+                               }
+                               println("};\n");
+                               println("#endif");
+                               pw.close();
+                               System.out.println("IoTCompiler: Generated struct " + stType + ".hpp...");
                        }
-                       println("};\n");
                }
        }
 
@@ -1350,19 +2564,14 @@ public class IoTCompiler {
                        DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                        List<String> methods = intDecl.getMethods();
-                       Set<String> includeClasses = getIncludeClasses(methods, intDecl);
+                       Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
                        printIncludeStatements(includeClasses); println("");
                        println("using namespace std;\n");
-                       // Write enum if any...
-                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
-                       writeEnumCplus(enumDecl);
-                       // Write struct if any...
-                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
-                       writeStructCplus(structDecl);
+                       //writeStructCplus(structDecl);
                        println("class " + intface); println("{");
                        println("public:");
                        // Write methods
-                       writeMethodCplusInterface(methods, intDecl);
+                       writeMethodCplusLocalInterface(methods, intDecl);
                        println("};");
                        println("#endif");
                        pw.close();
@@ -1395,17 +2604,19 @@ public class IoTCompiler {
                                println("#ifndef _" + newIntface.toUpperCase() + "_HPP__");
                                println("#define _" + newIntface.toUpperCase() + "_HPP__");
                                println("#include <iostream>");
+                               updateIntfaceObjIdMap(intface, newIntface);
                                // Pass in set of methods and get import classes
-                               Set<String> includeClasses = getIncludeClasses(intMeth.getValue(), intDecl);
+                               Set<String> methods = intMeth.getValue();
+                               Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, false);
                                List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
-                               List<String> allIncludeClasses = getAllImportClasses(stdIncludeClasses, includeClasses);
+                               List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
                                printIncludeStatements(allIncludeClasses); println("");                 
                                println("using namespace std;\n");
                                println("class " + newIntface);
                                println("{");
                                println("public:");
                                // Write methods
-                               writeMethodCplusInterface(intMeth.getValue(), intDecl);
+                               writeMethodCplusInterface(methods, intDecl);
                                println("};");
                                println("#endif");
                                pw.close();
@@ -1416,21 +2627,29 @@ public class IoTCompiler {
 
 
        /**
-        * HELPER: writeMethodCplusStub() writes the method of the stub
+        * HELPER: writeMethodCplusStub() writes the methods of the stub
         */
-       private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl) {
+       private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, String newStubClass) {
 
+               boolean isDefined = false;
                for (String method : methods) {
 
                        List<String> methParams = intDecl.getMethodParams(method);
                        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 = checkAndGetParamClass(methPrmTypes.get(i), true);
-                               String paramType = methPrmTypes.get(i);
-                               String methPrmType = checkAndGetCplusType(paramType);
+                               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);
                                // Check if this is the last element (don't print a comma)
@@ -1439,9 +2658,321 @@ public class IoTCompiler {
                                }
                        }
                        println(") { ");
-                       writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method);
+                       if (isCallbackMethod)
+                               writeCallbackMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType);
+                       writeStdMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType, isCallbackMethod);
                        println("}\n");
+                       // Write the init callback helper method
+                       if (isCallbackMethod && !isDefined) {
+                               writeInitCallbackCplusStub(callbackType, intDecl, newStubClass);
+                               writeInitCallbackSendInfoCplusStub(intDecl);
+                               isDefined = true;
+                       }
+               }
+       }
+
+
+       /**
+        * 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) {
+
+               // Check if this is single object, array, or list of objects
+               boolean isArrayOrList = false;
+               String callbackParam = null;
+               for (int i = 0; i < methParams.size(); i++) {
+
+                       String paramType = methPrmTypes.get(i);
+                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                               String param = methParams.get(i);
+                               if (isArrayOrList(paramType, param)) {  // Generate loop
+                                       println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {");
+                                       println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);");
+                                       isArrayOrList = true;
+                                       callbackParam = getSimpleIdentifier(param);
+                               } else
+                                       println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
+                                               getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
+                               println("vecCallbackObj.push_back(skel" + i + ");");
+                               if (isArrayOrList)
+                                       println("}");
+                               print("int ___paramCB" + i + " = ");
+                               if (isArrayOrList)
+                                       println(callbackParam + ".size();");
+                               else
+                                       println("1;");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: checkAndWriteEnumTypeCplusStub() writes the enum type (convert from enum to int)
+        */
+       private void checkAndWriteEnumTypeCplusStub(List<String> methParams, List<String> methPrmTypes) {
+
+               // Iterate and find enum declarations
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       if (isEnumClass(getGenericType(paramType))) {
+                       // Check if this is enum type
+                               if (isArrayOrList(paramType, param)) {  // An array or vector
+                                       println("int len" + i + " = " + getSimpleIdentifier(param) + ".size();");
+                                       println("vector<int> paramEnum" + i + "(len" + i + ");");
+                                       println("for (int i = 0; i < len" + i + "; i++) {");
+                                       println("paramEnum" + i + "[i] = (int) " + getSimpleIdentifier(param) + "[i];");
+                                       println("}");
+                               } else {        // Just one element
+                                       println("vector<int> paramEnum" + i + "(1);");
+                                       println("paramEnum" + i + "[0] = (int) " + param + ";");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: checkAndWriteEnumRetTypeCplusStub() writes the enum return type (convert from enum to int)
+        */
+       private void checkAndWriteEnumRetTypeCplusStub(String retType) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(getGenericType(retType));
+               // Take the inner type of generic
+               if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(retType);
+               if (isEnumClass(pureType)) {
+               // Check if this is enum type
+                       println("vector<int> retEnumInt;");
+                       println("void* retObj = &retEnumInt;");
+                       println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
+                       if (isArrayOrList(retType, retType)) {  // An array or vector
+                               println("int retLen = retEnumInt.size();");
+                               println("vector<" + pureType + "> retVal(retLen);");
+                               println("for (int i = 0; i < retLen; i++) {");
+                               println("retVal[i] = (" + pureType + ") retEnumInt[i];");
+                               println("}");
+                       } else {        // Just one element
+                               println(pureType + " retVal = (" + pureType + ") retEnumInt[0];");
+                       }
+                       println("return retVal;");
+               }
+       }
+
+
+       /**
+        * HELPER: checkAndWriteStructSetupCplusStub() writes the struct type setup
+        */
+       private void checkAndWriteStructSetupCplusStub(List<String> methParams, List<String> methPrmTypes, 
+                       InterfaceDecl intDecl, String method) {
+               
+               // Iterate and find struct declarations
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                       // Check if this is enum type
+                               println("int numParam" + i + " = 1;");
+                               int methodNumId = intDecl.getMethodNumId(method);
+                               String helperMethod = methodNumId + "struct" + i;
+                               println("int methodIdStruct" + i + " = " + intDecl.getHelperMethodNumId(helperMethod) + ";");
+                               println("string retTypeStruct" + i + " = \"void\";");
+                               println("string paramClsStruct" + i + "[] = { \"int\" };");
+                               print("int structLen" + i + " = ");
+                               if (isArrayOrList(paramType, param)) {  // An array
+                                       println(getSimpleArrayType(param) + ".size();");
+                               } else {        // Just one element
+                                       println("1;");
+                               }
+                               println("void* paramObjStruct" + i + "[] = { &structLen" + i + " };");
+                               println("void* retStructLen" + i + " = NULL;");
+                               println("rmiCall->remoteCall(objectId, methodIdStruct" + i + 
+                                               ", retTypeStruct" + i + ", paramClsStruct" + i + ", paramObjStruct" + i + 
+                                               ", numParam" + i + ", retStructLen" + i + ");\n");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeLengthStructParamClassCplusStub() writes lengths of params
+        */
+       private void writeLengthStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes) {
+
+               // Iterate and find struct declarations - count number of params
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               int members = getNumOfMembers(simpleType);
+                               if (isArrayOrList(paramType, param)) {  // An array or list
+                                       String structLen = getSimpleIdentifier(param) + ".size()";
+                                       print(members + "*" + structLen);
+                               } else
+                                       print(Integer.toString(members));
+                       } else
+                               print("1");
+                       if (i != methParams.size() - 1) {
+                               print("+");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructMembersCplusStub() writes member parameters of struct
+        */
+       private void writeStructMembersCplusStub(String simpleType, String paramType, String param) {
+
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); i++) {");
+               }
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+                               println("paramCls[pos] = \"" + prmTypeC + "\";");
+                               print("paramObj[pos++] = &" + getSimpleIdentifier(param) + "[i].");
+                               print(getSimpleIdentifier(members.get(i)));
+                               println(";");
+                       }
+                       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 + "\";");
+                               print("paramObj[pos++] = &" + param + ".");
+                               print(getSimpleIdentifier(members.get(i)));
+                               println(";");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructParamClassCplusStub() writes member parameters of struct
+        */
+       private void writeStructParamClassCplusStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+
+               print("int numParam = ");
+               writeLengthStructParamClassCplusStub(methParams, methPrmTypes);
+               println(";");
+               println("void* paramObj[numParam];");
+               println("string paramCls[numParam];");
+               println("int pos = 0;");
+               // Iterate again over the parameters
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       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 + "\";");
+                               print("paramObj[pos++] = &");
+                               print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
+                               println(";");
+                       }
+               }
+               
+       }
+
+
+       /**
+        * HELPER: writeStructRetMembersCplusStub() writes member parameters of struct for return statement
+        */
+       private void writeStructRetMembersCplusStub(String simpleType, String retType) {
+
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+               }
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               print("structRet[i]." + getSimpleIdentifier(members.get(i)));
+                               println(" = retParam" + i + "[i];");
+                       }
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
+                               print("structRet." + getSimpleIdentifier(members.get(i)));
+                               println(" = retParam" + i + ";");
+                       }
+               }
+               println("return structRet;");
+       }
+
+
+       /**
+        * HELPER: writeStructReturnCplusStub() writes member parameters of struct for return statement
+        */
+       private void writeStructReturnCplusStub(String simpleType, String retType) {
+
+               // Minimum retLen is 1 if this is a single struct object
+               println("int retLen = 0;");
+               println("void* retLenObj = { &retLen };");
+               // Handle the returned struct!!!
+               println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retLenObj);");
+               int numMem = getNumOfMembers(simpleType);
+               println("int numRet = " + numMem + "*retLen;");
+               println("string retCls[numRet];");
+               println("void* retObj[numRet];");
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               // Set up variables
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmTypeC = checkAndGetCplusType(memTypes.get(i));
+                               String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
+                               println(getSimpleType(getEnumType(prmType)) + " retParam" + i + "[retLen];");
+                       }
+               } 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)) + " retParam" + i + ";");
+                       }
+               }
+               println("int retPos = 0;");
+               // Get the struct declaration for this struct and generate initialization code
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = \"" + prmTypeC + "\";");
+                               println("retObj[retPos++] = &retParam" + 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("retCls[retPos] = \"" + prmTypeC + "\";");
+                               println("retObj[retPos++] = &retParam" + i + ";");
+                       }
                }
+               println("rmiCall->getStructObjects(retCls, numRet, retObj);");
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       println("vector<" + simpleType + "> structRet(retLen);");
+               } else
+                       println(simpleType + " structRet;");
+               writeStructRetMembersCplusStub(simpleType, retType);
        }
 
 
@@ -1449,47 +2980,72 @@ 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) {
+                       List<String> methPrmTypes, String method, String callbackType, boolean isCallbackMethod) {
 
-               println("int numParam = " + methParams.size() + ";");
+               checkAndWriteStructSetupCplusStub(methParams, methPrmTypes, intDecl, method);
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
                String retType = intDecl.getMethodType(method);
-               String retTypeC = checkAndGetCplusType(retType);
-               println("string retType = \"" + checkAndGetCplusArrayType(retTypeC) + "\";");
+               println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
+               checkAndWriteEnumTypeCplusStub(methParams, methPrmTypes);
                // Generate array of parameter types
-               print("string paramCls[] = { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
-                       String paramType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
-                       print("\"" + paramType + "\"");
-                       // Check if this is the last element (don't print a comma)
-                       if (i != methParams.size() - 1) {
-                               print(", ");
+               if (isStructPresent(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 (checkCallbackType(paramType, callbackType)) {
+                                       print("\"int\"");
+                               } else {
+                                       String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+                                       print("\"" + paramTypeC + "\"");
+                               }
+                               // Check if this is the last element (don't print a comma)
+                               if (i != methParams.size() - 1) {
+                                       print(", ");
+                               }
                        }
-               }
-               println(" };");
-               // Generate array of parameter objects
-               print("void* paramObj[] = { ");
-               for (int i = 0; i < methParams.size(); i++) {
-                       print("&" + checkAndGetCplusType(getSimpleIdentifier(methParams.get(i))));
-                       // Check if this is the last element (don't print a comma)
-                       if (i != methParams.size() - 1) {
-                               print(", ");
+                       println(" };");
+                       // Generate array of parameter objects
+                       print("void* paramObj[] = { ");
+                       for (int i = 0; i < methParams.size(); 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(", ");
+                               }
                        }
+                       println(" };");
                }
-               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;");
+                       // Generate array of parameter types
+                       if (isStructClass(getGenericType(getSimpleArrayType(retType)))) {
+                               writeStructReturnCplusStub(getGenericType(getSimpleArrayType(retType)), retType);
+                       } else {
+                       // Check if the return value NONPRIMITIVES
+                               if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) {
+                                       checkAndWriteEnumRetTypeCplusStub(retType);
+                               } else {
+                                       //if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                                       if (isArrayOrList(retType,retType))
+                                               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;");
+                               }
+                       }
                }
        }
 
@@ -1497,17 +3053,39 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesCplusStub() writes the properties of the stub class
         */
-       private void writePropertiesCplusStub(String intface, String newIntface) {
+       private void writePropertiesCplusPermission(String intface) {
+
+               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("const static int object" + newObjectId + "Id = " + newObjectId + ";\t//" + newIntface);
+                       println("static set<int> set" + newObjectId + "Allowed;");
+               }
+       }       
+
+       /**
+        * HELPER: writePropertiesCplusStub() writes the properties of the stub class
+        */
+       private void writePropertiesCplusStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
 
                println("IoTRMICall *rmiCall;");
-               //println("IoTRMIObject\t\t\t*rmiObj;");
-               println("string address;");
+               println("string callbackAddress;");
                println("vector<int> ports;\n");
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("const static int objectId = " + objId + ";");
-               mapNewIntfaceObjId.put(newIntface, objId);
-               mapIntfaceObjId.put(intface, objId++);
+               if (callbackExist) {
+               // We assume that each class only has one callback interface for now
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       println("// Callback properties");
+                       println("IoTRMIObject *rmiObj;");
+                       println("vector<" + callbackType + "*> vecCallbackObj;");
+                       println("static int objIdCnt;");
+                       // Generate permission stuff for callback stubs
+                       writePropertiesCplusPermission(callbackType);
+               }
                println("\n");
        }
 
@@ -1515,13 +3093,23 @@ public class IoTCompiler {
        /**
         * HELPER: writeConstructorCplusStub() writes the constructor of the stub class
         */
-       private void writeConstructorCplusStub(String newStubClass) {
+       private void writeConstructorCplusStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
                println(newStubClass + 
-                       "(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports) {");
-               println("address = _address;");
+                       "(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _ports) {");
+               println("callbackAddress = _callbackAddress;");
                println("ports = _ports;");
-               println("rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);");
+               println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev, _bResult);");
+               if (callbackExist) {
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist);
+                       println("thread th1 (&" + newStubClass + "::___initCallBack, this);");
+                       println("th1.detach();");
+                       println("___regCB();");
+               }
                println("}\n");
        }
 
@@ -1529,16 +3117,115 @@ public class IoTCompiler {
        /**
         * HELPER: writeDeconstructorCplusStub() writes the deconstructor of the stub class
         */
-       private void writeDeconstructorCplusStub(String newStubClass) {
+       private void writeDeconstructorCplusStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
                println("~" + newStubClass + "() {");
                println("if (rmiCall != NULL) {");
                println("delete rmiCall;");
                println("rmiCall = NULL;");
                println("}");
+               if (callbackExist) {
+               // We assume that each class only has one callback interface for now
+                       println("if (rmiObj != NULL) {");
+                       println("delete rmiObj;");
+                       println("rmiObj = NULL;");
+                       println("}");
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       println("for(" + callbackType + "* cb : vecCallbackObj) {");
+                       println("delete cb;");
+                       println("cb = NULL;");
+                       println("}");
+               }
                println("}");
                println("");
-               // Check if this is callback!!! and print "delete rmiObj and vecCBObj"
+       }
+
+
+       /**
+        * 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("}");
+               }
+       }
+
+
+       /**
+        * HELPER: writeInitCallbackCplusStub() writes the initialization of callback
+        */
+       private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, String newStubClass) {
+
+               println("void ___initCallBack() {");
+               println("bool bResult = false;");
+               int port = getPortCount(newStubClass);
+               println("rmiObj = new IoTRMIObject(ports[" + port + "], &bResult);");
+               println("while (true) {");
+               println("char* method = rmiObj->getMethodBytes();");
+               //writeCplusMethodCallbackPermission(intface);
+               println("int objId = IoTRMIObject::getObjectId(method);");
+               println("if (objId < vecCallbackObj.size()) {   // Check if still within range");
+               println(intface + "_CallbackSkeleton* skel = dynamic_cast<" + intface + 
+                       "_CallbackSkeleton*> (vecCallbackObj.at(objId));");
+               writeCplusMethodCallbackPermission(intface);
+               println("skel->invokeMethod(rmiObj);");
+               print("}");
+               println(" else {");
+               println("cerr << \"Illegal object Id: \" << to_string(objId);");
+               // TODO: perhaps need to change this into "throw" to make it cleaner (allow stack unfolding)
+               println("return;");
+               println("}");
+               println("}");
+               println("}\n");
+       }
+
+
+       /**
+        * HELPER: writeCplusInitCallbackPermission() writes the permission for callback
+        */
+       private void writeCplusInitCallbackPermission(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.insert(" + methodNumId + ");");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeInitCallbackSendInfoCplusStub() writes the initialization (send info part) of callback
+        */
+       private void writeInitCallbackSendInfoCplusStub(InterfaceDecl intDecl) {
+
+               // Generate info sending part
+               println("void ___regCB() {");
+               println("int numParam = 3;");
+               String method = "___initCallBack()";
+               int methodNumId = intDecl.getHelperMethodNumId(method);
+               println("int methodId = " + methodNumId + ";");
+               println("string retType = \"void\";");
+               println("string paramCls[] = { \"int*\", \"String\", \"int\" };");
+               println("int rev = 0;");
+               println("void* paramObj[] = { &ports, &callbackAddress, &rev };");
+               println("void* retObj = NULL;");
+               println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
+               println("}\n");
        }
 
 
@@ -1562,24 +3249,39 @@ public class IoTCompiler {
                                println("#ifndef _" + newStubClass.toUpperCase() + "_HPP__");
                                println("#define _" + newStubClass.toUpperCase() + "_HPP__");
                                println("#include <iostream>");
+                               // Find out if there are callback objects
+                               Set<String> methods = intMeth.getValue();
+                               DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                               InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                               Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
+                               boolean callbackExist = !callbackClasses.isEmpty();
+                               if (callbackExist)      // Need thread library if this has callback
+                                       println("#include <thread>");
                                println("#include \"" + newIntface + ".hpp\""); println("");            
                                println("using namespace std;"); println("");
                                println("class " + newStubClass + " : public " + newIntface); println("{");
                                println("private:\n");
-                               writePropertiesCplusStub(intface, newIntface);
+                               writePropertiesCplusStub(intface, newIntface, callbackExist, callbackClasses);
                                println("public:\n");
                                // Add default constructor and destructor
                                println(newStubClass + "() { }"); println("");
-                               writeConstructorCplusStub(newStubClass);
-                               writeDeconstructorCplusStub(newStubClass);
-                               DeclarationHandler decHandler = mapIntDeclHand.get(intface);
-                               InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                               writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses);
+                               writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodCplusStub(intMeth.getValue(), intDecl);
+                               writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass);
                                print("}"); println(";");
+                               if (callbackExist) {
+                                       Iterator it = callbackClasses.iterator();
+                                       String callbackType = (String) it.next();
+                                       // Generate permission stuff for callback stubs
+                                       DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType);
+                                       InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType);
+                                       writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback);
+                               }
+                               writeObjectIdCountInitializationCplus(newStubClass, callbackExist);
                                println("#endif");
                                pw.close();
-                               System.out.println("IoTCompiler: Generated stub class " + newIntface + ".hpp...");
+                               System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".hpp...");
                        }
                }
        }
@@ -1588,11 +3290,24 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesCplusCallbackStub() writes the properties of the stub class
         */
-       private void writePropertiesCplusCallbackStub(String intface, String newIntface) {
+       private void writePropertiesCplusCallbackStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
 
                println("IoTRMICall *rmiCall;");
                // Get the object Id
-               println("static int objectId;");
+               println("int objectId;");
+               println("vector<int> ports;\n");
+               println("string callbackAddress;");
+               if (callbackExist) {
+               // We assume that each class only has one callback interface for now
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       println("// Callback properties");
+                       println("IoTRMIObject *rmiObj;");
+                       println("vector<" + callbackType + "*> vecCallbackObj;");
+                       println("static int objIdCnt;");
+                       // TODO: Need to initialize address and ports if we want to have callback-in-callback
+                       writePropertiesCplusPermission(callbackType);
+               }
                println("\n");
        }
 
@@ -1600,11 +3315,23 @@ public class IoTCompiler {
        /**
         * HELPER: writeConstructorCplusCallbackStub() writes the constructor of the stub class
         */
-       private void writeConstructorCplusCallbackStub(String newStubClass) {
+       private void writeConstructorCplusCallbackStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
 
-               println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId) {");
+               println(newStubClass + "(IoTRMICall* _rmiCall, string _callbackAddress, int _objectId, vector<int> _ports) {");
                println("objectId = _objectId;");
+               println("callbackAddress = _callbackAddress;");
                println("rmiCall = _rmiCall;");
+               println("ports = _ports;");
+               if (callbackExist) {
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+                       writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist);
+                       println("thread th1 (&" + newStubClass + "::___initCallBack, this);");
+                       println("th1.detach();");
+                       println("___regCB();");
+               }
                println("}\n");
        }
 
@@ -1625,25 +3352,40 @@ public class IoTCompiler {
                                String newStubClass = newIntface + "_CallbackStub";
                                FileWriter fw = new FileWriter(path + "/" + newStubClass + ".hpp");
                                pw = new PrintWriter(new BufferedWriter(fw));
+                               // Find out if there are callback objects
+                               Set<String> methods = intMeth.getValue();
+                               DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                               InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                               Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
+                               boolean callbackExist = !callbackClasses.isEmpty();
                                // Write file headers
                                println("#ifndef _" + newStubClass.toUpperCase() + "_HPP__");
                                println("#define _" + newStubClass.toUpperCase() + "_HPP__");
                                println("#include <iostream>");
+                               if (callbackExist)
+                                       println("#include <thread>");
                                println("#include \"" + newIntface + ".hpp\""); println("");            
                                println("using namespace std;"); println("");
                                println("class " + newStubClass + " : public " + newIntface); println("{");
                                println("private:\n");
-                               writePropertiesCplusCallbackStub(intface, newIntface);
+                               writePropertiesCplusCallbackStub(intface, newIntface, callbackExist, callbackClasses);
                                println("public:\n");
                                // Add default constructor and destructor
                                println(newStubClass + "() { }"); println("");
-                               writeConstructorCplusCallbackStub(newStubClass);
-                               writeDeconstructorCplusStub(newStubClass);
-                               DeclarationHandler decHandler = mapIntDeclHand.get(intface);
-                               InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                               writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses);
+                               writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
                                // Write methods
-                               writeMethodCplusStub(intMeth.getValue(), intDecl);
-                               print("}"); println(";");
+                               writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass);
+                               println("};");
+                               if (callbackExist) {
+                                       Iterator it = callbackClasses.iterator();
+                                       String callbackType = (String) it.next();
+                                       // Generate permission stuff for callback stubs
+                                       DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType);
+                                       InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType);
+                                       writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback);
+                               }
+                               writeObjectIdCountInitializationCplus(newStubClass, callbackExist);
                                println("#endif");
                                pw.close();
                                System.out.println("IoTCompiler: Generated callback stub class " + newIntface + ".hpp...");
@@ -1655,37 +3397,109 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesCplusSkeleton() writes the properties of the skeleton class
         */
-       private void writePropertiesCplusSkeleton(String intface) {
+       private void writePropertiesCplusSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
 
                println(intface + " *mainObj;");
-               //println("private int ports;");
-               //println("private IoTRMICall rmiCall;");
+               println("vector<int> ports;");
+               println("string callbackAddress;");
+               // Callback
+               if (callbackExist) {
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       String exchangeType = checkAndGetParamClass(callbackType);
+                       println("// Callback properties");
+                       println("static int objIdCnt;");
+                       println("vector<" + exchangeType + "*> vecCallbackObj;");
+                       println("IoTRMICall *rmiCall;");
+               }
                println("IoTRMIObject *rmiObj;\n");
+               // Keep track of object Ids of all stubs registered to this interface
+               writePropertiesCplusPermission(intface);
+               println("\n");
+       }
+
+
+       /**
+        * HELPER: writeObjectIdCountInitializationCplus() writes the initialization of objIdCnt variable
+        */
+       private void writeObjectIdCountInitializationCplus(String newSkelClass, boolean callbackExist) {
+
+               if (callbackExist)
+                       println("int " + newSkelClass + "::objIdCnt = 0;");
+       }
+
+
+       /**
+        * HELPER: writePermissionInitializationCplus() writes the initialization of permission set
+        */
+       private void writePermissionInitializationCplus(String intface, String newSkelClass, InterfaceDecl intDecl) {
+
                // Keep track of object Ids of all stubs registered to this interface
                Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
-                       int newObjectId = mapNewIntfaceObjId.get(newIntface);
-//                     println("const static int object" + newObjectId + "Id = " + 
-//                             newObjectId + ";\t//" + newIntface);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
+                       print("set<int> " + newSkelClass + "::set" + newObjectId + "Allowed { ");
+                       Set<String> methodIds = intMeth.getValue();
+                       int i = 0;
+                       for (String methodId : methodIds) {
+                               int methodNumId = intDecl.getMethodNumId(methodId);
+                               print(Integer.toString(methodNumId));
+                               // Check if this is the last element (don't print a comma)
+                               if (i != methodIds.size() - 1) {
+                                       print(", ");
+                               }
+                               i++;
+                       }
+                       println(" };");
+               }       
+       }
+
+
+       /**
+        * HELPER: writeStructPermissionCplusSkeleton() writes permission for struct helper
+        */
+       private void writeStructPermissionCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, String intface) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       int helperMethodNumId = intDecl.getHelperMethodNumId(helperMethod);
+                                       // Iterate over interfaces to give permissions to
+                                       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.insert(" + helperMethodNumId + ");");
+                                       }
+                               }
+                       }
                }
-               // TODO: CALLBACKS!
-               // TODO: code the set of allowed functions here
-               println("\n");
        }
 
 
        /**
         * HELPER: writeConstructorCplusSkeleton() writes the constructor of the skeleton class
         */
-       private void writeConstructorCplusSkeleton(String newSkelClass, String intface) {
+       private void writeConstructorCplusSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
 
-               println(newSkelClass + "(" + intface + " *_mainObj, int _port) {");
+               println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _port) {");
                println("bool _bResult = false;");
                println("mainObj = _mainObj;");
+               println("callbackAddress = _callbackAddress;");
                println("rmiObj = new IoTRMIObject(_port, &_bResult);");
-               //println("set0Allowed = Arrays.asList(object0Permission);");
-               //println("___waitRequestInvokeMethod();");
+               writeCplusInitCallbackPermission(intface, intDecl, callbackExist);
+               writeStructPermissionCplusSkeleton(methods, intDecl, intface);
+               println("___waitRequestInvokeMethod();");
                println("}\n");
        }
 
@@ -1693,16 +3507,29 @@ public class IoTCompiler {
        /**
         * HELPER: writeDeconstructorCplusSkeleton() writes the deconstructor of the skeleton class
         */
-       private void writeDeconstructorCplusSkeleton(String newSkelClass) {
+       private void writeDeconstructorCplusSkeleton(String newSkelClass, boolean callbackExist, Set<String> callbackClasses) {
 
                println("~" + newSkelClass + "() {");
                println("if (rmiObj != NULL) {");
                println("delete rmiObj;");
                println("rmiObj = NULL;");
                println("}");
+               if (callbackExist) {
+               // We assume that each class only has one callback interface for now
+                       println("if (rmiCall != NULL) {");
+                       println("delete rmiCall;");
+                       println("rmiCall = NULL;");
+                       println("}");
+                       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("");
-               // Check if this is callback!!! and print "delete rmiObj and vecCBObj"
        }
 
 
@@ -1727,11 +3554,42 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeInitCallbackCplusSkeleton() writes the init callback method for skeleton class
+        */
+       private void writeInitCallbackCplusSkeleton(boolean callbackSkeleton, String intface) {
+
+               // This is a callback skeleton generation
+               if (callbackSkeleton)
+                       println("void ___regCB(IoTRMIObject* rmiObj) {");
+               else
+                       println("void ___regCB() {");
+               println("int numParam = 3;");
+               println("vector<int> param1;");
+               println("string param2 = \"\";");
+               println("int param3 = 0;");
+               println("string paramCls[] = { \"int*\", \"String\", \"int\" };");
+               println("void* paramObj[] = { &param1, &param2, &param3 };");
+               println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
+               println("bool bResult = false;");
+               String stubInt = null;
+               if (callbackSkeleton)
+                       stubInt = getStubInterface(intface) + "_CallbackStub";
+               else
+                       stubInt = getStubInterface(intface) + "_Stub";
+               int port = getPortCount(stubInt);
+               println("rmiCall = new IoTRMICall(param1[" + port + "], param2.c_str(), param3, &bResult);");
+               println("}\n");
+       }
+
+
        /**
         * HELPER: writeMethodCplusSkeleton() writes the method of the skeleton class
         */
-       private void writeMethodCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+       private void writeMethodCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, 
+                       Set<String> callbackClasses, boolean callbackSkeleton, String intface) {
 
+               boolean isDefined = false;
                for (String method : methods) {
 
                        List<String> methParams = intDecl.getMethodParams(method);
@@ -1739,9 +3597,16 @@ public class IoTCompiler {
                        String methodId = intDecl.getMethodId(method);
                        String methodType = checkAndGetCplusType(intDecl.getMethodType(method));
                        print(methodType + " " + methodId + "(");
+                       boolean isCallbackMethod = false;
+                       String callbackType = null;
                        for (int i = 0; i < methParams.size(); i++) {
 
-                               String paramType = checkAndGetParamClass(methPrmTypes.get(i), true);
+                               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));
                                print(methParamComplete);
@@ -1754,6 +3619,176 @@ public class IoTCompiler {
                        // Now, write the body of skeleton!
                        writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method));
                        println("}\n");
+                       if (isCallbackMethod && !isDefined) {
+                               writeInitCallbackCplusSkeleton(callbackSkeleton, intface);
+                               isDefined = true;
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeCallbackCplusNumStubs() writes the numStubs variable
+        */
+       private void writeCallbackCplusNumStubs(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+
+               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("int numStubs" + i + " = 0;");
+               }
+       }
+
+
+       /**
+        * HELPER: writeCallbackCplusStubGeneration() writes the callback stub generation part
+        */
+       private void writeCallbackCplusStubGeneration(List<String> methParams, List<String> methPrmTypes, String callbackType) {
+
+               // Iterate over callback objects
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       // Generate a loop if needed
+                       if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
+                               String exchParamType = checkAndGetParamClass(getGenericType(paramType));
+                               if (isArrayOrList(paramType, param)) {
+                                       println("vector<" + exchParamType + "*> stub" + i + ";");
+                                       println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
+                                       println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
+                                       println("stub" + i + ".push_back(cb" + i + ");");
+                                       println("vecCallbackObj.push_back(cb" + i + ");");
+                                       println("objIdCnt++;");
+                                       println("}");
+                               } else {
+                                       println(exchParamType + "* stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
+                                       println("vecCallbackObj.push_back(stub" + i + ");");
+                                       println("objIdCnt++;");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: checkAndWriteEnumTypeCplusSkeleton() writes the enum type (convert from enum to int)
+        */
+       private void checkAndWriteEnumTypeCplusSkeleton(List<String> methParams, List<String> methPrmTypes) {
+
+               // Iterate and find enum declarations
+               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
+                               if (isArrayOrList(paramType, param)) {  // An array
+                                       println("int len" + i + " = paramEnumInt" + i + ".size();");
+                                       println("vector<" + simpleType + "> paramEnum" + i + "(len" + i + ");");
+                                       println("for (int i=0; i < len" + i + "; i++) {");
+                                       println("paramEnum" + i + "[i] = (" + simpleType + ") paramEnumInt" + i + "[i];");
+                                       println("}");
+                               } else {        // Just one element
+                                       println(simpleType + " paramEnum" + i + ";");
+                                       println("paramEnum" + i + " = (" + simpleType + ") paramEnumInt" + i + "[0];");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: checkAndWriteEnumRetTypeCplusSkeleton() writes the enum return type (convert from enum to int)
+        */
+       private void checkAndWriteEnumRetTypeCplusSkeleton(String retType) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(getGenericType(retType));
+               // Take the inner type of generic
+               if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(retType);
+               if (isEnumClass(pureType)) {
+               // Check if this is enum type
+                       // Enum decoder
+                       if (isArrayOrList(retType, retType)) {  // An array
+                               println("int retLen = retEnum.size();");
+                               println("vector<int> retEnumInt(retLen);");
+                               println("for (int i=0; i < retLen; i++) {");
+                               println("retEnumInt[i] = (int) retEnum[i];");
+                               println("}");
+                       } else {        // Just one element
+                               println("vector<int> retEnumInt(1);");
+                               println("retEnumInt[0] = (int) retEnum;");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeMethodInputParameters() writes the parameter variables for C++ skeleton
+        */
+       private void writeMethodInputParameters(List<String> methParams, List<String> methPrmTypes, 
+                       Set<String> callbackClasses, String methodId) {
+
+               print(methodId + "(");
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = returnGenericCallbackType(methPrmTypes.get(i));
+                       if (callbackClasses.contains(paramType))
+                               print("stub" + i);
+                       else if (isEnumClass(getGenericType(paramType)))        // Check if this is enum type
+                               print("paramEnum" + i);
+                       else if (isStructClass(getGenericType(paramType)))      // Struct type
+                               print("paramStruct" + i);
+                       else
+                               print(getSimpleIdentifier(methParams.get(i)));
+                       if (i != methParams.size() - 1) {
+                               print(", ");
+                       }
+               }
+               println(");");
+       }
+
+
+       /**
+        * 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,
+                       String methodId, Set<String> callbackClasses) {
+
+               println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
+               if (isCallbackMethod)
+                       writeCallbackCplusStubGeneration(methParams, methPrmTypes, callbackType);
+               checkAndWriteEnumTypeCplusSkeleton(methParams, methPrmTypes);
+               writeStructMembersInitCplusSkeleton(intDecl, methParams, methPrmTypes, method);
+               // Check if this is "void"
+               String retType = intDecl.getMethodType(method);
+               // Check if this is "void"
+               if (retType.equals("void")) {
+                       writeMethodInputParameters(methParams, methPrmTypes, callbackClasses, methodId);
+               } else { // We do have a return value
+                       if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) // Enum type
+                               print(checkAndGetCplusType(retType) + " retEnum = ");
+                       else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+                               print(checkAndGetCplusType(retType) + " retStruct = ");
+                       else
+                               print(checkAndGetCplusType(retType) + " retVal = ");
+                       writeMethodInputParameters(methParams, methPrmTypes, callbackClasses, methodId);
+                       checkAndWriteEnumRetTypeCplusSkeleton(retType);
+                       if (isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+                               writeStructReturnCplusSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
+                       if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) // Enum type
+                               println("void* retObj = &retEnumInt;");
+                       else
+                               if (!isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+                                       println("void* retObj = &retVal;");
+                       String retTypeC = checkAndGetCplusType(retType);
+                       if (isStructClass(getSimpleArrayType(getGenericType(retType)))) // Struct type
+                               println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);");
+                       else
+                               println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusRetClsType(getEnumType(retType)) + "\");");
                }
        }
 
@@ -1762,87 +3797,398 @@ public class IoTCompiler {
         * HELPER: writeStdMethodHelperBodyCplusSkeleton() writes the standard method body helper in the skeleton class
         */
        private void writeStdMethodHelperBodyCplusSkeleton(InterfaceDecl intDecl, List<String> methParams,
-                       List<String> methPrmTypes, String method, String methodId) {
+                       List<String> methPrmTypes, String method, String methodId, Set<String> callbackClasses) {
 
                // Generate array of parameter types
+               boolean isCallbackMethod = false;
+               String callbackType = null;
                print("string paramCls[] = { ");
                for (int i = 0; i < methParams.size(); i++) {
-                       String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
-                       String paramType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
-                       print("\"" + paramType + "\"");
+                       String paramType = returnGenericCallbackType(methPrmTypes.get(i));
+                       if (callbackClasses.contains(paramType)) {
+                               isCallbackMethod = true;
+                               callbackType = paramType;
+                               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) {
                                print(", ");
                        }
                }
                println(" };");
                println("int numParam = " + methParams.size() + ";");
+               if (isCallbackMethod)
+                       writeCallbackCplusNumStubs(methParams, methPrmTypes, callbackType);
                // Generate parameters
                for (int i = 0; i < methParams.size(); i++) {
-                       String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
-                       String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
-                       println(methParamComplete + ";");
+                       String paramType = returnGenericCallbackType(methPrmTypes.get(i));
+                       if (!callbackClasses.contains(paramType)) {
+                               String methParamType = methPrmTypes.get(i);
+                               if (isEnumClass(getSimpleArrayType(getGenericType(methParamType)))) {   
+                               // Check if this is enum type
+                                       println("vector<int> paramEnumInt" + i + ";");
+                               } else {
+                                       String methPrmType = checkAndGetCplusType(methParamType);
+                                       String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
+                    println(methParamComplete + ";");
+                               }
+                       }
                }
                // Generate array of parameter objects
                print("void* paramObj[] = { ");
                for (int i = 0; i < methParams.size(); i++) {
-                       print("&" + getSimpleIdentifier(methParams.get(i)));
+                       String paramType = returnGenericCallbackType(methPrmTypes.get(i));
+                       if (callbackClasses.contains(paramType))
+                               print("&numStubs" + i);
+                       else if (isEnumClass(getGenericType(paramType)))        // Check if this is enum type
+                               print("&paramEnumInt" + i);
+                       else
+                               print("&" + getSimpleIdentifier(methParams.get(i)));
                        if (i != methParams.size() - 1) {
                                print(", ");
                        }
                }
                println(" };");
-               println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
-               String retType = intDecl.getMethodType(method);
-               // Check if this is "void"
-               if (retType.equals("void")) {
-                       print(methodId + "(");
-                       for (int i = 0; i < methParams.size(); i++) {
-                               print(getSimpleIdentifier(methParams.get(i)));
-                               if (i != methParams.size() - 1) {
-                                       print(", ");
+               // Write the return value part
+               writeMethodHelperReturnCplusSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, 
+                       callbackType, methodId, callbackClasses);
+       }
+
+
+       /**
+        * HELPER: writeStructMembersCplusSkeleton() writes member parameters of struct
+        */
+       private void writeStructMembersCplusSkeleton(String simpleType, String paramType, 
+                       String param, String method, InterfaceDecl intDecl, int iVar) {
+
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               int methodNumId = intDecl.getMethodNumId(method);
+               String counter = "struct" + methodNumId + "Size" + iVar;
+               // Set up variables
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmTypeC = checkAndGetCplusType(memTypes.get(i));
+                               String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i));
+                               println(getSimpleType(getEnumType(prmType)) + " param" + 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" + iVar + i + ";");
+                       }
+               }
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       println("for(int i = 0; i < " + counter + "; i++) {");
+               }
+               if (isArrayOrList(paramType, param)) {  // An array or list
+                       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" + 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" + iVar + i + ";");
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructMembersInitCplusSkeleton() writes member parameters initialization of struct
+        */
+       private void writeStructMembersInitCplusSkeleton(InterfaceDecl intDecl, List<String> methParams,
+                       List<String> methPrmTypes, String method) {
+
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               int methodNumId = intDecl.getMethodNumId(method);
+                               String counter = "struct" + methodNumId + "Size" + i;
+                               // Declaration
+                               if (isArrayOrList(paramType, param)) {  // An array or list
+                                       println("vector<" + simpleType + "> paramStruct" + i + "(" + counter + ");");
+                               } else
+                                       println(simpleType + " paramStruct" + i + ";");
+                               // Initialize members
+                               StructDecl structDecl = getStructDecl(simpleType);
+                               List<String> members = structDecl.getMembers(simpleType);
+                               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+                               if (isArrayOrList(paramType, param)) {  // An array or list
+                                       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" + 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" + i + j + ";");
+                                       }
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructReturnCplusSkeleton() writes parameters of struct for return statement
+        */
+       private void writeStructReturnCplusSkeleton(String simpleType, String retType) {
+
+               // Minimum retLen is 1 if this is a single struct object
+               if (isArrayOrList(retType, retType))
+                       println("int retLen = retStruct.size();");
+               else    // Just single struct object
+                       println("int retLen = 1;");
+               println("void* retLenObj = &retLen;");
+               println("rmiObj->sendReturnObj(retLenObj, \"int\");");
+               int numMem = getNumOfMembers(simpleType);
+               println("int numRetObj = " + numMem + "*retLen;");
+               println("string retCls[numRetObj];");
+               println("void* retObj[numRetObj];");
+               println("int retPos = 0;");
+               // Get the struct declaration for this struct and generate initialization code
+               StructDecl structDecl = getStructDecl(simpleType);
+               List<String> memTypes = structDecl.getMemberTypes(simpleType);
+               List<String> members = structDecl.getMembers(simpleType);
+               if (isArrayOrList(retType, retType)) {  // An array or list
+                       println("for(int i = 0; i < retLen; i++) {");
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = \"" + prmTypeC + "\";");
+                               print("retObj[retPos++] = &retStruct[i].");
+                               print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+                               println(";");
+                       }
+                       println("}");
+               } else {        // Just one struct element
+                       for (int i = 0; i < members.size(); i++) {
+                               String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i));
+                               println("retCls[retPos] = \"" + prmTypeC + "\";");
+                               print("retObj[retPos++] = &retStruct.");
+                               print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
+                               println(";");
+                       }
+               }
+
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperStructCplusSkeleton() writes the struct in skeleton
+        */
+       private void writeMethodHelperStructCplusSkeleton(InterfaceDecl intDecl, List<String> methParams,
+                       List<String> methPrmTypes, String method, String methodId, Set<String> callbackClasses) {
+
+               // Generate array of parameter objects
+               boolean isCallbackMethod = false;
+               String callbackType = null;
+               print("int numParam = ");
+               writeLengthStructParamClassSkeleton(methParams, methPrmTypes, method, intDecl);
+               println(";");
+               println("string paramCls[numParam];");
+               println("void* paramObj[numParam];");
+               println("int pos = 0;");
+               // Iterate again over the parameters
+               for (int i = 0; i < methParams.size(); i++) {
+                       String paramType = methPrmTypes.get(i);
+                       String param = methParams.get(i);
+                       String simpleType = getGenericType(paramType);
+                       if (isStructClass(simpleType)) {
+                               writeStructMembersCplusSkeleton(simpleType, paramType, param, method, intDecl, i);
+                       } else {
+                               String prmType = returnGenericCallbackType(methPrmTypes.get(i));
+                               if (callbackClasses.contains(prmType)) {
+                                       isCallbackMethod = true;
+                                       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
+                                       String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
+                                       if (isEnumClass(getGenericType(paramTypeC))) {  // Check if this is enum type
+                                               println("vector<int> paramEnumInt" + i + ";");
+                                       } else {
+                                               String methParamComplete = checkAndGetCplusArray(paramTypeC, methParams.get(i));
+                                               println(methParamComplete + ";");
+                                       }
+                                       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 + ";");
+                                       else
+                                               println("paramObj[pos++] = &" + getSimpleIdentifier(methParams.get(i)) + ";");
+                               }
+                       }
+               }
+               // Write the return value part
+               writeMethodHelperReturnCplusSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, 
+                       callbackType, methodId, callbackClasses);
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperCplusSkeleton() writes the method helper of the skeleton class
+        */
+       private void writeMethodHelperCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+
+               // Use this set to handle two same methodIds
+               Set<String> uniqueMethodIds = new HashSet<String>();
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       if (isStructPresent(methParams, methPrmTypes)) {        // Treat struct differently
+                               String methodId = intDecl.getMethodId(method);
+                               print("void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               String retType = intDecl.getMethodType(method);
+                               print(helperMethod + "(");
+                               boolean begin = true;
+                               for (int i = 0; i < methParams.size(); i++) { // Print size variables
+                                       String paramType = methPrmTypes.get(i);
+                                       String param = methParams.get(i);
+                                       String simpleType = getGenericType(paramType);
+                                       if (isStructClass(simpleType)) {
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
+                                               int methodNumId = intDecl.getMethodNumId(method);
+                                               print("int struct" + methodNumId + "Size" + i);
+                                       }
                                }
+                               println(") {");
+                               writeMethodHelperStructCplusSkeleton(intDecl, methParams, methPrmTypes, method, methodId, callbackClasses);
+                               println("}\n");
+                       } else {
+                               String methodId = intDecl.getMethodId(method);
+                               print("void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               // Check if this is "void"
+                               String retType = intDecl.getMethodType(method);
+                               println(helperMethod + "() {");
+                               // Now, write the helper body of skeleton!
+                               writeStdMethodHelperBodyCplusSkeleton(intDecl, methParams, methPrmTypes, method, methodId, callbackClasses);
+                               println("}\n");
                        }
-                       println(");");
-               } else { // We do have a return value
-                       print(checkAndGetCplusType(retType) + " retVal = " + methodId + "(");
+               }
+               // Write method helper for structs
+               writeMethodHelperStructSetupCplusSkeleton(methods, intDecl);
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperStructSetupCplusSkeleton() writes the method helper of struct in skeleton class
+        */
+       private void writeMethodHelperStructSetupCplusSkeleton(Collection<String> methods, 
+                       InterfaceDecl intDecl) {
+
+               // Use this set to handle two same methodIds
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       // Check for params with structs
                        for (int i = 0; i < methParams.size(); i++) {
-                               print(getSimpleIdentifier(methParams.get(i)));
-                               if (i != methParams.size() - 1) {
-                                       print(", ");
+                               String paramType = methPrmTypes.get(i);
+                               String param = methParams.get(i);
+                               String simpleType = getGenericType(paramType);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       print("int ___");
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       println(helperMethod + "() {");
+                                       // Now, write the helper body of skeleton!
+                                       println("string paramCls[] = { \"int\" };");
+                                       println("int numParam = 1;");
+                                       println("int param0 = 0;");
+                                       println("void* paramObj[] = { &param0 };");
+                                       println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
+                                       println("return param0;");
+                                       println("}\n");
                                }
                        }
-                       println(");");
-                       println("void* retObj = &retVal;");
-                       String retTypeC = checkAndGetCplusType(retType);
-                       println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusArrayType(retTypeC) + "\");");
                }
        }
 
 
        /**
-        * HELPER: writeMethodHelperCplusSkeleton() writes the method helper of the skeleton class
+        * HELPER: writeMethodHelperStructSetupCplusCallbackSkeleton() writes the method helper of struct in skeleton class
         */
-       private void writeMethodHelperCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+       private void writeMethodHelperStructSetupCplusCallbackSkeleton(Collection<String> methods, 
+                       InterfaceDecl intDecl) {
 
                // Use this set to handle two same methodIds
-               Set<String> uniqueMethodIds = new HashSet<String>();
                for (String method : methods) {
 
                        List<String> methParams = intDecl.getMethodParams(method);
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-                       String methodId = intDecl.getMethodId(method);
-                       print("void ___");
-                       String helperMethod = methodId;
-                       if (uniqueMethodIds.contains(methodId))
-                               helperMethod = helperMethod + intDecl.getMethodNumId(method);
-                       else
-                               uniqueMethodIds.add(methodId);
-                       // Check if this is "void"
-                       String retType = intDecl.getMethodType(method);
-                       println(helperMethod + "() {");
-                       // Now, write the helper body of skeleton!
-                       writeStdMethodHelperBodyCplusSkeleton(intDecl, methParams, methPrmTypes, method, methodId);
-                       println("}\n");
+                       // 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);
+                               if (isStructClass(simpleType)) {
+                                       int methodNumId = intDecl.getMethodNumId(method);
+                                       print("int ___");
+                                       String helperMethod = methodNumId + "struct" + i;
+                                       println(helperMethod + "(IoTRMIObject* rmiObj) {");
+                                       // Now, write the helper body of skeleton!
+                                       println("string paramCls[] = { \"int\" };");
+                                       println("int numParam = 1;");
+                                       println("int param0 = 0;");
+                                       println("void* paramObj[] = { &param0 };");
+                                       println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
+                                       println("return param0;");
+                                       println("}\n");
+                               }
+                       }
+               }
+       }
+
+
+       /**
+        * HELPER: writeCplusMethodPermission() writes permission checks in skeleton
+        */
+       private void writeCplusMethodPermission(String intface) {
+
+               // 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 (_objectId == object" + newObjectId + "Id) {");
+                       println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
+                       println("cerr << \"Object with object Id: \" << _objectId << \"  is not allowed to access method: \" << methodId << endl;");
+                       println("return;");
+                       println("}");
+                       println("}");
+                       println("else {");
+                       println("cerr << \"Object Id: \" << _objectId << \" not recognized!\" << endl;");
+                       println("return;");
+                       println("}");
                }
        }
 
@@ -1850,17 +4196,19 @@ public class IoTCompiler {
        /**
         * HELPER: writeCplusWaitRequestInvokeMethod() writes the main loop of the skeleton class
         */
-       private void writeCplusWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl) {
+       private void writeCplusWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist, String intface) {
 
                // Use this set to handle two same methodIds
                Set<String> uniqueMethodIds = new HashSet<String>();
                println("void ___waitRequestInvokeMethod() {");
                // Write variables here if we have callbacks or enums or structs
+               writeCountVarStructSkeleton(methods, intDecl);
                println("while (true) {");
                println("rmiObj->getMethodBytes();");
                println("int _objectId = rmiObj->getObjectId();");
                println("int methodId = rmiObj->getMethodId();");
-               // TODO: code the permission check here!
+               // Generate permission check
+               writeCplusMethodPermission(intface);
                println("switch (methodId) {");
                // Print methods and method Ids
                for (String method : methods) {
@@ -1872,8 +4220,17 @@ public class IoTCompiler {
                                helperMethod = helperMethod + methodNumId;
                        else
                                uniqueMethodIds.add(methodId);
-                       println(helperMethod + "(); break;");
+                       print(helperMethod + "(");
+                       writeInputCountVarStructSkeleton(method, intDecl);
+                       println("); break;");
+               }
+               String method = "___initCallBack()";
+               // Print case -9999 (callback handler) if callback exists
+               if (callbackExist) {
+                       int methodId = intDecl.getHelperMethodNumId(method);
+                       println("case " + methodId + ": ___regCB(); break;");
                }
+               writeMethodCallStructSkeleton(methods, intDecl);
                println("default: ");
                println("cerr << \"Method Id \" << methodId << \" not recognized!\" << endl;");
                println("throw exception();");
@@ -1904,28 +4261,33 @@ public class IoTCompiler {
                        DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                        List<String> methods = intDecl.getMethods();
-                       Set<String> includeClasses = getIncludeClasses(methods, intDecl);
+                       Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
                        List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
-                       List<String> allIncludeClasses = getAllImportClasses(stdIncludeClasses, includeClasses);
-                       printIncludeStatements(allIncludeClasses); println("");                 
+                       List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
+                       printIncludeStatements(allIncludeClasses); println("");
                        println("using namespace std;\n");
+                       // Find out if there are callback objects
+                       Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
+                       boolean callbackExist = !callbackClasses.isEmpty();
                        // Write class header
                        println("class " + newSkelClass + " : public " + intface); println("{");
                        println("private:\n");
                        // Write properties
-                       writePropertiesCplusSkeleton(intface);
+                       writePropertiesCplusSkeleton(intface, callbackExist, callbackClasses);
                        println("public:\n");
                        // Write constructor
-                       writeConstructorCplusSkeleton(newSkelClass, intface);
+                       writeConstructorCplusSkeleton(newSkelClass, intface, callbackExist, intDecl, methods);
                        // Write deconstructor
-                       writeDeconstructorCplusSkeleton(newSkelClass);
+                       writeDeconstructorCplusSkeleton(newSkelClass, callbackExist, callbackClasses);
                        // Write methods
-                       writeMethodCplusSkeleton(methods, intDecl);
+                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false, intface);
                        // Write method helper
-                       writeMethodHelperCplusSkeleton(methods, intDecl);
+                       writeMethodHelperCplusSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
-                       writeCplusWaitRequestInvokeMethod(methods, intDecl);
+                       writeCplusWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
                        println("};");
+                       writePermissionInitializationCplus(intface, newSkelClass, intDecl);
+                       writeObjectIdCountInitializationCplus(newSkelClass, callbackExist);
                        println("#endif");
                        pw.close();
                        System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".hpp...");
@@ -1936,12 +4298,23 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesCplusCallbackSkeleton() writes the properties of the callback skeleton class
         */
-       private void writePropertiesCplusCallbackSkeleton(String intface) {
+       private void writePropertiesCplusCallbackSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
 
                println(intface + " *mainObj;");
-               //println("IoTRMIObject *rmiObj;\n");
                // Keep track of object Ids of all stubs registered to this interface
-               println("static int objectId;");
+               println("int objectId;");
+               println("vector<int> ports;\n");
+               println("string callbackAddress;");
+               // Callback
+               if (callbackExist) {
+                       Iterator it = callbackClasses.iterator();
+                       String callbackType = (String) it.next();
+                       String exchangeType = checkAndGetParamClass(callbackType);
+                       println("// Callback properties");
+                       println("IoTRMICall* rmiCall;");
+                       println("vector<" + exchangeType + "*> vecCallbackObj;");
+                       println("static int objIdCnt;");
+               }
                println("\n");
        }
 
@@ -1949,19 +4322,47 @@ public class IoTCompiler {
        /**
         * HELPER: writeConstructorCplusCallbackSkeleton() writes the constructor of the skeleton class
         */
-       private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface) {
+       private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
 
-               println(newSkelClass + "(" + intface + " *_mainObj, int _objectId) {");
+               println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _objectId) {");
                println("mainObj = _mainObj;");
+               println("callbackAddress = _callbackAddress;");
                println("objectId = _objectId;");
                println("}\n");
        }
 
 
        /**
-        * HELPER: writeMethodHelperCplusCallbackSkeleton() writes the method helper of the callback skeleton class
+        * HELPER: writeDeconstructorCplusStub() writes the deconstructor of the stub class
+        */
+       private void writeDeconstructorCplusCallbackSkeleton(String newStubClass, boolean callbackExist, 
+                       Set<String> callbackClasses) {
+
+               println("~" + newStubClass + "() {");
+               if (callbackExist) {
+               // We assume that each class only has one callback interface for now
+                       println("if (rmiCall != NULL) {");
+                       println("delete rmiCall;");
+                       println("rmiCall = NULL;");
+                       println("}");
+                       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("");
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperCplusCallbackSkeleton() writes the method helper of callback skeleton class
         */
-       private void writeMethodHelperCplusCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+       private void writeMethodHelperCplusCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl, 
+                       Set<String> callbackClasses) {
 
                // Use this set to handle two same methodIds
                Set<String> uniqueMethodIds = new HashSet<String>();
@@ -1969,32 +4370,66 @@ public class IoTCompiler {
 
                        List<String> methParams = intDecl.getMethodParams(method);
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-                       String methodId = intDecl.getMethodId(method);
-                       print("void ___");
-                       String helperMethod = methodId;
-                       if (uniqueMethodIds.contains(methodId))
-                               helperMethod = helperMethod + intDecl.getMethodNumId(method);
-                       else
-                               uniqueMethodIds.add(methodId);
-                       // Check if this is "void"
-                       String retType = intDecl.getMethodType(method);
-                       println(helperMethod + "(IoTRMIObject* rmiObj) {");
-                       // Now, write the helper body of skeleton!
-                       writeStdMethodHelperBodyCplusSkeleton(intDecl, methParams, methPrmTypes, method, methodId);
-                       println("}\n");
+                       if (isStructPresent(methParams, methPrmTypes)) {        // Treat struct differently
+                               String methodId = intDecl.getMethodId(method);
+                               print("void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               String retType = intDecl.getMethodType(method);
+                               print(helperMethod + "(");
+                               boolean begin = true;
+                               for (int i = 0; i < methParams.size(); i++) { // Print size variables
+                                       String paramType = methPrmTypes.get(i);
+                                       String param = methParams.get(i);
+                                       String simpleType = getGenericType(paramType);
+                                       if (isStructClass(simpleType)) {
+                                               if (!begin)     // Generate comma for not the beginning variable
+                                                       print(", ");
+                                               else
+                                                       begin = false;
+                                               int methodNumId = intDecl.getMethodNumId(method);
+                                               print("int struct" + methodNumId + "Size" + i);
+                                       }
+                               }
+                               println(", IoTRMIObject* rmiObj) {");
+                               writeMethodHelperStructCplusSkeleton(intDecl, methParams, methPrmTypes, method, methodId, callbackClasses);
+                               println("}\n");
+                       } else {
+                               String methodId = intDecl.getMethodId(method);
+                               print("void ___");
+                               String helperMethod = methodId;
+                               if (uniqueMethodIds.contains(methodId))
+                                       helperMethod = helperMethod + intDecl.getMethodNumId(method);
+                               else
+                                       uniqueMethodIds.add(methodId);
+                               // Check if this is "void"
+                               String retType = intDecl.getMethodType(method);
+                               println(helperMethod + "(IoTRMIObject* rmiObj) {");
+                               // Now, write the helper body of skeleton!
+                               writeStdMethodHelperBodyCplusSkeleton(intDecl, methParams, methPrmTypes, method, methodId, callbackClasses);
+                               println("}\n");
+                       }
                }
+               // Write method helper for structs
+               writeMethodHelperStructSetupCplusCallbackSkeleton(methods, intDecl);
        }
 
 
        /**
-        * HELPER: writeCplusCallbackWaitRequestInvokeMethod() writes the main loop of the skeleton class
+        * HELPER: writeCplusCallbackWaitRequestInvokeMethod() writes the request invoke method of the skeleton callback class
         */
-       private void writeCplusCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl) {
+       private void writeCplusCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, 
+                       boolean callbackExist) {
 
                // Use this set to handle two same methodIds
                Set<String> uniqueMethodIds = new HashSet<String>();
                println("void invokeMethod(IoTRMIObject* rmiObj) {");
                // Write variables here if we have callbacks or enums or structs
+               writeCountVarStructSkeleton(methods, intDecl);
+               // Write variables here if we have callbacks or enums or structs
                println("int methodId = rmiObj->getMethodId();");
                // TODO: code the permission check here!
                println("switch (methodId) {");
@@ -2008,8 +4443,19 @@ public class IoTCompiler {
                                helperMethod = helperMethod + methodNumId;
                        else
                                uniqueMethodIds.add(methodId);
-                       println(helperMethod + "(rmiObj); break;");
+                       print(helperMethod + "(");
+                       if (writeInputCountVarStructSkeleton(method, intDecl))
+                               println(", rmiObj); break;");
+                       else
+                               println("rmiObj); break;");
+               }
+               String method = "___initCallBack()";
+               // Print case -9999 (callback handler) if callback exists
+               if (callbackExist) {
+                       int methodId = intDecl.getHelperMethodNumId(method);
+                       println("case " + methodId + ": ___regCB(rmiObj); break;");
                }
+               writeMethodCallStructCallbackSkeleton(methods, intDecl);
                println("default: ");
                println("cerr << \"Method Id \" << methodId << \" not recognized!\" << endl;");
                println("throw exception();");
@@ -2018,7 +4464,6 @@ public class IoTCompiler {
        }
 
 
-
        /**
         * generateCplusCallbackSkeletonClass() generate callback skeletons based on the methods list in C++
         */
@@ -2040,28 +4485,32 @@ public class IoTCompiler {
                        DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                        List<String> methods = intDecl.getMethods();
-                       Set<String> includeClasses = getIncludeClasses(methods, intDecl);
+                       Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
                        List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
-                       List<String> allIncludeClasses = getAllImportClasses(stdIncludeClasses, includeClasses);
+                       List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
                        printIncludeStatements(allIncludeClasses); println("");                 
+                       // Find out if there are callback objects
+                       Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
+                       boolean callbackExist = !callbackClasses.isEmpty();
                        println("using namespace std;\n");
                        // Write class header
                        println("class " + newSkelClass + " : public " + intface); println("{");
                        println("private:\n");
                        // Write properties
-                       writePropertiesCplusCallbackSkeleton(intface);
+                       writePropertiesCplusCallbackSkeleton(intface, callbackExist, callbackClasses);
                        println("public:\n");
                        // Write constructor
-                       writeConstructorCplusCallbackSkeleton(newSkelClass, intface);
+                       writeConstructorCplusCallbackSkeleton(newSkelClass, intface, callbackExist, intDecl, methods);
                        // Write deconstructor
-                       //writeDeconstructorCplusSkeleton(newSkelClass);
+                       writeDeconstructorCplusCallbackSkeleton(newSkelClass, callbackExist, callbackClasses);
                        // Write methods
-                       writeMethodCplusSkeleton(methods, intDecl);
+                       writeMethodCplusSkeleton(methods, intDecl, callbackClasses, true, intface);
                        // Write method helper
-                       writeMethodHelperCplusCallbackSkeleton(methods, intDecl);
+                       writeMethodHelperCplusCallbackSkeleton(methods, intDecl, callbackClasses);
                        // Write waitRequestInvokeMethod() - main loop
-                       writeCplusCallbackWaitRequestInvokeMethod(methods, intDecl);
+                       writeCplusCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist);
                        println("};");
+                       writeObjectIdCountInitializationCplus(newSkelClass, callbackExist);
                        println("#endif");
                        pw.close();
                        System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".hpp...");
@@ -2099,35 +4548,6 @@ public class IoTCompiler {
        }
 
 
-       /**
-        * generateReturnStmt() generate return statement based on methType
-        */
-       public String generateReturnStmt(String methType) {
-
-               // Generate dummy returns for now
-               if (methType.equals("short")||
-                       methType.equals("int")  ||
-                       methType.equals("long") ||
-                       methType.equals("float")||
-                       methType.equals("double")) {
-
-                       return "1";
-               } else if ( methType.equals("String")) {
-  
-                       return "\"a\"";
-               } else if ( methType.equals("char") ||
-                                       methType.equals("byte")) {
-
-                       return "\'a\'";
-               } else if ( methType.equals("boolean")) {
-
-                       return "true";
-               } else {
-                       return "null";
-               }
-       }
-
-
        /**
         * setDirectory() sets a new directory for stub files
         */
@@ -2173,7 +4593,7 @@ public class IoTCompiler {
                        pn = (ParseNode) parse.parse().value;
                } catch (Exception e) {
                        e.printStackTrace();
-                       throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file);
+                       throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file + "\n");
                }
 
                return pn;
@@ -2181,7 +4601,7 @@ public class IoTCompiler {
 
 
        /**================
-        * Helper functions
+        * Basic helper functions
         **================
         */
        boolean newline=true;
@@ -2204,16 +4624,22 @@ public class IoTCompiler {
        /**
         * This function converts Java to C++ type for compilation
         */
-       private String convertType(String jType) {
+       private String convertType(String type) {
 
-               return mapPrimitives.get(jType);
+               if (mapPrimitives.containsKey(type))
+                       return mapPrimitives.get(type);
+               else
+                       return type;
        }
 
 
+       /**
+        * A collection of methods with print-to-file functionality
+        */
        private void println(String str) {
                if (newline) {
                        int tab = tablevel;
-                       if (str.equals("}"))
+                       if (str.contains("}") && !str.contains("{"))
                                tab--;
                        for(int i=0; i<tab; i++)
                                pw.print("\t");
@@ -2225,6 +4651,7 @@ public class IoTCompiler {
 
 
        private void updatetabbing(String str) {
+
                tablevel+=count(str,'{')-count(str,'}');
        }
 
@@ -2279,7 +4706,23 @@ public class IoTCompiler {
        }
 
 
-       // Return parameter category, i.e. PRIMITIVES, NONPRIMITIVES, or USERDEFINED
+       // Check and find object Id for new interface in mapNewIntfaceObjId (callbacks)
+       // Throw an error if the new interface is not found!
+       // Basically the compiler needs to parse the policy (and requires) files for callback class first
+       private int getNewIntfaceObjectId(String newIntface) {
+
+//             if (!mapNewIntfaceObjId.containsKey(newIntface)) {
+//                     throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
+//                                                     "Please place the two files for callback class in front...\n");
+//                     return -1;
+//             } else {
+                       int retObjId = mapNewIntfaceObjId.get(newIntface);
+                       return retObjId;
+//             }
+       }
+
+
+       // Return parameter category, i.e. PRIMITIVES, NONPRIMITIVES, USERDEFINED, ENUM, or STRUCT
        private ParamCategory getParamCategory(String paramType) {
 
                if (mapPrimitives.containsKey(paramType)) {
@@ -2287,13 +4730,17 @@ public class IoTCompiler {
                // We can either use mapNonPrimitivesJava or mapNonPrimitivesCplus here
                } else if (mapNonPrimitivesJava.containsKey(getSimpleType(paramType))) {
                        return ParamCategory.NONPRIMITIVES;
+               } else if (isEnumClass(paramType)) {
+                       return ParamCategory.ENUM;
+               } else if (isStructClass(paramType)) {
+                       return ParamCategory.STRUCT;
                } else
                        return ParamCategory.USERDEFINED;
        }
 
 
        // Return full class name for non-primitives to generate Java import statements
-       // e.g. java.util.Set for Set, java.util.Map for Map
+       // e.g. java.util.Set for Set
        private String getNonPrimitiveJavaClass(String paramNonPrimitives) {
 
                return mapNonPrimitivesJava.get(paramNonPrimitives);
@@ -2301,7 +4748,7 @@ public class IoTCompiler {
 
 
        // Return full class name for non-primitives to generate Cplus include statements
-       // e.g. #include <set> for Set, #include <map> for Map
+       // e.g. #include <set> for Set
        private String getNonPrimitiveCplusClass(String paramNonPrimitives) {
 
                return mapNonPrimitivesCplus.get(paramNonPrimitives);
@@ -2321,6 +4768,18 @@ public class IoTCompiler {
        }
 
 
+       // Generate a set of standard classes for import statements
+       private List<String> getStandardJavaIntfaceImportClasses() {
+
+               List<String> importClasses = new ArrayList<String>();
+               // Add the standard list first
+               importClasses.add("java.util.List");
+               importClasses.add("java.util.ArrayList");
+
+               return importClasses;
+       }
+
+
        // Generate a set of standard classes for import statements
        private List<String> getStandardJavaImportClasses() {
 
@@ -2329,6 +4788,7 @@ public class IoTCompiler {
                importClasses.add("java.io.IOException");
                importClasses.add("java.util.List");
                importClasses.add("java.util.ArrayList");
+               importClasses.add("java.util.Arrays");
                importClasses.add("iotrmi.Java.IoTRMICall");
                importClasses.add("iotrmi.Java.IoTRMIObject");
 
@@ -2342,6 +4802,7 @@ public class IoTCompiler {
                List<String> importClasses = new ArrayList<String>();
                // Add the standard list first
                importClasses.add("<vector>");
+               importClasses.add("<set>");
                importClasses.add("\"IoTRMICall.hpp\"");
                importClasses.add("\"IoTRMIObject.hpp\"");
 
@@ -2349,18 +4810,17 @@ public class IoTCompiler {
        }
 
 
-       // Generate a set of standard classes for import statements
-       private List<String> getAllImportClasses(Collection<String> stdImportClasses, Collection<String> importClasses) {
+       // Combine all classes for import statements
+       private List<String> getAllLibClasses(Collection<String> stdLibClasses, Collection<String> libClasses) {
 
-               List<String> allImportClasses = new ArrayList<String>(stdImportClasses);
+               List<String> allLibClasses = new ArrayList<String>(stdLibClasses);
                // Iterate over the list of import classes
-               for (String str : importClasses) {
-                       if (!stdImportClasses.contains(str)) {
-                               stdImportClasses.add(str);
+               for (String str : libClasses) {
+                       if (!allLibClasses.contains(str)) {
+                               allLibClasses.add(str);
                        }
                }
-
-               return allImportClasses;
+               return allLibClasses;
        }
 
 
@@ -2383,8 +4843,148 @@ public class IoTCompiler {
        }
 
 
+       // Handle and return the correct enum declaration
+       // In Java, if we declare enum in Camera interface, then it becomes "Camera.<enum>"
+       private String getEnumParamDecl(String type, InterfaceDecl intDecl) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(type);
+               // Take the inner type of generic
+               if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+                       pureType = getTypeOfGeneric(type)[0];
+               if (isEnumClass(pureType)) {
+                       String enumType = intDecl.getInterface() + "." + type;
+                       return enumType;
+               } else
+                       return type;
+       }
+
+
+       // Handle and return the correct type
+       private String getEnumParam(String type, String param, int i) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(type);
+               // Take the inner type of generic
+               if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+                       pureType = getTypeOfGeneric(type)[0];
+               if (isEnumClass(pureType)) {
+                       String enumParam = "paramEnum" + i;
+                       return enumParam;
+               } else
+                       return param;
+       }
+
+
+       // Handle and return the correct enum declaration translate into int[]
+       private String getEnumType(String type) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(type);
+               // Take the inner type of generic
+               if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(type);
+               if (isEnumClass(pureType)) {
+                       String enumType = "int[]";
+                       return enumType;
+               } else
+                       return type;
+       }
+
+       // Handle and return the correct enum declaration translate into int* for C
+       private String getEnumCplusClsType(String type) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(type);
+               // Take the inner type of generic
+               if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(type);
+               if (isEnumClass(pureType)) {
+                       String enumType = "int*";
+                       return enumType;
+               } else
+                       return type;
+       }
+
+
+       // Handle and return the correct struct declaration
+       private String getStructType(String type) {
+
+               // Strips off array "[]" for return type
+               String pureType = getSimpleArrayType(type);
+               // Take the inner type of generic
+               if (getParamCategory(type) == ParamCategory.NONPRIMITIVES)
+                       pureType = getGenericType(type);
+               if (isStructClass(pureType)) {
+                       String structType = "int";
+                       return structType;
+               } else
+                       return type;
+       }
+
+
+       // Check if this an enum declaration
+       private boolean isEnumClass(String type) {
+
+               // Just iterate over the set of interfaces
+               for (String intface : mapIntfacePTH.keySet()) {
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       Set<String> setEnumDecl = enumDecl.getEnumDeclarations();
+                       if (setEnumDecl.contains(type))
+                               return true;
+               }
+               return false;
+       }
+
+
+       // Check if this an struct declaration
+       private boolean isStructClass(String type) {
+
+               // Just iterate over the set of interfaces
+               for (String intface : mapIntfacePTH.keySet()) {
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       List<String> listStructDecl = structDecl.getStructTypes();
+                       if (listStructDecl.contains(type))
+                               return true;
+               }
+               return false;
+       }
+
+
+       // Return a struct declaration
+       private StructDecl getStructDecl(String type) {
+
+               // Just iterate over the set of interfaces
+               for (String intface : mapIntfacePTH.keySet()) {
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       List<String> listStructDecl = structDecl.getStructTypes();
+                       if (listStructDecl.contains(type))
+                               return structDecl;
+               }
+               return null;
+       }
+
+
+       // Return number of members (-1 if not found)
+       private int getNumOfMembers(String type) {
+
+               // Just iterate over the set of interfaces
+               for (String intface : mapIntfacePTH.keySet()) {
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       List<String> listStructDecl = structDecl.getStructTypes();
+                       if (listStructDecl.contains(type))
+                               return structDecl.getNumOfMembers(type);
+               }
+               return -1;
+       }
+
+
        // Generate a set of classes for include statements
-       private Set<String> getIncludeClasses(Collection<String> methods, InterfaceDecl intDecl) {
+       private Set<String> getIncludeClasses(Collection<String> methods, InterfaceDecl intDecl, String intface, boolean needExchange) {
 
                Set<String> includeClasses = new HashSet<String>();
                for (String method : methods) {
@@ -2398,7 +4998,18 @@ public class IoTCompiler {
                                if (getParamCategory(simpleType) == ParamCategory.NONPRIMITIVES) {
                                        includeClasses.add("<" + getNonPrimitiveCplusClass(simpleType) + ">");
                                } else if (getParamCategory(simpleType) == ParamCategory.USERDEFINED) {
-                                       includeClasses.add("\"" + exchangeParamType(simpleType) + ".hpp\"");
+                                       // For original interface, we need it exchanged... not for stub interfaces
+                                       if (needExchange) {
+                                               includeClasses.add("\"" + exchangeParamType(simpleType) + ".hpp\"");
+                                               includeClasses.add("\"" + exchangeParamType(simpleType) + "_CallbackStub.hpp\"");
+                                       } else {
+                                               includeClasses.add("\"" + simpleType + ".hpp\"");
+                                               includeClasses.add("\"" + simpleType + "_CallbackSkeleton.hpp\"");
+                                       }
+                               } else if (getParamCategory(getSimpleArrayType(simpleType)) == ParamCategory.ENUM) {
+                                       includeClasses.add("\"" + simpleType + ".hpp\"");
+                               } else if (getParamCategory(getSimpleArrayType(simpleType)) == ParamCategory.STRUCT) {
+                                       includeClasses.add("\"" + simpleType + ".hpp\"");
                                } else if (param.contains("[]")) {
                                // Check if this is array for C++; translate into vector
                                        includeClasses.add("<vector>");
@@ -2435,6 +5046,7 @@ public class IoTCompiler {
        }
 
 
+       // Print import statements into file
        private void printImportStatements(Collection<String> importClasses) {
 
                for(String cls : importClasses) {
@@ -2443,6 +5055,7 @@ public class IoTCompiler {
        }
 
 
+       // Print include statements into file
        private void printIncludeStatements(Collection<String> includeClasses) {
 
                for(String cls : includeClasses) {
@@ -2462,6 +5075,30 @@ public class IoTCompiler {
        }
 
 
+       // Gets generic type inside "<" and ">"
+       private String getGenericType(String type) {
+
+               // Handle <, >, and , for 2-type generic/template
+               if (getParamCategory(type) == ParamCategory.NONPRIMITIVES) {
+                       String[] substr = type.split("<")[1].split(">")[0].split(",");
+                       return substr[0];
+               } else
+                       return type;
+       }
+
+
+       // This helper function strips off array declaration, e.g. int[] becomes int
+       private String getSimpleArrayType(String type) {
+
+               // Handle [ for array declaration
+               String substr = type;
+               if (type.contains("[]")) {
+                       substr = type.split("\\[\\]")[0];
+               }
+               return substr;
+       }
+
+
        // This helper function strips off array declaration, e.g. D[] becomes D
        private String getSimpleIdentifier(String ident) {
 
@@ -2474,6 +5111,7 @@ public class IoTCompiler {
        }
 
 
+       // Checks and gets type in C++
        private String checkAndGetCplusType(String paramType) {
 
                if (getParamCategory(paramType) == ParamCategory.PRIMITIVES) {
@@ -2484,17 +5122,22 @@ public class IoTCompiler {
                        if (paramType.contains("<") && paramType.contains(">")) {
 
                                String genericClass = getSimpleType(paramType);
-                               String[] genericType = getTypeOfGeneric(paramType);
+                               String genericType = getGenericType(paramType);
                                String cplusTemplate = null;
-                               if (genericType.length == 1) // Generic/template with one type
-                                       cplusTemplate = getNonPrimitiveCplusClass(genericClass) + 
-                                               "<" + convertType(genericType[0]) + ">";
-                               else // Generic/template with two types
-                                       cplusTemplate = getNonPrimitiveCplusClass(genericClass) + 
-                                               "<" + convertType(genericType[0]) + "," + convertType(genericType[1]) + ">";
+                               cplusTemplate = getNonPrimitiveCplusClass(genericClass);
+                               if(getParamCategory(getGenericType(paramType)) == ParamCategory.USERDEFINED) {
+                                       cplusTemplate = cplusTemplate + "<" + genericType + "*>";
+                               } else {
+                                       cplusTemplate = cplusTemplate + "<" + convertType(genericType) + ">";
+                               }
                                return cplusTemplate;
                        } else
                                return getNonPrimitiveCplusClass(paramType);
+               } else if(paramType.contains("[]")) {   // Array type (used for return type only)
+                       String cArray = "vector<" + convertType(getSimpleArrayType(paramType)) + ">";
+                       return cArray;
+               } else if(getParamCategory(paramType) == ParamCategory.USERDEFINED) {
+                       return paramType + "*";
                } else
                        // Just return it as is if it's not non-primitives
                        return paramType;
@@ -2558,6 +5201,52 @@ public class IoTCompiler {
        }
 
 
+       // Return the class type for class resolution (for return value)
+       // - Check and return C++ array class, e.g. int A[] into int*
+       // - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
+       private String checkAndGetCplusRetClsType(String paramType) {
+
+               String paramTypeRet = null;
+               // Check for array declaration
+               if (paramType.contains("[]")) {
+                       String type = paramType.split("\\[\\]")[0];
+                       paramTypeRet = getSimpleArrayType(type) + "*";
+               } else if (paramType.contains("<") && paramType.contains(">")) {
+                       // Just return it as is if it's not an array
+                       String type = paramType.split("<")[1].split(">")[0];
+                       paramTypeRet = "vector<" + getGenericType(type) + ">";
+               } else
+                       paramTypeRet = paramType;
+
+               return paramTypeRet;
+       }
+
+
+       // Return the class type for class resolution (for method arguments)
+       // - Check and return C++ array class, e.g. int A[] into int*
+       // - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
+       private String checkAndGetCplusArgClsType(String paramType, String param) {
+
+               String paramTypeRet = getEnumCplusClsType(paramType);
+               if (!paramTypeRet.equals(paramType)) 
+               // Just return if it is an enum type
+               // Type will still be the same if it's not an enum type
+                       return paramTypeRet;
+
+               // Check for array declaration
+               if (param.contains("[]")) {
+                       paramTypeRet = getSimpleArrayType(paramType) + "*";
+               } else if (paramType.contains("<") && paramType.contains(">")) {
+                       // Just return it as is if it's not an array
+                       String type = paramType.split("<")[1].split(">")[0];
+                       paramTypeRet = "vector<" + getGenericType(type) + ">";
+               } else
+                       paramTypeRet = paramType;
+
+               return paramTypeRet;
+       }
+
+
        // Detect array declaration, e.g. int A[],
        //              then generate type "int[]"
        private String checkAndGetArray(String paramType, String param) {
@@ -2578,17 +5267,18 @@ public class IoTCompiler {
        private boolean isArrayOrList(String paramType, String param) {
 
                // Check for array declaration
-               if (isArray(paramType, param))
+               if (isArray(param))
                        return true;
-               else if (isList(paramType, param))
+               else if (isList(paramType))
                        return true;
                else
                        return false;
        }
 
 
-       // Is array or list?
-       private boolean isArray(String paramType, String param) {
+       // Is array? 
+       // For return type we use retType as input parameter
+       private boolean isArray(String param) {
 
                // Check for array declaration
                if (param.contains("[]"))
@@ -2598,8 +5288,8 @@ public class IoTCompiler {
        }
 
 
-       // Is array or list?
-       private boolean isList(String paramType, String param) {
+       // Is list?
+       private boolean isList(String paramType) {
 
                // Check for array declaration
                if (paramType.contains("List"))
@@ -2610,15 +5300,14 @@ public class IoTCompiler {
 
 
        // Get the right type for a callback object
-       private String checkAndGetParamClass(String paramType, boolean needPtr) {
+       private String checkAndGetParamClass(String paramType) {
 
                // Check if this is generics
                if(getParamCategory(paramType) == ParamCategory.USERDEFINED) {
-                       // If true then return with pointer (C++)
-                       if (needPtr)
-                               return exchangeParamType(paramType) + "*";
-                       else    // Java, so no pointer needed
-                               return exchangeParamType(paramType);
+                       return exchangeParamType(paramType);
+               } else if (isList(paramType) &&
+                               (getParamCategory(getGenericType(paramType)) == ParamCategory.USERDEFINED)) {
+                       return "List<" + exchangeParamType(getGenericType(paramType)) + ">";
                } else
                        return paramType;
        }
@@ -2642,7 +5331,7 @@ public class IoTCompiler {
                        } else {
                                throw new Error("IoTCompiler: Ambiguous stub interfaces: " + setExchInt.toString() + 
                                        ". Only one new interface can be declared if the object " + intface +
-                                       " needs to be passed in as an input parameter!");
+                                       " needs to be passed in as an input parameter!\n");
                        }
                } else {
                // NULL value - this means policy files missing
@@ -2651,7 +5340,7 @@ public class IoTCompiler {
                                " If this is an array please type the brackets after the variable name," +
                                " e.g. \"String str[]\", not \"String[] str\"." +
                                " If this is a Collections (Java) / STL (C++) type, this compiler only" +
-                               " supports List/ArrayList (Java) or list (C++).");
+                               " supports List/ArrayList (Java) or list (C++).\n");
                }
        }
 
@@ -2688,12 +5377,16 @@ public class IoTCompiler {
 
                        // Generate everything if we don't see "-java" or "-cplus"
                        if (i == args.length) {
+                               comp.generateEnumJava();
+                               comp.generateStructJava();
                                comp.generateJavaLocalInterfaces();
                                comp.generateJavaInterfaces();
                                comp.generateJavaStubClasses();
                                comp.generateJavaCallbackStubClasses();
                                comp.generateJavaSkeletonClass();
                                comp.generateJavaCallbackSkeletonClass();
+                               comp.generateEnumCplus();
+                               comp.generateStructCplus();
                                comp.generateCplusLocalInterfaces();
                                comp.generateCPlusInterfaces();
                                comp.generateCPlusStubClasses();
@@ -2706,14 +5399,16 @@ public class IoTCompiler {
                                        // Error checking
                                        if (!args[i].equals("-java") &&
                                                !args[i].equals("-cplus")) {
-                                               throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i]);
+                                               throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i] + "\n");
                                        } else {
                                                if (i + 1 < args.length) {
                                                        comp.setDirectory(args[i+1]);
                                                } else
-                                                       throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i]);
+                                                       throw new Error("IoTCompiler: ERROR - please provide <directory> after option: " + args[i] + "\n");
 
                                                if (args[i].equals("-java")) {
+                                                       comp.generateEnumJava();
+                                                       comp.generateStructJava();
                                                        comp.generateJavaLocalInterfaces();
                                                        comp.generateJavaInterfaces();
                                                        comp.generateJavaStubClasses();
@@ -2721,6 +5416,8 @@ public class IoTCompiler {
                                                        comp.generateJavaSkeletonClass();
                                                        comp.generateJavaCallbackSkeletonClass();
                                                } else {
+                                                       comp.generateEnumCplus();
+                                                       comp.generateStructCplus();
                                                        comp.generateCplusLocalInterfaces();
                                                        comp.generateCPlusInterfaces();
                                                        comp.generateCPlusStubClasses();
@@ -2735,11 +5432,9 @@ public class IoTCompiler {
                } else {
                // Need to at least have exactly 2 parameters, i.e. main policy file and requires file
                        IoTCompiler.printUsage();
-                       throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!");
+                       throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!\n");
                }
        }
 }
 
 
-
-