private Map<String,ParseTreeHandler> mapIntfacePTH;
private Map<String,DeclarationHandler> mapIntDeclHand;
private Map<String,Map<String,Set<String>>> mapInt2NewInts;
+ private Map<String,String> mapInt2NewIntName;
// Data structure to store our types (primitives and non-primitives) for compilation
private Map<String,String> mapPrimitives;
private Map<String,String> mapNonPrimitivesJava;
private PrintWriter pw;
private String dir;
private String subdir;
+ private Map<String,Integer> mapPortCount; // Counter for ports
+ private static int portCount = 0;
/**
mapIntfacePTH = new HashMap<String,ParseTreeHandler>();
mapIntDeclHand = new HashMap<String,DeclarationHandler>();
mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
+ mapInt2NewIntName = new HashMap<String,String>();
mapIntfaceObjId = new HashMap<String,Integer>();
mapNewIntfaceObjId = new HashMap<String,Integer>();
mapPrimitives = new HashMap<String,String>();
arraysToMap(mapNonPrimitivesJava, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitiveJavaLibs);
mapNonPrimitivesCplus = new HashMap<String,String>();
arraysToMap(mapNonPrimitivesCplus, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
+ mapPortCount = new HashMap<String,Integer>();
pw = null;
dir = OUTPUT_DIRECTORY;
subdir = null;
}
// Add interface and methods information into map
mapNewIntMethods.put(strInt, setMethods);
+ // Map new interface method name to the original interface
+ // TODO: perhaps need to check in the future if we have more than 1 stub interface for one original interface
+ mapInt2NewIntName.put(origInt, strInt);
}
// Map the map of interface-methods to the original interface
mapInt2NewInts.put(origInt, mapNewIntMethods);
if (callbackExist) {
Iterator it = callbackClasses.iterator();
String callbackType = (String) it.next();
- writeConstructorJavaPermission(intface);
+ 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");
}
+ /**
+ * HELPER: getPortCount() gets port count for different stubs and skeletons
+ */
+ private int getPortCount(String intface) {
+
+ if (!mapPortCount.containsKey(intface))
+ mapPortCount.put(intface, portCount++);
+ return mapPortCount.get(intface);
+ }
+
+
/**
* HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
*/
- private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl, boolean isGenCallbackStub) {
+ 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 {");
- 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]);");
+ int port = getPortCount(newStubClass);
+ println("rmiObj = new IoTRMIObject(ports[" + port + "]);");
println("while (true) {");
println("byte[] method = rmiObj.getMethodBytes();");
- writeJavaMethodCallbackPermission(intface);
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 {");
/**
* HELPER: writeMethodJavaStub() writes the methods of the stub class
*/
- private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, boolean isGenCallbackStub) {
+ private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, String newStubClass) {
boolean isDefined = false;
for (String method : methods) {
println("}\n");
// Write the init callback helper method
if (isCallbackMethod && !isDefined) {
- writeInitCallbackJavaStub(callbackType, intDecl, isGenCallbackStub);
+ writeInitCallbackJavaStub(callbackType, intDecl, newStubClass);
isDefined = true;
}
}
}
+ /**
+ * HELPER: getStubInterface() gets stub interface name based on original interface
+ */
+ public String getStubInterface(String intface) {
+
+ return mapInt2NewIntName.get(intface);
+ }
+
+
/**
* generateJavaStubClasses() generate stubs based on the methods list in Java
*/
// Write constructor
writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
// Write methods
- writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, false);
+ writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass);
println("}");
pw.close();
System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java...");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
String callbackType = (String) it.next();
- writeConstructorJavaPermission(intface);
+ 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");
writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
// Write methods
// TODO: perhaps need to generate callback for callback
- writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, true);
+ writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses, newStubClass);
println("}");
pw.close();
System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
/**
* HELPER: writeInitCallbackJavaSkeleton() writes the init callback method for skeleton class
*/
- private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton) {
+ private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton, String intface) {
// This is a callback skeleton generation
if (callbackSkeleton)
print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int[].class, String.class, int.class },");
println("new Class<?>[] { null, null, null });");
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]);");
+ String stubInt = null;
+ if (callbackSkeleton)
+ stubInt = getStubInterface(intface) + "_CallbackStub";
else
- println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], ports[0]);");
+ 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
*/
private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses,
- boolean callbackSkeleton) {
+ boolean callbackSkeleton, String intface) {
boolean isDefined = false;
for (String method : methods) {
writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method));
println("}\n");
if (isCallbackMethod && !isDefined) { // Make sure that this function is only defined once!
- writeInitCallbackJavaSkeleton(callbackSkeleton);
+ writeInitCallbackJavaSkeleton(callbackSkeleton, intface);
isDefined = true;
}
}
println("}");
} else if (isList(paramType)) {
println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
- println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt, ports));");
+ println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports));");
println("objIdCnt++;");
println("}");
}
// Write constructor
writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
// Write methods
- writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false);
+ writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false, intface);
// Write method helper
writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
// Write waitRequestInvokeMethod() - main loop
// Write constructor
writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods);
// Write methods
- writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true);
+ writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true, intface);
// Write method helper
writeMethodHelperJavaCallbackSkeleton(methods, intDecl, callbackClasses);
// Write waitRequestInvokeMethod() - main loop
/**
* HELPER: writeMethodCplusStub() writes the methods of the stub
*/
- private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, boolean isGenCallbackStub) {
+ private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses, String newStubClass) {
boolean isDefined = false;
for (String method : methods) {
println("}\n");
// Write the init callback helper method
if (isCallbackMethod && !isDefined) {
- writeInitCallbackCplusStub(callbackType, intDecl, isGenCallbackStub);
+ writeInitCallbackCplusStub(callbackType, intDecl, newStubClass);
writeInitCallbackSendInfoCplusStub(intDecl);
isDefined = true;
}
// Get the object Id
Integer objId = mapIntfaceObjId.get(intface);
println("const 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();
private void writeConstructorCplusStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
println(newStubClass +
- "(int _port, const char* _skeletonAddress, const char* _callbackAddress, int _rev, bool* _bResult, vector<int> _ports) {");
+ "(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector<int> _ports) {");
println("callbackAddress = _callbackAddress;");
println("ports = _ports;");
println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev, _bResult);");
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();");
/**
* HELPER: writeInitCallbackCplusStub() writes the initialization of callback
*/
- private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, boolean isGenCallbackStub) {
+ private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl, String newStubClass) {
println("void ___initCallBack() {");
println("bool bResult = false;");
- 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);");
+ int port = getPortCount(newStubClass);
+ println("rmiObj = new IoTRMIObject(ports[" + port + "], &bResult);");
println("while (true) {");
println("char* method = rmiObj->getMethodBytes();");
- writeCplusMethodCallbackPermission(intface);
+ //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 {");
writeConstructorCplusStub(newStubClass, callbackExist, callbackClasses);
writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
// Write methods
- writeMethodCplusStub(methods, intDecl, callbackClasses, false);
+ writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass);
print("}"); println(";");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
println("IoTRMICall *rmiCall;");
// Get the object Id
println("int objectId;");
+ println("vector<int> ports;\n");
+ println("string callbackAddress;");
if (callbackExist) {
// We assume that each class only has one callback interface for now
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 callbackAddress;");
- println("vector<int> ports;\n");
writePropertiesCplusPermission(callbackType);
}
println("\n");
*/
private void writeConstructorCplusCallbackStub(String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
- println(newStubClass + "(IoTRMICall* _rmiCall, int _objectId, vector<int> _ports) {");
+ println(newStubClass + "(IoTRMICall* _rmiCall, string _callbackAddress, int _objectId, vector<int> _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();");
writeConstructorCplusCallbackStub(newStubClass, callbackExist, callbackClasses);
writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
// Write methods
- writeMethodCplusStub(methods, intDecl, callbackClasses, true);
+ writeMethodCplusStub(methods, intDecl, callbackClasses, newStubClass);
println("};");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
private void writePropertiesCplusSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
println(intface + " *mainObj;");
+ println("vector<int> ports;");
+ println("string callbackAddress;");
// Callback
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
*/
private void writeConstructorCplusSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
- println(newSkelClass + "(" + intface + " *_mainObj, int _port) {");
+ println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _port) {");
println("bool _bResult = false;");
println("mainObj = _mainObj;");
+ println("callbackAddress = _callbackAddress;");
println("rmiObj = new IoTRMIObject(_port, &_bResult);");
writeCplusInitCallbackPermission(intface, intDecl, callbackExist);
writeStructPermissionCplusSkeleton(methods, intDecl, intface);
/**
* HELPER: writeInitCallbackCplusSkeleton() writes the init callback method for skeleton class
*/
- private void writeInitCallbackCplusSkeleton(boolean callbackSkeleton) {
+ private void writeInitCallbackCplusSkeleton(boolean callbackSkeleton, String intface) {
// This is a callback skeleton generation
if (callbackSkeleton)
println("void* paramObj[] = { ¶m1, ¶m2, ¶m3 };");
println("rmiObj->getMethodParams(paramCls, numParam, paramObj);");
println("bool bResult = false;");
+ String stubInt = null;
if (callbackSkeleton)
- println("rmiCall = new IoTRMICall(param1[1], param2.c_str(), param3, &bResult);");
+ stubInt = getStubInterface(intface) + "_CallbackStub";
else
- println("rmiCall = new IoTRMICall(param1[0], param2.c_str(), param3, &bResult);");
+ 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
*/
private void writeMethodCplusSkeleton(Collection<String> methods, InterfaceDecl intDecl,
- Set<String> callbackClasses, boolean callbackSkeleton) {
+ Set<String> callbackClasses, boolean callbackSkeleton, String intface) {
boolean isDefined = false;
for (String method : methods) {
writeStdMethodBodyCplusSkeleton(methParams, methodId, intDecl.getMethodType(method));
println("}\n");
if (isCallbackMethod && !isDefined) {
- writeInitCallbackCplusSkeleton(callbackSkeleton);
+ writeInitCallbackCplusSkeleton(callbackSkeleton, intface);
isDefined = true;
}
}
// Write deconstructor
writeDeconstructorCplusSkeleton(newSkelClass, callbackExist, callbackClasses);
// Write methods
- writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false);
+ writeMethodCplusSkeleton(methods, intDecl, callbackClasses, false, intface);
// Write method helper
writeMethodHelperCplusSkeleton(methods, intDecl, callbackClasses);
// Write waitRequestInvokeMethod() - main loop
println(intface + " *mainObj;");
// Keep track of object Ids of all stubs registered to this interface
println("int objectId;");
+ println("vector<int> ports;\n");
+ println("string callbackAddress;");
// Callback
if (callbackExist) {
Iterator it = callbackClasses.iterator();
println("IoTRMICall* rmiCall;");
println("vector<" + exchangeType + "*> vecCallbackObj;");
println("static int objIdCnt;");
- println("vector<int> ports;\n");
}
println("\n");
}
*/
private void writeConstructorCplusCallbackSkeleton(String newSkelClass, String intface, boolean callbackExist, InterfaceDecl intDecl, Collection<String> methods) {
- println(newSkelClass + "(" + intface + " *_mainObj, int _objectId) {");
+ println(newSkelClass + "(" + intface + " *_mainObj, string _callbackAddress, int _objectId) {");
println("mainObj = _mainObj;");
+ println("callbackAddress = _callbackAddress;");
println("objectId = _objectId;");
println("}\n");
}
// Write deconstructor
writeDeconstructorCplusCallbackSkeleton(newSkelClass, callbackExist, callbackClasses);
// Write methods
- writeMethodCplusSkeleton(methods, intDecl, callbackClasses, true);
+ writeMethodCplusSkeleton(methods, intDecl, callbackClasses, true, intface);
// Write method helper
writeMethodHelperCplusCallbackSkeleton(methods, intDecl, callbackClasses);
// Write waitRequestInvokeMethod() - main loop