2025-01-11 02:17:31 +10:00
|
|
|
#include "counter_manager.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-13 19:59:39 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-11 02:17:31 +10:00
|
|
|
long long int CounterManager::get_counter() {
|
|
|
|
char data_from_file[128];
|
|
|
|
int num_bytes = 128;
|
|
|
|
|
|
|
|
int fd = open(
|
|
|
|
"counter.txt",
|
2025-01-10 16:59:44 +00:00
|
|
|
O_RDONLY,
|
|
|
|
0777
|
2025-01-11 02:17:31 +10:00
|
|
|
);
|
|
|
|
if (fd == -1) {
|
|
|
|
return 0; // Ошибка при открытии файла
|
|
|
|
}
|
|
|
|
|
|
|
|
read(fd, data_from_file, num_bytes);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return atoi(data_from_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CounterManager::set_counter(long long int new_counter) {
|
2025-01-13 19:59:39 +10:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-01-11 02:17:31 +10:00
|
|
|
std::string new_counter_string = std::to_string(new_counter);
|
|
|
|
int fd = open(
|
|
|
|
"counter.txt",
|
|
|
|
O_WRONLY|O_TRUNC|O_APPEND|O_CREAT,
|
|
|
|
0777
|
|
|
|
);
|
|
|
|
if (fd == -1) {
|
|
|
|
return -1; // Ошибка при открытии файла
|
|
|
|
}
|
|
|
|
|
|
|
|
write(fd, new_counter_string.c_str(), new_counter_string.length());
|
|
|
|
close(fd);
|
|
|
|
|
2025-01-13 19:59:39 +10:00
|
|
|
|
|
|
|
int status = remove(queue_member_path.c_str());
|
|
|
|
if (status != 0) {
|
|
|
|
perror("Error deleting file");
|
|
|
|
}
|
|
|
|
|
|
|
|
// std::cout << queue_member << " deleted" << "\n";
|
|
|
|
|
2025-01-11 02:17:31 +10:00
|
|
|
return 0;
|
|
|
|
}
|