1 #ifndef _IOTSLAVE_HPP__
2 #define _IOTSLAVE_HPP__
10 #include <dlfcn.h> // For dlopen, dlsym, etc.
13 #include "IoTDeviceAddress.hpp"
14 #include "IoTRelation.hpp"
17 /** Class IoTSlave is a communication class
18 * that interacts with IoTSlave.java to set up C++
19 * objects in Sentinel.
21 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
25 // Enumeration of master-slave communication codes
33 CREATE_NEW_IOTRELATION,
36 GET_ADD_IOTSET_OBJECT,
37 GET_DEVICE_IOTSET_OBJECT,
39 GET_IOTRELATION_FIRST_OBJECT,
40 GET_IOTRELATION_SECOND_OBJECT,
41 GET_ZB_DEV_IOTSET_OBJECT,
43 REINITIALIZE_IOTSET_FIELD,
44 REINITIALIZE_IOTRELATION_FIELD,
50 // Defining generic function pointers for
51 // create, destroy, and init functions of each class object
52 typedef void* create_t(void**);
53 typedef void destroy_t(void*);
54 typedef void init_t(void*);
61 const static int RCVBUFSIZE = 1024; // Size of receive buffer
62 const static int SKELPARAMSIZE = 3; // Number of params for skeleton
63 const static int STUBPARAMSIZE = 6; // Number of params for stub
64 const static string FILEPATH; // File path
65 const static string FILEEXT; // File extension
66 const static string SOEXT; // Shared object (.so) extension
67 const static string STRINGCLASS; // String class
68 const static string INTCLASS; // Int class
69 const static string CREATEFUNCTION; // The create function in class
70 const static string DESTROYFUNCTION; // The destroy function in class
71 const static string INITFUNCTION; // The init function in class
72 const static string LOCALHOST; // String "localhost"
73 const static string SHELL; // String ".sh"
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
92 void* irelFirstObject; // First object of IoTRelation
93 void* irelSecondObject; // Second object of IoTRelation
94 unordered_multimap<void*,void*>* irelObject; // Relation of object
95 IoTRelation<void*,void*>* iotrelObject; // IoTRelation of objects
96 vector<IoTRelation<void*,void*>*> vecIoTRel; // IoTRelation of object
99 ofstream log; // Log the messages
100 vector<string> args; // Hold the arguments for constructor (in string format)
101 vector<string> argClasses; // Hold the argument classes
102 //bool isDriverObject; // Set to true if this is IoTSlave instance for a driver object
103 void* objMainCls; // Main class handler, i.e. driver or controller object
104 void* objSkelCls; // Skeleton handler
105 void* objStubCls; // Stub handler
106 unordered_map<string, void*> mapObjNameStub; // Mapping between object name and stub
108 create_t* create_object;
109 destroy_t* destroy_object;
114 IoTSlave(string _serverAddress, int _serverPort, string _objectName);
117 string getServerAddress();
119 string getObjectName();
120 void sendInteger(int intSend);
122 void sendString(string strSend);
125 void unzipFile(string fileName);
128 bool recvEndTransfer();
129 void commIoTMaster();
130 void createObject(); // Create driver object
131 void createMainObject(); // Create main object
132 void createNewIoTSet();
133 void createNewIoTRelation();
134 void getDeviceIoTSetObject();
135 void getIoTRelationFirstObject();
136 void getIoTRelationSecondObject();
137 void reinitializeIoTSetField();
138 void reinitializeIoTRelationField();
139 void getIoTSetObject();
140 void invokeInitMethod();
141 void createDriverObject();
145 // Private helper functions
146 int* byteToInt(int* result, char* bytes);
147 char* intToByteArray(int i, char* bytes);
148 char* recvIter(char* recvBuffer, int recvLen);
149 char* recvFileIter(char* recvBuffer, int recvLen);
150 void* getObjectConverted(void* retObj, string object, string objectClass);
151 void openFile(string fileName);
152 void writeToFile(string logMsg);
154 void getObjectHandler(string objectClassName);
155 void instantiateMainObject();
156 void instantiateDriverObject();
157 void instantiateSkelObject();
158 void instantiateStubObject();
159 void runInitObject(IoTSlave* iotslave);
160 void getIoTSetRelationObject();
164 // Constant initialization
165 const string IoTSlave::FILEPATH = "./";
166 const string IoTSlave::FILEEXT = "_cpp.log";
167 const string IoTSlave::SOEXT = ".so";
168 const string IoTSlave::STRINGCLASS = "string";
169 const string IoTSlave::INTCLASS = "int";
170 const string IoTSlave::CREATEFUNCTION = "create";
171 const string IoTSlave::DESTROYFUNCTION = "destroy";
172 const string IoTSlave::INITFUNCTION = "init";
173 const string IoTSlave::LOCALHOST = "localhost";
174 const string IoTSlave::SHELL = ".sh";