Adding stub and skeleton for Lifxtest and LifxLightBulb; Creating build flow for...
[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
8 #include <dlfcn.h>              // For dlopen, dlsym, etc.
9
10 #include "ObjectFactory.hpp"
11 #include "ISet.hpp"
12 #include "IoTSet.hpp"
13 #include "IRelation.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 class IoTSlave {
49
50         private:
51                 // Constants
52                 const static int RCVBUFSIZE = 1024;                     // Size of receive buffer
53                 const static string FILEPATH;                           // File path
54                 const static string FILEEXT;                            // File extension
55                 const static string SOEXT;                              // Shared object (.so) extension
56                 const static string STRINGCLASS;                        // String class
57                 const static string INTCLASS;                           // Int class
58                 const static string CREATEFUNCTION;                     // The create function in class
59                 const static string DESTROYFUNCTION;            // The destroy function in class
60
61                 // Class properties
62                 string serverAddress;
63                 int serverPort;
64                 string objectName;
65                 string objectClassName;
66                 string objectInterfaceName;
67                 string objectSkelClass;         // Need to send from Java IoTSlave: sMessage.getObjectInterfaceName() + SKEL_CLASS_SUFFIX
68                 int objectRegPort;
69                 int objectStubPort;
70                 
71                 void* object;                           // Handler of object
72                 TCPSocket* socket;
73                 ofstream log;                           // Log the messages
74                 vector<string> args;            // Hold the arguments for constructor (in string format)
75                 vector<string> argClasses;      // Hold the argument classes
76                 // Object handlers
77                 create_t* create_object;
78                 destroy_t* destroy_object;
79
80         public:
81                 // Constructors
82                 IoTSlave(string _serverAddress, int _serverPort, string _objectName);
83                 ~IoTSlave();
84                 // Class methods
85                 string getServerAddress();
86                 int getServerPort();
87                 string getObjectName();
88                 void sendInteger(int intSend);
89                 int recvInteger();
90                 void sendString(string strSend);
91                 string recvString();
92                 // Main loop
93                 void sendAck();
94                 bool recvEndTransfer();
95                 void commIoTMaster();
96                 void createObject();            // Create driver object
97                 void createMainObject();        // Create main object
98                 void createNewIoTSet();
99                 void getDeviceIoTSetObject();
100
101         private:
102                 // Private helper functions
103                 int* byteToInt(int* result, char* bytes);
104                 char* intToByteArray(int i, char* bytes);
105                 char* recvIter(char* recvBuffer, int recvLen);
106                 void* getObjectConverted(void* retObj, string object, string objectClass);
107                 void openFile(string fileName);
108                 void writeToFile(string logMsg);
109                 void closeFile();
110                 void instantiateObject(string objectClassName);
111 };
112
113 // Constant initialization
114 const string IoTSlave::FILEPATH = "./";
115 const string IoTSlave::FILEEXT = "_cpp.log";
116 const string IoTSlave::SOEXT = ".so";
117 const string IoTSlave::STRINGCLASS = "string";
118 const string IoTSlave::INTCLASS = "int";
119 const string IoTSlave::CREATEFUNCTION = "create";
120 const string IoTSlave::DESTROYFUNCTION = "destroy";
121
122 #endif