Cleaning up code for runtime, installer, RMI, compiler for the Java side
[iot2.git] / iotjava / iotpolicy / IoTCompiler.java
index f127cdaf833f5d637d5e1847daa6ac1b3a2c44f2..58538c9c00d4455840045e03ac306452c36c4795 100644 (file)
@@ -61,6 +61,7 @@ public class IoTCompiler {
        private String subdir;
        private Map<String,Integer> mapPortCount;       // Counter for ports
        private static int portCount = 0;
+       private static int countObjId = 1;                      // Always increment object Id for a new stub/skeleton
        private String mainClass;
 
 
@@ -143,7 +144,7 @@ public class IoTCompiler {
                mapIntfacePTH.put(origInt, ptHandler);
                mapIntDeclHand.put(origInt, decHandler);
                // Set object Id counter to 0 for each interface
-               mapIntfaceObjId.put(origInt, new Integer(0));
+               mapIntfaceObjId.put(origInt, countObjId++);
        }
 
 
@@ -363,9 +364,9 @@ public class IoTCompiler {
         */
        private void updateIntfaceObjIdMap(String intface, String newIntface) {
 
+               // We are assuming that we only generate one stub per one skeleton at this point @Feb 2017
                Integer objId = mapIntfaceObjId.get(intface);
                mapNewIntfaceObjId.put(newIntface, objId);
-               mapIntfaceObjId.put(intface, objId++);
        }
 
 
@@ -416,8 +417,6 @@ public class IoTCompiler {
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
                        int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("private final static int object" + newObjectId + "Id = " + 
-                               newObjectId + ";\t//" + newIntface);
                        Set<String> methodIds = intMeth.getValue();
                        print("private static Integer[] object" + newObjectId + "Permission = { ");
                        int i = 0;
@@ -520,42 +519,6 @@ public class IoTCompiler {
        }
 
 
-       /**
-        * HELPER: writeJavaMethodCallbackPermission() writes permission checks in stub for callbacks
-        */
-       private void writeJavaMethodCallbackPermission(String intface) {
-
-               println("int methodId = IoTRMIObject.getMethodId(method);");
-               // Get all the different stubs
-               Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-               for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                       String newIntface = intMeth.getKey();
-                       int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
-                       println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);");
-                       println("}");
-               }
-       }
-
-
-       /**
-        * HELPER: writeJavaInitCallbackPermission() writes the permission for callback
-        */
-       private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) {
-
-               if (callbackExist) {
-                       String method = "___initCallBack()";
-                       int methodNumId = intDecl.getHelperMethodNumId(method);
-                       Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-                       for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                               String newIntface = intMeth.getKey();
-                               int newObjectId = getNewIntfaceObjectId(newIntface);
-                               println("set" + newObjectId + "Allowed.add(" + methodNumId + ");");
-                       }
-               }
-       }
-
-
        /**
         * HELPER: getPortCount() gets port count for different stubs and skeletons
         */
@@ -859,7 +822,6 @@ public class IoTCompiler {
                                println("retClsVal[retPos++] = null;");
                        }
                }
