Adding skeleton generation for Java and C++; excluding specific features for now...
authorrtrimana <rtrimana@uci.edu>
Fri, 11 Nov 2016 18:10:07 +0000 (10:10 -0800)
committerrtrimana <rtrimana@uci.edu>
Fri, 11 Nov 2016 18:10:07 +0000 (10:10 -0800)
config/iotpolicy/camerapolicy.pol
iotjava/iotpolicy/IoTCompiler.java
iotjava/iotrmi/Java/sample/TestClass_Skeleton.java

index d85eb53ce25f061402294d7481e73584c6128d8f..ac644307695abb5db8ab6274fd3334742a42d820 100644 (file)
@@ -6,7 +6,6 @@ public interface Camera {
     public String MethodC(String E, List<Integer> F);
     public float MethodD(List<String> G, float H);
     public boolean MethodE(String I, boolean J);
-       public void MethodF(LightBulb K);
 
        capability ImageCapture {
                description = "The quick brown fox jumps over the smart dog";
index 90734e3f5dde470427cbd19757be160c3be9086a..3dbc21f297ecad90da6a589d3945aadcab9d5c2b 100644 (file)
@@ -51,7 +51,8 @@ public class IoTCompiler {
        private Map<String,String> mapNonPrimitivesJava;
        private Map<String,String> mapNonPrimitivesCplus;
        // Other data structures
-       private Map<String,Integer> mapIntfaceObjId;    // Maps interface name to object Id
+       private Map<String,Integer> mapIntfaceObjId;            // Maps interface name to object Id
+       private Map<String,Integer> mapNewIntfaceObjId;         // Maps new interface name to its object Id (keep track of stubs)
        private PrintWriter pw;
        private String dir;
        private String subdir;
@@ -77,6 +78,7 @@ public class IoTCompiler {
                mapIntDeclHand = new HashMap<String,DeclarationHandler>();
                mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
                mapIntfaceObjId = new HashMap<String,Integer>();
+               mapNewIntfaceObjId = new HashMap<String,Integer>();
                mapPrimitives = new HashMap<String,String>();
                        arraysToMap(mapPrimitives, IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
                mapNonPrimitivesJava = new HashMap<String,String>();
@@ -119,7 +121,6 @@ public class IoTCompiler {
                mapIntDeclHand.put(origInt, decHandler);
                // Set object Id counter to 0 for each interface
                mapIntfaceObjId.put(origInt, new Integer(0));
-               //System.out.println("\nInterface: " + origInt + "\n\n");
        }
 
 
@@ -263,7 +264,7 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesJavaStub() writes the properties of the stub class
         */
-       private void writePropertiesJavaStub(String intface) {
+       private void writePropertiesJavaStub(String intface, String newIntface) {
 
                println("private IoTRMICall rmiCall;");
                //println("private IoTRMIObject rmiObj;");
@@ -272,6 +273,7 @@ public class IoTCompiler {
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("private final static int objectId = " + objId + ";");
+               mapNewIntfaceObjId.put(newIntface, objId);
                mapIntfaceObjId.put(intface, objId++);
                println("\n");
        }
@@ -389,10 +391,10 @@ public class IoTCompiler {
                                List<String> stdImportClasses = getStandardJavaImportClasses();
                                List<String> allImportClasses = getAllImportClasses(stdImportClasses, importClasses);
                                printImportStatements(allImportClasses); println("");
-                               // Write interface header
+                               // Write class header
                                println("public class " + newStubClass + " implements " + newIntface + " {\n");
                                // Write properties
-                               writePropertiesJavaStub(intface);
+                               writePropertiesJavaStub(intface, newIntface);
                                // Write constructor
                                writeConstructorJavaStub(newStubClass);
                                // Write methods
@@ -405,6 +407,241 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writePropertiesJavaSkeleton() writes the properties of the skeleton class
+        */
+       private void writePropertiesJavaSkeleton(String intface) {
+
+               println("private " + intface + " mainObj;");
+               //println("private int ports;");
+               //println("private IoTRMICall rmiCall;");
+               println("private IoTRMIObject rmiObj;\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);
+               }
+               println("\n");
+       }
+
+
+       /**
+        * HELPER: writeConstructorJavaSkeleton() writes the constructor of the skeleton class
+        */
+       private void writeConstructorJavaSkeleton(String newSkelClass, String intface) {
+
+               println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
+               println("mainObj = _mainObj;");
+               println("rmiObj = new IoTRMIObject(_port);");
+               //println("set0Allowed = Arrays.asList(object0Permission);");
+               println("___waitRequestInvokeMethod();");
+               println("}\n");
+       }
+
+
+       /**
+        * HELPER: writeStdMethodBodyJavaSkeleton() writes the standard method body in the skeleton class
+        */
+       private void writeStdMethodBodyJavaSkeleton(List<String> methParams, String methodId, String methodType) {
+
+               if (methodType.equals("void"))
+                       print("mainObj." + methodId + "(");
+               else
+                       print("return mainObj." + methodId + "(");
+               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(", ");
+                       }
+               }
+               println(");");
+       }
+
+
+       /**
+        * HELPER: writeMethodJavaSkeleton() writes the method of the skeleton class
+        */
+       private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       String methodId = intDecl.getMethodId(method);
+                       print("public " + intDecl.getMethodType(method) + " " + methodId + "(");
+                       for (int i = 0; i < methParams.size(); i++) {
+
+                               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 skeleton!
+                       writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method));
+                       println("}\n");
+               }
+       }
+
+
+       /**
+        * 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) {
+               // Generate array of parameter objects
+               print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { ");
+               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(", ");
+               }
+               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(" });");
+               // 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++) {
+                       String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                       print("(" + paramType + ") paramObj[" + i + "]");
+                       if (i != methParams.size() - 1)
+                               print(", ");
+               }
+               println(");");
+               if (!retType.equals("void"))
+                       println("rmiObj.sendReturnObj(retObj);");
+       }
+
+
+       /**
+        * HELPER: writeMethodHelperJavaSkeleton() writes the method helper of the skeleton class
+        */
+       private void writeMethodHelperJavaSkeleton(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("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);
+                       println("}\n");
+               }
+       }
+
+
+       /**
+        * HELPER: writeJavaWaitRequestInvokeMethod() writes the main loop of the skeleton class
+        */
+       private void writeJavaWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl) {
+
+               // 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
+               println("while (true) {");
+               println("rmiObj.getMethodBytes();");
+               println("int _objectId = rmiObj.getObjectId();");
+               println("int methodId = rmiObj.getMethodId();");
+               // TODO: code the permission check here!
+               println("switch (methodId) {");
+               // Print methods and method Ids
+               for (String method : methods) {
+                       String methodId = intDecl.getMethodId(method);
+                       int methodNumId = intDecl.getMethodNumId(method);
+                       print("case " + methodNumId + ": ___");
+                       String helperMethod = methodId;
+                       if (uniqueMethodIds.contains(methodId))
+                               helperMethod = helperMethod + methodNumId;
+                       else
+                               uniqueMethodIds.add(methodId);
+                       println(helperMethod + "(); break;");
+               }
+               println("default: ");
+               println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
+               println("}");
+               println("}");
+               println("}\n");
+       }
+
+
+       /**
+        * generateJavaSkeletonClass() generate skeletons based on the methods list in Java
+        */
+       public void generateJavaSkeletonClass() throws IOException {
+
+               // Create a new directory
+               createDirectory(dir);
+               for (String intface : mapIntfacePTH.keySet()) {
+                       // Open a new file to write into
+                       String newSkelClass = intface + "_Skeleton";
+                       FileWriter fw = new FileWriter(dir + "/" + newSkelClass + ".java");
+                       pw = new PrintWriter(new BufferedWriter(fw));
+                       // Pass in set of methods and get import classes
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                       List<String> methods = intDecl.getMethods();
+                       Set<String> importClasses = getImportClasses(methods, intDecl);
+                       List<String> stdImportClasses = getStandardJavaImportClasses();
+                       List<String> allImportClasses = getAllImportClasses(stdImportClasses, importClasses);
+                       printImportStatements(allImportClasses);
+                       // Write class header
+                       println("");
+                       println("public class " + newSkelClass  + " implements " + intface + " {\n");
+                       // Write properties
+                       writePropertiesJavaSkeleton(intface);
+                       // Write constructor
+                       writeConstructorJavaSkeleton(newSkelClass, intface);
+                       // Write methods
+                       writeMethodJavaSkeleton(methods, intDecl);
+                       // Write method helper
+                       writeMethodHelperJavaSkeleton(methods, intDecl);
+                       // Write waitRequestInvokeMethod() - main loop
+                       writeJavaWaitRequestInvokeMethod(methods, intDecl);
+                       println("}");
+                       pw.close();
+                       System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java...");
+               }
+       }
+
+
        /**
         * HELPER: writeMethodCplusInterface() writes the method of the interface
         */
@@ -553,12 +790,14 @@ public class IoTCompiler {
                println("int numParam = " + methParams.size() + ";");
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
                String retType = intDecl.getMethodType(method);
-               println("string retType = \"" + checkAndGetCplusType(retType) + "\";");
+               String retTypeC = checkAndGetCplusType(retType);
+               println("string retType = \"" + checkAndGetCplusArrayType(retTypeC) + "\";");
                // Generate array of parameter types
                print("string paramCls[] = { ");
                for (int i = 0; i < methParams.size(); i++) {
-                       String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                       print("\"" + getSimpleType(paramType) + "\"");
+                       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(", ");
@@ -594,7 +833,7 @@ public class IoTCompiler {
        /**
         * HELPER: writePropertiesCplusStub() writes the properties of the stub class
         */
-       private void writePropertiesCplusStub(String intface) {
+       private void writePropertiesCplusStub(String intface, String newIntface) {
 
                println("IoTRMICall\t\t\t*rmiCall;");
                //println("IoTRMIObject\t\t\t*rmiObj;");
@@ -603,6 +842,7 @@ public class IoTCompiler {
                // Get the object Id
                Integer objId = mapIntfaceObjId.get(intface);
                println("const static int\tobjectId = " + objId + ";");
+               mapNewIntfaceObjId.put(newIntface, objId);
                mapIntfaceObjId.put(intface, objId++);
                println("\n");
        }
@@ -662,7 +902,7 @@ public class IoTCompiler {
                                println("using namespace std;"); println("");
                                println("class " + newStubClass + " : public " + newIntface); println("{");
                                println("private:\n");
-                               writePropertiesCplusStub(intface);
+                               writePropertiesCplusStub(intface, newIntface);
                                println("public:\n");
                                // Add default constructor and destructor
                                println(newStubClass + "() { }"); println("");
@@ -681,6 +921,285 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writePropertiesCplusSkeleton() writes the properties of the skeleton class
+        */
+       private void writePropertiesCplusSkeleton(String intface) {
+
+               println(intface + " *mainObj;");
+               //println("private int ports;");
+               //println("private IoTRMICall rmiCall;");
+               println("IoTRMIObject *rmiObj;\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("const static int object" + newObjectId + "Id = " + 
+//                             newObjectId + ";\t//" + newIntface);
+               }
+               // 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) {
+
+               println(newSkelClass + "(" + intface + " *_mainObj, int _port) {");
+               println("bool _bResult = false;");
+               println("mainObj = _mainObj;");
+               println("rmiObj = new IoTRMIObject(_port, &_bResult);");
+               //println("set0Allowed = Arrays.asList(object0Permission);");
+               //println("___waitRequestInvokeMethod();");
+               println("}\n");
+       }
+
+
+       /**
+        * HELPER: writeDeconstructorCplusSkeleton() writes the deconstructor of the skeleton class
+        */
+       private void writeDeconstructorCplusSkeleton(String newSkelClass) {
+
+               println("~" + newSkelClass + "() {");
+               println("if (rmiObj != NULL) {");
+               println("delete rmiObj;");
+               println("rmiObj = NULL;");
+               println("}");
+               println("}");
+               println("");
+               // Check if this is callback!!! and print "delete rmiObj and vecCBObj"
+       }
+
+
+       /**
+        * HELPER: writeStdMethodBodyCplusSkeleton() writes the standard method body in the skeleton class
+        */
+       private void writeStdMethodBodyCplusSkeleton(List<String> methParams, String methodId, String methodType) {
+
+               if (methodType.equals("void"))
+                       print("mainObj->" + methodId + "(");
+               else
+                       print("return mainObj->" + methodId + "(");
+               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(", ");
+                       }
+               }
+               println(");");
+       }
+
+
+       /**
+        * HELPER: writeMethodCplusSkeleton() writes the method of the skeleton class
+        */
+       private void writeMethodCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+
+               for (String method : methods) {
+
+                       List<String> methParams = intDecl.getMethodParams(method);
+                       List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+                       String methodId = intDecl.getMethodId(method);
+                       String methodType = checkAndGetCplusType(intDecl.getMethodType(method));
+                       print(methodType + " " + methodId + "(");
+                       for (int i = 0; i < methParams.size(); i++) {
+                               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)
+                               if (i != methParams.size() - 1) {
+                                       print(", ");
+                               }
+                       }
+                       println(") {");
+                       // Now, write the body of skeleton!
+                       writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method));
+                       println("}\n");
+               }
+       }
+
+
+       /**
+        * 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) {
+
+               // 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 + "\"");
+                       if (i != methParams.size() - 1) {
+                               print(", ");
+                       }
+               }
+               println(" };");
+               println("int numParam = " + methParams.size() + ";");
+               // 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 + ";");
+               }
+               // Generate array of parameter objects
+               print("void* paramObj[] = { ");
+               for (int i = 0; i < methParams.size(); i++) {
+                       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(", ");
+                               }
+                       }
+                       println(");");
+               } else { // We do have a return value
+                       print(checkAndGetCplusType(retType) + " retVal = " + methodId + "(");
+                       for (int i = 0; i < methParams.size(); i++) {
+                               print(getSimpleIdentifier(methParams.get(i)));
+                               if (i != methParams.size() - 1) {
+                                       print(", ");
+                               }
+                       }
+                       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
+        */
+       private void writeMethodHelperCplusSkeleton(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");
+               }
+       }
+
+
+       /**
+        * HELPER: writeCplusWaitRequestInvokeMethod() writes the main loop of the skeleton class
+        */
+       private void writeCplusWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl) {
+
+               // 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
+               println("while (true) {");
+               println("rmiObj->getMethodBytes();");
+               println("int _objectId = rmiObj->getObjectId();");
+               println("int methodId = rmiObj->getMethodId();");
+               // TODO: code the permission check here!
+               println("switch (methodId) {");
+               // Print methods and method Ids
+               for (String method : methods) {
+                       String methodId = intDecl.getMethodId(method);
+                       int methodNumId = intDecl.getMethodNumId(method);
+                       print("case " + methodNumId + ": ___");
+                       String helperMethod = methodId;
+                       if (uniqueMethodIds.contains(methodId))
+                               helperMethod = helperMethod + methodNumId;
+                       else
+                               uniqueMethodIds.add(methodId);
+                       println(helperMethod + "(); break;");
+               }
+               println("default: ");
+               println("cerr << \"Method Id \" << methodId << \" not recognized!\" << endl;");
+               println("throw exception();");
+               println("}");
+               println("}");
+               println("}\n");
+       }
+
+
+       /**
+        * generateCplusSkeletonClass() generate skeletons based on the methods list in C++
+        */
+       public void generateCplusSkeletonClass() throws IOException {
+
+               // Create a new directory
+               createDirectory(dir);
+               for (String intface : mapIntfacePTH.keySet()) {
+                       // Open a new file to write into
+                       String newSkelClass = intface + "_Skeleton";
+                       FileWriter fw = new FileWriter(dir + "/" + newSkelClass + ".hpp");
+                       pw = new PrintWriter(new BufferedWriter(fw));
+                       // Write file headers
+                       println("#ifndef _" + newSkelClass.toUpperCase() + "_HPP__");
+                       println("#define _" + newSkelClass.toUpperCase() + "_HPP__");
+                       println("#include <iostream>");
+                       println("#include \"" + intface + ".hpp\"\n");
+                       // Pass in set of methods and get import classes
+                       DeclarationHandler decHandler = mapIntDeclHand.get(intface);
+                       InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
+                       List<String> methods = intDecl.getMethods();
+                       Set<String> includeClasses = getIncludeClasses(methods, intDecl);
+                       List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
+                       List<String> allIncludeClasses = getAllImportClasses(stdIncludeClasses, includeClasses);
+                       printIncludeStatements(allIncludeClasses); println("");                 
+                       println("using namespace std;\n");
+                       // Write class header
+                       println("class " + newSkelClass + " : public " + intface); println("{");
+                       println("private:\n");
+                       // Write properties
+                       writePropertiesCplusSkeleton(intface);
+                       println("public:\n");
+                       // Write constructor
+                       writeConstructorCplusSkeleton(newSkelClass, intface);
+                       // Write deconstructor
+                       writeDeconstructorCplusSkeleton(newSkelClass);
+                       // Write methods
+                       writeMethodCplusSkeleton(methods, intDecl);
+                       // Write method helper
+                       writeMethodHelperCplusSkeleton(methods, intDecl);
+                       // Write waitRequestInvokeMethod() - main loop
+                       writeCplusWaitRequestInvokeMethod(methods, intDecl);
+                       println("};");
+                       println("#endif");
+                       pw.close();
+                       System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".hpp...");
+               }
+       }
+
+
        /**
         * generateInitializer() generate initializer based on type
         */
