added ipc

This commit is contained in:
Hendrik Schutter 2020-08-04 11:59:45 +02:00
parent daa3a27edb
commit a61d4321f6
3 changed files with 111 additions and 25 deletions

View File

@ -18,6 +18,9 @@
#include <curses.h> #include <curses.h>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
#include <mutex>
#include <sys/select.h>
using namespace std; using namespace std;
@ -32,6 +35,8 @@ template <typename T, typename I> T* iterator_to_pointer(I i)
return (&(*i)); return (&(*i));
} }
class reHDD class reHDD
{ {
protected: protected:
@ -44,13 +49,21 @@ private:
vector <Drive> vecDrives; //stores all drive data vector <Drive> vecDrives; //stores all drive data
void searchDrives(vector <Drive>* pvecDrives); static void searchDrives(vector <Drive>* pvecDrives);
static void printDrives(vector <Drive>* pvecDrives);
static void filterIgnoredDrives(vector <Drive>* pvecDrives);
static void addSMARTData(vector <Drive>* pvecDrives);
static void ThreadScannDevices();
void printDrives(vector <Drive>* pvecDrives);
void filterIgnoredDrives(vector <Drive>* pvecDrives);
void addSMARTData(vector <Drive>* pvecDrives);
static void ThreadDevices();

View File

@ -8,7 +8,16 @@
#include "../include/reHDD.h" #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 <Drive> vecNewDrives; //store found drives that are updated every 5sec
static fd_set selectSet;
/** /**
* \brief app constructor * \brief app constructor
@ -29,23 +38,28 @@ void reHDD::app_logic(void)
{ {
cout << "app logic" << endl; cout << "app logic" << endl;
thread thDevices(ThreadDevices);
int result = pipe(fdSearchDrives);
//searchDrives(&vecDrives); //search for new drives and store them in list FD_ZERO(&selectSet);
//filterIgnoredDrives(&vecDrives); //filter out ignored drives FD_SET(fdSearchDrives[0], &selectSet);
//addSMARTData(&vecDrives); //add S.M.A.R.T. Data to the drives
// printDrives(&vecDrives); //print currently attached drives
thread thDevices(ThreadScannDevices);
int result = pipe (fd);
while(1){ while(1){
int iSelectReturn = select(FD_SETSIZE, &selectSet, NULL, NULL, NULL);
if( FD_ISSET(fdSearchDrives[0], &selectSet)) {
char ch; char ch;
result = read (fd[0],&ch,1); result = read (fdSearchDrives[0],&ch,1);
if (result != 1) { if (result != 1) {
perror("read"); perror("read");
@ -54,6 +68,17 @@ while(1){
printf ("From Main Thread: %c\n", ch); 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(); 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; 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 (fdSearchDrives[1], "A",1);
int result = write (fd[1], "A",1);
//cout << result << endl; //cout << result << endl;
if (result != 1){ if (result != 1){
perror ("write error"); perror ("write error");

View File

@ -13,7 +13,54 @@
"string_view": "cpp", "string_view": "cpp",
"ostream": "cpp", "ostream": "cpp",
"chrono": "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 "git.ignoreLimitWarning": true
} }