From a61d4321f6ea749910e3115aebddbc176ee6f64a Mon Sep 17 00:00:00 2001 From: localhorst Date: Tue, 4 Aug 2020 11:59:45 +0200 Subject: [PATCH] added ipc --- include/reHDD.h | 23 +++++++++++---- src/reHDD.cpp | 64 +++++++++++++++++++++++++++++------------ vcCodium.code-workspace | 49 ++++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 25 deletions(-) diff --git a/include/reHDD.h b/include/reHDD.h index 46a7d05..48fd308 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -18,6 +18,9 @@ #include #include #include +#include +#include + using namespace std; @@ -32,6 +35,8 @@ template T* iterator_to_pointer(I i) return (&(*i)); } + + class reHDD { protected: @@ -44,13 +49,21 @@ private: vector vecDrives; //stores all drive data - void searchDrives(vector * pvecDrives); + static void searchDrives(vector * pvecDrives); + + static void printDrives(vector * pvecDrives); + + + static void filterIgnoredDrives(vector * pvecDrives); + + + static void addSMARTData(vector * pvecDrives); + + static void ThreadScannDevices(); + + - void printDrives(vector * pvecDrives); - void filterIgnoredDrives(vector * pvecDrives); - void addSMARTData(vector * pvecDrives); - static void ThreadDevices(); diff --git a/src/reHDD.cpp b/src/reHDD.cpp index 72780b7..ddb783f 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -8,7 +8,16 @@ #include "../include/reHDD.h" -static int fd[2];//File descriptor for creating a pipe +static int fdSearchDrives[2];//File descriptor for pipe that informs if new drives are found + +static int fdUserInput[2];//File descriptor for pipe that informs if a user input occoures + +static std::mutex mxScannDrives; + +static vector vecNewDrives; //store found drives that are updated every 5sec + + +static fd_set selectSet; /** * \brief app constructor @@ -29,23 +38,28 @@ void reHDD::app_logic(void) { cout << "app logic" << endl; - thread thDevices(ThreadDevices); + int result = pipe(fdSearchDrives); - //searchDrives(&vecDrives); //search for new drives and store them in list - //filterIgnoredDrives(&vecDrives); //filter out ignored drives - //addSMARTData(&vecDrives); //add S.M.A.R.T. Data to the drives - // printDrives(&vecDrives); //print currently attached drives + FD_ZERO(&selectSet); + FD_SET(fdSearchDrives[0], &selectSet); + thread thDevices(ThreadScannDevices); - - int result = pipe (fd); + while(1){ + + int iSelectReturn = select(FD_SETSIZE, &selectSet, NULL, NULL, NULL); + + + if( FD_ISSET(fdSearchDrives[0], &selectSet)) { + + char ch; - result = read (fd[0],&ch,1); + result = read (fdSearchDrives[0],&ch,1); if (result != 1) { perror("read"); @@ -54,6 +68,17 @@ while(1){ printf ("From Main Thread: %c\n", ch); + mxScannDrives.lock(); + printDrives(&vecNewDrives); + //replace with old list + // action if needed + mxScannDrives.unlock(); + + } + + + + } @@ -62,8 +87,6 @@ while(1){ thDevices.join(); - std::cout << std::endl; - /* @@ -81,7 +104,7 @@ while(1){ */ } -void reHDD::ThreadDevices(){ +void reHDD::ThreadScannDevices(){ @@ -90,19 +113,22 @@ while(true){ cout << "Thread" << endl; + mxScannDrives.lock(); + vecNewDrives.clear(); + + + searchDrives(&vecNewDrives); //search for new drives and store them in list + filterIgnoredDrives(&vecNewDrives); //filter out ignored drives + addSMARTData(&vecNewDrives); //add S.M.A.R.T. Data to the drives + + mxScannDrives.unlock(); - - - - - - - int result = write (fd[1], "A",1); + int result = write (fdSearchDrives[1], "A",1); //cout << result << endl; if (result != 1){ perror ("write error"); diff --git a/vcCodium.code-workspace b/vcCodium.code-workspace index 9860bd5..9feb538 100644 --- a/vcCodium.code-workspace +++ b/vcCodium.code-workspace @@ -13,7 +13,54 @@ "string_view": "cpp", "ostream": "cpp", "chrono": "cpp", - "thread": "cpp" + "thread": "cpp", + "deque": "cpp", + "vector": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ranges": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" }, "git.ignoreLimitWarning": true }