add queue

This commit is contained in:
Ivanov Matvey 2025-01-13 19:59:39 +10:00
parent b20ce7c4de
commit be84bd1735
11 changed files with 142 additions and 33 deletions

View File

@ -3,10 +3,11 @@ cmake_minimum_required(VERSION 3.13)
project(hello-world-cpp-logger) project(hello-world-cpp-logger)
add_library(my_time STATIC my_time.cpp)
add_library(log_writer STATIC log_writer.cpp) add_library(log_writer STATIC log_writer.cpp)
add_library(process_launcher STATIC process_launcher.cpp) add_library(process_launcher STATIC process_launcher.cpp)
add_library(counter_manager STATIC counter_manager.cpp) add_library(counter_manager STATIC counter_manager.cpp)
add_executable(main main.cpp) add_executable(main main.cpp)
target_link_libraries(main log_writer process_launcher counter_manager) target_link_libraries(main my_time log_writer process_launcher counter_manager)

View File

@ -1 +1 @@
28 107

View File

@ -2,6 +2,9 @@
long long int CounterManager::get_counter() { long long int CounterManager::get_counter() {
char data_from_file[128]; char data_from_file[128];
int num_bytes = 128; int num_bytes = 128;
@ -24,6 +27,57 @@ long long int CounterManager::get_counter() {
int CounterManager::set_counter(long long int new_counter) { int CounterManager::set_counter(long long int new_counter) {
long long int current_timestamp = MyTime::get_timestamp(), foreign_timestamp;
std::string queue_directory = "queue";
std::string queue_member = std::to_string(current_timestamp) + "_" + std::to_string(getpid()) + ".txt";
std::string queue_member_path = queue_directory + "/" + queue_member;
std::string filename, temp_str;
int queue_member_fd = open(queue_member_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (queue_member_fd == -1) { // Если ошибка при открытии
std::cerr << "queue member can't be created" << std::endl;
return -1;
}
close(queue_member_fd);
// std::cout << queue_member << " created" << "\n";
bool start_operation = 0;
DIR* dir;
struct dirent* entry;
while (!start_operation) {
usleep((useconds_t)(100 * 1000));
// std::cout << queue_member << " wait" << "\n";
dir = opendir(queue_directory.c_str());
start_operation = 1;
while ((entry = readdir(dir)) != nullptr) {
// Исключаем "." и ".."
filename = std::string(entry->d_name);
if (filename == "." || filename == "..") {
continue;
}
// std::cout << queue_member << " file prev: " << filename << "\n";
temp_str = "";
for (char sym : filename) {
if (sym == '_') {
break;
}
temp_str += sym;
}
foreign_timestamp = atoll(temp_str.c_str());
if (foreign_timestamp < current_timestamp) {
start_operation = 0;
break;
}
}
}
std::string new_counter_string = std::to_string(new_counter); std::string new_counter_string = std::to_string(new_counter);
int fd = open( int fd = open(
"counter.txt", "counter.txt",
@ -37,5 +91,13 @@ int CounterManager::set_counter(long long int new_counter) {
write(fd, new_counter_string.c_str(), new_counter_string.length()); write(fd, new_counter_string.c_str(), new_counter_string.length());
close(fd); close(fd);
int status = remove(queue_member_path.c_str());
if (status != 0) {
perror("Error deleting file");
}
// std::cout << queue_member << " deleted" << "\n";
return 0; return 0;
} }

View File

@ -1,7 +1,13 @@
#include "my_time.h"
#include <string> #include <string>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h>
#include <iostream>
class CounterManager { class CounterManager {
public: public:

View File

@ -1,5 +1,6 @@
#include "log_writer.h" #include "log_writer.h"
int LogWriter::write_log(const std::string& log_line) { int LogWriter::write_log(const std::string& log_line) {
std::string new_line = "\n"; std::string new_line = "\n";
int fd = open( int fd = open(
@ -20,23 +21,12 @@ int LogWriter::write_log(const std::string& log_line) {
} }
std::string LogWriter::get_current_time() {
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
std::string res = asctime (timeinfo);
res.pop_back();
return res;
}
void LogWriter::log_start_line(){ void LogWriter::log_start_line(){
std::string log_line = ( std::string log_line = (
"start programm with pid " + "start programm with pid " +
std::to_string(getpid()) + std::to_string(getpid()) +
" at " + " at " +
get_current_time() MyTime::get_current_time()
); );
write_log(log_line); write_log(log_line);
} }
@ -45,7 +35,7 @@ void LogWriter::log_start_line(){
void LogWriter::log_counter(long long int counter){ void LogWriter::log_counter(long long int counter){
std::string log_line = ( std::string log_line = (
"counter at " + "counter at " +
get_current_time() + MyTime::get_current_time() +
" in process with pid " + " in process with pid " +
std::to_string(getpid()) + std::to_string(getpid()) +
" is " + " is " +
@ -59,7 +49,7 @@ void LogWriter::log_copy_start(std::string copy_name) {
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is started at " + " is started at " +
get_current_time() + MyTime::get_current_time() +
" with pid " + " with pid " +
std::to_string(getpid()) std::to_string(getpid())
); );
@ -70,7 +60,7 @@ void LogWriter::log_copy_finish(std::string copy_name) {
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is finished at " + " is finished at " +
get_current_time() MyTime::get_current_time()
); );
write_log(log_line); write_log(log_line);
} }

View File

@ -1,7 +1,9 @@
#include "my_time.h"
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h>
class LogWriter { class LogWriter {
@ -12,9 +14,6 @@ public:
static void log_copy_finish(std::string copy_name); static void log_copy_finish(std::string copy_name);
static void log_copy_error(std::string copy_name); static void log_copy_error(std::string copy_name);
protected:
static std::string get_current_time();
private: private:
static int write_log(const std::string& log_line); static int write_log(const std::string& log_line);

View File

@ -2,7 +2,9 @@
#include "log_writer.h" #include "log_writer.h"
#include "counter_manager.h" #include "counter_manager.h"
#include <sys/time.h> #include <sys/stat.h>
#include <iostream>
struct two_pid { struct two_pid {
@ -32,13 +34,6 @@ struct two_pid {
#endif #endif
long long int get_timestamp_ms() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (((long long int)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc >= 2) { if (argc >= 2) {
@ -59,17 +54,38 @@ int main(int argc, char* argv[]) {
} }
} }
// create dir if it doesn't exist
struct stat info;
std::string dirpath = "queue";
if (!(stat(dirpath.c_str(), &info) == 0 && (info.st_mode & S_IFDIR))) {
mkdir(dirpath.c_str()) == 0;
}
DIR* dir;
struct dirent* entry;
std::string filename_temp;
dir = opendir(dirpath.c_str());
while ((entry = readdir(dir)) != nullptr) {
// Исключаем "." и ".."
filename_temp = std::string(entry->d_name);
if (filename_temp == "." || filename_temp == "..") {
continue;
}
remove((dirpath + "\\" + filename_temp).c_str());
}
LogWriter::log_start_line(); LogWriter::log_start_line();
struct two_pid pid_of_copy_processes; struct two_pid pid_of_copy_processes;
long long int last_counter_increment = get_timestamp_ms(); long long int last_counter_increment = MyTime::get_timestamp();
long long int last_counter_logging = get_timestamp_ms(); long long int last_counter_logging = MyTime::get_timestamp();
long long int last_copies_launching = get_timestamp_ms(); long long int last_copies_launching = MyTime::get_timestamp();
long long int current_time; long long int current_time;
while (1) { while (1) {
current_time = get_timestamp_ms(); current_time = MyTime::get_timestamp();
if ((current_time - last_counter_increment) >= 300) { if ((current_time - last_counter_increment) >= 300) {
last_counter_increment = current_time; last_counter_increment = current_time;

19
task_3/my_time.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "my_time.h"
std::string MyTime::get_current_time() {
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
std::string res = asctime (timeinfo);
res.pop_back();
return res;
}
long long int MyTime::get_timestamp() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (((long long int)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}

16
task_3/my_time.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef MY_TIME_H
#define MY_TIME_H
#include <string>
#include <time.h>
#include <sys/time.h>
class MyTime {
public:
static std::string get_current_time();
static long long int get_timestamp();
};
#endif // MY_TIME_H

View File

View File