@@ -1101,6 +1620,47 @@ public class IoTCompiler {
 
                return paramComplete;
        }
+       
+
+       // Detect array declaration, e.g. int A[],
+       //              then generate "int A[]" in C++ as "vector<int> A"
+       // This method just returns the type
+       private String checkAndGetCplusArrayType(String paramType) {
+
+               String paramTypeRet = null;
+               // Check for array declaration
+               if (paramType.contains("[]")) {
+                       String type = paramType.split("\\[\\]")[0];
+                       paramTypeRet = checkAndGetCplusType(type) + "[]";
+               } else if (paramType.contains("vector")) {
+                       // Just return it as is if it's not an array
+                       String type = paramType.split("<")[1].split(">")[0];
+                       paramTypeRet = checkAndGetCplusType(type) + "[]";
+               } else
+                       paramTypeRet = paramType;
+
+               return paramTypeRet;
+       }
+       
+       
+       // Detect array declaration, e.g. int A[],
+       //              then generate "int A[]" in C++ as "vector<int> A"
+       // This method just returns the type
+       private String checkAndGetCplusArrayType(String paramType, String param) {
+
+               String paramTypeRet = null;
+               // Check for array declaration
+               if (param.contains("[]")) {
+                       paramTypeRet = checkAndGetCplusType(paramType) + "[]";
+               } else if (paramType.contains("vector")) {
+                       // Just return it as is if it's not an array
+                       String type = paramType.split("<")[1].split(">")[0];
+                       paramTypeRet = checkAndGetCplusType(type) + "[]";
+               } else
+                       paramTypeRet = paramType;
+
+               return paramTypeRet;
+       }
 
 
        // Detect array declaration, e.g. int A[],
