From be84bd17356aca9db93abcd51cf75d9fcc996d81 Mon Sep 17 00:00:00 2001 From: matv864 Date: Mon, 13 Jan 2025 19:59:39 +1000 Subject: [PATCH] add queue --- task_3/CMakeLists.txt | 3 +- task_3/counter.txt | 2 +- task_3/counter_manager.cpp | 62 ++++++++++++++++++++++++++++ task_3/counter_manager.h | 6 +++ task_3/log_writer.cpp | 20 +++------ task_3/log_writer.h | 7 ++-- task_3/main.cpp | 40 ++++++++++++------ task_3/my_time.cpp | 19 +++++++++ task_3/my_time.h | 16 +++++++ task_3/queue/1736762345499_18672.txt | 0 task_3/queue/1736762345500_740.txt | 0 11 files changed, 142 insertions(+), 33 deletions(-) create mode 100644 task_3/my_time.cpp create mode 100644 task_3/my_time.h create mode 100644 task_3/queue/1736762345499_18672.txt create mode 100644 task_3/queue/1736762345500_740.txt diff --git a/task_3/CMakeLists.txt b/task_3/CMakeLists.txt index cf59185..0b8477f 100644 --- a/task_3/CMakeLists.txt +++ b/task_3/CMakeLists.txt @@ -3,10 +3,11 @@ cmake_minimum_required(VERSION 3.13) project(hello-world-cpp-logger) +add_library(my_time STATIC my_time.cpp) add_library(log_writer STATIC log_writer.cpp) add_library(process_launcher STATIC process_launcher.cpp) add_library(counter_manager STATIC counter_manager.cpp) add_executable(main main.cpp) -target_link_libraries(main log_writer process_launcher counter_manager) \ No newline at end of file +target_link_libraries(main my_time log_writer process_launcher counter_manager) \ No newline at end of file diff --git a/task_3/counter.txt b/task_3/counter.txt index 368f89c..e3b5acb 100644 --- a/task_3/counter.txt +++ b/task_3/counter.txt @@ -1 +1 @@ -28 \ No newline at end of file +107 \ No newline at end of file diff --git a/task_3/counter_manager.cpp b/task_3/counter_manager.cpp index 599e9ba..bccf2ac 100644 --- a/task_3/counter_manager.cpp +++ b/task_3/counter_manager.cpp @@ -2,6 +2,9 @@ + + + long long int CounterManager::get_counter() { char data_from_file[128]; int num_bytes = 128; @@ -24,6 +27,57 @@ long long int CounterManager::get_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); int fd = open( "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()); 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; } \ No newline at end of file diff --git a/task_3/counter_manager.h b/task_3/counter_manager.h index c8f228c..6b2cc68 100644 --- a/task_3/counter_manager.h +++ b/task_3/counter_manager.h @@ -1,7 +1,13 @@ +#include "my_time.h" + #include #include #include +#include + +#include + class CounterManager { public: diff --git a/task_3/log_writer.cpp b/task_3/log_writer.cpp index 962dd3e..d9897c8 100644 --- a/task_3/log_writer.cpp +++ b/task_3/log_writer.cpp @@ -1,5 +1,6 @@ #include "log_writer.h" + int LogWriter::write_log(const std::string& log_line) { std::string new_line = "\n"; 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(){ std::string log_line = ( "start programm with pid " + std::to_string(getpid()) + " at " + - get_current_time() + MyTime::get_current_time() ); write_log(log_line); } @@ -45,7 +35,7 @@ void LogWriter::log_start_line(){ void LogWriter::log_counter(long long int counter){ std::string log_line = ( "counter at " + - get_current_time() + + MyTime::get_current_time() + " in process with pid " + std::to_string(getpid()) + " is " + @@ -59,7 +49,7 @@ void LogWriter::log_copy_start(std::string copy_name) { std::string log_line = ( copy_name + " is started at " + - get_current_time() + + MyTime::get_current_time() + " with pid " + std::to_string(getpid()) ); @@ -70,7 +60,7 @@ void LogWriter::log_copy_finish(std::string copy_name) { std::string log_line = ( copy_name + " is finished at " + - get_current_time() + MyTime::get_current_time() ); write_log(log_line); } diff --git a/task_3/log_writer.h b/task_3/log_writer.h index 4371cd9..249696e 100644 --- a/task_3/log_writer.h +++ b/task_3/log_writer.h @@ -1,7 +1,9 @@ +#include "my_time.h" + #include #include #include -#include + class LogWriter { @@ -12,9 +14,6 @@ public: static void log_copy_finish(std::string copy_name); static void log_copy_error(std::string copy_name); -protected: - static std::string get_current_time(); - private: static int write_log(const std::string& log_line); diff --git a/task_3/main.cpp b/task_3/main.cpp index 4d0a23a..9e56292 100644 --- a/task_3/main.cpp +++ b/task_3/main.cpp @@ -2,7 +2,9 @@ #include "log_writer.h" #include "counter_manager.h" -#include +#include +#include + struct two_pid { @@ -32,13 +34,6 @@ struct two_pid { #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[]) { 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(); struct two_pid pid_of_copy_processes; - long long int last_counter_increment = get_timestamp_ms(); - long long int last_counter_logging = get_timestamp_ms(); - long long int last_copies_launching = get_timestamp_ms(); + long long int last_counter_increment = MyTime::get_timestamp(); + long long int last_counter_logging = MyTime::get_timestamp(); + long long int last_copies_launching = MyTime::get_timestamp(); long long int current_time; while (1) { - current_time = get_timestamp_ms(); + current_time = MyTime::get_timestamp(); if ((current_time - last_counter_increment) >= 300) { last_counter_increment = current_time; diff --git a/task_3/my_time.cpp b/task_3/my_time.cpp new file mode 100644 index 0000000..34b3c02 --- /dev/null +++ b/task_3/my_time.cpp @@ -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); +} \ No newline at end of file diff --git a/task_3/my_time.h b/task_3/my_time.h new file mode 100644 index 0000000..993fc7e --- /dev/null +++ b/task_3/my_time.h @@ -0,0 +1,16 @@ +#ifndef MY_TIME_H +#define MY_TIME_H + +#include +#include +#include + + +class MyTime { +public: + static std::string get_current_time(); + static long long int get_timestamp(); +}; + + +#endif // MY_TIME_H \ No newline at end of file diff --git a/task_3/queue/1736762345499_18672.txt b/task_3/queue/1736762345499_18672.txt new file mode 100644 index 0000000..e69de29 diff --git a/task_3/queue/1736762345500_740.txt b/task_3/queue/1736762345500_740.txt new file mode 100644 index 0000000..e69de29