- /**
- * HELPER: writePropertiesJavaCallbackStub() writes the properties of the callback stub class
- */
- 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 int objectId = 0;");
- 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("private IoTRMIObject rmiObj;");
- println("List<" + callbackType + "> listCallbackObj;");
- println("private static 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: writeConstructorJavaCallbackStub() writes the constructor of the callback stub class
- */
- private void writeConstructorJavaCallbackStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
-
- // 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");
- }
-
-
- /**
- * generateJavaCallbackStubClasses() generate callback stubs based on the methods list in Java
- * <p>
- * Callback stubs gets the IoTRMICall objects from outside of the class as contructor input
- * because all these stubs are populated by the class that takes in this object as a callback
- * object. In such a class, we only use one socket, hence one IoTRMICall, for all callback objects.
- */
- public void generateJavaCallbackStubClasses() 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();
- String newStubClass = newIntface + "_CallbackStub";
- FileWriter fw = new FileWriter(path + "/" + newStubClass + ".java");
- pw = new PrintWriter(new BufferedWriter(fw));
- DeclarationHandler decHandler = mapIntDeclHand.get(intface);
- InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
- // Pass in set of methods and get import classes
- Set<String> methods = intMeth.getValue();
- Set<String> importClasses = getImportClasses(methods, intDecl);
- List<String> stdImportClasses = getStandardJavaImportClasses();
- List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
- printImportStatements(allImportClasses); println("");
- // Find out if there are callback objects
- Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
- boolean callbackExist = !callbackClasses.isEmpty();
- // Write class header
- println("public class " + newStubClass + " implements " + newIntface + " {\n");
- // Write properties
- writePropertiesJavaCallbackStub(intface, newIntface, callbackExist, callbackClasses);
- // Write constructor
- writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
- // Write methods
- // TODO: perhaps need to generate callback for callback
- writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass);
- println("}");
- pw.close();
- System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
- }
- }
- }
-
-