Preparing for callback testing (creating new policy files); Adding error messsages...
authorrtrimana <rtrimana@uci.edu>
Tue, 29 Nov 2016 01:01:22 +0000 (17:01 -0800)
committerrtrimana <rtrimana@uci.edu>
Tue, 29 Nov 2016 01:01:22 +0000 (17:01 -0800)
config/iotpolicy/callbackpolicy.pol [new file with mode: 0644]
config/iotpolicy/callbackrequires.pol [new file with mode: 0644]
config/iotpolicy/testclasspolicy.pol
iotjava/Makefile
iotjava/iotpolicy/IoTCompiler.java
iotjava/iotrmi/C++/basics/CallBack.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/basics/TestClass.hpp
iotjava/iotrmi/Java/basics/CallBack.java [new file with mode: 0644]
iotjava/iotrmi/Java/basics/TestClass.java

diff --git a/config/iotpolicy/callbackpolicy.pol b/config/iotpolicy/callbackpolicy.pol
new file mode 100644 (file)
index 0000000..947b899
--- /dev/null
@@ -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 (file)
index 0000000..96126d3
--- /dev/null
@@ -0,0 +1,3 @@
+
+requires CallBackInterface with CallBack as interface CallBackInterfaceWithCallBack;
+
index 36ded083b60c9899e6fe5fee633f2c2566af14db..ebe25337afd9187c0c51ec324e75eb913585cecd 100644 (file)
@@ -24,6 +24,9 @@ public interface TestClassInterface {
        public List<Boolean> getBooleanList(List<Boolean> in);
        public List<Character> getCharList(List<Character> 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<Double> in)";
                method = "getBooleanList(List<Boolean> in)";
                method = "getCharList(List<Character> in)";
+               method = "registerCallback(CallBackInterface _cb)";
+               method = "callBack()";
                method = "getA()";
                method = "setA(int _int)";
                method = "setB(float _float)";
index ab069c48b94eec2448c9e20eac9f19aa09398afc..c00b34de6ead60fd9959467c7e025fe88d5fc27a 100644 (file)
@@ -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
index 651a4b8cda51d19214416a84c679631052cb552d..5af3c5ea6c98860e979ed268aa4a41fcb601dcce 100644 (file)
@@ -387,7 +387,7 @@ public class IoTCompiler {
                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);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("private final static int object" + newObjectId + "Id = " + 
                                newObjectId + ";\t//" + newIntface);
                        Set<String> methodIds = intMeth.getValue();
@@ -446,7 +446,7 @@ public class IoTCompiler {
                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);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("set" + newObjectId + "Allowed = Arrays.asList(object" + newObjectId +"Permission);");
                }
        }
@@ -482,7 +482,7 @@ public class IoTCompiler {
                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);
+                       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<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);
+                       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<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);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        println("const static int object" + newObjectId + "Id = " + newObjectId + ";");
                        println("const static set<int> set" + newObjectId + "Allowed;");
                }
@@ -3066,7 +3066,7 @@ public class IoTCompiler {
                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);
+                       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<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);
+                       int newObjectId = getNewIntfaceObjectId(newIntface);
                        print("const set<int> " + newSkelClass + "::set" + newObjectId + "Allowed {");
                        Set<String> methodIds = intMeth.getValue();
                        int i = 0;
@@ -4007,7 +4007,7 @@ public class IoTCompiler {
                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);
+                       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 <directory> after option: " + args[i]);
+                                                       throw new Error("IoTCompiler: ERROR - please provide <directory> 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 (file)
index 0000000..834f2a8
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef _CALLBACK_HPP__
+#define _CALLBACK_HPP__
+
+#include <iostream>
+#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
+
index fe758d728932b5f601a38ae98e193af516c6b69d..e5c38980f07cd15c42c2d018e1d65fb1e5714f8f 100644 (file)
@@ -37,6 +37,11 @@ class TestClass : public TestClassInterface {
                vector<bool>            getBooleanList(vector<bool> in);
                vector<char>            getCharList(vector<char> in);
 
+               // Callbacks
+               void                            registerCallback(CallBackInterface* _cb);
+               //void                          registerCallback(vector<CallBackInterface*> _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<CallBackInterface*>      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<CallBackInterface*> _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 (file)
index 0000000..83969af
--- /dev/null
@@ -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;
+       }
+}
index 6373238874d12ff8784608bd284c0e812cb2db18..616606d573adc6dc4764ec53cd22496e15060fbd 100644 (file)
@@ -10,6 +10,7 @@ public class TestClass implements TestClassInterface {
        private int intA;
        private float floatB;
        private String stringC;
+       private List<CallBackInterface> 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) {