- private void writeMethodHelperJavaCallbackSkeleton(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(", "); 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 request invoke method of the callback skeleton class
- */
- private void writeJavaCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
-
- // Use this set to handle two same methodIds
- 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) {");
- // 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("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
- println("}");
- println("}\n");
- }
-
-
- /**
- * generateJavaCallbackSkeletonClass() generate callback skeletons based on the methods list in Java
- */
- public void generateJavaCallbackSkeletonClass() 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 + ".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 = getAllLibClasses(stdImportClasses, importClasses);
- printImportStatements(allImportClasses);
- // Find out if there are callback objects
- Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
- boolean callbackExist = !callbackClasses.isEmpty();
- // Write class header
- println("");
- println("public class " + newSkelClass + " implements " + intface + " {\n");
- // Write properties
- writePropertiesJavaCallbackSkeleton(intface, callbackExist);
- // Write constructor
- writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods);
- // Write methods
- writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true);
- // Write method helper
- writeMethodHelperJavaCallbackSkeleton(methods, intDecl, callbackClasses);
- // Write waitRequestInvokeMethod() - main loop
- writeJavaCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist);
- println("}");
- pw.close();
- System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".java...");
- }
- }
-
-
- /**
- * 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
- */
- private void writeMethodCplusInterface(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 = 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: generateEnumCplus() writes the enumeration declaration
- */
- public void generateEnumCplus() throws IOException {