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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
24#include <iostream>
25
26#include <algorithm>
27#include <sstream>
28
29#include "model/conn_states.h"
31#include "model/conn_params.h"
32
33bool ConnData::IsDriverStatsMatch(const DriverStats& ds) const {
34 return ds.driver_bus == driver_stats.driver_bus &&
35 ds.driver_iface == driver_stats.driver_iface;
36}
37
38bool ConnData::IsDriverMatch(NavAddr::Bus bus, const std::string& iface) const {
39 return driver_stats.driver_bus == bus && driver_stats.driver_iface == iface;
40}
41
42std::string ConnData::to_string() {
43 std::stringstream ss;
44 return ss.str();
45}
46
47ConnStates::ConnStates() {
48 auto& registry = CommDriverRegistry::GetInstance();
49 m_driverlist_lstnr.Init(registry.evt_driverlist_change,
50 [&](ObservedEvt& ev) { OnDriverListChange(ev); });
51 m_driverstat_lstnr.Init(registry.evt_driver_stats,
52 [&](ObservedEvt& ev) { OnDriverStats(ev); });
53}
54
55void ConnStates::HandleDriverStats(const DriverStats& stats) {
56 auto conn_params = TheConnectionParams();
57 auto found_param = std::find_if(
58 conn_params.begin(), conn_params.end(), [stats](ConnectionParams* cp) {
59 return cp->GetStrippedDSPort() == stats.driver_iface &&
60 cp->GetCommProtocol() == stats.driver_bus;
61 });
62 bool disabled = found_param != conn_params.end() && !(*found_param)->bEnabled;
63 auto found_state = std::find_if(
64 m_states.begin(), m_states.end(),
65 [stats](ConnData& cd) { return cd.IsDriverStatsMatch(stats); });
66 if (found_state == m_states.end()) {
67 ConnData data;
68 data.state = ConnState::NoStats;
69 data.driver_stats = stats;
70 m_states.push_back(data);
71 evt_conn_status_change.Notify(std::make_shared<ConnData>(data), "");
72 } else {
73 ConnState old_state = found_state->state;
74 if (!stats.available) {
75 found_state->state = ConnState::Unavailable;
76 } else if (found_state->driver_stats.rx_count == stats.rx_count &&
77 found_state->driver_stats.tx_count == stats.tx_count) {
78 found_state->state = ConnState::NoData;
79 } else {
80 found_state->state = ConnState::Ok;
81 }
82 found_state->driver_stats = stats;
83 if (found_state->state != old_state)
84 evt_conn_status_change.Notify(std::make_shared<ConnData>(*found_state),
85 "");
86 }
87}
88
89ConnState ConnStates::GetDriverState(NavAddr::Bus bus,
90 const std::string& iface) {
91 auto found = std::find_if(
92 m_states.begin(), m_states.end(),
93 [bus, iface](ConnData& cd) { return cd.IsDriverMatch(bus, iface); });
94 return found == m_states.end() ? ConnState::NoStats : found->state;
95}
96
98void ConnStates::OnDriverListChange(const ObservedEvt&) {
99 auto& drivers = CommDriverRegistry::GetInstance().GetDrivers();
100 auto end = std::remove_if(m_states.begin(), m_states.end(), [&](ConnData cd) {
101 return FindDriver(drivers, cd.driver_stats.driver_iface,
102 cd.driver_stats.driver_bus) == nullptr;
103 });
104 m_states.erase(end, m_states.end());
105}
106
107void ConnStates::OnDriverStats(const ObservedEvt& ev) {
108 auto stats_ptr = UnpackEvtPointer<DriverStats>(ev);
109 if (stats_ptr) HandleDriverStats(*stats_ptr);
110}
const std::vector< DriverPtr > & GetDrivers() const
Runtime data for a driver and thus connection.
Definition conn_states.h:48
EventVar evt_conn_status_change
Notified with a shared_ptr<ConnData> when the state is changed.
Definition conn_states.h:71
const void Notify()
Notify all listeners, no data supplied.
void Init(const KeyProvider &kp, std::function< void(ObservedEvt &ev)> action)
Initiate an object yet not listening.
Definition observable.h:255
Adds a std::shared<void> element to wxCommandEvent.
Communication statistics infrastructure.
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.