Updating skeletons and stubs for C++; Fixing one bug in IoTCompiler for C++ skeleton...
[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_DRIVER_OBJECT,
30         CREATE_OBJECT,
31         CREATE_MAIN_OBJECT,
32         CREATE_NEW_IOTSET,
33         CREATE_NEW_IOTRELATION,
34         END_TRANSFER,
35         END_SESSION,
36         GET_ADD_IOTSET_OBJECT,
37         GET_DEVICE_IOTSET_OBJECT,
38         GET_IOTSET_OBJECT,
39         GET_IOTRELATION_FIRST_OBJECT,
40         GET_IOTRELATION_SECOND_OBJECT,
41         GET_ZB_DEV_IOTSET_OBJECT,
42         INVOKE_INIT_METHOD,
43         REINITIALIZE_IOTSET_FIELD,
44         REINITIALIZE_IOTRELATION_FIELD,
45         TRANSFER_FILE,
46         
47 };
48
49
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*);
55
56
57 class IoTSlave {
58
59         private:
60                 // Constants
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 int STUBPARAMSIZE = 5;                     // 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"
74                 const static string SHELL;                                      // String ".sh"
75
76                 // Class properties
77                 string serverAddress;
78                 int serverPort;
79                 string hostAddress;
80                 string mainObjectName;
81                 string objectName;
82                 string objectClassName;
83                 string objectInterfaceName;
84                 string objectSkelClass;         // Need to send from Java IoTSlave: sMessage.getObjectInterfaceName() + SKEL_CLASS_SUFFIX
85                 string objectStubClass;         // Need to send from Java IoTSlave: sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX
86                 int objectRegPort;
87                 int objectStubPort;
88                 vector<int>* ports;                     // Now used to contain callback ports
89                 string objectFieldName;                                         // Field name that is going to be initialized with IoTSet or IoTRelation
90                 unordered_set<void*>* isetObject;                       // Set of object
91                 IoTSet<void*>* iotsetObject;                            // IoTSet of object
92                 vector<IoTSet<void*>*> vecIoTSet;                       // IoTSet of object
93                 void* irelFirstObject;                                                  // First object of IoTRelation
94                 void* irelSecondObject;                                                 // Second object of IoTRelation
95                 unordered_multimap<void*,void*>* irelObject;    // Relation of object
96                 IoTRelation<void*,void*>* iotrelObject;                 // IoTRelation of objects
97                 vector<IoTRelation<void*,void*>*> vecIoTRel;    // IoTRelation of object
98
99                 TCPSocket* socket;
100                 ofstream log;                                           // Log the messages
101                 vector<string> args;                            // Hold the arguments for constructor (in string format)
102                 vector<string> argClasses;                      // Hold the argument classes
103                 //bool isDriverObject;                          // Set to true if this is IoTSlave instance for a driver object
104                 void* objMainCls;                                       // Main class handler, i.e. driver or controller object
105                 void* objSkelCls;                                       // Skeleton handler
106                 void* objStubCls;                                       // Stub handler
107                 unordered_map<string, void*> mapObjNameStub;    // Mapping between object name and stub
108                 // Object handlers
109                 create_t* create_object;
110                 destroy_t* destroy_object;
111                 init_t* init_object;
112
113         public:
114                 // Constructors
115                 IoTSlave(string _serverAddress, int _serverPort, string _objectName);
116                 ~IoTSlave();
117                 // Class methods
118                 string getServerAddress();
119                 int getServerPort();
120                 string getObjectName();
121                 void sendInteger(int intSend);
122                 int recvInteger();
123                 void sendString(string strSend);
124                 string recvString();
125                 string recvFile();
126                 void unzipFile(string fileName);
127                 // Main loop
128                 void sendAck();
129                 bool recvEndTransfer();
130                 void commIoTMaster();
131                 void createObject();            // Create driver object
132                 void createMainObject();        // Create main object
133                 void createNewIoTSet();
134                 void createNewIoTRelation();
135                 void getDeviceIoTSetObject();
136                 void getIoTRelationFirstObject();
137                 void getIoTRelationSecondObject();
138                 void reinitializeIoTSetField();
139                 void reinitializeIoTRelationField();
140                 void getIoTSetObject();
141                 void invokeInitMethod();
142                 void createDriverObject();
143                 void transferFile();
144
145         private:
146                 // Private helper functions
147                 int* byteToInt(int* result, char* bytes);
148                 char* intToByteArray(int i, char* bytes);
149                 char* recvIter(char* recvBuffer, int recvLen);
150                 char* recvFileIter(char* recvBuffer, int recvLen);
151                 void* getObjectConverted(void* retObj, string object, string objectClass);
152                 void openFile(string fileName);
153                 void writeToFile(string logMsg);
154                 void closeFile();
155                 void getObjectHandler(string objectClassName);
156                 void instantiateMainObject();
157                 void instantiateDriverObject();
158                 void instantiateSkelObject();
159                 void instantiateStubObject();
160                 void runInitObject(IoTSlave* iotslave);
161                 void getIoTSetRelationObject();
162                 void createStub();
163 };
164
165 // Constant initialization
166 const string IoTSlave::FILEPATH = "./";
167 const string IoTSlave::FILEEXT = "_cpp.log";
168 const string IoTSlave::SOEXT = ".so";
169 const string IoTSlave::STRINGCLASS = "string";
170 const string IoTSlave::INTCLASS = "int";
171 const string IoTSlave::CREATEFUNCTION = "create";
172 const string IoTSlave::DESTROYFUNCTION = "destroy";
173 const string IoTSlave::INITFUNCTION = "init";
174 const string IoTSlave::LOCALHOST = "localhost";
175 const string IoTSlave::SHELL = ".sh";
176
177 #endif