From: rtrimana Date: Tue, 29 Nov 2016 01:01:22 +0000 (-0800) Subject: Preparing for callback testing (creating new policy files); Adding error messsages... X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iot2.git;a=commitdiff_plain;h=f4d0e8746bf8895c2f644b8c1b3ef4df6e23547d Preparing for callback testing (creating new policy files); Adding error messsages that require callback policy files (with class definition) to be parsed first --- diff --git a/config/iotpolicy/callbackpolicy.pol b/config/iotpolicy/callbackpolicy.pol new file mode 100644 index 0000000..947b899 --- /dev/null +++ b/config/iotpolicy/callbackpolicy.pol @@ -0,0 +1,14 @@ +public interface CallBackInterface { + + public int printInt(); + public void setInt(int _i); + + capability CallBack { + description = "The quick brown fox jumps over the smart dog"; + description = "Another description"; + method = "printInt()"; + method = "setInt(int _i)"; + } +} + + diff --git a/config/iotpolicy/callbackrequires.pol b/config/iotpolicy/callbackrequires.pol new file mode 100644 index 0000000..96126d3 --- /dev/null +++ b/config/iotpolicy/callbackrequires.pol @@ -0,0 +1,3 @@ + +requires CallBackInterface with CallBack as interface CallBackInterfaceWithCallBack; + diff --git a/config/iotpolicy/testclasspolicy.pol b/config/iotpolicy/testclasspolicy.pol index 36ded08..ebe2533 100644 --- a/config/iotpolicy/testclasspolicy.pol +++ b/config/iotpolicy/testclasspolicy.pol @@ -24,6 +24,9 @@ public interface TestClassInterface { public List getBooleanList(List in); public List getCharList(List in); + public void registerCallback(CallBackInterface _cb); + public int callBack(); + public int getA(); public void setA(int _int); public void setB(float _float); @@ -55,6 +58,8 @@ public interface TestClassInterface { method = "getDoubleList(List in)"; method = "getBooleanList(List in)"; method = "getCharList(List in)"; + method = "registerCallback(CallBackInterface _cb)"; + method = "callBack()"; method = "getA()"; method = "setA(int _int)"; method = "setB(float _float)"; diff --git a/iotjava/Makefile b/iotjava/Makefile index ab069c4..c00b34d 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -21,7 +21,8 @@ compiler: PHONY += run-compiler run-compiler: #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler camerapolicy.pol camerarequires.pol lightbulbpolicy.pol lightbulbrequires.pol -cplus Cplus -java Java - cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy.pol testclassrequires.pol -cplus Cplus -java Java + #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy.pol testclassrequires.pol callbackpolicy.pol callbackrequires.pol -cplus Cplus -java Java + cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler callbackpolicy.pol callbackrequires.pol testclasspolicy.pol testclassrequires.pol -cplus Cplus -java Java # Runtime system compilation PHONY += runtime diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 651a4b8..5af3c5e 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -387,7 +387,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(newIntface); println("private final static int object" + newObjectId + "Id = " + newObjectId + ";\t//" + newIntface); Set methodIds = intMeth.getValue(); @@ -446,7 +446,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(newIntface); println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);"); } } @@ -482,7 +482,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(newIntface); println("if (!set" + newObjectId + "Allowed.contains(methodId)) {"); println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);"); println("}"); @@ -1984,7 +1984,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(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);"); @@ -2971,7 +2971,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(newIntface); println("const static int object" + newObjectId + "Id = " + newObjectId + ";"); println("const static set set" + newObjectId + "Allowed;"); } @@ -3066,7 +3066,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(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);"); @@ -3299,7 +3299,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(newIntface); print("const set " + newSkelClass + "::set" + newObjectId + "Allowed {"); Set methodIds = intMeth.getValue(); int i = 0; @@ -4007,7 +4007,7 @@ public class IoTCompiler { Map> mapNewIntMethods = mapInt2NewInts.get(intface); for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); - int newObjectId = mapNewIntfaceObjId.get(newIntface); + int newObjectId = getNewIntfaceObjectId(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;"); @@ -4420,7 +4420,7 @@ public class IoTCompiler { pn = (ParseNode) parse.parse().value; } catch (Exception e) { e.printStackTrace(); - throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file); + throw new Error("IoTCompiler: ERROR parsing policy file or wrong command line option: " + file + "\n"); } return pn; @@ -4530,6 +4530,21 @@ public class IoTCompiler { } + // Check and find object Id for new interface in mapNewIntfaceObjId (callbacks) + // Throw an error if the new interface is not found! + // 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 { + int retObjId = mapNewIntfaceObjId.get(newIntface); + return retObjId; + } + } + + // Return parameter category, i.e. PRIMITIVES, NONPRIMITIVES, USERDEFINED, ENUM, or STRUCT private ParamCategory getParamCategory(String paramType) { @@ -5117,7 +5132,7 @@ public class IoTCompiler { } else { throw new Error("IoTCompiler: Ambiguous stub interfaces: " + setExchInt.toString() + ". Only one new interface can be declared if the object " + intface + - " needs to be passed in as an input parameter!"); + " needs to be passed in as an input parameter!\n"); } } else { // NULL value - this means policy files missing @@ -5126,7 +5141,7 @@ public class IoTCompiler { " If this is an array please type the brackets after the variable name," + " e.g. \"String str[]\", not \"String[] str\"." + " If this is a Collections (Java) / STL (C++) type, this compiler only" + - " supports List/ArrayList (Java) or list (C++)."); + " supports List/ArrayList (Java) or list (C++).\n"); } } @@ -5185,12 +5200,12 @@ public class IoTCompiler { // Error checking if (!args[i].equals("-java") && !args[i].equals("-cplus")) { - throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i]); + throw new Error("IoTCompiler: ERROR - unrecognized command line option: " + args[i] + "\n"); } else { if (i + 1 < args.length) { comp.setDirectory(args[i+1]); } else - throw new Error("IoTCompiler: ERROR - please provide after option: " + args[i]); + throw new Error("IoTCompiler: ERROR - please provide after option: " + args[i] + "\n"); if (args[i].equals("-java")) { comp.generateEnumJava(); @@ -5218,7 +5233,7 @@ public class IoTCompiler { } else { // Need to at least have exactly 2 parameters, i.e. main policy file and requires file IoTCompiler.printUsage(); - throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!"); + throw new Error("IoTCompiler: At least two arguments (main and requires policy files) have to be provided!\n"); } } } diff --git a/iotjava/iotrmi/C++/basics/CallBack.hpp b/iotjava/iotrmi/C++/basics/CallBack.hpp new file mode 100644 index 0000000..834f2a8 --- /dev/null +++ b/iotjava/iotrmi/C++/basics/CallBack.hpp @@ -0,0 +1,41 @@ +#ifndef _CALLBACK_HPP__ +#define _CALLBACK_HPP__ + +#include +#include "CallBackInterface.hpp" + +using namespace std; + +class CallBack : public CallBackInterface { + public: + CallBack(int _i); + + int printInt(); + void setInt(int _i); + + private: + int intA; +}; + + +// Constructor +CallBack::CallBack(int _i) { + + intA = _i; +} + + +int CallBack::printInt() { + + cout << "Integer: " << intA << endl; + return intA; +} + + +void CallBack::setInt(int _i) { + + intA = _i; +} + +#endif + diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index fe758d7..e5c3898 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -37,6 +37,11 @@ class TestClass : public TestClassInterface { vector getBooleanList(vector in); vector getCharList(vector in); + // Callbacks + void registerCallback(CallBackInterface* _cb); + //void registerCallback(vector _cb); + int callBack(); + int getA(); void setA(int _int); void setB(float _float); @@ -49,6 +54,7 @@ class TestClass : public TestClassInterface { int intA; float floatB; string stringC; + vector cbvec; }; @@ -70,6 +76,33 @@ TestClass::TestClass(int _int, float _float, string _string) { } +void TestClass::registerCallback(CallBackInterface* _cb) { + + cbvec.push_back(_cb); + cout << "Registering callback object!" << endl; +} + + +/*void TestClass::registerCallback(vector _cb) { + + for (CallBackInterface* cb : _cb) { + cbvec.push_back(cb); + cout << "Registering callback object!" << endl; + } +}*/ + + +int TestClass::callBack() { + + int sum = 0; + for (CallBackInterface* cb : cbvec) { + sum = sum + cb->printInt(); + } + + return sum; +} + + // Single variables char TestClass::getByte(char in) { diff --git a/iotjava/iotrmi/Java/basics/CallBack.java b/iotjava/iotrmi/Java/basics/CallBack.java new file mode 100644 index 0000000..83969af --- /dev/null +++ b/iotjava/iotrmi/Java/basics/CallBack.java @@ -0,0 +1,29 @@ + +public class CallBack implements CallBackInterface { + + /** + * Class Properties + */ + private int intA; + + /** + * Constructors + */ + public CallBack(int _i) { + + intA = _i; + } + + + public int printInt() { + + System.out.println("Integer: " + intA); + return intA; + } + + + public void setInt(int _i) { + + intA = _i; + } +} diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java index 6373238..616606d 100644 --- a/iotjava/iotrmi/Java/basics/TestClass.java +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -10,6 +10,7 @@ public class TestClass implements TestClassInterface { private int intA; private float floatB; private String stringC; + private List cblist; /** * Constructors @@ -30,6 +31,33 @@ public class TestClass implements TestClassInterface { } + // Callback + public void registerCallback(CallBackInterface _cb) { + + cblist.add(cb); + System.out.println("Registering callback object!"); + } + + + /*public void registerCallback(CallBackInterface[] _cb) { + + for (CallBackInterface cb : _cb) { + cblist.add(cb); + System.out.println("Registering callback object!"); + } + }*/ + + + public int callBack() { + + int sum = 0; + for (CallBackInterface cb : cblist) { + sum = sum + cb.printInt(); + } + return sum; + } + + // Single variables public byte getByte(byte in) {