-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
synchronizationController.hpp
81 lines (73 loc) · 2.55 KB
/
synchronizationController.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Wazuh RSYNC
* Copyright (C) 2015, Wazuh Inc.
* October 2, 2023.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/
#ifndef _SYNCHRONIZATION_CONTROLLER_HPP
#define _SYNCHRONIZATION_CONTROLLER_HPP
#include "commonDefs.h"
#include "loggerHelper.h"
#include "rsync_exception.h"
#include <mutex>
#include <shared_mutex>
#include <unordered_map>
namespace RSync
{
class SynchronizationController final
{
public:
void start(const RSYNC_HANDLE key, const std::string& table, const int32_t value)
{
std::lock_guard<std::mutex> lock{ m_mutex };
m_data[key][table] = value;
}
void stop(const RSYNC_HANDLE key)
{
std::lock_guard<std::mutex> lock{ m_mutex };
if (m_data.find(key) != m_data.end())
{
m_data.erase(key);
}
}
void clear()
{
std::lock_guard<std::mutex> lock{ m_mutex };
m_data.clear();
}
void checkId(const RSYNC_HANDLE key, const std::string& table, const int32_t value)
{
std::lock_guard<std::mutex> lock{ m_mutex };
const auto it = m_data.find(key);
if (it == m_data.end())
{
throw rsync_error { HANDLE_NOT_FOUND };
}
else
{
const auto itTable = it->second.find(table);
if (itTable != it->second.end())
{
if (value < itTable->second)
{
itTable->second = value;
}
if (value > itTable->second)
{
Log::debugVerbose << "Sync id: " << std::to_string(value) << " is not the current id: "
<< std::to_string(itTable->second) << " for table: "<< table << LogEndl;
throw std::runtime_error { "Sync id is not the current id" };
}
}
}
}
private:
std::unordered_map<RSYNC_HANDLE, std::unordered_map<std::string, int32_t>> m_data;
std::mutex m_mutex;
};
} // namespace RSync
#endif // _SYNCHRONIZATION_CONTROLLER_HPP