Cleaning up C++ slave.
[iot2.git] / iotjava / iotruntime / cpp / iotslave / IoTSlave.hpp
index 343a2c2ef7e9fcb3535bc693099b5e329e649018..bbac3988c813df24718e913d20b95b1ad8eda889 100644 (file)
@@ -5,20 +5,18 @@
 #include <fstream>
 #include <vector>
 #include <thread>
+#include <cstdlib>
 
 #include <dlfcn.h>             // For dlopen, dlsym, etc.
 
-//#include "ObjectFactory.hpp"
-//#include "ISet.hpp"
 #include "IoTSet.hpp"
 #include "IoTDeviceAddress.hpp"
-//#include "IRelation.hpp"
 #include "IoTRelation.hpp"
 #include "Socket.cpp"
 
 /** Class IoTSlave is a communication class
  *  that interacts with IoTSlave.java to set up C++
- *  objects in Sentinel.
+ *  objects in Vigilia.
  *
  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
  * @version     1.0
@@ -28,6 +26,7 @@
 enum IoTCommCode {
 
        ACKNOWLEDGED,
+       CREATE_DRIVER_OBJECT,
        CREATE_OBJECT,
        CREATE_MAIN_OBJECT,
        CREATE_NEW_IOTSET,
@@ -44,7 +43,7 @@ enum IoTCommCode {
        REINITIALIZE_IOTSET_FIELD,
        REINITIALIZE_IOTRELATION_FIELD,
        TRANSFER_FILE,
-
+       
 };
 
 
@@ -55,13 +54,14 @@ typedef void destroy_t(void*);
 typedef void init_t(void*);
 
 
-class IoTSlave {
+class IoTSlave final {
 
        private:
                // Constants
                const static int RCVBUFSIZE = 1024;                     // Size of receive buffer
                const static int SKELPARAMSIZE = 3;                     // Number of params for skeleton
-               const static int STUBPARAMSIZE = 6;                     // Number of params for stub
+               const static int STUBPARAMSIZE = 5;                     // Number of params for stub
+               const static string LOGFILEPATH;                        // Log file path
                const static string FILEPATH;                           // File path
                const static string FILEEXT;                            // File extension
                const static string SOEXT;                              // Shared object (.so) extension
@@ -71,6 +71,7 @@ class IoTSlave {
                const static string DESTROYFUNCTION;            // The destroy function in class
                const static string INITFUNCTION;                       // The init function in class
                const static string LOCALHOST;                          // String "localhost"
+               const static string SHELL;                                      // String ".sh"
 
                // Class properties
                string serverAddress;
@@ -84,16 +85,21 @@ class IoTSlave {
                string objectStubClass;         // Need to send from Java IoTSlave: sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX
                int objectRegPort;
                int objectStubPort;
-               vector<int> ports;                      // Now used to contain callback ports
-               string objectFieldName;                         // Field name that is going to be initialized with IoTSet or IoTRelation
-               unordered_set<void*>* isetObject;       // Set of object
-               IoTSet<void*>* iotsetObject;            // IoTSet of object
-               vector<IoTSet<void*>*> vecIoTSet;       // IoTSet of object
+               string objectFieldName;                                         // Field name that is going to be initialized with IoTSet or IoTRelation
+               unordered_set<void*>* isetObject;                       // Set of object
+               IoTSet<void*>* iotsetObject;                            // IoTSet of object
+               vector<IoTSet<void*>*> vecIoTSet;                       // IoTSet of object
+               void* irelFirstObject;                                                  // First object of IoTRelation
+               void* irelSecondObject;                                                 // Second object of IoTRelation
+               unordered_multimap<void*,void*>* irelObject;    // Relation of object
+               IoTRelation<void*,void*>* iotrelObject;                 // IoTRelation of objects
+               vector<IoTRelation<void*,void*>*> vecIoTRel;    // IoTRelation of object
+
                TCPSocket* socket;
                ofstream log;                                           // Log the messages
                vector<string> args;                            // Hold the arguments for constructor (in string format)
                vector<string> argClasses;                      // Hold the argument classes
-               bool isDriverObject;                            // Set to true if this is IoTSlave instance for a driver object
+               //bool isDriverObject;                          // Set to true if this is IoTSlave instance for a driver object
                void* objMainCls;                                       // Main class handler, i.e. driver or controller object
                void* objSkelCls;                                       // Skeleton handler
                void* objStubCls;                                       // Stub handler
@@ -115,6 +121,8 @@ class IoTSlave {
                int recvInteger();
                void sendString(string strSend);
                string recvString();
+               string recvFile();
+               void unzipFile(string fileName);
                // Main loop
                void sendAck();
                bool recvEndTransfer();
@@ -122,16 +130,23 @@ class IoTSlave {
                void createObject();            // Create driver object
                void createMainObject();        // Create main object
                void createNewIoTSet();
+               void createNewIoTRelation();
                void getDeviceIoTSetObject();
+               void getIoTRelationFirstObject();
+               void getIoTRelationSecondObject();
                void reinitializeIoTSetField();
+               void reinitializeIoTRelationField();
                void getIoTSetObject();
                void invokeInitMethod();
+               void createDriverObject();
+               void transferFile();
 
        private:
                // Private helper functions
                int* byteToInt(int* result, char* bytes);
                char* intToByteArray(int i, char* bytes);
                char* recvIter(char* recvBuffer, int recvLen);
+               char* recvFileIter(char* recvBuffer, int recvLen);
                void* getObjectConverted(void* retObj, string object, string objectClass);
                void openFile(string fileName);
                void writeToFile(string logMsg);
@@ -142,9 +157,12 @@ class IoTSlave {
                void instantiateSkelObject();
                void instantiateStubObject();
                void runInitObject(IoTSlave* iotslave);
+               void getIoTSetRelationObject();
+               void createStub();
 };
 
 // Constant initialization
+const string IoTSlave::LOGFILEPATH = "../log/";
 const string IoTSlave::FILEPATH = "./";
 const string IoTSlave::FILEEXT = "_cpp.log";
 const string IoTSlave::SOEXT = ".so";
@@ -154,5 +172,6 @@ const string IoTSlave::CREATEFUNCTION = "create";
 const string IoTSlave::DESTROYFUNCTION = "destroy";
 const string IoTSlave::INITFUNCTION = "init";
 const string IoTSlave::LOCALHOST = "localhost";
+const string IoTSlave::SHELL = ".sh";
 
 #endif