Modified and tested IoTMaster for IoTSet in C++; completed IoTMaster and IoTSlave...
[iot2.git] / iotjava / iotruntime / cpp / iotslave / IoTSlave.hpp
1 #ifndef _IOTSLAVE_HPP__
2 #define _IOTSLAVE_HPP__
3
4 #include <iostream>
5 #include <fstream>
6 #include <vector>
7 #include <thread>
8 #include <cstdlib>
9
10 #include <dlfcn.h>              // For dlopen, dlsym, etc.
11
12 #include "IoTSet.hpp"
13 #include "IoTDeviceAddress.hpp"
14 #include "IoTRelation.hpp"
15 #include "Socket.cpp"
16
17 /** Class IoTSlave is a communication class
18  *  that interacts with IoTSlave.java to set up C++
19  *  objects in Sentinel.
20  *
21  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
22  * @version     1.0
23  * @since       2017-01-12
24  */
25 // Enumeration of master-slave communication codes
26 enum IoTCommCode {
27
28         ACKNOWLEDGED,
29         CREATE_OBJECT,
30         CREATE_MAIN_OBJECT,
31         CREATE_NEW_IOTSET,
32         CREATE_NEW_IOTRELATION,
33         END_TRANSFER,
34         END_SESSION,
35         GET_ADD_IOTSET_OBJECT,
36         GET_DEVICE_IOTSET_OBJECT,
37         GET_IOTSET_OBJECT,
38         GET_IOTRELATION_FIRST_OBJECT,
39         GET_IOTRELATION_SECOND_OBJECT,
40         GET_ZB_DEV_IOTSET_OBJECT,
41         INVOKE_INIT_METHOD,
42         REINITIALIZE_IOTSET_FIELD,
43         REINITIALIZE_IOTRELATION_FIELD,
44         TRANSFER_FILE,
45
46 };
47
48
49 // Defining generic function pointers for 
50 // create, destroy, and init functions of each class object
51 typedef void* create_t(void**);
52 typedef void destroy_t(void*);
53 typedef void init_t(void*);
54
55
56 class IoTSlave {
57
58         private:
59                 // Constants
60                 const static int RCVBUFSIZE = 1024;                     // Size of receive buffer
61                 const static int SKELPARAMSIZE = 3;                     // Number of params for skeleton
62                 const static int STUBPARAMSIZE = 6;                     // Number of params for stub
63                 const static string FILEPATH;                           // File path
64                 const static string FILEEXT;                            // File extension
65                 const static string SOEXT;                              // Shared object (.so) extension
66                 const static string STRINGCLASS;                        // String class
67                 const static string INTCLASS;                           // Int class
68                 const static string CREATEFUNCTION;                     // The create function in class
69                 const static string DESTROYFUNCTION;            // The destroy function in class
70                 const static string INITFUNCTION;                       // The init function in class
71                 const static string LOCALHOST;                          // String "localhost"
72                 const static string SHELL;                                      // String ".sh"
73
74                 // Class properties
75                 string serverAddress;
76                 int serverPort;
77                 string hostAddress;
78                 string mainObjectName;
79                 string objectName;
80                 string objectClassName;
81                 string objectInterfaceName;
82                 string objectSkelClass;         // Need to send from Java IoTSlave: sMessage.getObjectInterfaceName() + SKEL_CLASS_SUFFIX
83                 string objectStubClass;         // Need to send from Java IoTSlave: sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX
84                 int objectRegPort;
85                 int objectStubPort;
86                 vector<int> ports;                      // Now used to contain callback ports
87                 string objectFieldName;                                         // Field name that is going to be initialized with IoTSet or IoTRelation
88                 unordered_set<void*>* isetObject;                       // Set of object
89                 IoTSet<void*>* iotsetObject;                            // IoTSet of object
90                 vector<IoTSet<void*>*> vecIoTSet;                       // IoTSet of object
91                 void* irelFirstObject;                                                  // First object of IoTRelation
92                 void* irelSecondObject;                                                 // Second object of IoTRelation
93                 unordered_multimap<void*,void*>* irelObject;    // Relation of object
94                 IoTRelation<void*,void*>* iotrelObject;                 // IoTRelation of objects
95                 vector<IoTRelation<void*,void*>*> vecIoTRel;    // IoTRelation of object
96
97                 TCPSocket* socket;
98                 ofstream log;                                           // Log the messages
99                 vector<string> args;                            // Hold the arguments for constructor (in string format)
100                 vector<string> argClasses;                      // Hold the argument classes
101                 bool isDriverObject;                            // Set to true if this is IoTSlave instance for a driver object
102                 void* objMainCls;                                       // Main class handler, i.e. driver or controller object
103                 void* objSkelCls;                                       // Skeleton handler
104                 void* objStubCls;                                       // Stub handler
105                 unordered_map<string, void*> mapObjNameStub;    // Mapping between object name and stub
106                 // Object handlers
107                 create_t* create_object;
108                 destroy_t* destroy_object;
109                 init_t* init_object;
110
111         public:
112                 // Constructors
113                 IoTSlave(string _serverAddress, int _serverPort, string _objectName);
114                 ~IoTSlave();
115                 // Class methods
116                 string getServerAddress();
117                 int getServerPort();
118                 string getObjectName();
119                 void sendInteger(int intSend);
120                 int recvInteger();
121                 void sendString(string strSend);
122                 string recvString();
123                 string recvFile();
124                 void unzipFile(string fileName);
125                 // Main loop
126                 void sendAck();
127                 bool recvEndTransfer();
128                 void commIoTMaster();
129                 void createObject();            // Create driver object
130                 void createMainObject();        // Create main object
131                 void createNewIoTSet();
132                 void createNewIoTRelation();
133                 void getDeviceIoTSetObject();
134                 void getIoTRelationFirstObject();
135                 void getIoTRelationSecondObject();
136                 void reinitializeIoTSetField();
137                 void reinitializeIoTRelationField();
138                 void getIoTSetObject();
139                 void invokeInitMethod();
140                 void transferFile();
141
142         private:
143                 // Private helper functions
144                 int* byteToInt(int* result, char* bytes);
145                 char* intToByteArray(int i, char* bytes);
146                 char* recvIter(char* recvBuffer, int recvLen);
147                 char* recvFileIter(char* recvBuffer, int recvLen);
148                 void* getObjectConverted(void* retObj, string object, string objectClass);
149                 void openFile(string fileName);
150                 void writeToFile(string logMsg);
151                 void closeFile();
152                 void getObjectHandler(string objectClassName);
153                 void instantiateMainObject();
154                 void instantiateDriverObject();
155                 void instantiateSkelObject();
156                 void instantiateStubObject();
157                 void runInitObject(IoTSlave* iotslave);
158                 void getIoTSetRelationObject();
159                 void createStub();
160 };
161
162 // Constant initialization
163 const string IoTSlave::FILEPATH = "./";
164 const string IoTSlave::FILEEXT = "_cpp.log";
165 const string IoTSlave::SOEXT = ".so";
166 const string IoTSlave::STRINGCLASS = "string";
167 const string IoTSlave::INTCLASS = "int";
168 const string IoTSlave::CREATEFUNCTION = "create";
169 const string IoTSlave::DESTROYFUNCTION = "destroy";
170 const string IoTSlave::INITFUNCTION = "init";
171 const string IoTSlave::LOCALHOST = "localhost";
172 const string IoTSlave::SHELL = ".sh";
173
174 #endif