Replace multi-process logic to multi-thread logic #2
113
src/main.cpp
113
src/main.cpp
@@ -1,62 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <syslog.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include "locations.h"
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include "usermanager.h"
|
||||
#include "guarder.h"
|
||||
|
||||
#define MAXNAMESIZE 255
|
||||
#define SCAN_DONE_SIG SIGRTMIN
|
||||
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
|
||||
|
||||
int p1[2];
|
||||
guarder guard;
|
||||
userManager manager;
|
||||
std::condition_variable cv;
|
||||
std::mutex mtx;
|
||||
|
||||
void flushManagerToPipe()
|
||||
void readingThreadFunc()
|
||||
{
|
||||
if (!guard.isFlagOn())
|
||||
{
|
||||
std::vector<std::string> users = manager.getFlaggedUsers();
|
||||
|
||||
for (std::vector<std::string>::iterator it = users.begin(); it != users.end(); ++it)
|
||||
{
|
||||
int size = it->size();
|
||||
write(p1[1], &size, sizeof(int));
|
||||
write(p1[1], it->data(), size * sizeof(char));
|
||||
}
|
||||
|
||||
manager.unflagAllUsers();
|
||||
}
|
||||
}
|
||||
|
||||
void handler(int sig)
|
||||
{
|
||||
if (sig == SCAN_DONE_SIG)
|
||||
{
|
||||
flushManagerToPipe();
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
||||
pipe(p1);
|
||||
|
||||
pid_t parent = getpid();
|
||||
pid_t child = fork();
|
||||
|
||||
if (child > 0) // parent
|
||||
{
|
||||
signal(SCAN_DONE_SIG, handler);
|
||||
close(p1[0]); // read
|
||||
|
||||
FILE *logpipe = popen(LOGFILE, "r");
|
||||
std::array<char, 256> buffer;
|
||||
|
||||
@@ -69,51 +29,44 @@ int main()
|
||||
|
||||
std::vector<std::string> x = splitLogFile(line, '|');
|
||||
std::string user(x.at(USER_LOG_LOCATION));
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
manager.addUser(user);
|
||||
manager.setUserFlagged(user);
|
||||
}
|
||||
|
||||
cv.notify_one();
|
||||
|
||||
std::cout << "User find: " << user << std::endl;
|
||||
|
||||
flushManagerToPipe();
|
||||
}
|
||||
}
|
||||
|
||||
fclose(logpipe);
|
||||
close(p1[1]); // write
|
||||
}
|
||||
else // child
|
||||
void scannerThreadFunc()
|
||||
{
|
||||
close(p1[1]); // write
|
||||
|
||||
int size;
|
||||
char *buffer = nullptr;
|
||||
|
||||
while (read(p1[0], &size, sizeof(int)))
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
while (true)
|
||||
{
|
||||
if (buffer == nullptr)
|
||||
buffer = new char[size];
|
||||
cv.wait(lock, []
|
||||
{ return manager.isAnybodyInQueue(); });
|
||||
|
||||
read(p1[0], buffer, size * sizeof(char));
|
||||
std::string name(buffer);
|
||||
std::vector<std::string> scanUsers = manager.getFlaggedUsers();
|
||||
manager.unflagAllUsers();
|
||||
|
||||
std::cout << "Scan received for: " << name << std::endl;
|
||||
|
||||
guard.setFlagOn();
|
||||
system((std::string(SCAN_CMD_USR) + name).data());
|
||||
guard.setFlagOff();
|
||||
|
||||
kill(parent, SCAN_DONE_SIG);
|
||||
|
||||
if (buffer != nullptr)
|
||||
for (std::vector<std::string>::iterator it = scanUsers.begin(); it != scanUsers.end(); it++)
|
||||
{
|
||||
delete[] buffer;
|
||||
buffer = nullptr;
|
||||
system((std::string(SCAN_CMD_USR) + *it).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(p1[0]); // read
|
||||
}
|
||||
int main()
|
||||
{
|
||||
std::thread readingThread(readingThreadFunc);
|
||||
std::thread scannerThread(scannerThreadFunc);
|
||||
|
||||
closelog();
|
||||
readingThread.join();
|
||||
scannerThread.join();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
return 0;
|
||||
}
|
||||
@@ -101,6 +101,19 @@ public:
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool isAnybodyInQueue()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||
{
|
||||
if (it->second)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _USERMAN_H
|
||||
Reference in New Issue
Block a user