added ipc

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

View File

@ -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 <Drive> 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");