Cleaning up C++ slave.
[iot2.git] / iotjava / iotruntime / cpp / iotslave / IoTSlave.hpp
index f998a09c54ee931fd6ec6a28a3c5fb29a1a82cea..bbac3988c813df24718e913d20b95b1ad8eda889 100644 (file)
@@ -4,19 +4,19 @@
 #include <iostream>
 #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 "IRelation.hpp"
+#include "IoTDeviceAddress.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
@@ -26,6 +26,7 @@
 enum IoTCommCode {
 
        ACKNOWLEDGED,
+       CREATE_DRIVER_OBJECT,
        CREATE_OBJECT,
        CREATE_MAIN_OBJECT,
        CREATE_NEW_IOTSET,
@@ -42,14 +43,25 @@ enum IoTCommCode {
        REINITIALIZE_IOTSET_FIELD,
        REINITIALIZE_IOTRELATION_FIELD,
        TRANSFER_FILE,
-
+       
 };
 
-class IoTSlave {
+
+// Defining generic function pointers for 
+// create, destroy, and init functions of each class object
+typedef void* create_t(void**);
+typedef void destroy_t(void*);
+typedef void init_t(void*);
+
+
+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 = 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
@@ -57,25 +69,45 @@ class IoTSlave {
                const static string INTCLASS;                           // Int class
                const static string CREATEFUNCTION;                     // The create function in class
                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;
                int serverPort;
+               string hostAddress;
+               string mainObjectName;
                string objectName;
                string objectClassName;
                string objectInterfaceName;
                string objectSkelClass;         // Need to send from Java IoTSlave: sMessage.getObjectInterfaceName() + SKEL_CLASS_SUFFIX
+               string objectStubClass;         // Need to send from Java IoTSlave: sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX
                int objectRegPort;
                int objectStubPort;
-               
-               void* object;                           // Handler 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
+               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
+               void* objMainCls;                                       // Main class handler, i.e. driver or controller object
+               void* objSkelCls;                                       // Skeleton handler
+               void* objStubCls;                                       // Stub handler
+               unordered_map<string, void*> mapObjNameStub;    // Mapping between object name and stub
                // Object handlers
                create_t* create_object;
                destroy_t* destroy_object;
+               init_t* init_object;
 
        public:
                // Constructors
@@ -89,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();
@@ -96,21 +130,39 @@ 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);
                void closeFile();
-               void instantiateObject(string objectClassName);
+               void getObjectHandler(string objectClassName);
+               void instantiateMainObject();
+               void instantiateDriverObject();
+               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";
@@ -118,5 +170,8 @@ const string IoTSlave::STRINGCLASS = "string";
 const string IoTSlave::INTCLASS = "int";
 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