From 4b928ccacd87cf190fb7a1b7ff5649a96487d09b Mon Sep 17 00:00:00 2001 From: rtrimana Date: Fri, 25 Nov 2016 14:06:36 -0800 Subject: [PATCH] Adding new policy, Java, and Cpp files for testing/debugging --- config/iotpolicy/testclasspolicy.pol | 32 +++++++ config/iotpolicy/testclassrequires.pol | 3 + iotjava/Makefile | 30 +++++- iotjava/iotpolicy/IoTCompiler.java | 19 ++-- iotjava/iotrmi/C++/basics/TestClass.hpp | 92 +++++++++++++++++++ .../iotrmi/C++/basics/TestClass_Skeleton.cpp | 20 ++++ iotjava/iotrmi/C++/basics/TestClass_Stub.cpp | 28 ++++++ iotjava/iotrmi/Java/basics/TestClass.java | 74 +++++++++++++++ .../Java/basics/TestClass_Skeleton.java | 11 +++ .../iotrmi/Java/basics/TestClass_Stub.java | 23 +++++ 10 files changed, 322 insertions(+), 10 deletions(-) create mode 100644 config/iotpolicy/testclasspolicy.pol create mode 100644 config/iotpolicy/testclassrequires.pol create mode 100644 iotjava/iotrmi/C++/basics/TestClass.hpp create mode 100644 iotjava/iotrmi/C++/basics/TestClass_Skeleton.cpp create mode 100644 iotjava/iotrmi/C++/basics/TestClass_Stub.cpp create mode 100644 iotjava/iotrmi/Java/basics/TestClass.java create mode 100644 iotjava/iotrmi/Java/basics/TestClass_Skeleton.java create mode 100644 iotjava/iotrmi/Java/basics/TestClass_Stub.java diff --git a/config/iotpolicy/testclasspolicy.pol b/config/iotpolicy/testclasspolicy.pol new file mode 100644 index 0000000..21eb09e --- /dev/null +++ b/config/iotpolicy/testclasspolicy.pol @@ -0,0 +1,32 @@ +public interface TestClassInterface { + + public void setA(int _int); + public void setB(float _float); + public void setC(String _string); + public String sumArray(String newA[]); + public int setAndGetA(int newA); + public int setACAndGetA(String newC, int newA); + + capability Setters { + description = "All the set methods"; + method = "setA(int _int)"; + method = "setB(float _float)"; + method = "setC(String _string)"; + } + + capability SetAndGets { + description = "All the set-and-get methods"; + method = "sumArray(String newA[])"; + method = "setAndGetA(int newA)"; + method = "setACAndGetA(String newC, int newA)"; + } + + struct StructJ { + + string name; + float value; + int year; + } +} + + diff --git a/config/iotpolicy/testclassrequires.pol b/config/iotpolicy/testclassrequires.pol new file mode 100644 index 0000000..713fb4e --- /dev/null +++ b/config/iotpolicy/testclassrequires.pol @@ -0,0 +1,3 @@ + +requires TestClassInterface with Setters, SetAndGets as interface TestClassComplete; + diff --git a/iotjava/Makefile b/iotjava/Makefile index d7f6a9d..ce06bed 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -20,7 +20,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 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 # Runtime system compilation PHONY += runtime @@ -30,11 +31,34 @@ runtime: # TODO: Can remove this later - just to test-compile the resulted files from the compiler PHONY += compile compile: + cp ./iotrmi/Java/basics/* $(BIN_DIR)/iotpolicy/output_files/Java + cp ./iotrmi/C++/basics/* $(BIN_DIR)/iotpolicy/output_files/Cplus cd $(BIN_DIR)/iotpolicy/output_files; cp *.java ./Java cd $(BIN_DIR)/iotpolicy/output_files; cp *.hpp ./Cplus -# cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) *.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) *.java # cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./*.hpp --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ - cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./Camera_Skeleton.hpp --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass_Skeleton.cpp -o ./TestClass_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass_Stub.cpp -o ./TestClass_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + +PHONY += run-java-skeleton +run-java-skeleton: + cd ../bin/iotpolicy/output_files/Java; $(JAVA) -cp .:../../../$(BIN_DIR) TestClass_Skeleton + +PHONY += run-java-stub +run-java-stub: + cd ../bin/iotpolicy/output_files/Java; $(JAVA) -cp .:../../../$(BIN_DIR) TestClass_Stub + +PHONY += run-cplus-skeleton +run-cplus-skeleton: + ../bin/iotpolicy/output_files/Cplus/TestClass_Skeleton.out + +PHONY += run-cplus-stub +run-cplus-stub: + ../bin/iotpolicy/output_files/Cplus/TestClass_Stub.out + +PHONY += folderclean +folderclean: + rm -rf $(BIN_DIR)/iotpolicy/output_files PHONY += clean clean: diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 674c98c..028373b 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -1589,7 +1589,8 @@ public class IoTCompiler { * HELPER: writeMethodHelperReturnJavaSkeleton() writes return statement part in skeleton */ private void writeMethodHelperReturnJavaSkeleton(InterfaceDecl intDecl, List methParams, - List methPrmTypes, String method, boolean isCallbackMethod, String callbackType) { + List methPrmTypes, String method, boolean isCallbackMethod, String callbackType, + boolean isStructMethod) { checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes); Map mapStubParam = null; @@ -1616,7 +1617,10 @@ public class IoTCompiler { print("paramStruct" + i); } else { String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); - print("(" + prmType + ") paramObj[offset" + i + "]"); + if (isStructMethod) + print("(" + prmType + ") paramObj[offset" + i + "]"); + else + print("(" + prmType + ") paramObj[" + i + "]"); } if (i != methParams.size() - 1) print(", "); @@ -1684,7 +1688,7 @@ public class IoTCompiler { println("Object[] paramObj = rmiObj.getMethodParams(paramCls, paramClsGen);"); writeStructMembersInitJavaSkeleton(intDecl, methParams, methPrmTypes, method); // Write the return value part - writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType); + writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, true); } @@ -1726,7 +1730,7 @@ public class IoTCompiler { } println(" });"); // Write the return value part - writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType); + writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, false); } @@ -1980,10 +1984,10 @@ public class IoTCompiler { 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("}"); println("else {"); println("throw new Error(\"Object Id: \" + _objectId + \" not recognized!\");"); println("}"); - println("}"); } } @@ -3635,7 +3639,8 @@ public class IoTCompiler { println("vector paramEnumInt" + i + ";"); } else { String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); - println(methParamComplete + ";"); + //println(methParamComplete + " = " + generateCplusInitializer(methPrmType) + ";"); + println(methParamComplete + ";"); } } } @@ -3991,11 +3996,11 @@ public class IoTCompiler { println("cerr << \"Object with object Id: \" << _objectId << \" is not allowed to access method: \" << methodId << endl;"); println("exit(-1);"); println("}"); + println("}"); println("else {"); println("cerr << \"Object Id: \" << _objectId << \" not recognized!\" << endl;"); println("exit(-1);"); println("}"); - println("}"); } } diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp new file mode 100644 index 0000000..91f7e5c --- /dev/null +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -0,0 +1,92 @@ +#ifndef _TESTCLASS_HPP__ +#define _TESTCLASS_HPP__ + +#include +#include +#include +#include "TestClassInterface.hpp" + +using namespace std; + +class TestClass : public TestClassInterface { + public: + TestClass(); + TestClass(int _int, float _float, string _string); + + void setA(int _int); + void setB(float _float); + void setC(string _string); + string sumArray(vector newA); + int setAndGetA(int newA); + int setACAndGetA(string newC, int newA); + + private: + int intA; + float floatB; + string stringC; +}; + + +TestClass::TestClass() { + + intA = 1; + floatB = 2; + stringC = "345"; + // cbvec doesn't need to be initialized again +} + + +TestClass::TestClass(int _int, float _float, string _string) { + + intA = _int; + floatB = _float; + stringC = _string; + // cbvec doesn't need to be initialized again +} + + +void TestClass::setA(int _int) { + + intA = _int; +} + + +void TestClass::setB(float _float) { + + floatB = _float; +} + + +void TestClass::setC(string _string) { + + stringC = _string; +} + + +string TestClass::sumArray(vector newA) { + + string sum = ""; + int len = newA.size(); + for(int c = 0; c < len; c++) { + sum = sum + newA[c]; + } + return sum; +} + + +int TestClass::setAndGetA(int newA) { + + intA = newA; + return intA; +} + + +int TestClass::setACAndGetA(string newC, int newA) { + + stringC = newC; + intA = newA; + return intA; +} + +#endif + diff --git a/iotjava/iotrmi/C++/basics/TestClass_Skeleton.cpp b/iotjava/iotrmi/C++/basics/TestClass_Skeleton.cpp new file mode 100644 index 0000000..cea5030 --- /dev/null +++ b/iotjava/iotrmi/C++/basics/TestClass_Skeleton.cpp @@ -0,0 +1,20 @@ +#include +#include +#include "TestClassInterface_Skeleton.hpp" +#include "TestClass.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + TestClassInterface *tc; + TestClassInterface_Skeleton *tcSkel; + + int port = 5010; + tc = new TestClass(3, 5.0, "7911"); + tcSkel = new TestClassInterface_Skeleton(tc, port); + + delete tc; + delete tcSkel; + return 0; +} diff --git a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp new file mode 100644 index 0000000..20defbb --- /dev/null +++ b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp @@ -0,0 +1,28 @@ +#include +#include +#include "TestClassComplete_Stub.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + + int port = 5010; + const char* address = "localhost"; + int rev = 0; + bool bResult = false; + vector ports; + ports.push_back(12345); + + TestClassComplete *tcStub = new TestClassComplete_Stub(port, address, rev, &bResult, ports); + cout << "Return value: " << tcStub->setAndGetA(123) << endl; + cout << "Return value: " << tcStub->setACAndGetA("string", 123) << endl; + vector input; + input.push_back("123"); + input.push_back("456"); + input.push_back("987"); + + cout << "Return value: " << tcStub->sumArray(input) << endl; + + return 0; +} diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java new file mode 100644 index 0000000..128e0ad --- /dev/null +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -0,0 +1,74 @@ +import java.util.Set; +import java.util.List; +import java.util.ArrayList; + +public class TestClass implements TestClassInterface { + + /** + * Class Properties + */ + private int intA; + private float floatB; + private String stringC; + + /** + * Constructors + */ + public TestClass() { + + intA = 1; + floatB = 2; + stringC = "345"; + } + + + public TestClass(int _int, float _float, String _string) { + + intA = _int; + floatB = _float; + stringC = _string; + } + + + public void setA(int _int) { + + intA = _int; + } + + + public void setB(float _float) { + + floatB = _float; + } + + + public void setC(String _string) { + + stringC = _string; + } + + + // Getters + public String sumArray(String[] newA) { + + String sum = ""; + for (String i : newA) + sum = sum + i; + return sum; + } + + + public int setAndGetA(int newA) { + + intA = newA; + return intA; + } + + + public int setACAndGetA(String newC, int newA) { + + stringC = newC; + intA = newA; + return intA; + } +} diff --git a/iotjava/iotrmi/Java/basics/TestClass_Skeleton.java b/iotjava/iotrmi/Java/basics/TestClass_Skeleton.java new file mode 100644 index 0000000..a5b7360 --- /dev/null +++ b/iotjava/iotrmi/Java/basics/TestClass_Skeleton.java @@ -0,0 +1,11 @@ + +public class TestClass_Skeleton { + + public static void main(String[] args) throws Exception { + + int port = 5010; + TestClass tc = new TestClass(3, 5f, "7911"); + TestClassInterface_Skeleton tcSkel = new TestClassInterface_Skeleton(tc, port); + + } +} diff --git a/iotjava/iotrmi/Java/basics/TestClass_Stub.java b/iotjava/iotrmi/Java/basics/TestClass_Stub.java new file mode 100644 index 0000000..0a39b76 --- /dev/null +++ b/iotjava/iotrmi/Java/basics/TestClass_Stub.java @@ -0,0 +1,23 @@ +import java.util.Arrays; +import iotruntime.master.CommunicationHandler; + +public class TestClass_Stub { + + public static void main(String[] args) throws Exception { + + CommunicationHandler comHan = new CommunicationHandler(true); + int numOfPorts = 1; + int[] ports = comHan.getCallbackPorts(numOfPorts); + + int port = 5010; + String address = "localhost"; + int rev = 0; + + System.out.println("Allocated ports: " + Arrays.toString(ports)); + + TestClassComplete_Stub tcstub = new TestClassComplete_Stub(port, address, rev, ports); + System.out.println("Return value: " + tcstub.setAndGetA(123)); + System.out.println("Return value: " + tcstub.setACAndGetA("string", 123)); + System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" })); + } +} -- 2.34.1