println("private IoTRMIObject rmiObj;");
println("List<" + callbackType + "> listCallbackObj;");
println("private static int objIdCnt = 0;");
+ // Generate permission stuff for callback stubs
+ DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+ InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(callbackType);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newCallbackIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newCallbackIntface);
+ println("private final static int object" + newObjectId + "Id = " +
+ newObjectId + ";\t//" + newCallbackIntface);
+ Set<String> methodIds = intMeth.getValue();
+ print("private static Integer[] object" + newObjectId + "Permission = { ");
+ int i = 0;
+ for (String methodId : methodIds) {
+ int methodNumId = intDecl.getMethodNumId(methodId);
+ print(Integer.toString(methodNumId));
+ // Check if this is the last element (don't print a comma)
+ if (i != methodIds.size() - 1) {
+ print(", ");
+ }
+ i++;
+ }
+ println(" };");
+ println("private List<Integer> set" + newObjectId + "Allowed;");
+ }
}
println("\n");
}
/**
* HELPER: writeConstructorJavaStub() writes the constructor of the stub class
*/
- private void writeConstructorJavaStub(String intface, boolean callbackExist, Set<String> callbackClasses) {
+ private void writeConstructorJavaStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
- println("public " + intface + "(int _port, String _address, int _rev, int[] _ports) throws Exception {");
+ println("public " + newStubClass + "(int _port, String _address, int _rev, int[] _ports) throws Exception {");
println("address = _address;");
println("ports = _ports;");
println("rmiCall = new IoTRMICall(_port, _address, _rev);");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
String callbackType = (String) it.next();
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);");
+ }
println("listCallbackObj = new ArrayList<" + callbackType + ">();");
println("___initCallBack();");
}
}
+ /**
+ * HELPER: writeJavaMethodCallbackPermission() writes permission checks in stub for callbacks
+ */
+ private void writeJavaMethodCallbackPermission(String intface) {
+
+ println("int methodId = IoTRMIObject.getMethodId(method);");
+ // Get all the different stubs
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
+ println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);");
+ println("}");
+ }
+ }
+
+
/**
* HELPER: writeConstructorJavaStub() writes the constructor of the stub class
*/
println("rmiObj = new IoTRMIObject(ports[0]);");
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) {");
// Write properties
writePropertiesJavaStub(intface, newIntface, callbackExist, callbackClasses);
// Write constructor
- writeConstructorJavaStub(newStubClass, callbackExist, callbackClasses);
+ writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
// Write methods
writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
println("}");
println("private IoTRMIObject rmiObj;");
println("List<" + callbackType + "> listCallbackObj;");
println("private static int objIdCnt = 0;");
+ // Generate permission stuff for callback stubs
+ DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
+ InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(callbackType);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newCallbackIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newCallbackIntface);
+ println("private final static int object" + newObjectId + "Id = " +
+ newObjectId + ";\t//" + newCallbackIntface);
+ Set<String> methodIds = intMeth.getValue();
+ print("private static Integer[] object" + newObjectId + "Permission = { ");
+ int i = 0;
+ for (String methodId : methodIds) {
+ int methodNumId = intDecl.getMethodNumId(methodId);
+ print(Integer.toString(methodNumId));
+ // Check if this is the last element (don't print a comma)
+ if (i != methodIds.size() - 1) {
+ print(", ");
+ }
+ i++;
+ }
+ println(" };");
+ println("private List<Integer> set" + newObjectId + "Allowed;");
+ }
}
println("\n");
}
/**
* HELPER: writeConstructorJavaCallbackStub() writes the constructor of the callback stub class
*/
- private void writeConstructorJavaCallbackStub(String intface, boolean callbackExist, Set<String> callbackClasses) {
+ 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 " + intface + "(IoTRMICall _rmiCall, int _objectId) throws Exception {");
+ println("public " + newStubClass + "(IoTRMICall _rmiCall, int _objectId) throws Exception {");
println("objectId = _objectId;");
println("rmiCall = _rmiCall;");
if (callbackExist) {
Iterator it = callbackClasses.iterator();
String callbackType = (String) it.next();
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);");
+ }
println("listCallbackObj = new ArrayList<" + callbackType + ">();");
println("___initCallBack();");
println("// TODO: Add address and port initialization here if we want callback in callback!");
// Write properties
writePropertiesJavaCallbackStub(intface, newIntface, callbackExist, callbackClasses);
// Write constructor
- writeConstructorJavaCallbackStub(newStubClass, callbackExist, callbackClasses);
+ writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
// Write methods
// TODO: perhaps need to generate callback for callback
writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
/**
* HELPER: writePropertiesJavaSkeleton() writes the properties of the skeleton class
*/
- private void writePropertiesJavaSkeleton(String intface, boolean callbackExist) {
+ private void writePropertiesJavaSkeleton(String intface, boolean callbackExist, InterfaceDecl intDecl) {
println("private " + intface + " mainObj;");
//println("private int ports;");
int newObjectId = mapNewIntfaceObjId.get(newIntface);
println("private final static int object" + newObjectId + "Id = " +
newObjectId + ";\t//" + newIntface);
+ Set<String> methodIds = intMeth.getValue();
+ print("private static Integer[] object" + newObjectId + "Permission = { ");
+ int i = 0;
+ for (String methodId : methodIds) {
+ int methodNumId = intDecl.getMethodNumId(methodId);
+ print(Integer.toString(methodNumId));
+ // Check if this is the last element (don't print a comma)
+ if (i != methodIds.size() - 1) {
+ print(", ");
+ }
+ i++;
+ }
+ println(" };");
+ println("private List<Integer> set" + newObjectId + "Allowed;");
}
println("\n");
}
println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
println("mainObj = _mainObj;");
println("rmiObj = new IoTRMIObject(_port);");
- //println("set0Allowed = Arrays.asList(object0Permission);");
+ // Generate permission control initialization
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);");
+ }
println("___waitRequestInvokeMethod();");
println("}\n");
}
}
+ /**
+ * HELPER: writeJavaMethodPermission() writes permission checks in skeleton
+ */
+ private void writeJavaMethodPermission(String intface) {
+
+ // Get all the different stubs
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("if (_objectId == object" + newObjectId + "Id) {");
+ println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
+ println("throw new Error(\"Object with object Id: \" + _objectId + \" is not allowed to access method: \" + methodId);");
+ println("}");
+ println("else {");
+ println("throw new Error(\"Object Id: \" + _objectId + \" not recognized!\");");
+ println("}");
+ println("}");
+ }
+ }
+
+
/**
* HELPER: writeJavaWaitRequestInvokeMethod() writes the main loop of the skeleton class
*/
- private void writeJavaWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
+ private void writeJavaWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist, String intface) {
// Use this set to handle two same methodIds
Set<String> uniqueMethodIds = new HashSet<String>();
println("rmiObj.getMethodBytes();");
println("int _objectId = rmiObj.getObjectId();");
println("int methodId = rmiObj.getMethodId();");
- // TODO: code the permission check here!
+ // Generate permission check
+ writeJavaMethodPermission(intface);
println("switch (methodId) {");
// Print methods and method Ids
for (String method : methods) {
println("");
println("public class " + newSkelClass + " implements " + intface + " {\n");
// Write properties
- writePropertiesJavaSkeleton(intface, callbackExist);
+ writePropertiesJavaSkeleton(intface, callbackExist, intDecl);
// Write constructor
writeConstructorJavaSkeleton(newSkelClass, intface);
// Write methods
// Write method helper
writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
// Write waitRequestInvokeMethod() - main loop
- writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist);
+ writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
println("}");
pw.close();
System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java...");
println("IoTRMIObject *rmiObj;");
println("vector<" + callbackType + "*> vecCallbackObj;");
println("static int objIdCnt;");
+ // Generate permission stuff for callback stubs
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(callbackType);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newCallbackIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newCallbackIntface);
+ println("const static int object" + newObjectId + "Id = " + newObjectId + ";");
+ println("const static set<int> set" + newObjectId + "Allowed;");
+ }
}
println("\n");
}
/**
- * HELPER: writeInitCallbackCplusStub() writes the constructor of the stub class
+ * HELPER: writeCplusMethodCallbackPermission() writes permission checks in stub for callbacks
+ */
+ private void writeCplusMethodCallbackPermission(String intface) {
+
+ println("int methodId = IoTRMIObject::getMethodId(method);");
+ // Get all the different stubs
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
+ println("cerr << \"Callback object for " + intface + " is not allowed to access method: \" << methodId;");
+ println("exit(-1);");
+ println("}");
+ }
+ }
+
+
+ /**
+ * HELPER: writeInitCallbackCplusStub() writes the initialization of callback
*/
private void writeInitCallbackCplusStub(String intface, InterfaceDecl intDecl) {
println("rmiObj = new IoTRMIObject(ports[0], &bResult);");
println("while (true) {");
println("char* method = rmiObj->getMethodBytes();");
- println("int methodId = IoTRMIObject::getMethodId(method);");
+ 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 +
/**
- * HELPER: writeInitCallbackSendInfoCplusStub() writes the constructor of the stub class
+ * HELPER: writeInitCallbackSendInfoCplusStub() writes the initialization of callback
*/
private void writeInitCallbackSendInfoCplusStub(InterfaceDecl intDecl) {
// Write methods
writeMethodCplusStub(methods, intDecl, callbackClasses);
print("}"); println(";");
+ if (callbackExist)
+ writePermissionInitializationCplus(intface, newStubClass, intDecl);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".hpp...");
// TODO: Need to initialize address and ports if we want to have callback-in-callback
println("string address;");
println("vector<int> ports;\n");
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(callbackType);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newCallbackIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newCallbackIntface);
+ println("const static int object" + newObjectId + "Id = " + newObjectId + ";");
+ println("const static set<int> set" + newObjectId + "Allowed;");
+ }
}
println("\n");
}
writeDeconstructorCplusStub(newStubClass, callbackExist, callbackClasses);
// Write methods
writeMethodCplusStub(methods, intDecl, callbackClasses);
- print("}"); println(";");
+ println("};");
+ if (callbackExist)
+ writePermissionInitializationCplus(intface, newStubClass, intDecl);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated callback stub class " + newIntface + ".hpp...");
private void writePropertiesCplusSkeleton(String intface, boolean callbackExist, Set<String> callbackClasses) {
println(intface + " *mainObj;");
- //println("private int ports;");
// Callback
if (callbackExist) {
Iterator it = callbackClasses.iterator();
for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
String newIntface = intMeth.getKey();
int newObjectId = mapNewIntfaceObjId.get(newIntface);
-// println("const static int object" + newObjectId + "Id = " +
-// newObjectId + ";\t//" + newIntface);
+ println("const static int object" + newObjectId + "Id = " + newObjectId + ";");
+ println("const static set<int> set" + newObjectId + "Allowed;");
}
println("\n");
}
+ /**
+ * HELPER: writePermissionInitializationCplus() writes the initialization of permission set
+ */
+ private void writePermissionInitializationCplus(String intface, String newSkelClass, InterfaceDecl intDecl) {
+
+ // Keep track of object Ids of all stubs registered to this interface
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ print("const set<int> " + newSkelClass + "::set" + newObjectId + "Allowed {");
+ Set<String> methodIds = intMeth.getValue();
+ int i = 0;
+ for (String methodId : methodIds) {
+ int methodNumId = intDecl.getMethodNumId(methodId);
+ print(Integer.toString(methodNumId));
+ // Check if this is the last element (don't print a comma)
+ if (i != methodIds.size() - 1) {
+ print(", ");
+ }
+ i++;
+ }
+ println(" };");
+ }
+ }
+
+
/**
* HELPER: writeConstructorCplusSkeleton() writes the constructor of the skeleton class
*/
}
+ /**
+ * HELPER: writeCplusMethodPermission() writes permission checks in skeleton
+ */
+ private void writeCplusMethodPermission(String intface) {
+
+ // Get all the different stubs
+ Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
+ for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
+ String newIntface = intMeth.getKey();
+ int newObjectId = mapNewIntfaceObjId.get(newIntface);
+ println("if (_objectId == object" + newObjectId + "Id) {");
+ println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {");
+ println("cerr << \"Object with object Id: \" << _objectId << \" is not allowed to access method: \" << methodId << endl;");
+ println("exit(-1);");
+ println("}");
+ println("else {");
+ println("cerr << \"Object Id: \" << _objectId << \" not recognized!\" << endl;");
+ println("exit(-1);");
+ println("}");
+ println("}");
+ }
+ }
+
+
/**
* HELPER: writeCplusWaitRequestInvokeMethod() writes the main loop of the skeleton class
*/
- private void writeCplusWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
+ private void writeCplusWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist, String intface) {
// Use this set to handle two same methodIds
Set<String> uniqueMethodIds = new HashSet<String>();
println("rmiObj->getMethodBytes();");
println("int _objectId = rmiObj->getObjectId();");
println("int methodId = rmiObj->getMethodId();");
- // TODO: code the permission check here!
+ // Generate permission check
+ writeCplusMethodPermission(intface);
println("switch (methodId) {");
// Print methods and method Ids
for (String method : methods) {
// Write method helper
writeMethodHelperCplusSkeleton(methods, intDecl, callbackClasses);
// Write waitRequestInvokeMethod() - main loop
- writeCplusWaitRequestInvokeMethod(methods, intDecl, callbackExist);
+ writeCplusWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
println("};");
+ writePermissionInitializationCplus(intface, newSkelClass, intDecl);
println("#endif");
pw.close();
System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".hpp...");
importClasses.add("java.io.IOException");
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");
List<String> importClasses = new ArrayList<String>();
// Add the standard list first
importClasses.add("<vector>");
+ importClasses.add("<set>");
importClasses.add("\"IoTRMICall.hpp\"");
importClasses.add("\"IoTRMIObject.hpp\"");