2016-11-02 15:22:45 -04:00
|
|
|
#include "modules/network.hpp"
|
|
|
|
|
2016-11-19 09:49:03 -05:00
|
|
|
#include "drawtypes/animation.hpp"
|
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/ramp.hpp"
|
2016-12-09 03:02:47 -05:00
|
|
|
#include "utils/factory.hpp"
|
2016-11-19 09:49:03 -05:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 17:04:31 -05:00
|
|
|
template class module<network_module>;
|
|
|
|
|
2016-12-21 02:00:09 -05:00
|
|
|
network_module::network_module(const bar_settings& bar, string name_)
|
|
|
|
: timer_module<network_module>(bar, move(name_)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
// Load configuration values
|
2016-12-30 17:32:05 -05:00
|
|
|
m_interface = m_conf.get(name(), "interface", m_interface);
|
|
|
|
m_ping_nth_update = m_conf.get(name(), "ping-interval", m_ping_nth_update);
|
|
|
|
m_udspeed_minwidth = m_conf.get(name(), "udspeed-minwidth", m_udspeed_minwidth);
|
|
|
|
m_accumulate = m_conf.get(name(), "accumulate-stats", m_accumulate);
|
2016-12-30 21:04:01 -05:00
|
|
|
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 1s);
|
2017-11-03 02:05:26 -04:00
|
|
|
m_unknown_up = m_conf.get<bool>(name(), "unknown-as-up", false);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-21 21:01:50 -05:00
|
|
|
m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%");
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
// Add formats
|
2016-11-25 02:42:31 -05:00
|
|
|
m_formatter->add(FORMAT_CONNECTED, TAG_LABEL_CONNECTED, {TAG_RAMP_SIGNAL, TAG_RAMP_QUALITY, TAG_LABEL_CONNECTED});
|
2016-11-02 15:22:45 -04:00
|
|
|
m_formatter->add(FORMAT_DISCONNECTED, TAG_LABEL_DISCONNECTED, {TAG_LABEL_DISCONNECTED});
|
|
|
|
|
|
|
|
// Create elements for format-connected
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_formatter->has(TAG_RAMP_SIGNAL, FORMAT_CONNECTED)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_ramp_signal = load_ramp(m_conf, name(), TAG_RAMP_SIGNAL);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_RAMP_QUALITY, FORMAT_CONNECTED)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_ramp_quality = load_ramp(m_conf, name(), TAG_RAMP_QUALITY);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
if (m_formatter->has(TAG_LABEL_CONNECTED, FORMAT_CONNECTED)) {
|
|
|
|
m_label[connection_state::CONNECTED] =
|
|
|
|
load_optional_label(m_conf, name(), TAG_LABEL_CONNECTED, "%ifname% %local_ip%");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create elements for format-disconnected
|
|
|
|
if (m_formatter->has(TAG_LABEL_DISCONNECTED, FORMAT_DISCONNECTED)) {
|
2016-11-25 02:42:31 -05:00
|
|
|
m_label[connection_state::DISCONNECTED] = load_optional_label(m_conf, name(), TAG_LABEL_DISCONNECTED, "");
|
2016-11-02 15:22:45 -04:00
|
|
|
m_label[connection_state::DISCONNECTED]->reset_tokens();
|
|
|
|
m_label[connection_state::DISCONNECTED]->replace_token("%ifname%", m_interface);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create elements for format-packetloss if we are told to test connectivity
|
|
|
|
if (m_ping_nth_update > 0) {
|
|
|
|
m_formatter->add(FORMAT_PACKETLOSS, TAG_LABEL_CONNECTED,
|
|
|
|
{TAG_ANIMATION_PACKETLOSS, TAG_LABEL_PACKETLOSS, TAG_LABEL_CONNECTED});
|
|
|
|
|
|
|
|
if (m_formatter->has(TAG_LABEL_PACKETLOSS, FORMAT_PACKETLOSS)) {
|
2016-11-25 02:42:31 -05:00
|
|
|
m_label[connection_state::PACKETLOSS] = load_optional_label(m_conf, name(), TAG_LABEL_PACKETLOSS, "");
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_formatter->has(TAG_ANIMATION_PACKETLOSS, FORMAT_PACKETLOSS)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_animation_packetloss = load_animation(m_conf, name(), TAG_ANIMATION_PACKETLOSS);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get an intstance of the network interface
|
2016-11-25 07:55:15 -05:00
|
|
|
if (net::is_wireless_interface(m_interface)) {
|
2016-12-09 03:02:47 -05:00
|
|
|
m_wireless = factory_util::unique<net::wireless_network>(m_interface);
|
2017-11-03 02:05:26 -04:00
|
|
|
m_wireless->set_unknown_up(m_unknown_up);
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-12-09 03:02:47 -05:00
|
|
|
m_wired = factory_util::unique<net::wired_network>(m_interface);
|
2017-11-03 02:05:26 -04:00
|
|
|
m_wired->set_unknown_up(m_unknown_up);
|
2016-11-25 07:55:15 -05:00
|
|
|
};
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
// We only need to start the subthread if the packetloss animation is used
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_animation_packetloss) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_threads.emplace_back(thread(&network_module::subthread_routine, this));
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void network_module::teardown() {
|
|
|
|
m_wireless.reset();
|
|
|
|
m_wired.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool network_module::update() {
|
2016-11-25 02:42:31 -05:00
|
|
|
net::network* network =
|
|
|
|
m_wireless ? static_cast<net::network*>(m_wireless.get()) : static_cast<net::network*>(m_wired.get());
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-14 06:42:58 -05:00
|
|
|
if (!network->query(m_accumulate)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_log.warn("%s: Failed to query interface '%s'", name(), m_interface);
|
2018-04-16 16:19:01 -04:00
|
|
|
m_connected = false;
|
2016-11-02 15:22:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (m_wireless) {
|
|
|
|
m_signal = m_wireless->signal();
|
|
|
|
m_quality = m_wireless->quality();
|
|
|
|
}
|
|
|
|
} catch (const net::network_error& err) {
|
|
|
|
m_log.warn("%s: Error getting interface data (%s)", name(), err.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_connected = network->connected();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto upspeed = network->upspeed(m_udspeed_minwidth);
|
|
|
|
auto downspeed = network->downspeed(m_udspeed_minwidth);
|
|
|
|
|
|
|
|
// Update label contents
|
|
|
|
const auto replace_tokens = [&](label_t& label) {
|
|
|
|
label->reset_tokens();
|
|
|
|
label->replace_token("%ifname%", m_interface);
|
|
|
|
label->replace_token("%local_ip%", network->ip());
|
2018-06-10 16:51:43 -04:00
|
|
|
label->replace_token("%local_ip6%", network->ip6());
|
2016-11-02 15:22:45 -04:00
|
|
|
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());
|
2017-01-13 14:03:08 -05:00
|
|
|
label->replace_token("%signal%", to_string(m_signal));
|
|
|
|
label->replace_token("%quality%", to_string(m_quality));
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_label[connection_state::CONNECTED]) {
|
2016-11-02 15:22:45 -04:00
|
|
|
replace_tokens(m_label[connection_state::CONNECTED]);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_label[connection_state::PACKETLOSS]) {
|
2016-11-02 15:22:45 -04:00
|
|
|
replace_tokens(m_label[connection_state::PACKETLOSS]);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2019-01-12 09:48:23 -05:00
|
|
|
if (m_label[connection_state::DISCONNECTED]) {
|
|
|
|
replace_tokens(m_label[connection_state::DISCONNECTED]);
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string network_module::get_format() const {
|
2016-11-25 07:55:15 -05:00
|
|
|
if (!m_connected) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return FORMAT_DISCONNECTED;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (m_packetloss && m_ping_nth_update > 0) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return FORMAT_PACKETLOSS;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-11-02 15:22:45 -04:00
|
|
|
return FORMAT_CONNECTED;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
bool network_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_LABEL_CONNECTED) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label.at(connection_state::CONNECTED));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_LABEL_DISCONNECTED) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label.at(connection_state::DISCONNECTED));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_LABEL_PACKETLOSS) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label.at(connection_state::PACKETLOSS));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_ANIMATION_PACKETLOSS) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_animation_packetloss->get());
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_RAMP_SIGNAL) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_ramp_signal->get_by_percentage(m_signal));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_RAMP_QUALITY) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_ramp_quality->get_by_percentage(m_quality));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-11-02 15:22:45 -04:00
|
|
|
return false;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void network_module::subthread_routine() {
|
|
|
|
const chrono::milliseconds framerate{m_animation_packetloss->framerate()};
|
|
|
|
|
|
|
|
while (running()) {
|
2019-04-04 21:36:33 -04:00
|
|
|
auto now = chrono::steady_clock::now();
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_connected && m_packetloss) {
|
2019-03-09 00:14:39 -05:00
|
|
|
m_animation_packetloss->increment();
|
2016-11-02 15:22:45 -04:00
|
|
|
broadcast();
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2019-03-09 00:14:39 -05:00
|
|
|
|
|
|
|
now += framerate;
|
|
|
|
this_thread::sleep_until(now);
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_log.trace("%s: Reached end of network subthread", name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|