Perfecting IoTSlave for C++; Found one issue with SSH/Java process execution in which...
[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
9 #include <dlfcn.h>              // For dlopen, dlsym, etc.
10
11 //#include "ObjectFactory.hpp"
12 //#include "ISet.hpp"
13 #include "IoTSet.hpp"
14 #include "IoTDeviceAddress.hpp"
15 //#include "IRelation.hpp"
16 #include "IoTRelation.hpp"
17 #include "Socket.cpp"
18
19 /** Class IoTSlave is a communication class
20  *  that interacts with IoTSlave.java to set up C++
21  *  objects in Sentinel.
22  *
23  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
24  * @version     1.0
25  * @since       2017-01-12
26  */
27 // Enumeration of master-slave communication codes
28 enum IoTCommCode {
29
30         ACKNOWLEDGED,
31         CREATE_OBJECT,
32         CREATE_MAIN_OBJECT,
33         CREATE_NEW_IOTSET,
34         CREATE_NEW_IOTRELATION,
35         END_TRANSFER,
36         END_SESSION,
37         GET_ADD_IOTSET_OBJECT,
38         GET_DEVICE_IOTSET_OBJECT,
39         GET_IOTSET_OBJECT,
40         GET_IOTRELATION_FIRST_OBJECT,
41         GET_IOTRELATION_SECOND_OBJECT,
42         GET_ZB_DEV_IOTSET_OBJECT,
43         INVOKE_INIT_METHOD,
44         REINITIALIZE_IOTSET_FIELD,
45         REINITIALIZE_IOTRELATION_FIELD,
46         TRANSFER_FILE,
47
48 };
49
50
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*);
56
57
58 class IoTSlave {
59
60         private:
61                 // Constants
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"
74
75                 // Class properties
76                 string serverAddress;
77                 int serverPort;
78                 string hostAddress;
79                 string mainObjectName;
80                 string objectName;
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
85                 int objectRegPort;
86                 int objectStubPort;
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                 TCPSocket* socket;
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
101                 // Object handlers
102                 create_t* create_object;
103                 destroy_t* destroy_object;
104                 init_t* init_object;
105
106         public:
107                 // Constructors
108                 IoTSlave(string _serverAddress, int _serverPort, string _objectName);
109                 ~IoTSlave();
110                 // Class methods
111                 string getServerAddress();
112                 int getServerPort();
113                 string getObjectName();
114                 void sendInteger(int intSend);
115                 int recvInteger();
116                 void sendString(string strSend);
117                 string recvString();
118                 // Main loop
119                 void sendAck();
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();
129
130         private:
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);
138                 void closeFile();
139                 void getObjectHandler(string objectClassName);
140                 void instantiateMainObject();
141                 void instantiateDriverObject();
142                 void instantiateSkelObject();
143                 void instantiateStubObject();
144                 void runInitObject(IoTSlave* iotslave);
145 };
146
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";
157
158 #endif