1 #ifndef _IOTSLAVE_HPP__
2 #define _IOTSLAVE_HPP__
9 #include <dlfcn.h> // For dlopen, dlsym, etc.
11 //#include "ObjectFactory.hpp"
14 #include "IoTDeviceAddress.hpp"
15 //#include "IRelation.hpp"
16 #include "IoTRelation.hpp"
19 /** Class IoTSlave is a communication class
20 * that interacts with IoTSlave.java to set up C++
21 * objects in Sentinel.
23 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
27 // Enumeration of master-slave communication codes
34 CREATE_NEW_IOTRELATION,
37 GET_ADD_IOTSET_OBJECT,
38 GET_DEVICE_IOTSET_OBJECT,
40 GET_IOTRELATION_FIRST_OBJECT,
41 GET_IOTRELATION_SECOND_OBJECT,
42 GET_ZB_DEV_IOTSET_OBJECT,
44 REINITIALIZE_IOTSET_FIELD,
45 REINITIALIZE_IOTRELATION_FIELD,
51 // Defining generic function pointers for
52 // create, destroy, and init functions of each class object
53 typedef void* create_t(void**);
54 typedef void destroy_t(void*);
55 typedef void init_t(void*);
62 const static int RCVBUFSIZE = 1024; // Size of receive buffer
63 const static int SKELPARAMSIZE = 3; // Number of params for skeleton
64 const static int STUBPARAMSIZE = 6; // Number of params for stub
65 const static string FILEPATH; // File path
66 const static string FILEEXT; // File extension
67 const static string SOEXT; // Shared object (.so) extension
68 const static string STRINGCLASS; // String class
69 const static string INTCLASS; // Int class
70 const static string CREATEFUNCTION; // The create function in class
71 const static string DESTROYFUNCTION; // The destroy function in class
72 const static string INITFUNCTION; // The init function in class
73 const static string LOCALHOST; // String "localhost"
79 string mainObjectName;
81 string objectClassName;
82 string objectInterfaceName;
83 string objectSkelClass; // Need to send from Java IoTSlave: sMessage.getObjectInterfaceName() + SKEL_CLASS_SUFFIX
84 string objectStubClass; // Need to send from Java IoTSlave: sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX
87 vector<int> ports; // Now used to contain callback ports
88 string objectFieldName; // Field name that is going to be initialized with IoTSet or IoTRelation
89 unordered_set<void*>* isetObject; // Set of object
90 IoTSet<void*>* iotsetObject; // IoTSet of object
91 vector<IoTSet<void*>*> vecIoTSet; // IoTSet of object
93 ofstream log; // Log the messages
94 vector<string> args; // Hold the arguments for constructor (in string format)
95 vector<string> argClasses; // Hold the argument classes
96 bool isDriverObject; // Set to true if this is IoTSlave instance for a driver object
97 void* objMainCls; // Main class handler, i.e. driver or controller object
98 void* objSkelCls; // Skeleton handler
99 void* objStubCls; // Stub handler
100 unordered_map<string, void*> mapObjNameStub; // Mapping between object name and stub
102 create_t* create_object;
103 destroy_t* destroy_object;
108 IoTSlave(string _serverAddress, int _serverPort, string _objectName);
111 string getServerAddress();
113 string getObjectName();
114 void sendInteger(int intSend);
116 void sendString(string strSend);
120 bool recvEndTransfer();
121 void commIoTMaster();
122 void createObject(); // Create driver object
123 void createMainObject(); // Create main object
124 void createNewIoTSet();
125 void getDeviceIoTSetObject();
126 void reinitializeIoTSetField();
127 void getIoTSetObject();
128 void invokeInitMethod();
131 // Private helper functions
132 int* byteToInt(int* result, char* bytes);
133 char* intToByteArray(int i, char* bytes);
134 char* recvIter(char* recvBuffer, int recvLen);
135 void* getObjectConverted(void* retObj, string object, string objectClass);
136 void openFile(string fileName);
137 void writeToFile(string logMsg);
139 void getObjectHandler(string objectClassName);
140 void instantiateMainObject();
141 void instantiateDriverObject();
142 void instantiateSkelObject();
143 void instantiateStubObject();
144 void runInitObject(IoTSlave* iotslave);
147 // Constant initialization
148 const string IoTSlave::FILEPATH = "./";
149 const string IoTSlave::FILEEXT = "_cpp.log";
150 const string IoTSlave::SOEXT = ".so";
151 const string IoTSlave::STRINGCLASS = "string";
152 const string IoTSlave::INTCLASS = "int";
153 const string IoTSlave::CREATEFUNCTION = "create";
154 const string IoTSlave::DESTROYFUNCTION = "destroy";
155 const string IoTSlave::INITFUNCTION = "init";
156 const string IoTSlave::LOCALHOST = "localhost";