2016-05-31 03:58:58 +00:00
|
|
|
#pragma once
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "adapters/net.hpp"
|
2016-10-18 07:34:35 +00:00
|
|
|
#include "components/config.hpp"
|
|
|
|
#include "drawtypes/animation.hpp"
|
2016-05-19 14:41:06 +00:00
|
|
|
#include "drawtypes/animation.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
2016-05-19 14:41:06 +00:00
|
|
|
#include "drawtypes/ramp.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "modules/meta.hpp"
|
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
|
|
|
namespace modules {
|
2016-10-18 07:34:35 +00:00
|
|
|
enum class connection_state { NONE = 0, CONNECTED, DISCONNECTED, PACKETLOSS };
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
class network_module : public timer_module<network_module> {
|
|
|
|
public:
|
|
|
|
using timer_module::timer_module;
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// Load configuration values
|
2016-10-18 07:34:35 +00:00
|
|
|
REQ_CONFIG_VALUE(name(), m_interface, "interface");
|
|
|
|
GET_CONFIG_VALUE(name(), m_ping_nth_update, "ping-interval");
|
|
|
|
GET_CONFIG_VALUE(name(), m_udspeed_minwidth, "udspeed-minwidth");
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
|
|
|
|
|
|
|
|
// Add formats
|
2016-10-18 08:22:48 +00:00
|
|
|
m_formatter->add(FORMAT_CONNECTED, TAG_LABEL_CONNECTED,
|
|
|
|
{TAG_RAMP_SIGNAL, TAG_RAMP_QUALITY, TAG_LABEL_CONNECTED});
|
2016-10-18 07:34:35 +00:00
|
|
|
m_formatter->add(FORMAT_DISCONNECTED, TAG_LABEL_DISCONNECTED, {TAG_LABEL_DISCONNECTED});
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
// Create elements for format-connected
|
|
|
|
if (m_formatter->has(TAG_RAMP_SIGNAL, FORMAT_CONNECTED))
|
|
|
|
m_ramp_signal = get_config_ramp(m_conf, name(), TAG_RAMP_SIGNAL);
|
2016-10-18 07:34:35 +00:00
|
|
|
if (m_formatter->has(TAG_RAMP_QUALITY, FORMAT_CONNECTED))
|
|
|
|
m_ramp_quality = get_config_ramp(m_conf, name(), TAG_RAMP_QUALITY);
|
2016-06-15 03:32:35 +00:00
|
|
|
if (m_formatter->has(TAG_LABEL_CONNECTED, FORMAT_CONNECTED)) {
|
2016-10-18 07:34:35 +00:00
|
|
|
m_label[connection_state::CONNECTED] =
|
|
|
|
get_optional_config_label(m_conf, name(), TAG_LABEL_CONNECTED, "%ifname% %local_ip%");
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create elements for format-disconnected
|
|
|
|
if (m_formatter->has(TAG_LABEL_DISCONNECTED, FORMAT_DISCONNECTED)) {
|
2016-10-18 07:34:35 +00:00
|
|
|
m_label[connection_state::DISCONNECTED] =
|
|
|
|
get_optional_config_label(m_conf, name(), TAG_LABEL_DISCONNECTED, "");
|
2016-10-18 23:26:17 +00:00
|
|
|
m_label[connection_state::DISCONNECTED]->reset_tokens();
|
2016-10-18 07:34:35 +00:00
|
|
|
m_label[connection_state::DISCONNECTED]->replace_token("%ifname%", m_interface);
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create elements for format-packetloss if we are told to test connectivity
|
|
|
|
if (m_ping_nth_update > 0) {
|
2016-10-18 07:34:35 +00:00
|
|
|
m_formatter->add(FORMAT_PACKETLOSS, TAG_LABEL_CONNECTED,
|
2016-06-15 03:32:35 +00:00
|
|
|
{TAG_ANIMATION_PACKETLOSS, TAG_LABEL_PACKETLOSS, TAG_LABEL_CONNECTED});
|
|
|
|
|
|
|
|
if (m_formatter->has(TAG_LABEL_PACKETLOSS, FORMAT_PACKETLOSS)) {
|
2016-10-18 07:34:35 +00:00
|
|
|
m_label[connection_state::PACKETLOSS] =
|
|
|
|
get_optional_config_label(m_conf, name(), TAG_LABEL_PACKETLOSS, "");
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_ANIMATION_PACKETLOSS, FORMAT_PACKETLOSS))
|
|
|
|
m_animation_packetloss = get_config_animation(m_conf, name(), TAG_ANIMATION_PACKETLOSS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an intstance of the network interface
|
2016-10-18 07:34:35 +00:00
|
|
|
if (net::is_wireless_interface(m_interface))
|
|
|
|
m_wireless = net::wireless_t{new net::wireless_t::element_type(m_interface)};
|
|
|
|
else
|
|
|
|
m_wired = net::wired_t{new net::wired_t::element_type(m_interface)};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
// We only need to start the subthread if the packetloss animation is used
|
|
|
|
if (m_animation_packetloss)
|
|
|
|
m_threads.emplace_back(thread(&network_module::subthread_routine, this));
|
|
|
|
}
|
|
|
|
|
2016-10-18 15:55:34 +00:00
|
|
|
void teardown() {
|
|
|
|
m_wireless.reset();
|
|
|
|
m_wired.reset();
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
bool update() {
|
|
|
|
net::network* network = m_wireless ? dynamic_cast<net::network*>(m_wireless.get())
|
|
|
|
: dynamic_cast<net::network*>(m_wired.get());
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
if (!network->query()) {
|
|
|
|
m_log.warn("%s: Failed to query interface '%s'", name(), m_interface);
|
|
|
|
return false;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
try {
|
|
|
|
if (m_wireless) {
|
|
|
|
m_signal = m_wireless->signal();
|
|
|
|
m_quality = m_wireless->quality();
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
2016-10-18 07:34:35 +00:00
|
|
|
} catch (const net::network_error& err) {
|
|
|
|
m_log.warn("%s: Error getting interface data (%s)", name(), err.what());
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
m_connected = network->connected();
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
// Ignore the first run
|
|
|
|
if (m_counter == -1) {
|
|
|
|
m_counter = 0;
|
|
|
|
} else if (m_ping_nth_update > 0 && m_connected && (++m_counter % m_ping_nth_update) == 0) {
|
|
|
|
m_packetloss = !network->ping();
|
|
|
|
m_counter = 0;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
auto upspeed = network->upspeed(m_udspeed_minwidth);
|
|
|
|
auto downspeed = network->downspeed(m_udspeed_minwidth);
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
// Update label contents
|
2016-10-18 08:22:48 +00:00
|
|
|
const auto replace_tokens = [&](label_t& label) {
|
2016-10-18 23:26:17 +00:00
|
|
|
label->reset_tokens();
|
2016-10-18 07:34:35 +00:00
|
|
|
label->replace_token("%ifname%", m_interface);
|
|
|
|
label->replace_token("%local_ip%", network->ip());
|
|
|
|
label->replace_token("%upspeed%", upspeed);
|
|
|
|
label->replace_token("%downspeed%", downspeed);
|
|
|
|
|
|
|
|
if (m_wired) {
|
|
|
|
label->replace_token("%linkspeed%", m_wired->linkspeed());
|
|
|
|
} else if (m_wireless) {
|
|
|
|
label->replace_token("%essid%", m_wireless->essid());
|
|
|
|
label->replace_token("%signal%", to_string(m_signal) + "%");
|
|
|
|
label->replace_token("%quality%", to_string(m_quality) + "%");
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
2016-10-18 07:34:35 +00:00
|
|
|
};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 23:26:17 +00:00
|
|
|
if (m_label[connection_state::CONNECTED])
|
|
|
|
replace_tokens(m_label[connection_state::CONNECTED]);
|
|
|
|
if (m_label[connection_state::PACKETLOSS])
|
|
|
|
replace_tokens(m_label[connection_state::PACKETLOSS]);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-18 23:26:17 +00:00
|
|
|
string get_format() const {
|
2016-06-15 03:32:35 +00:00
|
|
|
if (!m_connected)
|
|
|
|
return FORMAT_DISCONNECTED;
|
2016-10-18 07:34:35 +00:00
|
|
|
else if (m_packetloss && m_ping_nth_update > 0)
|
2016-06-15 03:32:35 +00:00
|
|
|
return FORMAT_PACKETLOSS;
|
|
|
|
else
|
|
|
|
return FORMAT_CONNECTED;
|
|
|
|
}
|
|
|
|
|
2016-10-18 23:26:17 +00:00
|
|
|
bool build(builder* builder, string tag) const {
|
2016-06-15 03:32:35 +00:00
|
|
|
if (tag == TAG_LABEL_CONNECTED)
|
2016-10-18 23:26:17 +00:00
|
|
|
builder->node(m_label.at(connection_state::CONNECTED));
|
2016-06-15 03:32:35 +00:00
|
|
|
else if (tag == TAG_LABEL_DISCONNECTED)
|
2016-10-18 23:26:17 +00:00
|
|
|
builder->node(m_label.at(connection_state::DISCONNECTED));
|
2016-06-15 03:32:35 +00:00
|
|
|
else if (tag == TAG_LABEL_PACKETLOSS)
|
2016-10-18 23:26:17 +00:00
|
|
|
builder->node(m_label.at(connection_state::PACKETLOSS));
|
2016-06-15 03:32:35 +00:00
|
|
|
else if (tag == TAG_ANIMATION_PACKETLOSS)
|
|
|
|
builder->node(m_animation_packetloss->get());
|
|
|
|
else if (tag == TAG_RAMP_SIGNAL)
|
2016-10-18 07:34:35 +00:00
|
|
|
builder->node(m_ramp_signal->get_by_percentage(m_signal));
|
|
|
|
else if (tag == TAG_RAMP_QUALITY)
|
|
|
|
builder->node(m_ramp_quality->get_by_percentage(m_quality));
|
2016-06-15 03:32:35 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void subthread_routine() {
|
2016-10-18 07:34:35 +00:00
|
|
|
const chrono::milliseconds framerate{m_animation_packetloss->framerate()};
|
|
|
|
const auto dur = chrono::duration<double>(framerate);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
while (enabled()) {
|
2016-10-18 07:34:35 +00:00
|
|
|
if (m_connected && m_packetloss)
|
2016-06-15 03:32:35 +00:00
|
|
|
broadcast();
|
|
|
|
sleep(dur);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_log.trace("%s: Reached end of network subthread", name());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-26 00:14:56 +00:00
|
|
|
static constexpr auto FORMAT_CONNECTED = "format-connected";
|
|
|
|
static constexpr auto FORMAT_PACKETLOSS = "format-packetloss";
|
|
|
|
static constexpr auto FORMAT_DISCONNECTED = "format-disconnected";
|
|
|
|
static constexpr auto TAG_RAMP_SIGNAL = "<ramp-signal>";
|
2016-10-18 07:34:35 +00:00
|
|
|
static constexpr auto TAG_RAMP_QUALITY = "<ramp-quality>";
|
2016-05-26 00:14:56 +00:00
|
|
|
static constexpr auto TAG_LABEL_CONNECTED = "<label-connected>";
|
|
|
|
static constexpr auto TAG_LABEL_DISCONNECTED = "<label-disconnected>";
|
|
|
|
static constexpr auto TAG_LABEL_PACKETLOSS = "<label-packetloss>";
|
|
|
|
static constexpr auto TAG_ANIMATION_PACKETLOSS = "<animation-packetloss>";
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
net::wired_t m_wired;
|
|
|
|
net::wireless_t m_wireless;
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
ramp_t m_ramp_signal;
|
2016-10-18 07:34:35 +00:00
|
|
|
ramp_t m_ramp_quality;
|
2016-06-15 03:32:35 +00:00
|
|
|
animation_t m_animation_packetloss;
|
2016-10-18 07:34:35 +00:00
|
|
|
map<connection_state, label_t> m_label;
|
2016-05-31 09:49:33 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
stateflag m_connected{false};
|
2016-10-18 07:34:35 +00:00
|
|
|
stateflag m_packetloss{false};
|
2016-05-19 14:41:06 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
int m_signal = 0;
|
|
|
|
int m_quality = 0;
|
2016-06-15 03:32:35 +00:00
|
|
|
int m_counter = -1; // -1 to ignore the first run
|
2016-10-18 07:34:35 +00:00
|
|
|
|
|
|
|
string m_interface;
|
|
|
|
int m_ping_nth_update = 0;
|
2016-10-11 12:07:59 +00:00
|
|
|
int m_udspeed_minwidth = 3;
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|