Skip to content

Dynamic ntp time source selection in timestamp_mw#364

Draft
WiktorM8 wants to merge 5 commits into
masterfrom
wimu/dynamic_timestamp_mw
Draft

Dynamic ntp time source selection in timestamp_mw#364
WiktorM8 wants to merge 5 commits into
masterfrom
wimu/dynamic_timestamp_mw

Conversation

@WiktorM8
Copy link
Copy Markdown

No description provided.

Wiktor Muller added 2 commits May 2, 2026 19:35
- NodeInfo storage using std::unordered_map
- Implemented best neighbor selection algorithm
- Unit tests for discovery manager
- configuration manager
- integration with controller
- unit tests
@WiktorM8 WiktorM8 self-assigned this May 11, 2026
Copy link
Copy Markdown
Collaborator

@Mateusz-Krajewski Mateusz-Krajewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Comment thread deployment/cpu/ec/ntp_config.json Outdated
@@ -0,0 +1,5 @@
{
"ip": "192.168.10.51",
"ntp_class": 3,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ntp_class": 3,
"ntp_class": 4,


auto raw_json = parser.GetObject();

if (raw_json.contains("ntp_class") && raw_json["ntp_class"].is_number_integer()) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (raw_json.contains("ntp_class") && raw_json["ntp_class"].is_number_integer()) {
auto ntp_class = raw_json.GetNumer<uint8_t>("ntp_class");
config.ntp_class = ntp_class.value_or(kDefault_device_class);

tak samo dla reszty (do definiowania defaultowych wartosci użyj namespace {static constexpr auto kDefault_device_class = 7;}

namespace srp {
namespace tinyNTP {

constexpr const char* CONFIG_FILEPATH = "/srp/opt/cpu_srp/ntp_config.json";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constexpr const char* CONFIG_FILEPATH = "/srp/opt/cpu_srp/ntp_config.json";
namespace {
static constexpr auto kConfig_file_path = "/srp/opt/cpu_srp/ntp_config.json";
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpplint rzuca błąd za nienazwany namespace w .hpp


std::optional<std::string> readMyIP();
std::vector<uint8_t> socket_callback(const std::string& ip, const std::uint16_t& port,
const std::vector<std::uint8_t> payload);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const std::vector<std::uint8_t> payload);
const std::vector<std::uint8_t>& payload);

Comment on lines 43 to 44
int64_t CalculateOffset(const int64_t T0, const int64_t T1, const int64_t T2, const int64_t T3);
uint64_t CalculateRoundTripDelay(const int64_t T0, const int64_t T1, const int64_t T2, const int64_t T3);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int64_t CalculateOffset(const int64_t T0, const int64_t T1, const int64_t T2, const int64_t T3);
uint64_t CalculateRoundTripDelay(const int64_t T0, const int64_t T1, const int64_t T2, const int64_t T3);
int64_t CalculateOffset(const int64_t& T0, const int64_t& T1, const int64_t& T2, const int64_t& T3);
uint64_t CalculateRoundTripDelay(const int64_t& T0, const int64_t& T1, const int64_t& T2, const int64_t& T3);

void DiscoveryManager::UpdateNode(const std::string& ip, uint8_t ntp_class, bool holdover) {
std::lock_guard<std::mutex> lock(map_mutex_);

NodeInfo& node = neighbors_[ip];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pootencjalny segmentation fault?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neighbors_ to std::unordered_map<std::string, NodeInfo>, więc raczej nie. Tu co najwyżej pytanie, czy nie zmienić tego na zwykły vector?

Copy link
Copy Markdown
Collaborator

@Mateusz-Krajewski Mateusz-Krajewski Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bardzo dobrze że jest to unordered map, ale jeśli w neightbors_ nie będzie ip do którego się odwołujesz dostaniesz Segmentation Fault, użyj np find.
cytując https://www.geeksforgeeks.org/cpp/unordered_map-in-cpp-stl/

#include <bits/stdc++.h>
using namespace std;

int main() {
    unordered_map<int, string> um =
    {{1, "Geeks"}, {2, "For"}, {3, "C++"}};
    
    // Finding element with key 2
    auto it = um.find(2);
    
    if (it != um.end())
        cout << it->first << ": " << it->second;
    else cout << "Not Found";
    return 0;
}

Comment thread mw/timestamp_mw/ntp/discovery/discovery_manager.cpp Outdated
Comment thread mw/timestamp_mw/ntp/discovery/discovery_manager.cpp Outdated
Comment thread mw/timestamp_mw/ntp/config/config_manager.cpp Outdated
Wiktor Muller added 3 commits June 1, 2026 13:33
- Initialization of udp and udp multicast sockets
- Connecting to mechanism of chosing best neighbor
- Resolving conversations
- Fixing code to pass cpplint tests
- Fixing code to pass unit tests
- separate callback functions for socket
- safety checks for invalid message types and rapid change of function between server and client mode
- header setting encoding test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants