From 0c354a510ae5b360f5f04ff328d3b9a94f471e56 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Fri, 11 Nov 2016 17:03:58 -0800 Subject: [PATCH] Adding feature for enum and struct generations --- config/iotpolicy/lightbulbpolicy.pol | 3 +- iotjava/iotpolicy/IoTCompiler.java | 127 +++++++++++++++++- .../iotpolicy/tree/DeclarationHandler.java | 28 ++++ iotjava/iotpolicy/tree/ParseTreeHandler.java | 65 +++++++++ iotjava/iotpolicy/tree/StructDecl.java | 1 + iotjava/iotrmi/C++/IoTRMICall.cpp | 29 ++++ iotjava/iotrmi/C++/IoTRMIObject.cpp | 22 +++ iotjava/iotrmi/C++/IoTSocketClient.cpp | 60 +++++++++ iotjava/iotrmi/C++/IoTSocketServer.cpp | 54 ++++++++ iotjava/iotrmi/C++/Test.cpp | 37 +++++ iotjava/iotrmi/C++/sample/EnumC.hpp | 6 + .../Java/sample/TestClassInterface.java | 14 ++ 12 files changed, 444 insertions(+), 2 deletions(-) create mode 100644 iotjava/iotrmi/C++/IoTRMICall.cpp create mode 100644 iotjava/iotrmi/C++/IoTRMIObject.cpp create mode 100644 iotjava/iotrmi/C++/IoTSocketClient.cpp create mode 100644 iotjava/iotrmi/C++/IoTSocketServer.cpp create mode 100644 iotjava/iotrmi/C++/Test.cpp diff --git a/config/iotpolicy/lightbulbpolicy.pol b/config/iotpolicy/lightbulbpolicy.pol index 3d131b6..b879626 100644 --- a/config/iotpolicy/lightbulbpolicy.pol +++ b/config/iotpolicy/lightbulbpolicy.pol @@ -24,7 +24,8 @@ public interface LightBulb { APPLE, ORANGE, - GRAPE + GRAPE, + MANGO } } diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 3dbc21f..f3a1ce1 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -24,6 +24,8 @@ import iotpolicy.tree.DeclarationHandler; import iotpolicy.tree.CapabilityDecl; import iotpolicy.tree.InterfaceDecl; import iotpolicy.tree.RequiresDecl; +import iotpolicy.tree.EnumDecl; +import iotpolicy.tree.StructDecl; import iotrmi.Java.IoTRMITypes; @@ -107,15 +109,26 @@ public class IoTCompiler { ParseTreeHandler ptHandler = new ParseTreeHandler(origInt, pnPol, pnReq); DeclarationHandler decHandler = new DeclarationHandler(); // Process ParseNode and generate Declaration objects + // Interface ptHandler.processInterfaceDecl(); InterfaceDecl intDecl = ptHandler.getInterfaceDecl(); decHandler.addInterfaceDecl(origInt, intDecl); + // Capabilities ptHandler.processCapabilityDecl(); CapabilityDecl capDecl = ptHandler.getCapabilityDecl(); decHandler.addCapabilityDecl(origInt, capDecl); + // Requires ptHandler.processRequiresDecl(); RequiresDecl reqDecl = ptHandler.getRequiresDecl(); decHandler.addRequiresDecl(origInt, reqDecl); + // Enumeration + ptHandler.processEnumDecl(); + EnumDecl enumDecl = ptHandler.getEnumDecl(); + decHandler.addEnumDecl(origInt, enumDecl); + // Struct + ptHandler.processStructDecl(); + StructDecl structDecl = ptHandler.getStructDecl(); + decHandler.addStructDecl(origInt, structDecl); mapIntfacePTH.put(origInt, ptHandler); mapIntDeclHand.put(origInt, decHandler); @@ -193,6 +206,55 @@ public class IoTCompiler { } + /** + * HELPER: writeEnumJava() writes the enumeration declaration + */ + private void writeEnumJava(EnumDecl enumDecl) { + + Set enumTypes = enumDecl.getEnumDeclarations(); + // Iterate over enum declarations + for (String enType : enumTypes) { + + println("public enum " + enType + " {"); + List enumMembers = enumDecl.getMembers(enType); + for (int i = 0; i < enumMembers.size(); i++) { + + String member = enumMembers.get(i); + print(member); + // Check if this is the last element (don't print a comma) + if (i != enumMembers.size() - 1) + println(","); + else + println(""); + } + println("}\n"); + } + } + + + /** + * HELPER: writeStructJava() writes the struct declaration + */ + private void writeStructJava(StructDecl structDecl) { + + List structTypes = structDecl.getStructTypes(); + // Iterate over enum declarations + for (String stType : structTypes) { + + println("public class " + stType + " {"); + List structMemberTypes = structDecl.getMemberTypes(stType); + List structMembers = structDecl.getMembers(stType); + for (int i = 0; i < structMembers.size(); i++) { + + String memberType = structMemberTypes.get(i); + String member = structMembers.get(i); + println("public static " + memberType + " " + member + ";"); + } + println("}\n"); + } + } + + /** * generateJavaLocalInterface() writes the local interface and provides type-checking. *

@@ -218,6 +280,12 @@ public class IoTCompiler { // Write interface header println(""); println("public interface " + intface + " {"); + // Write enum if any... + EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface); + writeEnumJava(enumDecl); + // Write struct if any... + StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface); + writeStructJava(structDecl); // Write methods writeMethodJavaInterface(methods, intDecl); println("}"); @@ -250,7 +318,7 @@ public class IoTCompiler { printImportStatements(importClasses); // Write interface header println(""); - println("public interface " + newIntface + " {"); + println("public interface " + newIntface + " {\n"); // Write methods writeMethodJavaInterface(intMeth.getValue(), intDecl); println("}"); @@ -671,6 +739,57 @@ public class IoTCompiler { } + /** + * HELPER: writeEnumCplus() writes the enumeration declaration + */ + private void writeEnumCplus(EnumDecl enumDecl) { + + Set enumTypes = enumDecl.getEnumDeclarations(); + // Iterate over enum declarations + for (String enType : enumTypes) { + + println("enum " + enType + " {"); + List enumMembers = enumDecl.getMembers(enType); + for (int i = 0; i < enumMembers.size(); i++) { + + String member = enumMembers.get(i); + print(member); + // Check if this is the last element (don't print a comma) + if (i != enumMembers.size() - 1) + println(","); + else + println(""); + } + println("};\n"); + } + } + + + /** + * HELPER: writeStructCplus() writes the struct declaration + */ + private void writeStructCplus(StructDecl structDecl) { + + List structTypes = structDecl.getStructTypes(); + // Iterate over enum declarations + for (String stType : structTypes) { + + println("struct " + stType + " {"); + List structMemberTypes = structDecl.getMemberTypes(stType); + List structMembers = structDecl.getMembers(stType); + for (int i = 0; i < structMembers.size(); i++) { + + String memberType = structMemberTypes.get(i); + String member = structMembers.get(i); + String structTypeC = checkAndGetCplusType(memberType); + String structComplete = checkAndGetCplusArray(structTypeC, member); + println(structComplete + ";"); + } + println("};\n"); + } + } + + /** * generateCplusLocalInterfaces() writes the local interfaces and provides type-checking. *

@@ -698,6 +817,12 @@ public class IoTCompiler { Set includeClasses = getIncludeClasses(methods, intDecl); printIncludeStatements(includeClasses); println(""); println("using namespace std;\n"); + // Write enum if any... + EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface); + writeEnumCplus(enumDecl); + // Write struct if any... + StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface); + writeStructCplus(structDecl); println("class " + intface); println("{"); println("public:"); // Write methods diff --git a/iotjava/iotpolicy/tree/DeclarationHandler.java b/iotjava/iotpolicy/tree/DeclarationHandler.java index 0b8089f..48856ca 100644 --- a/iotjava/iotpolicy/tree/DeclarationHandler.java +++ b/iotjava/iotpolicy/tree/DeclarationHandler.java @@ -18,6 +18,8 @@ public class DeclarationHandler { private Map mapInt2IntfaceDecl; private Map mapInt2CapabDecl; private Map mapInt2ReqDecl; + private Map mapInt2EnumDecl; + private Map mapInt2StructDecl; /** * Class constructors @@ -27,6 +29,8 @@ public class DeclarationHandler { mapInt2IntfaceDecl = new HashMap(); mapInt2CapabDecl = new HashMap(); mapInt2ReqDecl = new HashMap(); + mapInt2EnumDecl = new HashMap(); + mapInt2StructDecl = new HashMap(); } @@ -51,6 +55,18 @@ public class DeclarationHandler { } + public void addEnumDecl(String origInt, Declaration enumDecl) { + + mapInt2EnumDecl.put(origInt, enumDecl); + } + + + public void addStructDecl(String origInt, Declaration structDecl) { + + mapInt2StructDecl.put(origInt, structDecl); + } + + /** * Getters */ @@ -70,4 +86,16 @@ public class DeclarationHandler { return mapInt2ReqDecl.get(origInt); } + + + public Declaration getEnumDecl(String origInt) { + + return mapInt2EnumDecl.get(origInt); + } + + + public Declaration getStructDecl(String origInt) { + + return mapInt2StructDecl.get(origInt); + } } diff --git a/iotjava/iotpolicy/tree/ParseTreeHandler.java b/iotjava/iotpolicy/tree/ParseTreeHandler.java index 0d2b1c5..42f42f1 100644 --- a/iotjava/iotpolicy/tree/ParseTreeHandler.java +++ b/iotjava/iotpolicy/tree/ParseTreeHandler.java @@ -204,6 +204,71 @@ public final class ParseTreeHandler { } + /** + * processEnumDecl() processes "enum" declaration part + */ + public void processEnumDecl() { + + ParseNodeVector pnv = pnPol.getChildren(); + ParseNode pnRoot = pnv.elementAt(0); + ParseNodeVector pnvGen2 = pnRoot.getChildren(); + // Get the third child of root for "enum" + ParseNode pnGen2 = pnvGen2.elementAt(3); + ParseNodeVector pnvGen3 = pnGen2.getChildren(); + // Iterate over a list of enum declaration - can be empty too + for(int i = 0; i < pnvGen3.size(); i++) { + + ParseNode pnGen4 = pnvGen3.elementAt(i); + ParseNodeVector pnvGen5 = pnGen4.getChildren(); + // Get the type of the enum + ParseNode pnGen5_enum_ident = pnvGen5.elementAt(0); + ParseNode pnGen5_enum_list = pnvGen5.elementAt(1); + ParseNodeVector pnvGen6_members = pnGen5_enum_list.getChildren(); + // Browse through enum declarations + for (int j = 0; j < pnvGen6_members.size(); j++) { + ParseNode pnGen6 = pnvGen6_members.elementAt(j); + ParseNodeVector pnvGen7 = pnGen6.getChildren(); + ParseNode pnGen7 = pnvGen7.elementAt(0); + enumDecl.addNewMember(pnGen5_enum_ident.getLiteral().toString(), + pnGen7.getLiteral().toString()); + } + } + } + + + /** + * processStructDecl() processes "struct" declaration part + */ + public void processStructDecl() { + + ParseNodeVector pnv = pnPol.getChildren(); + ParseNode pnRoot = pnv.elementAt(0); + ParseNodeVector pnvGen2 = pnRoot.getChildren(); + // Get the fourth child of root for "struct" + ParseNode pnGen2 = pnvGen2.elementAt(4); + ParseNodeVector pnvGen3 = pnGen2.getChildren(); + // Iterate over a list of struct declaration - can be empty too + for(int i = 0; i < pnvGen3.size(); i++) { + + ParseNode pnGen4 = pnvGen3.elementAt(i); + ParseNodeVector pnvGen5 = pnGen4.getChildren(); + // Get the type of the enum + ParseNode pnGen5_enum_ident = pnvGen5.elementAt(0); + ParseNode pnGen5_enum_list = pnvGen5.elementAt(1); + ParseNodeVector pnvGen6_members = pnGen5_enum_list.getChildren(); + // Browse through enum declarations + for (int j = 0; j < pnvGen6_members.size(); j++) { + ParseNode pnGen6 = pnvGen6_members.elementAt(j); + ParseNodeVector pnvGen7 = pnGen6.getChildren(); + ParseNode pnGen7_type = pnvGen7.elementAt(0); + ParseNode pnGen7_ident = pnvGen7.elementAt(1); + structDecl.addNewMember(pnGen5_enum_ident.getLiteral().toString(), + pnGen7_type.getLiteral().toString(), pnGen7_ident.getLiteral().toString()); + } + } + } + + /** * getInterfaceDecl() returns InterfaceDecl object */ diff --git a/iotjava/iotpolicy/tree/StructDecl.java b/iotjava/iotpolicy/tree/StructDecl.java index 56a6fe4..ef58b55 100644 --- a/iotjava/iotpolicy/tree/StructDecl.java +++ b/iotjava/iotpolicy/tree/StructDecl.java @@ -67,6 +67,7 @@ public class StructDecl extends Declaration { memberList.add(newMember); } else { // New declaration + listStructs.add(structType); List newMemberTypeList = new ArrayList(); newMemberTypeList.add(newMemberType); listMemberTypes.add(newMemberTypeList); diff --git a/iotjava/iotrmi/C++/IoTRMICall.cpp b/iotjava/iotrmi/C++/IoTRMICall.cpp new file mode 100644 index 0000000..aefe533 --- /dev/null +++ b/iotjava/iotrmi/C++/IoTRMICall.cpp @@ -0,0 +1,29 @@ +#include +#include +#include "IoTRMICall.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + int port = 5010; + const char* address = "localhost"; + int rev = 0; + bool bResult = false; + + int numRet = 3; + string retCls[] = { "int", "string", "int" }; + int param1 = 0; + string param2 = ""; + int param3 = 0; + void* retObj[] = { ¶m1, ¶m2, ¶m3 }; + + IoTRMICall *rc = new IoTRMICall(port, address, rev, &bResult); + char retBytes[] = { 0, 0, 4, -46, 0, 0, 0, 10, 116, 101, 115, 116, 115, 116, 114, 105, 110, 103, 0, 0, 21, 56 }; + rc->getReturnObjects(retBytes, retCls, numRet, retObj); + cout << "Param1: " << param1 << endl; + cout << "Param2: " << param2 << endl; + cout << "Param3: " << param3 << endl; + + return 0; +} diff --git a/iotjava/iotrmi/C++/IoTRMIObject.cpp b/iotjava/iotrmi/C++/IoTRMIObject.cpp new file mode 100644 index 0000000..a2e321a --- /dev/null +++ b/iotjava/iotrmi/C++/IoTRMIObject.cpp @@ -0,0 +1,22 @@ +#include +#include +#include "IoTRMIObject.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + int port = 5010; + bool bResult = false; + IoTRMIObject *ro = new IoTRMIObject(port, &bResult); + + int numRet = 3; + string retCls[] = { "int", "string", "int" }; + int param1 = 1234; + string param2 = "teststring"; + int param3 = 5432; + void* retObj[] = { ¶m1, ¶m2, ¶m3 }; + ro->sendReturnObj(retObj, retCls, numRet); + + return 0; +} diff --git a/iotjava/iotrmi/C++/IoTSocketClient.cpp b/iotjava/iotrmi/C++/IoTSocketClient.cpp new file mode 100644 index 0000000..ce9f0a5 --- /dev/null +++ b/iotjava/iotrmi/C++/IoTSocketClient.cpp @@ -0,0 +1,60 @@ +#include +#include +#include "IoTSocketClient.hpp" + +using namespace std; + +#define SIZE 10 /* how many items per packet */ +#define NUM_PACKS 3 /* number of times we'll do it */ + +int main(int argc, char *argv[]) +{ + char D[SIZE]; + + /* if no command line arguments passed, we'll default to + these two port number */ + int port = 5010; + int rev = 0; + bool bResult = false; + + fflush(NULL); + IoTSocketClient mylink(port, "127.0.0.1", rev, &bResult); + + if (!bResult) + { + printf("Failed to create Client object!\n"); + return 0; + } + + printf("Client, made connection...\n"); + fflush(NULL); + + /* put some dummy data in our arrays */ + + for (int i = 0; i < SIZE; i++) + { + D[i] = i; + } + + for (int i = 0; i < NUM_PACKS; i++) + { + printf("Client, receiving bytes, iteration %d\n", i); + fflush(NULL); + mylink.receiveBytes(D); + } + + char str[50]; + char* str2; + fflush(NULL); + str2 = mylink.receiveBytes(str); + string s(str2); + cout << "Received text: " << s << endl; + + printf("Client, closing connection...\n"); + fflush(NULL); + mylink.close(); + + printf("Client, done...\n"); + fflush(NULL); + return 0; +} diff --git a/iotjava/iotrmi/C++/IoTSocketServer.cpp b/iotjava/iotrmi/C++/IoTSocketServer.cpp new file mode 100644 index 0000000..95e1e60 --- /dev/null +++ b/iotjava/iotrmi/C++/IoTSocketServer.cpp @@ -0,0 +1,54 @@ +#include +#include +#include "IoTSocketServer.hpp" +#include "IoTRMIUtil.hpp" + +using namespace std; + + +#define SIZE 10 /* how many items per packet */ +#define NUM_PACKS 3 /* number of times we'll do it */ + +int main(int argc, char *argv[]) +{ + char D[SIZE]; + bool bResult = false; + + /* if no command line arguments passed, we'll default to + these two port number */ + int port = 5010; + + fflush(NULL); + + IoTSocketServer mylink(port, &bResult); + if (!bResult) + { + printf("Failed to create Server object!\n"); + return 0; + } + + /* put some dummy data in our arrays */ + for (int i = 0,j = 100; i < SIZE; i++, j--) + { + //D[i] = i; + D[i] = j; + } + printf("Server, waiting for connection...\n"); + fflush(NULL); + mylink.connect(); + printf("Server, got a connection...\n"); + fflush(NULL); + + char bytes[24]; + mylink.receiveBytes(bytes); + cout << "Received bytes: "; + IoTRMIUtil::printBytes(bytes, 24, false); + + printf("Server, closing connection...\n"); + fflush(NULL); + mylink.close(); + + printf("Server, done...\n"); + fflush(NULL); + return 0; +} diff --git a/iotjava/iotrmi/C++/Test.cpp b/iotjava/iotrmi/C++/Test.cpp new file mode 100644 index 0000000..7c77d30 --- /dev/null +++ b/iotjava/iotrmi/C++/Test.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include "IoTRMITypes.hpp" + +using namespace std; + + +int main(int argc, char *argv[]) +{ + std::array myints; + std::cout << "size of myints: " << myints.size() << std::endl; + std::cout << "sizeof(myints): " << sizeof(myints) << std::endl; + + int test[5] = { 0 }; + std::memcpy(myints.data(), test, 5); + + std::vector test2 (test, test + sizeof(test)/sizeof(int)); + + string test3[2] = { "test1", "test2" }; + std::vector test4 (test3, test3 + sizeof(test3)/sizeof(string)); + std::cout << "vector[0]: " << test4[0] << std::endl; + + std::vector primJava (IoTRMITypes::primitivesJava, + IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string)); + std::vector primCplus (IoTRMITypes::primitivesCplus, + IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string)); + + map mymap; + IoTRMITypes::arraysToMap(mymap, primJava, primCplus); + for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) + std::cout << it->first << " => " << it->second << '\n'; + + std::cout << "Result of find: " << mymap.find("Boolean")->second << std::endl; + + return 0; +} diff --git a/iotjava/iotrmi/C++/sample/EnumC.hpp b/iotjava/iotrmi/C++/sample/EnumC.hpp index 529ae73..055f28b 100644 --- a/iotjava/iotrmi/C++/sample/EnumC.hpp +++ b/iotjava/iotrmi/C++/sample/EnumC.hpp @@ -9,5 +9,11 @@ enum EnumC { GRAPE } enumC; +/*enum EnumD { + APPLE, + ORANGE, + GRAPE +};*/ + #endif diff --git a/iotjava/iotrmi/Java/sample/TestClassInterface.java b/iotjava/iotrmi/Java/sample/TestClassInterface.java index 4f7c62a..7f7bb3e 100644 --- a/iotjava/iotrmi/Java/sample/TestClassInterface.java +++ b/iotjava/iotrmi/Java/sample/TestClassInterface.java @@ -4,6 +4,20 @@ import java.util.Set; public interface TestClassInterface { + public class StructJ { + + public static String name; + public static float value; + public static int year; + } + + public enum EnumJ { + + APPLE, + ORANGE, + GRAPE + } + public void setA(int _int); public void setB(float _float); public void setC(String _string); -- 2.34.1