refactor(network): wait for new address

This commit is contained in:
Alexis 2018-11-23 16:04:29 +01:00
parent 2b05b8e69a
commit baa7f52e21
3 changed files with 16 additions and 7 deletions

View File

@ -15,7 +15,7 @@ int main(int argc, char* argv[])
waybar::client = &c;
std::signal(SIGUSR1, [] (int /*signal*/) {
for (auto& bar : waybar::client->bars) {
(*bar).toggle();
bar->toggle();
}
});

View File

@ -101,7 +101,7 @@ const std::string waybar::modules::Battery::getState(uint8_t capacity) const
return a.second < b.second;
});
std::string valid_state;
for (auto state : states) {
for (auto const& state : states) {
if (capacity <= state.second && valid_state.empty()) {
label_.get_style_context()->add_class(state.first);
valid_state = state.first;

View File

@ -50,6 +50,7 @@ void waybar::modules::Network::worker()
uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf),
RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
bool need_update = false;
bool new_addr = false;
for (auto nh = reinterpret_cast<struct nlmsghdr *>(buf); NLMSG_OK(nh, len);
nh = NLMSG_NEXT(nh, len)) {
if (nh->nlmsg_type == NLMSG_DONE) {
@ -58,6 +59,9 @@ void waybar::modules::Network::worker()
if (nh->nlmsg_type == NLMSG_ERROR) {
continue;
}
if (nh->nlmsg_type == RTM_NEWADDR) {
new_addr = true;
}
if (nh->nlmsg_type < RTM_NEWADDR) {
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
if (rtif->ifi_index == static_cast<int>(ifid_)) {
@ -69,9 +73,15 @@ void waybar::modules::Network::worker()
}
}
if (ifid_ <= 0 && !config_["interface"].isString()) {
// Need to wait before get external interface
thread_.sleep_for(std::chrono::seconds(1));
ifid_ = getExternalInterface();
if (new_addr) {
// Need to wait before get external interface
while (ifid_ <= 0) {
ifid_ = getExternalInterface();
thread_.sleep_for(std::chrono::seconds(1));
}
} else {
ifid_ = getExternalInterface();
}
if (ifid_ > 0) {
char ifname[IF_NAMESIZE];
if_indextoname(ifid_, ifname);
@ -319,13 +329,12 @@ int waybar::modules::Network::netlinkRequest(int fd, void *req,
int waybar::modules::Network::netlinkResponse(int fd, void *resp,
uint32_t resplen, uint32_t groups)
{
int ret;
struct sockaddr_nl sa = {};
sa.nl_family = AF_NETLINK;
sa.nl_groups = groups;
struct iovec iov = { resp, resplen };
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
ret = recvmsg(fd, &msg, 0);
auto ret = recvmsg(fd, &msg, 0);
if (msg.msg_flags & MSG_TRUNC) {
return -1;
}