APPLE,
ORANGE,
- GRAPE
+ GRAPE,
+ MANGO
}
}
import iotpolicy.tree.CapabilityDecl;
import iotpolicy.tree.InterfaceDecl;
import iotpolicy.tree.RequiresDecl;
+import iotpolicy.tree.EnumDecl;
+import iotpolicy.tree.StructDecl;
import iotrmi.Java.IoTRMITypes;
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);
}
+ /**
+ * HELPER: writeEnumJava() writes the enumeration declaration
+ */
+ private void writeEnumJava(EnumDecl enumDecl) {
+
+ Set<String> enumTypes = enumDecl.getEnumDeclarations();
+ // Iterate over enum declarations
+ for (String enType : enumTypes) {
+
+ println("public enum " + enType + " {");
+ List<String> 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<String> structTypes = structDecl.getStructTypes();
+ // Iterate over enum declarations
+ for (String stType : structTypes) {
+
+ println("public class " + stType + " {");
+ List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+ List<String> 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.
* <p>
// 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("}");
printImportStatements(importClasses);
// Write interface header
println("");
- println("public interface " + newIntface + " {");
+ println("public interface " + newIntface + " {\n");
// Write methods
writeMethodJavaInterface(intMeth.getValue(), intDecl);
println("}");
}
+ /**
+ * HELPER: writeEnumCplus() writes the enumeration declaration
+ */
+ private void writeEnumCplus(EnumDecl enumDecl) {
+
+ Set<String> enumTypes = enumDecl.getEnumDeclarations();
+ // Iterate over enum declarations
+ for (String enType : enumTypes) {
+
+ println("enum " + enType + " {");
+ List<String> 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<String> structTypes = structDecl.getStructTypes();
+ // Iterate over enum declarations
+ for (String stType : structTypes) {
+
+ println("struct " + stType + " {");
+ List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+ List<String> 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.
* <p>
Set<String> 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
private Map<String,Declaration> mapInt2IntfaceDecl;
private Map<String,Declaration> mapInt2CapabDecl;
private Map<String,Declaration> mapInt2ReqDecl;
+ private Map<String,Declaration> mapInt2EnumDecl;
+ private Map<String,Declaration> mapInt2StructDecl;
/**
* Class constructors
mapInt2IntfaceDecl = new HashMap<String,Declaration>();
mapInt2CapabDecl = new HashMap<String,Declaration>();
mapInt2ReqDecl = new HashMap<String,Declaration>();
+ mapInt2EnumDecl = new HashMap<String,Declaration>();
+ mapInt2StructDecl = new HashMap<String,Declaration>();
}
}
+ public void addEnumDecl(String origInt, Declaration enumDecl) {
+
+ mapInt2EnumDecl.put(origInt, enumDecl);
+ }
+
+
+ public void addStructDecl(String origInt, Declaration structDecl) {
+
+ mapInt2StructDecl.put(origInt, structDecl);
+ }
+
+
/**
* Getters
*/
return mapInt2ReqDecl.get(origInt);
}
+
+
+ public Declaration getEnumDecl(String origInt) {
+
+ return mapInt2EnumDecl.get(origInt);
+ }
+
+
+ public Declaration getStructDecl(String origInt) {
+
+ return mapInt2StructDecl.get(origInt);
+ }
}
}
+ /**
+ * 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
*/
memberList.add(newMember);
} else {
// New declaration
+ listStructs.add(structType);
List<String> newMemberTypeList = new ArrayList<String>();
newMemberTypeList.add(newMemberType);
listMemberTypes.add(newMemberTypeList);
--- /dev/null
+#include <iostream>
+#include <string>
+#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;
+}
--- /dev/null
+#include <iostream>
+#include <string>
+#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;
+}
--- /dev/null
+#include <iostream>
+#include <string>
+#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;
+}
--- /dev/null
+#include <iostream>
+#include <string>
+#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;
+}
--- /dev/null
+#include <iostream>
+#include <string>
+#include <cstring>
+#include "IoTRMITypes.hpp"
+
+using namespace std;
+
+
+int main(int argc, char *argv[])
+{
+ std::array<int,5> 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<int> test2 (test, test + sizeof(test)/sizeof(int));
+
+ string test3[2] = { "test1", "test2" };
+ std::vector<string> test4 (test3, test3 + sizeof(test3)/sizeof(string));
+ std::cout << "vector[0]: " << test4[0] << std::endl;
+
+ std::vector<string> primJava (IoTRMITypes::primitivesJava,
+ IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string));
+ std::vector<string> primCplus (IoTRMITypes::primitivesCplus,
+ IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string));
+
+ map<string,string> mymap;
+ IoTRMITypes::arraysToMap(mymap, primJava, primCplus);
+ for (std::map<string,string>::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;
+}
GRAPE
} enumC;
+/*enum EnumD {
+ APPLE,
+ ORANGE,
+ GRAPE
+};*/
+
#endif
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);