Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 10 | namespace { |
| 11 | std::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áš Pecka | 76fa2ff | 2021-03-15 16:28:06 +0100 | [diff] [blame] | 28 | EventWatcher::EventWatcher(std::function<void(Event)> callback) |
| 29 | : notifRecvCb(std::move(callback)) |
| 30 | { |
| 31 | } |
| 32 | |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 33 | EventWatcher::~EventWatcher() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void 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áš Pecka | 76fa2ff | 2021-03-15 16:28:06 +0100 | [diff] [blame] | 58 | { |
| 59 | std::lock_guard<std::mutex> lock(*mutex); |
| 60 | events->push_back(e); |
| 61 | } |
| 62 | |
| 63 | notifRecvCb(e); |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::vector<EventWatcher::Event>::size_type EventWatcher::count() const |
| 67 | { |
| 68 | std::lock_guard<std::mutex> lock(*mutex); |
| 69 | return events->size(); |
| 70 | } |
| 71 | |
| 72 | EventWatcher::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 | } |