From: rtrimana Date: Wed, 1 Feb 2017 23:37:27 +0000 (-0800) Subject: Fixed compiler for Java code generation (not heavily tested yet, but fixes include... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=3f0edb3f709c64c5e674e27bf83773da8a758d9c;p=iot2.git Fixed compiler for Java code generation (not heavily tested yet, but fixes include return value generation, enum, and struct handling) --- diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index a46a20b..030d856 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -190,6 +190,10 @@ public class IoTCompiler { mapInt2NewInts.put(origInt, mapNewIntMethods); } + +/*================ + * Java generation + *================/ /** * HELPER: writeMethodJavaLocalInterface() writes the method of the local interface @@ -431,28 +435,21 @@ public class IoTCompiler { /** * HELPER: writePropertiesJavaStub() writes the properties of the stub class */ - private void writePropertiesJavaStub(String intface, String newIntface, boolean callbackExist, Set callbackClasses) { + private void writePropertiesJavaStub(String intface, Set methods, InterfaceDecl intDecl) { - println("private IoTRMICall rmiCall;"); - println("private String callbackAddress;"); - println("private int[] ports;\n"); // Get the object Id Integer objId = mapIntfaceObjId.get(intface); - println("private final static int objectId = " + objId + ";"); - //mapNewIntfaceObjId.put(newIntface, objId); - //mapIntfaceObjId.put(intface, objId++); - 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 int objIdCnt = 0;"); - // Generate permission stuff for callback stubs - DeclarationHandler decHandler = mapIntDeclHand.get(callbackType); - InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType); - writePropertiesJavaPermission(callbackType, intDecl); + println("private int objectId = " + objId + ";"); + println("private IoTRMIComm rmiComm;"); + // Write the list of AtomicBoolean variables + println("// Synchronization variables"); + for (String method : methods) { + // Generate AtomicBooleans for methods that have return values + String returnType = intDecl.getMethodType(method); + int methodNumId = intDecl.getMethodNumId(method); + if (!returnType.equals("void")) { + println("private AtomicBoolean retValueReceived" + methodNumId + " = new AtomicBoolean(false);"); + } } println("\n"); } @@ -475,21 +472,40 @@ public class IoTCompiler { /** * HELPER: writeConstructorJavaStub() writes the constructor of the stub class */ - private void writeConstructorJavaStub(String intface, String newStubClass, boolean callbackExist, Set callbackClasses) { + private void writeConstructorJavaStub(String intface, String newStubClass, Set methods, InterfaceDecl intDecl) { - println("public " + newStubClass + "(int _port, String _skeletonAddress, String _callbackAddress, int _rev, int[] _ports) throws Exception {"); - println("callbackAddress = _callbackAddress;"); - println("ports = _ports;"); - println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev);"); - 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("public " + newStubClass + "(int _localPortSend, int _localPortRecv, int _portSend, int _portRecv, String _skeletonAddress, int _rev) throws Exception {"); + println("rmiComm = new IoTRMICommClient(_localPortSend, _localPortRecv, _portSend, _portRecv, _skeletonAddress, _rev);"); + // Register the AtomicBoolean variables + for (String method : methods) { + // Generate AtomicBooleans for methods that have return values + String returnType = intDecl.getMethodType(method); + int methodNumId = intDecl.getMethodNumId(method); + if (!returnType.equals("void")) { + println("rmiComm.registerStub(objectId, " + methodNumId + ", retValueReceived" + methodNumId + ");"); + } + } + println("IoTRMIUtil.mapStub.put(objectId, this);"); + println("}\n"); + } + + + /** + * HELPER: writeCallbackConstructorJavaStub() writes the callback constructor of the stub class + */ + private void writeCallbackConstructorJavaStub(String intface, String newStubClass, Set methods, InterfaceDecl intDecl) { + + println("public " + newStubClass + "(IoTRMIComm _rmiComm, int _objectId) throws Exception {"); + println("rmiComm = _rmiComm;"); + println("objectId = _objectId;"); + // Register the AtomicBoolean variables + for (String method : methods) { + // Generate AtomicBooleans for methods that have return values + String returnType = intDecl.getMethodType(method); + int methodNumId = intDecl.getMethodNumId(method); + if (!returnType.equals("void")) { + println("rmiComm.registerStub(objectId, " + methodNumId + ", retValueReceived" + methodNumId + ");"); + } } println("}\n"); } @@ -542,50 +558,6 @@ public class IoTCompiler { } - /** - * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub - */ - private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl, String newStubClass) { - - println("public void ___initCallBack() {"); - // Generate main thread for callbacks - println("Thread thread = new Thread() {"); - println("public void run() {"); - println("try {"); - int port = getPortCount(newStubClass); - println("rmiObj = new IoTRMIObject(ports[" + port + "]);"); - println("while (true) {"); - println("byte[] method = rmiObj.getMethodBytes();"); - println("int objId = IoTRMIObject.getObjectId(method);"); - println(intface + "_CallbackSkeleton skel = (" + intface + "_CallbackSkeleton) listCallbackObj.get(objId);"); - println("if (skel != null) {"); - writeJavaMethodCallbackPermission(intface); - println("skel.invokeMethod(rmiObj);"); - print("}"); - println(" else {"); - println("throw new Error(\"" + intface + ": Object with Id \" + objId + \" not found!\");"); - println("}"); - println("}"); - print("}"); - println(" catch (Exception ex) {"); - println("ex.printStackTrace();"); - println("throw new Error(\"Error instantiating class " + intface + "_CallbackSkeleton!\");"); - println("}"); - println("}"); - println("};"); - println("thread.start();\n"); - // Generate info sending part - String method = "___initCallBack()"; - int methodNumId = intDecl.getHelperMethodNumId(method); - println("int methodId = " + methodNumId + ";"); - println("Class retType = void.class;"); - println("Class[] paramCls = new Class[] { int[].class, String.class, int.class };"); - println("Object[] paramObj = new Object[] { ports, callbackAddress, 0 };"); - println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); - println("}\n"); - } - - /** * HELPER: checkAndWriteEnumTypeJavaStub() writes the enum type (convert from enum to int) */ @@ -622,8 +594,10 @@ public class IoTCompiler { /** * HELPER: checkAndWriteEnumRetTypeJavaStub() writes the enum return type (convert from enum to int) */ - private void checkAndWriteEnumRetTypeJavaStub(String retType) { + private void checkAndWriteEnumRetTypeJavaStub(String retType, String method, InterfaceDecl intDecl) { + // Write the wait-for-return-value part + writeWaitForReturnValue(method, intDecl, "Object retObj = rmiComm.getReturnValue(retType, null);"); // Strips off array "[]" for return type String pureType = getSimpleArrayType(getGenericType(retType)); // Take the inner type of generic @@ -679,9 +653,8 @@ public class IoTCompiler { } else { // Just one element println("Object[] paramObjStruct" + i + " = new Object[] { new Integer(1) };"); } - println("rmiCall.remoteCall(objectId, methodIdStruct" + i + - ", retTypeStruct" + i + ", null, paramClsStruct" + i + - ", paramObjStruct" + i + ");\n"); + println("rmiComm.remoteCall(objectId, methodIdStruct" + i + + ", paramClsStruct" + i + ", paramObjStruct" + i + ");\n"); } } } @@ -830,7 +803,7 @@ public class IoTCompiler { for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); print("structRet[i]." + getSimpleIdentifier(members.get(i))); - println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];"); + println(" = (" + getSimpleType(getEnumType(prmType)) + ") retActualObj[retObjPos++];"); } println("}"); } else if (isList(retType)) { // A list @@ -838,7 +811,7 @@ public class IoTCompiler { for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); print("structRetMem." + getSimpleIdentifier(members.get(i))); - println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];"); + println(" = (" + getSimpleType(getEnumType(prmType)) + ") retActualObj[retObjPos++];"); } println("structRet.add(structRetMem);"); println("}"); @@ -846,7 +819,7 @@ public class IoTCompiler { for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); print("structRet." + getSimpleIdentifier(members.get(i))); - println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];"); + println(" = (" + getSimpleType(getEnumType(prmType)) + ") retActualObj[retObjPos++];"); } } println("return structRet;"); @@ -856,12 +829,12 @@ public class IoTCompiler { /** * HELPER: writeStructReturnJavaStub() writes parameters if struct is present for return statement */ - private void writeStructReturnJavaStub(String simpleType, String retType) { + private void writeStructReturnJavaStub(String simpleType, String retType, String method, InterfaceDecl intDecl) { - // Handle the returned struct!!! - println("Object retLenObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); + // Handle the returned struct size + writeWaitForReturnValue(method, intDecl, "Object retObj = rmiComm.getReturnValue(retType, null);"); // Minimum retLen is 1 if this is a single struct object - println("int retLen = (int) retLenObj;"); + println("int retLen = (int) retObj;"); int numMem = getNumOfMembers(simpleType); println("Class[] retCls = new Class[" + numMem + "*retLen];"); println("Class[] retClsVal = new Class[" + numMem + "*retLen];"); @@ -885,7 +858,9 @@ public class IoTCompiler { println("retClsVal[retPos++] = null;"); } } - println("Object[] retObj = rmiCall.getStructObjects(retCls, retClsVal);"); + //println("Object[] retActualObj = rmiComm.getStructObjects(retCls, retClsVal);"); + // Handle the actual returned struct + writeWaitForReturnValue(method, intDecl, "Object[] retActualObj = rmiComm.getStructObjects(retCls, retClsVal);"); if (isArray(retType)) { // An array println(simpleType + "[] structRet = new " + simpleType + "[retLen];"); println("for(int i = 0; i < retLen; i++) {"); @@ -900,6 +875,20 @@ public class IoTCompiler { } + /** + * HELPER: writeWaitForReturnValue() writes the synchronization part for return values + */ + private void writeWaitForReturnValue(String method, InterfaceDecl intDecl, String getReturnValue) { + + println("// Waiting for return value"); + int methodNumId = intDecl.getMethodNumId(method); + println("while (!retValueReceived" + methodNumId + ".get());"); + println(getReturnValue); + println("retValueReceived" + methodNumId + ".set(false);"); + println("rmiComm.setGetReturnBytes();\n"); + } + + /** * HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class */ @@ -919,7 +908,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String prmType = methPrmTypes.get(i); if (checkCallbackType(prmType, callbackType)) { // Check if this has callback object - print("int.class"); + print("int[].class"); } else { // Generate normal classes if it's not a callback object String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); print(getSimpleType(getEnumType(paramType)) + ".class"); @@ -935,13 +924,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object - //if (isArray(methPrmTypes.get(i), methParams.get(i))) - if (isArray(methParams.get(i))) - print(getSimpleIdentifier(methParams.get(i)) + ".length"); - else if (isList(methPrmTypes.get(i))) - print(getSimpleIdentifier(methParams.get(i)) + ".size()"); - else - print("new Integer(1)"); + print("objIdSent"); } else print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i)); // Check if this is the last element (don't print a comma) @@ -951,26 +934,26 @@ public class IoTCompiler { } println(" };"); } + // Send method call first and wait for return value separately + println("rmiComm.remoteCall(objectId, methodId, paramCls, paramObj);"); // Check if this is "void" - if (retType.equals("void")) { - println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); - } else { // We do have a return value + if (!retType.equals("void")) { // We do have a return value // Generate array of parameter types if (isStructClass(getGenericType(getSimpleArrayType(retType)))) { - writeStructReturnJavaStub(getGenericType(getSimpleArrayType(retType)), retType); + writeStructReturnJavaStub(getGenericType(getSimpleArrayType(retType)), retType, method, intDecl); } else { // This is an enum type if (getParamCategory(getGenericType(getSimpleArrayType(retType))) == ParamCategory.ENUM) { - println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); - checkAndWriteEnumRetTypeJavaStub(retType); + //println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); + checkAndWriteEnumRetTypeJavaStub(retType, method, intDecl); } else if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) { // Check if the return value NONPRIMITIVES String retGenValType = getGenericType(retType); println("Class retGenValType = " + retGenValType + ".class;"); - println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);"); + writeWaitForReturnValue(method, intDecl, "Object retObj = rmiComm.getReturnValue(retType, retGenValType);"); println("return (" + retType + ")retObj;"); } else { - println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);"); + writeWaitForReturnValue(method, intDecl, "Object retObj = rmiComm.getReturnValue(retType, null);"); println("return (" + retType + ")retObj;"); } } @@ -1003,12 +986,66 @@ public class IoTCompiler { } + /** + * HELPER: writeCallbackInstantiationMethodBodyJavaStub() writes the callback object instantiation in the method of the stub class + */ + private void writeCallbackInstantiationMethodBodyJavaStub(String paramIdent, String callbackType, int counter, boolean isMultipleCallbacks) { + + println("if (!IoTRMIUtil.mapSkel.containsKey(" + paramIdent + ")) {"); + println("int newObjIdSent = rmiComm.getObjectIdCounter();"); + if (isMultipleCallbacks) + println("objIdSent[i++] = newObjIdSent;"); + else + println("objIdSent[0] = newObjIdSent;"); + println("rmiComm.decrementObjectIdCounter();"); + println(callbackType + "_Skeleton skel" + counter + " = new " + callbackType + "_Skeleton(" + paramIdent + ", rmiComm, newObjIdSent);"); + println("IoTRMIUtil.mapSkel.put(" + paramIdent + ", skel0);"); + println("IoTRMIUtil.mapSkelId.put(" + paramIdent + ", newObjIdSent);"); + println("Thread thread = new Thread() {"); + println("public void run() {"); + println("try {"); + println("skel0.___waitRequestInvokeMethod();"); + println("} catch (Exception ex) {"); + println("ex.printStackTrace();"); + println("throw new Error(\"Exception when trying to run ___waitRequestInvokeMethod() for " + + callbackType + "_Skeleton!\");"); + println("}"); + println("}"); + println("};"); + println("thread.start();"); + println("while(!skel0.didAlreadyInitWaitInvoke());"); + println("}"); + println("else"); + println("{"); + println("int newObjIdSent = IoTRMIUtil.mapSkelId.get(" + paramIdent + ");"); + if (isMultipleCallbacks) + println("objIdSent[i++] = newObjIdSent;"); + else + println("objIdSent[0] = newObjIdSent;"); + println("}"); + } + + /** * HELPER: writeCallbackMethodBodyJavaStub() writes the callback method of the stub class */ private void writeCallbackMethodBodyJavaStub(InterfaceDecl intDecl, List methParams, List methPrmTypes, String method, String callbackType) { + // Determine callback object counter type (List vs. single variable) + print("int[] objIdSent = "); + for (int i = 0; i < methParams.size(); i++) { + String paramType = methPrmTypes.get(i); + if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object + String param = methParams.get(i); + if (isArray(methParams.get(i))) + println("new int[" + getSimpleIdentifier(methParams.get(i)) + ".length];"); + else if (isList(methPrmTypes.get(i))) + println("new int[" + getSimpleIdentifier(methParams.get(i)) + ".size()];"); + else + println("new int[1];"); + } + } println("try {"); // Check if this is single object, array, or list of objects for (int i = 0; i < methParams.size(); i++) { @@ -1016,12 +1053,11 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String param = methParams.get(i); if (isArrayOrList(paramType, param)) { // Generate loop + println("int i = 0;"); println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {"); - println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);"); + writeCallbackInstantiationMethodBodyJavaStub("cb", callbackType, i, true); } else - println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" + - getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);"); - println("listCallbackObj.add(skel" + i + ");"); + writeCallbackInstantiationMethodBodyJavaStub(getSimpleIdentifier(param), callbackType, i, false); if (isArrayOrList(paramType, param)) println("}"); } @@ -1072,7 +1108,7 @@ public class IoTCompiler { println("}\n"); // Write the init callback helper method if (isCallbackMethod && !isDefined) { - writeInitCallbackJavaStub(callbackType, intDecl, newStubClass); + //writeInitCallbackJavaStub(callbackType, intDecl, newStubClass); isDefined = true; } } @@ -1119,9 +1155,11 @@ public class IoTCompiler { // Write class header println("public class " + newStubClass + " implements " + newIntface + " {\n"); // Write properties - writePropertiesJavaStub(intface, newIntface, callbackExist, callbackClasses); + writePropertiesJavaStub(intface, intMeth.getValue(), intDecl); // Write constructor - writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses); + writeConstructorJavaStub(intface, newStubClass, intMeth.getValue(), intDecl); + // Write callback constructor (used if this stub is treated as a callback stub) + writeCallbackConstructorJavaStub(intface, newStubClass, intMeth.getValue(), intDecl); // Write methods writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass); println("}"); @@ -1132,122 +1170,19 @@ public class IoTCompiler { } - /** - * HELPER: writePropertiesJavaCallbackStub() writes the properties of the callback stub class - */ - private void writePropertiesJavaCallbackStub(String intface, String newIntface, boolean callbackExist, Set 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 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 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 - *

- * 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> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> 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 methods = intMeth.getValue(); - Set importClasses = getImportClasses(methods, intDecl); - List stdImportClasses = getStandardJavaImportClasses(); - List allImportClasses = getAllLibClasses(stdImportClasses, importClasses); - printImportStatements(allImportClasses); println(""); - // Find out if there are callback objects - Set 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..."); - } - } - } - - /** * HELPER: writePropertiesJavaSkeleton() writes the properties of the skeleton class */ - private void writePropertiesJavaSkeleton(String intface, boolean callbackExist, InterfaceDecl intDecl) { + private void writePropertiesJavaSkeleton(String intface, InterfaceDecl intDecl) { println("private " + intface + " mainObj;"); - //println("private int ports;"); - println("private IoTRMIObject rmiObj;\n"); - println("private String callbackAddress;"); - // Callback - if (callbackExist) { - println("private int objIdCnt = 0;"); - println("private IoTRMICall rmiCall;"); - println("private int[] ports;\n"); - } + println("private int objectId = 0;"); + println("// Communications and synchronizations"); + println("private IoTRMIComm rmiComm;"); + println("private AtomicBoolean didAlreadyInitWaitInvoke;"); + println("private AtomicBoolean methodReceived;"); + println("private byte[] methodBytes = null;"); + println("// Permissions"); writePropertiesJavaPermission(intface, intDecl); println("\n"); } @@ -1287,17 +1222,53 @@ public class IoTCompiler { /** * HELPER: writeConstructorJavaSkeleton() writes the constructor of the skeleton class */ - private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection methods, boolean callbackExist) { + private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, + Collection methods, boolean callbackExist) { - println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _port) throws Exception {"); + println("public " + newSkelClass + "(" + intface + " _mainObj, int _portSend, int _portRecv) throws Exception {"); println("mainObj = _mainObj;"); - println("callbackAddress = _callbackAddress;"); - println("rmiObj = new IoTRMIObject(_port);"); + println("rmiComm = new IoTRMICommServer(_portSend, _portRecv);"); // Generate permission control initialization writeConstructorJavaPermission(intface); - writeJavaInitCallbackPermission(intface, intDecl, callbackExist); + //writeJavaInitCallbackPermission(intface, intDecl, callbackExist); writeStructPermissionJavaSkeleton(methods, intDecl, intface); + println("IoTRMIUtil.mapSkel.put(_mainObj, this);"); + println("IoTRMIUtil.mapSkelId.put(_mainObj, objectId);"); + println("didAlreadyInitWaitInvoke = new AtomicBoolean(false);"); + println("methodReceived = new AtomicBoolean(false);"); + println("rmiComm.registerSkeleton(objectId, methodReceived);"); + println("Thread thread1 = new Thread() {"); + println("public void run() {"); + println("try {"); println("___waitRequestInvokeMethod();"); + println("}"); + println("catch (Exception ex)"); + println("{"); + println("ex.printStackTrace();"); + println("}"); + println("}"); + println("};"); + println("thread1.start();"); + println("}\n"); + } + + + /** + * HELPER: writeCallbackConstructorJavaSkeleton() writes the constructor of the skeleton class + */ + private void writeCallbackConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, + Collection methods, boolean callbackExist) { + + println("public " + newSkelClass + "(" + intface + " _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception {"); + println("mainObj = _mainObj;"); + println("rmiComm = _rmiComm;"); + println("objectId = _objectId;"); + // Generate permission control initialization + writeConstructorJavaPermission(intface); + writeStructPermissionJavaSkeleton(methods, intDecl, intface); + println("didAlreadyInitWaitInvoke = new AtomicBoolean(false);"); + println("methodReceived = new AtomicBoolean(false);"); + println("rmiComm.registerSkeleton(objectId, methodReceived);"); println("}\n"); } @@ -1323,30 +1294,6 @@ public class IoTCompiler { } - /** - * HELPER: writeInitCallbackJavaSkeleton() writes the init callback method for skeleton class - */ - private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton, String intface) { - - // This is a callback skeleton generation - if (callbackSkeleton) - println("public void ___regCB(IoTRMIObject rmiObj) throws IOException {"); - else - println("public void ___regCB() throws IOException {"); - print("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int[].class, String.class, int.class },"); - println("new Class[] { null, null, null });"); - println("ports = (int[]) paramObj[0];"); - String stubInt = null; - if (callbackSkeleton) - stubInt = getStubInterface(intface) + "_CallbackStub"; - else - stubInt = getStubInterface(intface) + "_Stub"; - int port = getPortCount(stubInt); - println("rmiCall = new IoTRMICall(ports[" + port + "], (String) paramObj[1], (int) paramObj[2]);"); - println("}\n"); - } - - /** * HELPER: writeMethodJavaSkeleton() writes the method of the skeleton class */ @@ -1381,13 +1328,32 @@ public class IoTCompiler { writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method)); println("}\n"); if (isCallbackMethod && !isDefined) { // Make sure that this function is only defined once! - writeInitCallbackJavaSkeleton(callbackSkeleton, intface); + //writeInitCallbackJavaSkeleton(callbackSkeleton, intface); isDefined = true; } } } + /** + * HELPER: writeCallbackInstantiationJavaStubGeneration() writes the instantiation of callback stubs + */ + private void writeCallbackInstantiationJavaStubGeneration(int counter, String exchParamType) { + + //println("int objIdRecv = (int[]) paramObj[0];"); + println(exchParamType + " newStub = null;"); + println("if(!IoTRMIUtil.mapStub.containsKey(objIdRecv)) {"); + println("newStub = new " + exchParamType + "_Stub(rmiComm, objIdRecv);"); + println("IoTRMIUtil.mapStub.put(objIdRecv, newStub);"); + println("rmiComm.setObjectIdCounter(objIdRecv);"); + println("rmiComm.decrementObjectIdCounter();"); + println("}"); + println("else {"); + println("newStub = (" + exchParamType + "_Stub) IoTRMIUtil.mapStub.get(objIdRecv);"); + println("}"); + } + + /** * HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part */ @@ -1405,31 +1371,35 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String exchParamType = checkAndGetParamClass(getGenericType(paramType)); // Print array if this is array or list if this is a list of callback objects - if (isArray(param)) { - println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];"); + println("int[] stubIdArray" + i + " = (int[]) paramObj[" + offsetPfx + i + "];"); + if (isArray(param)) { + println("int numStubs" + i + " = stubIdArray" + i + ".length;"); println(exchParamType + "[] stub" + i + " = new " + exchParamType + "[numStubs" + i + "];"); } else if (isList(paramType)) { - println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];"); + println("int numStubs" + i + " = stubIdArray" + i + ".length;"); println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();"); } else { - println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);"); - println("objIdCnt++;"); + println("int objIdRecv = stubIdArray" + i + "[0];"); + writeCallbackInstantiationJavaStubGeneration(i, exchParamType); } } // Generate a loop if needed if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object String exchParamType = checkAndGetParamClass(getGenericType(paramType)); if (isArray(param)) { - println("for (int objId = 0; objId < numStubs" + i + "; objId++) {"); - println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);"); - println("objIdCnt++;"); + println("for (int i = 0; i < numStubs" + i + "; i++) {"); + println("int objIdRecv = stubIdArray" + i + "[numStubs" + i + "];"); + writeCallbackInstantiationJavaStubGeneration(i, exchParamType); + println("stub" + i + "[i] = newStub;"); println("}"); } else if (isList(paramType)) { - println("for (int objId = 0; objId < numStubs" + i + "; objId++) {"); - println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports));"); - println("objIdCnt++;"); + println("for (int i = 0; i < numStubs" + i + "; i++) {"); + println("int objIdRecv = stubIdArray" + i + "[numStubs" + i + "];"); + writeCallbackInstantiationJavaStubGeneration(i, exchParamType); + println("stub" + i + ".add(newStub);"); println("}"); - } + } else + println(exchParamType + " stub" + i + " = newStub;"); mapStubParam.put(i, "stub" + i); // List of all stub parameters } } @@ -1667,7 +1637,7 @@ public class IoTCompiler { else // Just single struct object println("int retLen = 1;"); println("Object retLenObj = retLen;"); - println("rmiObj.sendReturnObj(retLenObj);"); + println("rmiComm.sendReturnObj(retLenObj, localMethodBytes);"); int numMem = getNumOfMembers(simpleType); println("Class[] retCls = new Class[" + numMem + "*retLen];"); println("Object[] retObj = new Object[" + numMem + "*retLen];"); @@ -1756,12 +1726,12 @@ public class IoTCompiler { if (!retType.equals("void")) { if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type checkAndWriteEnumRetConvJavaSkeleton(retType); - println("rmiObj.sendReturnObj(retObj);"); + println("rmiComm.sendReturnObj(retObj, localMethodBytes);"); } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type writeStructReturnJavaSkeleton(getSimpleArrayType(getGenericType(retType)), retType); - println("rmiObj.sendReturnObj(retCls, retObj);"); + println("rmiComm.sendReturnObj(retCls, retObj, localMethodBytes);"); } else - println("rmiObj.sendReturnObj(retObj);"); + println("rmiComm.sendReturnObj(retObj, localMethodBytes);"); } if (isCallbackMethod) { // Catch exception if this is callback print("}"); @@ -1782,6 +1752,8 @@ public class IoTCompiler { // Generate array of parameter objects boolean isCallbackMethod = false; String callbackType = null; + println("byte[] localMethodBytes = methodBytes;"); + println("rmiComm.setGetMethodBytes();"); print("int paramLen = "); writeLengthStructParamClassSkeleton(methParams, methPrmTypes, method, intDecl); println(";"); @@ -1800,7 +1772,7 @@ public class IoTCompiler { if (callbackClasses.contains(prmType)) { isCallbackMethod = true; callbackType = prmType; - println("paramCls[pos] = int.class;"); + println("paramCls[pos] = int[].class;"); println("paramClsGen[pos++] = null;"); } else { // Generate normal classes if it's not a callback object String paramTypeOth = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); @@ -1814,7 +1786,7 @@ public class IoTCompiler { } } } - println("Object[] paramObj = rmiObj.getMethodParams(paramCls, paramClsGen);"); + println("Object[] paramObj = rmiComm.getMethodParams(paramCls, paramClsGen, localMethodBytes);"); writeStructMembersInitJavaSkeleton(intDecl, methParams, methPrmTypes, method); // Write the return value part writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, true); @@ -1830,14 +1802,16 @@ public class IoTCompiler { // Generate array of parameter objects boolean isCallbackMethod = false; String callbackType = null; - print("Object[] paramObj = rmiObj.getMethodParams(new Class[] { "); + println("byte[] localMethodBytes = methodBytes;"); + println("rmiComm.setGetMethodBytes();"); + print("Object[] paramObj = rmiComm.getMethodParams(new Class[] { "); for (int i = 0; i < methParams.size(); i++) { String paramType = returnGenericCallbackType(methPrmTypes.get(i)); if (callbackClasses.contains(paramType)) { isCallbackMethod = true; callbackType = paramType; - print("int.class"); + print("int[].class"); } else { // Generate normal classes if it's not a callback object String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); print(getSimpleType(getEnumType(prmType)) + ".class"); @@ -1845,9 +1819,9 @@ public class IoTCompiler { if (i != methParams.size() - 1) print(", "); } - println(" }, "); + //println(" }, "); // Generate generic class if it's a generic type.. null otherwise - print("new Class[] { "); + print(" }, new Class[] { "); for (int i = 0; i < methParams.size(); i++) { String prmType = methPrmTypes.get(i); if ((getParamCategory(prmType) == ParamCategory.NONPRIMITIVES) && @@ -1859,7 +1833,7 @@ public class IoTCompiler { if (i != methParams.size() - 1) print(", "); } - println(" });"); + println(" }, localMethodBytes);"); // Write the return value part writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, false); } @@ -1953,7 +1927,9 @@ public class IoTCompiler { String helperMethod = methodNumId + "struct" + i; println(helperMethod + "() {"); // Now, write the helper body of skeleton! - println("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, new Class[] { null });"); + println("byte[] localMethodBytes = methodBytes;"); + println("rmiComm.setGetMethodBytes();"); + println("Object[] paramObj = rmiComm.getMethodParams(new Class[] { int.class }, new Class[] { null }, localMethodBytes);"); println("return (int) paramObj[0];"); println("}\n"); } @@ -1984,7 +1960,7 @@ public class IoTCompiler { String helperMethod = methodNumId + "struct" + i; println(helperMethod + "(IoTRMIObject rmiObj) {"); // Now, write the helper body of skeleton! - println("Object[] paramObj = rmiObj.getMethodParams(new Class[] { int.class }, new Class[] { null });"); + println("Object[] paramObj = rmiComm.getMethodParams(new Class[] { int.class }, new Class[] { null });"); println("return (int) paramObj[0];"); println("}\n"); } @@ -2038,7 +2014,7 @@ public class IoTCompiler { else begin = false; int methodNumId = intDecl.getMethodNumId(method); - print("struct" + methodNumId + "Size" + i); + print("struct" + methodNumId + "Size" + i + "Final"); } } return structExist; @@ -2119,26 +2095,56 @@ public class IoTCompiler { println("}"); println("}"); println("else {"); - println("throw new Error(\"Object Id: \" + _objectId + \" not recognized!\");"); + println("continue;"); println("}"); } } + /** + * HELPER: writeFinalInputCountVarStructSkeleton() writes the final version of input counter variable of struct for skeleton + */ + private boolean writeFinalInputCountVarStructSkeleton(String method, InterfaceDecl intDecl) { + + List methParams = intDecl.getMethodParams(method); + List methPrmTypes = intDecl.getMethodParamTypes(method); + boolean structExist = false; + boolean begin = true; + // Check for params with structs + for (int i = 0; i < methParams.size(); i++) { + String paramType = methPrmTypes.get(i); + String param = methParams.get(i); + String simpleType = getGenericType(paramType); + if (isStructClass(simpleType)) { + structExist = true; + int methodNumId = intDecl.getMethodNumId(method); + println("final int struct" + methodNumId + "Size" + i + + "Final = struct" + methodNumId + "Size" + i + ";"); + } + } + return structExist; + } + + /** * HELPER: writeJavaWaitRequestInvokeMethod() writes the main loop of the skeleton class */ - private void writeJavaWaitRequestInvokeMethod(Collection methods, InterfaceDecl intDecl, boolean callbackExist, String intface) { + private void writeJavaWaitRequestInvokeMethod(Collection methods, InterfaceDecl intDecl, String intface) { // Use this set to handle two same methodIds Set uniqueMethodIds = new HashSet(); - println("private void ___waitRequestInvokeMethod() throws IOException {"); + println("public void ___waitRequestInvokeMethod() throws IOException {"); // Write variables here if we have callbacks or enums or structs writeCountVarStructSkeleton(methods, intDecl); + println("didAlreadyInitWaitInvoke.compareAndSet(false, true);"); println("while (true) {"); - println("rmiObj.getMethodBytes();"); - println("int _objectId = rmiObj.getObjectId();"); - println("int methodId = rmiObj.getMethodId();"); + println("if (!methodReceived.get()) {"); + println("continue;"); + println("}"); + println("methodBytes = rmiComm.getMethodBytes();"); + println("methodReceived.set(false);"); + println("int _objectId = IoTRMIComm.getObjectId(methodBytes);"); + println("int methodId = IoTRMIComm.getMethodId(methodBytes);"); // Generate permission check writeJavaMethodPermission(intface); println("switch (methodId) {"); @@ -2146,7 +2152,13 @@ public class IoTCompiler { for (String method : methods) { String methodId = intDecl.getMethodId(method); int methodNumId = intDecl.getMethodNumId(method); - print("case " + methodNumId + ": ___"); + println("case " + methodNumId + ":"); + // Check for stuct counters + writeFinalInputCountVarStructSkeleton(method, intDecl); + println("new Thread() {"); + println("public void run() {"); + println("try {"); + print("___"); String helperMethod = methodId; if (uniqueMethodIds.contains(methodId)) helperMethod = helperMethod + methodNumId; @@ -2154,14 +2166,16 @@ public class IoTCompiler { uniqueMethodIds.add(methodId); print(helperMethod + "("); writeInputCountVarStructSkeleton(method, intDecl); - println("); break;"); + println(");"); + println("}"); + println("catch (Exception ex) {"); + println("ex.printStackTrace();"); + println("}"); + println("}"); + println("}.start();"); + println("break;"); } String method = "___initCallBack()"; - // Print case -9999 (callback handler) if callback exists - if (callbackExist) { - int methodId = intDecl.getHelperMethodNumId(method); - println("case " + methodId + ": ___regCB(); break;"); - } writeMethodCallStructSkeleton(methods, intDecl); println("default: "); println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");"); @@ -2172,201 +2186,26 @@ public class IoTCompiler { /** - * generateJavaSkeletonClass() generate skeletons based on the methods list in Java + * HELPER: writeReturnDidAlreadyInitWaitInvoke() writes the function to return didAlreadyInitWaitInvoke */ - public void generateJavaSkeletonClass() throws IOException { + private void writeReturnDidAlreadyInitWaitInvoke() { - // Create a new directory - String path = createDirectories(dir, subdir); - for (String intface : mapIntfacePTH.keySet()) { - // Open a new file to write into - String newSkelClass = intface + "_Skeleton"; - 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 methods = intDecl.getMethods(); - Set importClasses = getImportClasses(methods, intDecl); - List stdImportClasses = getStandardJavaImportClasses(); - List allImportClasses = getAllLibClasses(stdImportClasses, importClasses); - printImportStatements(allImportClasses); - // Find out if there are callback objects - Set callbackClasses = getCallbackClasses(methods, intDecl); - boolean callbackExist = !callbackClasses.isEmpty(); - // Write class header - println(""); - println("public class " + newSkelClass + " implements " + intface + " {\n"); - // Write properties - writePropertiesJavaSkeleton(intface, callbackExist, intDecl); - // Write constructor - writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist); - // Write methods - writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface); - // Write method helper - writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses); - // Write waitRequestInvokeMethod() - main loop - writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface); - println("}"); - pw.close(); - System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java..."); - } - } - - - /** - * HELPER: writePropertiesJavaCallbackSkeleton() writes the properties of the callback skeleton class - */ - private void writePropertiesJavaCallbackSkeleton(String intface, boolean callbackExist) { - - println("private " + intface + " mainObj;"); - // For callback skeletons, this is its own object Id - println("private int objectId = 0;"); - println("private String callbackAddress;"); - // Callback - if (callbackExist) { - - println("private int objIdCnt = 0;"); - println("private IoTRMICall rmiCall;"); - println("private int[] ports;\n"); - } - println("\n"); - } - - - /** - * HELPER: writeConstructorJavaCallbackSkeleton() writes the constructor of the skeleton class - */ - private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection methods) { - - println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _objectId) throws Exception {"); - println("callbackAddress = _callbackAddress;"); - println("mainObj = _mainObj;"); - println("objectId = _objectId;"); + println("public boolean didAlreadyInitWaitInvoke() {"); + println("return didAlreadyInitWaitInvoke.get();"); println("}\n"); } /** - * HELPER: writeMethodHelperJavaCallbackSkeleton() writes the method helper of the callback skeleton class - */ - private void writeMethodHelperJavaCallbackSkeleton(Collection methods, InterfaceDecl intDecl, Set callbackClasses) { - - // Use this set to handle two same methodIds - Set uniqueMethodIds = new HashSet(); - for (String method : methods) { - - List methParams = intDecl.getMethodParams(method); - List 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(", "); - else - 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 methods, InterfaceDecl intDecl, boolean callbackExist) { - - // Use this set to handle two same methodIds - Set uniqueMethodIds = new HashSet(); - 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 + * generateJavaSkeletonClass() generate skeletons based on the methods list in Java */ - public void generateJavaCallbackSkeletonClass() throws IOException { + public void generateJavaSkeletonClass() 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"; + String newSkelClass = intface + "_Skeleton"; FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".java"); pw = new PrintWriter(new BufferedWriter(fw)); // Pass in set of methods and get import classes @@ -2384,21 +2223,29 @@ public class IoTCompiler { println(""); println("public class " + newSkelClass + " implements " + intface + " {\n"); // Write properties - writePropertiesJavaCallbackSkeleton(intface, callbackExist); + writePropertiesJavaSkeleton(intface, intDecl); // Write constructor - writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods); + writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist); + // Write constructor that is called when this object is a callback object + writeCallbackConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist); + // Write function to return didAlreadyInitWaitInvoke + writeReturnDidAlreadyInitWaitInvoke(); // Write methods - writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true, intface); + writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface); // Write method helper - writeMethodHelperJavaCallbackSkeleton(methods, intDecl, callbackClasses); + writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses); // Write waitRequestInvokeMethod() - main loop - writeJavaCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist); + writeJavaWaitRequestInvokeMethod(methods, intDecl, intface); println("}"); pw.close(); - System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".java..."); + System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java..."); } } + +/*================ + * C++ generation + *================/ /** * HELPER: writeMethodCplusLocalInterface() writes the method of the local interface @@ -2664,8 +2511,8 @@ public class IoTCompiler { println("}\n"); // Write the init callback helper method if (isCallbackMethod && !isDefined) { - writeInitCallbackCplusStub(callbackType, intDecl, newStubClass); - writeInitCallbackSendInfoCplusStub(intDecl); + //writeInitCallbackCplusStub(callbackType, intDecl, newStubClass); + //writeInitCallbackSendInfoCplusStub(intDecl); isDefined = true; } } @@ -3105,10 +2952,10 @@ public class IoTCompiler { String callbackType = (String) it.next(); DeclarationHandler decHandler = mapIntDeclHand.get(callbackType); InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType); - writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist); - println("thread th1 (&" + newStubClass + "::___initCallBack, this);"); - println("th1.detach();"); - println("___regCB();"); + //writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist); + //println("thread th1 (&" + newStubClass + "::___initCallBack, this);"); + //println("th1.detach();"); + //println("___regCB();"); } println("}\n"); } @@ -3161,74 +3008,6 @@ public class IoTCompiler { } - /** - * HELPER: writeInitCallbackCplusStub() writes the initialization of callback - */ - private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, String newStubClass) { - - println("void ___initCallBack() {"); - println("bool bResult = false;"); - int port = getPortCount(newStubClass); - println("rmiObj = new IoTRMIObject(ports[" + port + "], &bResult);"); - println("while (true) {"); - println("char* method = rmiObj->getMethodBytes();"); - //writeCplusMethodCallbackPermission(intface); - println("int objId = IoTRMIObject::getObjectId(method);"); - println("if (objId < vecCallbackObj.size()) { // Check if still within range"); - println(intface + "_CallbackSkeleton* skel = dynamic_cast<" + intface + - "_CallbackSkeleton*> (vecCallbackObj.at(objId));"); - writeCplusMethodCallbackPermission(intface); - println("skel->invokeMethod(rmiObj);"); - print("}"); - println(" else {"); - println("cerr << \"Illegal object Id: \" << to_string(objId);"); - // TODO: perhaps need to change this into "throw" to make it cleaner (allow stack unfolding) - println("return;"); - println("}"); - println("}"); - println("}\n"); - } - - - /** - * HELPER: writeCplusInitCallbackPermission() writes the permission for callback - */ - private void writeCplusInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) { - - if (callbackExist) { - String method = "___initCallBack()"; - int methodNumId = intDecl.getHelperMethodNumId(method); - Map> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { - String newIntface = intMeth.getKey(); - int newObjectId = getNewIntfaceObjectId(newIntface); - println("set" + newObjectId + "Allowed.insert(" + methodNumId + ");"); - } - } - } - - - /** - * HELPER: writeInitCallbackSendInfoCplusStub() writes the initialization (send info part) of callback - */ - private void writeInitCallbackSendInfoCplusStub(InterfaceDecl intDecl) { - - // Generate info sending part - println("void ___regCB() {"); - println("int numParam = 3;"); - String method = "___initCallBack()"; - int methodNumId = intDecl.getHelperMethodNumId(method); - println("int methodId = " + methodNumId + ";"); - println("string retType = \"void\";"); - println("string paramCls[] = { \"int*\", \"String\", \"int\" };"); - println("int rev = 0;"); - println("void* paramObj[] = { &ports, &callbackAddress, &rev };"); - println("void* retObj = NULL;"); - println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);"); - println("}\n"); - } - - /** * generateCPlusStubClasses() generate stubs based on the methods list in C++ */ @@ -3287,113 +3066,6 @@ public class IoTCompiler { } - /** - * HELPER: writePropertiesCplusCallbackStub() writes the properties of the stub class - */ - private void writePropertiesCplusCallbackStub(String intface, String newIntface, boolean callbackExist, Set callbackClasses) { - - println("IoTRMICall *rmiCall;"); - // Get the object Id - println("int objectId;"); - println("vector ports;\n"); - println("string callbackAddress;"); - 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("IoTRMIObject *rmiObj;"); - println("vector<" + callbackType + "*> vecCallbackObj;"); - println("static int objIdCnt;"); - // TODO: Need to initialize address and ports if we want to have callback-in-callback - writePropertiesCplusPermission(callbackType); - } - println("\n"); - } - - - /** - * HELPER: writeConstructorCplusCallbackStub() writes the constructor of the stub class - */ - private void writeConstructorCplusCallbackStub(String newStubClass, boolean callbackExist, Set callbackClasses) { - - println(newStubClass + "(IoTRMICall* _rmiCall, string _callbackAddress, int _objectId, vector _ports) {"); - println("objectId = _objectId;"); - println("callbackAddress = _callbackAddress;"); - println("rmiCall = _rmiCall;"); - println("ports = _ports;"); - if (callbackExist) { - Iterator it = callbackClasses.iterator(); - String callbackType = (String) it.next(); - DeclarationHandler decHandler = mapIntDeclHand.get(callbackType); - InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType); - writeCplusInitCallbackPermission(callbackType, intDecl, callbackExist); - println("thread th1 (&" + newStubClass + "::___initCallBack, this);"); - println("th1.detach();"); - println("___regCB();"); - } - println("}\n"); - } - - - /** - * generateCPlusCallbackStubClasses() generate callback stubs based on the methods list in C++ - */ - public void generateCPlusCallbackStubClasses() throws IOException { - - // Create a new directory - String path = createDirectories(dir, subdir); - for (String intface : mapIntfacePTH.keySet()) { - - Map> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { - // Open a new file to write into - String newIntface = intMeth.getKey(); - String newStubClass = newIntface + "_CallbackStub"; - FileWriter fw = new FileWriter(path + "/" + newStubClass + ".hpp"); - pw = new PrintWriter(new BufferedWriter(fw)); - // Find out if there are callback objects - Set methods = intMeth.getValue(); - DeclarationHandler decHandler = mapIntDeclHand.get(intface); - InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); - Set callbackClasses = getCallbackClasses(methods, intDecl); - boolean callbackExist = !callbackClasses.isEmpty(); - // Write file headers - println("#ifndef _" + newStubClass.toUpperCase() + "_HPP__"); - println("#define _" + newStubClass.toUpperCase() + "_HPP__"); - println("#include "); - if (callbackExist) - println("#include "); - println("#include \"" + newIntface + ".hpp\""); println(""); - println("using namespace std;"); println(""); - println("class " + newStubClass + " : public " + newIntface); println("{"); - println("private:\n"); - writePropertiesCplusCallbackStub(intface, newIntface, callbackExist, callbackClasses); - println("public:\n"); - // Add default constructor and destructor - println(newStubClass + "() { }"); println(""); - writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses); - writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses); - // Write methods - writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass); - println("};"); - if (callbackExist) { - Iterator it = callbackClasses.iterator(); - String callbackType = (String) it.next(); - // Generate permission stuff for callback stubs - DeclarationHandler decHandlerCallback = mapIntDeclHand.get(callbackType); - InterfaceDecl intDeclCallback = (InterfaceDecl) decHandlerCallback.getInterfaceDecl(callbackType); - writePermissionInitializationCplus(callbackType, newStubClass, intDeclCallback); - } - writeObjectIdCountInitializationCplus(newStubClass, callbackExist); - println("#endif"); - pw.close(); - System.out.println("IoTCompiler: Generated callback stub class " + newIntface + ".hpp..."); - } - } - } - - /** * HELPER: writePropertiesCplusSkeleton() writes the properties of the skeleton class */ @@ -3497,7 +3169,7 @@ public class IoTCompiler { println("mainObj = _mainObj;"); println("callbackAddress = _callbackAddress;"); println("rmiObj = new IoTRMIObject(_port, &_bResult);"); - writeCplusInitCallbackPermission(intface, intDecl, callbackExist); + //writeCplusInitCallbackPermission(intface, intDecl, callbackExist); writeStructPermissionCplusSkeleton(methods, intDecl, intface); println("___waitRequestInvokeMethod();"); println("}\n"); @@ -3554,35 +3226,6 @@ public class IoTCompiler { } - /** - * HELPER: writeInitCallbackCplusSkeleton() writes the init callback method for skeleton class - */ - private void writeInitCallbackCplusSkeleton(boolean callbackSkeleton, String intface) { - - // This is a callback skeleton generation - if (callbackSkeleton) - println("void ___regCB(IoTRMIObject* rmiObj) {"); - else - println("void ___regCB() {"); - println("int numParam = 3;"); - println("vector param1;"); - println("string param2 = \"\";"); - println("int param3 = 0;"); - println("string paramCls[] = { \"int*\", \"String\", \"int\" };"); - println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };"); - println("rmiObj->getMethodParams(paramCls, numParam, paramObj);"); - println("bool bResult = false;"); - String stubInt = null; - if (callbackSkeleton) - stubInt = getStubInterface(intface) + "_CallbackStub"; - else - stubInt = getStubInterface(intface) + "_Stub"; - int port = getPortCount(stubInt); - println("rmiCall = new IoTRMICall(param1[" + port + "], param2.c_str(), param3, &bResult);"); - println("}\n"); - } - - /** * HELPER: writeMethodCplusSkeleton() writes the method of the skeleton class */ @@ -3620,7 +3263,7 @@ public class IoTCompiler { writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method)); println("}\n"); if (isCallbackMethod && !isDefined) { - writeInitCallbackCplusSkeleton(callbackSkeleton, intface); + //writeInitCallbackCplusSkeleton(callbackSkeleton, intface); isDefined = true; } } @@ -4295,229 +3938,6 @@ public class IoTCompiler { } - /** - * HELPER: writePropertiesCplusCallbackSkeleton() writes the properties of the callback skeleton class - */ - private void writePropertiesCplusCallbackSkeleton(String intface, boolean callbackExist, Set callbackClasses) { - - println(intface + " *mainObj;"); - // Keep track of object Ids of all stubs registered to this interface - println("int objectId;"); - println("vector 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 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 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 methods, InterfaceDecl intDecl, - Set callbackClasses) { - - // Use this set to handle two same methodIds - Set uniqueMethodIds = new HashSet(); - for (String method : methods) { - - List methParams = intDecl.getMethodParams(method); - List 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 methods, InterfaceDecl intDecl, - boolean callbackExist) { - - // Use this set to handle two same methodIds - Set uniqueMethodIds = new HashSet(); - 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 "); - 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 methods = intDecl.getMethods(); - Set includeClasses = getIncludeClasses(methods, intDecl, intface, true); - List stdIncludeClasses = getStandardCplusIncludeClasses(); - List allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses); - printIncludeStatements(allIncludeClasses); println(""); - // Find out if there are callback objects - Set 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..."); - } - } - - /** * generateInitializer() generate initializer based on type */ @@ -4789,8 +4209,13 @@ public class IoTCompiler { importClasses.add("java.util.List"); importClasses.add("java.util.ArrayList"); importClasses.add("java.util.Arrays"); - importClasses.add("iotrmi.Java.IoTRMICall"); - importClasses.add("iotrmi.Java.IoTRMIObject"); + importClasses.add("java.util.Map"); + importClasses.add("java.util.HashMap"); + importClasses.add("java.util.concurrent.atomic.AtomicBoolean"); + importClasses.add("iotrmi.Java.IoTRMIComm"); + importClasses.add("iotrmi.Java.IoTRMICommClient"); + importClasses.add("iotrmi.Java.IoTRMICommServer"); + importClasses.add("iotrmi.Java.IoTRMIUtil"); return importClasses; } @@ -5382,17 +4807,17 @@ public class IoTCompiler { comp.generateJavaLocalInterfaces(); comp.generateJavaInterfaces(); comp.generateJavaStubClasses(); - comp.generateJavaCallbackStubClasses(); + //comp.generateJavaCallbackStubClasses(); comp.generateJavaSkeletonClass(); - comp.generateJavaCallbackSkeletonClass(); + //comp.generateJavaCallbackSkeletonClass(); comp.generateEnumCplus(); comp.generateStructCplus(); comp.generateCplusLocalInterfaces(); comp.generateCPlusInterfaces(); comp.generateCPlusStubClasses(); - comp.generateCPlusCallbackStubClasses(); + //comp.generateCPlusCallbackStubClasses(); comp.generateCplusSkeletonClass(); - comp.generateCplusCallbackSkeletonClass(); + //comp.generateCplusCallbackSkeletonClass(); } else { // Check other options while(i < args.length) { @@ -5412,18 +4837,18 @@ public class IoTCompiler { comp.generateJavaLocalInterfaces(); comp.generateJavaInterfaces(); comp.generateJavaStubClasses(); - comp.generateJavaCallbackStubClasses(); + //comp.generateJavaCallbackStubClasses(); comp.generateJavaSkeletonClass(); - comp.generateJavaCallbackSkeletonClass(); + //comp.generateJavaCallbackSkeletonClass(); } else { comp.generateEnumCplus(); comp.generateStructCplus(); comp.generateCplusLocalInterfaces(); comp.generateCPlusInterfaces(); comp.generateCPlusStubClasses(); - comp.generateCPlusCallbackStubClasses(); + //comp.generateCPlusCallbackStubClasses(); comp.generateCplusSkeletonClass(); - comp.generateCplusCallbackSkeletonClass(); + //comp.generateCplusCallbackSkeletonClass(); } } i = i + 2; diff --git a/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp index e524f4d..be174b8 100644 --- a/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp @@ -46,10 +46,8 @@ void TestClassComplete_Stub::registerCallback(CallBackInterface* _cb) { IoTRMIUtil::mapSkel->insert(make_pair(_cb, skel0)); IoTRMIUtil::mapSkelId->insert(make_pair(_cb, objIdSent)); cout << "Create new skeleton for TestClass! ID=" << objIdSent << endl; - //thread th0 (&CallBackInterface_Skeleton::___waitRequestInvokeMethod, std::ref(skel0)); thread th0 (&CallBackInterface_Skeleton::___waitRequestInvokeMethod, std::ref(skel0), std::ref(skel0)); th0.detach(); - //while(!didAlreadyInitWaitInvoke); while(!skel0->didInitWaitInvoke()); } else { auto itId = IoTRMIUtil::mapSkelId->find(_cb); @@ -57,7 +55,6 @@ void TestClassComplete_Stub::registerCallback(CallBackInterface* _cb) { cout << "Skeleton exists for TestClass! ID=" << objIdSent << endl; } - //int ___paramCB0 = 1; int ___paramCB0 = objIdSent; int methodId = 1; string retType = "void"; @@ -138,7 +135,7 @@ int main(int argc, char *argv[]) //ports.push_back(43212); TestClassComplete *tcStub = new TestClassComplete_Stub(portSend, portRecv, skeletonAddress, rev, &bResult); - //cout << "Getting return value from getShort(): " << tcStub->getShort(1234) << endl; +// cout << "Getting return value from getShort(): " << tcStub->getShort(1234) << endl; //cout << "Getting return value from getShort(): " << tcStub->getShort(4321) << endl; //cout << "Getting return value from getShort(): " << tcStub->getShort(5678) << endl; cout << "==== CALLBACK ====" << endl; diff --git a/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp b/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp index 65524f7..34e4709 100644 --- a/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp +++ b/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp @@ -148,7 +148,6 @@ void TestClassInterface_Skeleton::___waitRequestInvokeMethod(TestClassInterface_ //___callBack(skel); break; default: cerr << "Method Id " << methodId << " not recognized!" << endl; - //throw exception(); return; } cout << "Out of switch statement!" << endl; diff --git a/iotjava/iotrmi/Java/IoTRMIComm.java b/iotjava/iotrmi/Java/IoTRMIComm.java index 6840214..16f84cb 100644 --- a/iotjava/iotrmi/Java/IoTRMIComm.java +++ b/iotjava/iotrmi/Java/IoTRMIComm.java @@ -338,24 +338,6 @@ public abstract class IoTRMIComm { public abstract void remoteCall(int objectId, int methodId, Class[] paramCls, Object[] paramObj); - /** - * getReturnValue() returns return value object - */ - public Object getReturnValue(Class retType, Class retGenTypeVal) { - - // Receive return value and return it to caller - // Now just strip off the object ID and method ID - int headerLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN + IoTRMIUtil.PACKET_TYPE_LEN; - int valByteLen = retValueBytes.length - headerLen; - byte[] retValBytes = new byte[valByteLen]; - // Method Id is positioned after object Id in the byte array - System.arraycopy(retValueBytes, headerLen, retValBytes, 0, valByteLen); - Object retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retValBytes); - // This means the right object and method have gotten the return value, so we set this back to false - return retObj; - } - - /** * methodToBytes() returns byte representation of a method */ @@ -411,24 +393,40 @@ public abstract class IoTRMIComm { } + /** + * getReturnValue() returns return value object + */ + public Object getReturnValue(Class retType, Class retGenTypeVal) { + + // Receive return value and return it to caller + // Now just strip off the object ID and method ID + int headerLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN + IoTRMIUtil.PACKET_TYPE_LEN; + int valByteLen = retValueBytes.length - headerLen; + byte[] retValBytes = new byte[valByteLen]; + // Method Id is positioned after object Id in the byte array + System.arraycopy(retValueBytes, headerLen, retValBytes, 0, valByteLen); + Object retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retValBytes); + // This means the right object and method have gotten the return value, so we set this back to false + return retObj; + } + + /** * getStructObjects() calls a method remotely by passing in parameters and getting a return Object */ - /*public synchronized Object[] getStructObjects(Class[] retType, Class[] retGenTypeVal) { + public Object[] getStructObjects(Class[] retType, Class[] retGenTypeVal) { // Receive return value and return it to caller - Object[] retObj = null; - byte[] retObjBytes = null; - try { - retObjBytes = rmiClientRecv.receiveBytes(retObjBytes); - } catch (IOException ex) { - ex.printStackTrace(); - throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()"); - } - retObj = getReturnObjects(retObjBytes, retType, retGenTypeVal); + // Now just strip off the object ID and method ID + int headerLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN + IoTRMIUtil.PACKET_TYPE_LEN; + int valByteLen = retValueBytes.length - headerLen; + byte[] retValBytes = new byte[valByteLen]; + // Method Id is positioned after object Id in the byte array + System.arraycopy(retValueBytes, headerLen, retValBytes, 0, valByteLen); + Object[] retObj = getReturnObjects(retValBytes, retType, retGenTypeVal); return retObj; - }*/ + } /** diff --git a/iotjava/iotrmi/Java/basics/TestClassCallbacks_Stub.java b/iotjava/iotrmi/Java/basics/TestClassCallbacks_Stub.java index f3b2cbc..1220e3f 100644 --- a/iotjava/iotrmi/Java/basics/TestClassCallbacks_Stub.java +++ b/iotjava/iotrmi/Java/basics/TestClassCallbacks_Stub.java @@ -33,15 +33,15 @@ public class TestClassCallbacks_Stub { CallBackInterface cbSingle1 = new CallBack(2356); tcstub.registerCallback(cbSingle1); System.out.println("Registered callback!"); - CallBackInterface cbSingle2 = new CallBack(2360); - tcstub.registerCallback(cbSingle2); - System.out.println("Registered callback!"); +// CallBackInterface cbSingle2 = new CallBack(2360); +// tcstub.registerCallback(cbSingle2); +// System.out.println("Registered callback!"); System.out.println("Return value from callback 1: " + tcstub.callBack() + "\n\n"); - System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)4576) + "\n\n"); - System.out.println("Return value from callback 2: " + tcstub.callBack() + "\n\n"); - System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)1233) + "\n\n"); - System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)1321) + "\n\n"); +// System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)4576) + "\n\n"); +// System.out.println("Return value from callback 2: " + tcstub.callBack() + "\n\n"); +// System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)1233) + "\n\n"); +// System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)1321) + "\n\n"); while(true) {} } } diff --git a/localconfig/iotpolicy/development/testclasspolicy_callbacks.pol b/localconfig/iotpolicy/development/testclasspolicy_callbacks.pol index e79088c..2883100 100644 --- a/localconfig/iotpolicy/development/testclasspolicy_callbacks.pol +++ b/localconfig/iotpolicy/development/testclasspolicy_callbacks.pol @@ -3,12 +3,22 @@ public interface TestClassInterface { public short getShort(short in); public void registerCallback(CallBackInterface _cb); public int callBack(); + public List getCharList(List in); + public Enum[] handleEnumStruct(Enum en[], List str, char c); + public List handleStructList(List str); + public void registerCallbackArray(CallBackInterface _cb[]); + public void registerCallbackList(List _cb); capability Callbacks { description = "All the set-and-get methods"; method = "getShort(short in)"; method = "registerCallback(CallBackInterface _cb)"; method = "callBack()"; + method = "getCharList(List in)"; + method = "handleEnumStruct(Enum en[], List str, char c)"; + method = "handleStructList(List str)"; + method = "registerCallbackArray(CallBackInterface _cb[])"; + method = "registerCallbackList(List _cb)"; } enum Enum {