@@ -1202,9 +1762,11 @@ public class IoTCompiler {
                                comp.generateJavaLocalInterfaces();
                                comp.generateJavaInterfaces();
                                comp.generateJavaStubClasses();
+                               comp.generateJavaSkeletonClass();
                                comp.generateCplusLocalInterfaces();
                                comp.generateCPlusInterfaces();
                                comp.generateCPlusStubClasses();
+                               comp.generateCplusSkeletonClass();
                        } else {
                        // Check other options
                                while(i < args.length) {
@@ -1222,10 +1784,12 @@ public class IoTCompiler {
                                                        comp.generateJavaLocalInterfaces();
                                                        comp.generateJavaInterfaces();
                                                        comp.generateJavaStubClasses();
+                                                       comp.generateJavaSkeletonClass();
                                                } else {
                                                        comp.generateCplusLocalInterfaces();
                                                        comp.generateCPlusInterfaces();
                                                        comp.generateCPlusStubClasses();
+                                                       comp.generateCplusSkeletonClass();
                                                }
                                        }
                                        i = i + 2;
index 5958f1f347aa60691bc311ff1a82905ee747a050..822e0644c318090528f08697baf85a3b6f9a2198 100644 (file)
@@ -2,13 +2,8 @@ package iotrmi.Java.sample;
 
 import java.io.IOException;
 import java.util.Arrays;
-import java.util.Set;
-import java.util.Map;
-import java.util.HashMap;
 
 import java.util.List;
-import java.util.ArrayList;
-import java.util.Collections;
 
 import iotrmi.Java.IoTRMIObject;
 import iotrmi.Java.IoTRMICall;
@@ -18,10 +13,12 @@ public class TestClass_Skeleton implements TestClassInterface {
        /**
         * Class Constants
         */
-       private static int objIdCnt = 0; // Counter for callback object Ids
        private TestClassInterface tc;
        private int port;
        private IoTRMIObject rmiObj;
+
+       // Callback stuff
+       private static int objIdCnt = 0; // Counter for callback object Ids
        private IoTRMICall rmiCall;
        private CallBackInterface cbstub;
 
@@ -329,7 +326,7 @@ public class TestClass_Skeleton implements TestClassInterface {
                                case 11: structSize1 = ___structSize(); break;
                                case 12: ___handleEnum(); break;
                                default:
-                                       throw new Error("Method Id not recognized!");
+                                       throw new Error("Method Id " + methodId + " not recognized!");
                        }
 
                }