blob: 7ce2eba43f9a64464b8752a30fa40582fc9294ba [file] [log] [blame]
Tomáš Peckaa18702d2021-01-25 18:07:23 +01001/*
2 * Copyright (C) 2016-2018 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Jan Kundrát <jan.kundrat@cesnet.cz>
5 *
6*/
7
8#include "events.h"
9
10namespace {
11std::string sr_ev_notif_type_to_string(const sr_ev_notif_type_t notif_type)
12{
13 switch (notif_type) {
14 case SR_EV_NOTIF_REALTIME:
15 return "SR_EV_NOTIF_REALTIME";
16 case SR_EV_NOTIF_REPLAY:
17 return "SR_EV_NOTIF_REPLAY";
18 case SR_EV_NOTIF_REPLAY_COMPLETE:
19 return "SR_EV_NOTIF_REPLAY_COMPLETE";
20 case SR_EV_NOTIF_STOP:
21 return "SR_EV_NOTIF_STOP";
22 default:
23 return "[unknown event type]";
24 }
25}
26}
27
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010028EventWatcher::EventWatcher(std::function<void(Event)> callback)
29 : notifRecvCb(std::move(callback))
30{
31}
32
Tomáš Peckaa18702d2021-01-25 18:07:23 +010033EventWatcher::~EventWatcher()
34{
35}
36
37void EventWatcher::operator()(
38 [[maybe_unused]] ::sysrepo::S_Session session,
39 const sr_ev_notif_type_t notif_type,
40 const char* xpath,
41 const ::sysrepo::S_Vals vals,
42 time_t timestamp)
43{
44 Event e;
45 e.xPath = xpath;
46 e.received = std::chrono::steady_clock::now();
47
48 auto log = spdlog::get("main");
49 log->info("SR event {} {} {}", sr_ev_notif_type_to_string(notif_type), timestamp, xpath);
50
51 for (size_t i = 0; i < vals->val_cnt(); ++i) {
52 const auto& v = vals->val(i);
53 auto s = v->val_to_string();
54 log->debug(" {}: {}", v->xpath(), s);
55 e.data[v->xpath()] = s;
56 }
57
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010058 {
59 std::lock_guard<std::mutex> lock(*mutex);
60 events->push_back(e);
61 }
62
63 notifRecvCb(e);
Tomáš Peckaa18702d2021-01-25 18:07:23 +010064}
65
66std::vector<EventWatcher::Event>::size_type EventWatcher::count() const
67{
68 std::lock_guard<std::mutex> lock(*mutex);
69 return events->size();
70}
71
72EventWatcher::Event EventWatcher::peek(const std::vector<EventWatcher::Event>::size_type index) const
73{
74 std::lock_guard<std::mutex> lock(*mutex);
75 return (*events)[index];
76}