-               //println("Object[] retActualObj = rmiComm.getStructObjects(retCls, retClsVal);");
                // Handle the actual returned struct
                writeWaitForReturnValueJava(method, intDecl, "Object[] retActualObj = rmiComm.getStructObjects(retCls, retClsVal);");
                if (isArray(retType)) {                 // An array
@@ -1123,7 +1085,6 @@ public class IoTCompiler {
                        // Now, write the body of stub!
                        if (isCallbackMethod)
                                writeCallbackMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
-                       //else
                        writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
                        println("}\n");
                }
@@ -1191,7 +1152,8 @@ public class IoTCompiler {
        private void writePropertiesJavaSkeleton(String intface, InterfaceDecl intDecl) {
 
                println("private " + intface + " mainObj;");
-               println("private int objectId = 0;");
+               Integer objId = mapIntfaceObjId.get(intface);
+               println("private int objectId = " + objId + ";");
                println("// Communications and synchronizations");
                println("private IoTRMIComm rmiComm;");
                println("private AtomicBoolean didAlreadyInitWaitInvoke;");
@@ -1245,7 +1207,6 @@ public class IoTCompiler {
                println("rmiComm = new IoTRMICommServer(_portSend, _portRecv);");
                // Generate permission control initialization
                writeConstructorJavaPermission(intface);
-               //writeJavaInitCallbackPermission(intface, intDecl, callbackExist);
                writeStructPermissionJavaSkeleton(methods, intDecl, intface);
                println("IoTRMIUtil.mapSkel.put(_mainObj, this);");
                println("IoTRMIUtil.mapSkelId.put(_mainObj, objectId);");
@@ -1753,7 +1714,6 @@ public class IoTCompiler {
 
                // Generate array of parameter objects
                boolean isCallbackMethod = false;
-               //String callbackType = null;
                Set<String> callbackType = new HashSet<String>();
                println("byte[] localMethodBytes = methodBytes;");
                println("rmiComm.setGetMethodBytes();");
@@ -1805,7 +1765,6 @@ public class IoTCompiler {
 
                // Generate array of parameter objects
                boolean isCallbackMethod = false;
-               //String callbackType = null;
                Set<String> callbackType = new HashSet<String>();
                println("byte[] localMethodBytes = methodBytes;");
                println("rmiComm.setGetMethodBytes();");
@@ -1815,7 +1774,6 @@ public class IoTCompiler {
                        String paramType = returnGenericCallbackType(methPrmTypes.get(i));
                        if (callbackClasses.contains(paramType)) {
                                isCallbackMethod = true;
-                               //callbackType = paramType;
                                callbackType.add(paramType);
                                print("int[].class");
                        } else {        // Generate normal classes if it's not a callback object
@@ -1825,7 +1783,6 @@ public class IoTCompiler {
                        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++) {
@@ -2778,7 +2735,6 @@ public class IoTCompiler {
                                        println("1;");
                                }
                                println("void* paramObjStruct" + i + "[] = { &structLen" + i + " };");
-                               //println("void* retStructLen" + i + " = NULL;");
                                println("rmiComm->remoteCall(objectId, methodIdStruct" + i + 
                                                ", paramClsStruct" + i + ", paramObjStruct" + i + 
                                                ", numParam" + i + ");\n");
@@ -2958,7 +2914,6 @@ public class IoTCompiler {
                                println("retObj[retPos++] = &retParam" + i + ";");
                        }
                }
-               //println("rmiComm->getStructObjects(retCls, numRet, retObj);");
                writeWaitForReturnValueCplus(method, intDecl, "rmiComm->getStructObjects(retCls, numRet, retObj);");
                if (isArrayOrList(retType, retType)) {  // An array or list
                        println("vector<" + simpleType + "> structRet(retLen);");
@@ -3065,7 +3020,6 @@ public class IoTCompiler {
                for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
                        String newIntface = intMeth.getKey();
                        int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("int objectId = " + newObjectId + ";");
                        println("static set<int> set" + newObjectId + "Allowed;");
                }
        }       
@@ -3153,25 +3107,6 @@ public class IoTCompiler {
        }
 
 
-       /**
-        * HELPER: writeCplusMethodCallbackPermission() writes permission checks in stub for callbacks
-        */
-       private void writeCplusMethodCallbackPermission(String intface) {
-
-               println("int methodId = IoTRMIObject::getMethodId(method);");
-               // Get all the different stubs
-               Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
-               for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
-                       String newIntface = intMeth.getKey();
-                       int newObjectId = getNewIntfaceObjectId(newIntface);
-                       println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
-                       println("cerr << \"Callback object for " + intface + " is not allowed to access method: \" << methodId;");
-                       println("return;");
-                       println("}");
-               }
-       }
-
-
        /**
         * generateCPlusStubClassesHpp() generate stubs based on the methods list in C++ (.hpp file)
         */
@@ -3239,11 +3174,8 @@ public class IoTCompiler {
                println(newStubClass + "* obj = (" + newStubClass + "*) t;");
                println("delete obj;");
                println("}\n");
-               //println("extern \"C\" void init" + newStubClass + "(void* t) {");
-               //println(newStubClass + "* obj = (" + newStubClass + "*) t;");
-               //println("obj->init();");
-               //println("while(true);");
-               //println("}\n");
+               println("extern \"C\" void init" + newStubClass + "(void* t) {");
+               println("}\n");
        }
 
 
@@ -3280,7 +3212,6 @@ public class IoTCompiler {
                                }
                                println("using namespace std;"); println("");
                                // Add default constructor and destructor
-                               //println(newStubClass + "() { }"); println("");
                                writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses, methods, intDecl);
                                writeCallbackConstructorCplusStub(newStubClass, callbackExist, callbackClasses, methods, intDecl);
                                writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
@@ -3310,6 +3241,8 @@ public class IoTCompiler {
                println("IoTRMIComm *rmiComm;");
                println("char* methodBytes;");
                println("int methodLen;");
+               Integer objId = mapIntfaceObjId.get(intface);
+               println("int objectId = " + objId + ";");
                // Keep track of object Ids of all stubs registered to this interface
                writePropertiesCplusPermission(intface);
                println("// Synchronization variables");
@@ -3534,9 +3467,7 @@ public class IoTCompiler {
                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;");
                                println("vector<int> numStubIdArray" + i + ";");
                }
        }
@@ -3719,14 +3650,12 @@ public class IoTCompiler {
 
                // Generate array of parameter types
                boolean isCallbackMethod = false;
-               //String callbackType = null;
                Set<String> callbackType = new HashSet<String>();
                print("string paramCls[] = { ");
                for (int i = 0; i < methParams.size(); i++) {
                        String paramType = returnGenericCallbackType(methPrmTypes.get(i));
                        if (callbackClasses.contains(paramType)) {
                                isCallbackMethod = true;
-                               //callbackType = paramType;
                                callbackType.add(paramType);
                                print("\"int*\"");
                        } else {        // Generate normal classes if it's not a callback object
@@ -3933,9 +3862,7 @@ public class IoTCompiler {
                                String prmType = returnGenericCallbackType(methPrmTypes.get(i));
                                if (callbackClasses.contains(prmType)) {
                                        isCallbackMethod = true;
-                                       //callbackType = prmType;
                                        callbackType.add(prmType);
-                                       //println("int numStubs" + i + " = 0;");
                                        println("vector<int> numStubIdArray" + i + ";");
                                        println("paramCls[pos] = \"int*\";");
                                        println("paramObj[pos++] = &numStubIdArray" + i + ";");
@@ -4338,17 +4265,14 @@ public class IoTCompiler {
 
                println("extern \"C\" void* create" + newSkelClass + "(void** params) {");
                println("// Args: *_mainObj, int _portSend, int _portRecv");
-               println("return new " + newSkelClass + "((" + intface + "*) params[0], *((int*) params[0]), *((int*) params[1]));");
+               println("return new " + newSkelClass + "((" + intface + "*) params[0], *((int*) params[1]), *((int*) params[2]));");
                println("}\n");
                println("extern \"C\" void destroy" + newSkelClass + "(void* t) {");
                println(newSkelClass + "* obj = (" + newSkelClass + "*) t;");
                println("delete obj;");
                println("}\n");
-               //println("extern \"C\" void init" + newSkelClass + "(void* t) {");
-               //println(newSkelClass + "* obj = (" + newSkelClass + "*) t;");
-               //println("obj->init();");
-               //println("while(true);");
-               //println("}\n");
+               println("extern \"C\" void init" + newSkelClass + "(void* t) {");
+               println("}\n");
        }
 
 
@@ -4454,7 +4378,7 @@ public class IoTCompiler {
        public static void printUsage() {
 
                System.out.println();
-               System.out.println("Sentinel interface and stub compiler version 1.0");
+               System.out.println("Vigilia interface and stub compiler version 1.0");
                System.out.println("Copyright (c) 2015-2016 University of California, Irvine - Programming Language Group.");
                System.out.println("All rights reserved.");
                System.out.println("Usage:");
@@ -4602,14 +4526,8 @@ public class IoTCompiler {
        // Basically the compiler needs to parse the policy (and requires) files for callback class first
        private int getNewIntfaceObjectId(String newIntface) {
 
-//             if (!mapNewIntfaceObjId.containsKey(newIntface)) {
-//                     throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
-//                                                     "Please place the two files for callback class in front...\n");
-//                     return -1;
-//             } else {
-                       int retObjId = mapNewIntfaceObjId.get(newIntface);
-                       return retObjId;
-//             }
+               int retObjId = mapNewIntfaceObjId.get(newIntface);
+               return retObjId;
        }
 
 
@@ -4699,8 +4617,6 @@ public class IoTCompiler {
                // Add the standard list first
                importClasses.add("<vector>");
                importClasses.add("<set>");
-               //importClasses.add("\"IoTRMICall.hpp\"");
-               //importClasses.add("\"IoTRMIObject.hpp\"");
                importClasses.add("\"IoTRMIComm.hpp\"");
                importClasses.add("\"IoTRMICommClient.hpp\"");
                importClasses.add("\"IoTRMICommServer.hpp\"");