}
+ /**
+ * HELPER: updateIntfaceObjIdMap() updates the mapping between new interface and object Id
+ */
+ private void updateIntfaceObjIdMap(String intface, String newIntface) {
+
+ Integer objId = mapIntfaceObjId.get(intface);
+ mapNewIntfaceObjId.put(newIntface, objId);
+ mapIntfaceObjId.put(intface, objId++);
+ }
+
+
/**
* generateJavaInterfaces() generate stub interfaces based on the methods list in Java
*/
// Write interface header
println("");
println("public interface " + newIntface + " {\n");
+ updateIntfaceObjIdMap(intface, newIntface);
// Write methods
writeMethodJavaInterface(methods, intDecl);
println("}");
// Get the object Id
Integer objId = mapIntfaceObjId.get(intface);
println("private final static int objectId = " + objId + ";");
- mapNewIntfaceObjId.put(newIntface, objId);
- mapIntfaceObjId.put(intface, 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();
/**
* HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
*/
- private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl) {
+ private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl, boolean isGenCallbackStub) {
println("public void ___initCallBack() {");
// Generate main thread for callbacks
println("Thread thread = new Thread() {");
println("public void run() {");
println("try {");
- println("rmiObj = new IoTRMIObject(ports[0]);");
+ if (isGenCallbackStub) // Use the second port for a _CallbackStub class (callback in callback)
+ println("rmiObj = new IoTRMIObject(ports[1]);");
+ else
+ println("rmiObj = new IoTRMIObject(ports[0]);");
println("while (true) {");
println("byte[] method = rmiObj.getMethodBytes();");
writeJavaMethodCallbackPermission(intface);
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[0], callbackAddress, 0 };");
+ 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");
}
String param = methParams.get(i);
if (isArrayOrList(paramType, param)) { // Generate loop
println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {");
- println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);");
+ println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);");
} else
println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
- getSimpleIdentifier(param) + ", objIdCnt++);");
+ getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
println("listCallbackObj.add(skel" + i + ");");
if (isArrayOrList(paramType, param))
println("}");
/**
* HELPER: writeMethodJavaStub() writes the methods of the stub class
*/
- private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+ private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, boolean isGenCallbackStub) {
boolean isDefined = false;
for (String method : methods) {
println("}\n");
// Write the init callback helper method
if (isCallbackMethod && !isDefined) {
- writeInitCallbackJavaStub(callbackType, intDecl);
+ writeInitCallbackJavaStub(callbackType, intDecl, isGenCallbackStub);
isDefined = true;
}
}
// Write constructor
writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
// Write methods
- writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+ writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, false);
println("}");
pw.close();
System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java...");
private void writePropertiesJavaCallbackStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
println("private IoTRMICall rmiCall;");
- println("private String address;");
+ println("private String callbackAddress;");
println("private int[] ports;\n");
// Get the object Id
println("private int objectId = 0;");
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, int _objectId) throws Exception {");
+ 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(intface);
println("listCallbackObj = new ArrayList<" + callbackType + ">();");
println("___initCallBack();");
- println("// TODO: Add address and port initialization here if we want callback in callback!");
}
println("}\n");
}
writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
// Write methods
// TODO: perhaps need to generate callback for callback
- writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
+ writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, true);
println("}");
pw.close();
System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
println("private " + intface + " mainObj;");
//println("private int ports;");
println("private IoTRMIObject rmiObj;\n");
+ println("private String callbackAddress;");
// Callback
if (callbackExist) {
println("private static int objIdCnt = 0;");
println("private IoTRMICall rmiCall;");
+ println("private int[] ports;\n");
}
writePropertiesJavaPermission(intface, intDecl);
println("\n");
*/
private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods, boolean callbackExist) {
- println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
+ println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _port) throws Exception {");
println("mainObj = _mainObj;");
+ println("callbackAddress = _callbackAddress;");
println("rmiObj = new IoTRMIObject(_port);");
// Generate permission control initialization
writeConstructorJavaPermission(intface);
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 },");
+ print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class, String.class, int.class },");
println("new Class<?>[] { null, null, null });");
- println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);");
+ println("ports = (int[]) paramObj[0];");
+ if (callbackSkeleton) // If this is a callback skeleton then use the other port
+ println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], ports[1]);");
+ else
+ println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], ports[0]);");
println("}\n");
}
println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();");
} else {
- println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+ println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
println("objIdCnt++;");
}
}
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, objIdCnt);");
+ println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
println("objIdCnt++;");
println("}");
} else if (isList(paramType)) {
println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
- println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt));");
+ println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt, ports));");
println("objIdCnt++;");
println("}");
}
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 static int objIdCnt = 0;");
println("private IoTRMICall rmiCall;");
+ println("private int[] ports;\n");
}
println("\n");
}
*/
private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
- println("public " + newSkelClass + "(" + intface + " _mainObj, int _objectId) throws Exception {");
+ println("public " + newSkelClass + "(" + intface + " _mainObj, String _callbackAddress, int _objectId) throws Exception {");
+ println("callbackAddress = _callbackAddress;");
println("mainObj = _mainObj;");
println("objectId = _objectId;");
println("}\n");
println("#ifndef _" + newIntface.toUpperCase() + "_HPP__");
println("#define _" + newIntface.toUpperCase() + "_HPP__");
println("#include <iostream>");
+ updateIntfaceObjIdMap(intface, newIntface);
// Pass in set of methods and get import classes
Set<String> methods = intMeth.getValue();
Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, false);
/**
* HELPER: writeMethodCplusStub() writes the methods of the stub
*/
- private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
+ private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, boolean isGenCallbackStub) {
boolean isDefined = false;
for (String method : methods) {
println("}\n");
// Write the init callback helper method
if (isCallbackMethod && !isDefined) {
- writeInitCallbackCplusStub(callbackType, intDecl);
+ writeInitCallbackCplusStub(callbackType, intDecl, isGenCallbackStub);
writeInitCallbackSendInfoCplusStub(intDecl);
isDefined = true;
}
String param = methParams.get(i);
if (isArrayOrList(paramType, param)) { // Generate loop
println("for (" + getGenericType(paramType) + "* cb : " + getSimpleIdentifier(param) + ") {");
- println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);");
+ println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, callbackAddress, objIdCnt++);");
isArrayOrList = true;
callbackParam = getSimpleIdentifier(param);
} else
println(callbackType + "_CallbackSkeleton* skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
- getSimpleIdentifier(param) + ", objIdCnt++);");
+ getSimpleIdentifier(param) + ", callbackAddress, objIdCnt++);");
println("vecCallbackObj.push_back(skel" + i + ");");
if (isArrayOrList)
println("}");
// Get the object Id
Integer objId = mapIntfaceObjId.get(intface);
println("const static int objectId = " + objId + ";");
- mapNewIntfaceObjId.put(newIntface, objId);
- mapIntfaceObjId.put(intface, 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();
/**
* HELPER: writeInitCallbackCplusStub() writes the initialization of callback
*/
- private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl) {
+ private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, boolean isGenCallbackStub) {
println("void ___initCallBack() {");
println("bool bResult = false;");
- println("rmiObj = new IoTRMIObject(ports[0], &bResult);");
+ if (isGenCallbackStub) // Use the second port for a _CallbackStub class (callback in callback)
+ println("rmiObj = new IoTRMIObject(ports[1], &bResult);");
+ else
+ println("rmiObj = new IoTRMIObject(ports[0], &bResult);");
println("while (true) {");
println("char* method = rmiObj->getMethodBytes();");
writeCplusMethodCallbackPermission(intface);
int methodNumId = intDecl.getHelperMethodNumId(method);
println("int methodId = " + methodNumId + ";");
println("string retType = \"void\";");
- println("string paramCls[] = { \"int\", \"String\", \"int\" };");
+ println("string paramCls[] = { \"int*\", \"String\", \"int\" };");
println("int rev = 0;");
- println("void* paramObj[] = { &ports[0], &callbackAddress, &rev };");
+ println("void* paramObj[] = { &ports, &callbackAddress, &rev };");
println("void* retObj = NULL;");
println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
println("}\n");
writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses);
writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
// Write methods
- writeMethodCplusStub(methods, intDecl, callbackClasses);
+ writeMethodCplusStub(methods, intDecl, callbackClasses, false);
print("}"); println(";");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
println("vector<" + callbackType + "*> vecCallbackObj;");
println("static int objIdCnt;");
// TODO: Need to initialize address and ports if we want to have callback-in-callback
- println("string address;");
+ println("string callbackAddress;");
println("vector<int> ports;\n");
writePropertiesCplusPermission(callbackType);
}
*/
private void writeConstructorCplusCallbackStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
- println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId) {");
+ println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId, vector<int> _ports) {");
println("objectId = _objectId;");
println("rmiCall = _rmiCall;");
+ println("ports = _ports;");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
String callbackType = (String) it.next();
writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses);
writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
// Write methods
- writeMethodCplusStub(methods, intDecl, callbackClasses);
+ writeMethodCplusStub(methods, intDecl, callbackClasses, true);
println("};");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
println("static int objIdCnt;");
println("vector<" + exchangeType + "*> vecCallbackObj;");
println("IoTRMICall *rmiCall;");
+ println("vector<int> ports;");
}
println("IoTRMIObject *rmiObj;\n");
// Keep track of object Ids of all stubs registered to this interface
else
println("void ___regCB() {");
println("int numParam = 3;");
- println("int param1 = 0;");
+ println("vector<int> param1;");
println("string param2 = \"\";");
println("int param3 = 0;");
- println("string paramCls[] = { \"int\", \"String\", \"int\" };");
+ println("string paramCls[] = { \"int*\", \"String\", \"int\" };");
println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };");
println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
println("bool bResult = false;");
- println("rmiCall = new IoTRMICall(param1, param2.c_str(), param3, &bResult);");
+ if (callbackSkeleton)
+ println("rmiCall = new IoTRMICall(param1[1], param2.c_str(), param3, &bResult);");
+ else
+ println("rmiCall = new IoTRMICall(param1[0], param2.c_str(), param3, &bResult);");
println("}\n");
}
if (isArrayOrList(paramType, param)) {
println("vector<" + exchParamType + "*> stub" + i + ";");
println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
- println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+ println(exchParamType + "* cb" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
println("stub" + i + ".push_back(cb" + i + ");");
println("vecCallbackObj.push_back(cb" + i + ");");
println("objIdCnt++;");
println("}");
} else {
- println(exchParamType + "* stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
+ println(exchParamType + "* stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports);");
println("vecCallbackObj.push_back(stub" + i + ");");
println("objIdCnt++;");
}
println("IoTRMICall* rmiCall;");
println("vector<" + exchangeType + "*> vecCallbackObj;");
println("static int objIdCnt;");
+ println("vector<int> ports;\n");
}
println("\n");
}
// Basically the compiler needs to parse the policy (and requires) files for callback class first
private int getNewIntfaceObjectId(String newIntface) {
- if (!mapNewIntfaceObjId.containsKey(newIntface)) {
- throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
- "Please place the two files for callback class in front...\n");
- } else {
+// if (!mapNewIntfaceObjId.containsKey(newIntface)) {
+// throw new Error("IoTCompiler: Need to parse policy and requires files for callback class first! " +
+// "Please place the two files for callback class in front...\n");
+// return -1;
+// } else {
int retObjId = mapNewIntfaceObjId.get(newIntface);
return retObjId;
- }
+// }
}