- /**
- * HELPER: writePropertiesCplusCallbackSkeleton() writes the properties of the callback skeleton class
- */
- private void writePropertiesCplusCallbackSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
-
- println(intface + " *mainObj;");
- // Keep track of object Ids of all stubs registered to this interface
- 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");
- }
-
-
- /**
- * HELPER: writeConstructorCplusCallbackSkeleton() writes the constructor of the skeleton class
- */
- private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
-
- println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _objectId) {");
- println("mainObj = _mainObj;");
- println("callbackAddress = _callbackAddress;");
- println("objectId = _objectId;");
- println("}\n");
- }
-
-
- /**
- * 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,
- 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(", 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 request invoke method of the skeleton callback class
- */
- 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) {");
- // 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);
- 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();");
- println("}");
- println("}\n");
- }
-
-
- /**
- * generateCplusCallbackSkeletonClass() generate callback skeletons based on the methods list in C++
- */
- public void generateCplusCallbackSkeletonClass() throws IOException {
-
- // Create a new directory
- String path = createDirectories(dir, subdir);
- for (String intface : mapIntfacePTH.keySet()) {
- // Open a new file to write into
- String newSkelClass = intface + "_CallbackSkeleton";
- FileWriter fw = new FileWriter(path + "/" + 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, intface, true);
- List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
- 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, callbackExist, callbackClasses);
- println("public:\n");
- // Write constructor
- writeConstructorCplusCallbackSkeleton(newSkelClass, intface, callbackExist, intDecl, methods);
- // Write deconstructor
- writeDeconstructorCplusCallbackSkeleton(newSkelClass, callbackExist, callbackClasses);
- // Write methods
- writeMethodCplusSkeleton(methods, intDecl, callbackClasses, true, intface);
- // Write method helper
- writeMethodHelperCplusCallbackSkeleton(methods, intDecl, callbackClasses);
- // Write waitRequestInvokeMethod() - main loop
- writeCplusCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist);
- println("};");
- writeObjectIdCountInitializationCplus(newSkelClass, callbackExist);
- println("#endif");
- pw.close();
- System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".hpp...");
- }
- }
-
-