OpenCPN Partial API docs
Loading...
Searching...
No Matches
conn_states.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2024 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <algorithm>
25#include <iostream>
26#include <sstream>
27
28#include "model/conn_states.h"
30#include "model/conn_params.h"
31
32bool ConnData::IsDriverStatsMatch(const DriverStats& ds) const {
33 return ds.driver_bus == driver_stats.driver_bus &&
34 ds.driver_iface == driver_stats.driver_iface;
35}
36
37bool ConnData::IsDriverMatch(NavAddr::Bus bus, const std::string& iface) const {
38 return driver_stats.driver_bus == bus && driver_stats.driver_iface == iface;
39}
40
41std::string ConnData::to_string() {
42 std::stringstream ss;
43 return ss.str();
44}
45
46ConnStates::ConnStates() {
47 auto& registry = CommDriverRegistry::GetInstance();
48 m_driverlist_lstnr.Init(registry.evt_driverlist_change,
49 [&](ObservedEvt& ev) { OnDriverListChange(ev); });
50 m_driverstat_lstnr.Init(registry.evt_driver_stats,
51 [&](ObservedEvt& ev) { OnDriverStats(ev); });
52}
53
54void ConnStates::HandleDriverStats(const DriverStats& stats) {
55 auto conn_params = TheConnectionParams();
56 auto found_param = std::find_if(
57 conn_params.begin(), conn_params.end(), [stats](ConnectionParams* cp) {
58 return cp->GetStrippedDSPort() == stats.driver_iface &&
59 cp->GetCommProtocol() == stats.driver_bus;
60 });
61 bool disabled = found_param != conn_params.end() && !(*found_param)->bEnabled;
62 auto found_state = std::find_if(
63 m_states.begin(), m_states.end(),
64 [stats](ConnData& cd) { return cd.IsDriverStatsMatch(stats); });
65 if (found_state == m_states.end()) {
66 ConnData data;
67 data.state = ConnState::NoStats;
68 data.driver_stats = stats;
69 m_states.push_back(data);
70 evt_conn_status_change.Notify(std::make_shared<ConnData>(data), "");
71 } else {
72 ConnState old_state = found_state->state;
73 if (!stats.available) {
74 found_state->state = ConnState::Unavailable;
75 } else if (found_state->driver_stats.rx_count == stats.rx_count &&
76 found_state->driver_stats.tx_count == stats.tx_count) {
77 found_state->state = ConnState::NoData;
78 } else {
79 found_state->state = ConnState::Ok;
80 }
81 found_state->driver_stats = stats;
82 if (found_state->state != old_state)
83 evt_conn_status_change.Notify(std::make_shared<ConnData>(*found_state),
84 "");
85 }
86}
87
88ConnState ConnStates::GetDriverState(NavAddr::Bus bus,
89 const std::string& iface) {
90 auto found = std::find_if(
91 m_states.begin(), m_states.end(),
92 [bus, iface](ConnData& cd) { return cd.IsDriverMatch(bus, iface); });
93 return found == m_states.end() ? ConnState::NoStats : found->state;
94}
95
97void ConnStates::OnDriverListChange(const ObservedEvt&) {
98 auto& drivers = CommDriverRegistry::GetInstance().GetDrivers();
99 auto end = std::remove_if(m_states.begin(), m_states.end(), [&](ConnData cd) {
100 return FindDriver(drivers, cd.driver_stats.driver_iface,
101 cd.driver_stats.driver_bus) == nullptr;
102 });
103 m_states.erase(end, m_states.end());
104}
105
106void ConnStates::OnDriverStats(const ObservedEvt& ev) {
107 auto stats_ptr = UnpackEvtPointer<DriverStats>(ev);
108 if (stats_ptr) HandleDriverStats(*stats_ptr);
109}
const std::vector< DriverPtr > & GetDrivers() const
Runtime data for a driver and thus connection.
Definition conn_states.h:47
EventVar evt_conn_status_change
Notified with a shared_ptr<ConnData> when the state is changed.
Definition conn_states.h:70
void Notify() override
Notify all listeners, no data supplied.
void Init(const KeyProvider &kp, const std::function< void(ObservedEvt &ev)> &action)
Initiate an object yet not listening.
Definition observable.h:295
Custom event class for OpenCPN's notification system.
Communication statistics infrastructure.
Connection parameters.
Runtime connection/driver state definitions.
Driver statistics report.
unsigned tx_count
Number of bytes sent since program start.
unsigned rx_count
Number of bytes received since program start.