Skip to content

Commit 9997f23

Browse files
Don't wait to initalize WS_uplink. Added really simple logbuffer
1 parent 1769146 commit 9997f23

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

register_plugin.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,7 @@ void setup_uplink()
4141

4242
auto& en0 = net::Super_stack::get<net::IP4>(0);
4343

44-
// already initialized
45-
if(en0.is_configured())
46-
{
47-
uplink = std::make_unique<WS_uplink>(en0);
48-
}
49-
// if not, register on config event
50-
else
51-
{
52-
en0.on_config([] (auto& inet) {
53-
uplink = std::make_unique<WS_uplink>(inet);
54-
});
55-
}
56-
44+
uplink = std::make_unique<WS_uplink>(en0);
5745

5846
OS::on_panic(uplink::on_panic);
5947

ws_uplink.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,27 @@ namespace uplink {
4848
: inet_{inet}, id_{inet.link_addr().to_string()},
4949
parser_({this, &WS_uplink::handle_transport})
5050
{
51-
Expects(inet.ip_addr() != 0 && "Network interface not configured");
51+
OS::add_stdout({this, &WS_uplink::send_log});
5252

5353
read_config();
5454

55-
start(inet);
56-
57-
OS::add_stdout({this, &WS_uplink::send_log});
58-
59-
/*parser_.on_header = [](const auto& hdr) {
60-
MYINFO("Header: Code: %u Len: %u", static_cast<uint8_t>(hdr.code), hdr.length);
61-
};*/
55+
if(inet_.is_configured())
56+
{
57+
start(inet);
58+
}
59+
// if not, register on config event
60+
else
61+
{
62+
MYINFO("Interface not yet configured, starts when ready.");
63+
inet_.on_config({this, &WS_uplink::start});
64+
}
6265
}
6366

6467
void WS_uplink::start(net::Inet<net::IP4>& inet) {
6568
MYINFO("Starting WS uplink on %s with ID: %s",
6669
inet.ifname().c_str(), id_.c_str());
6770

71+
Expects(inet.ip_addr() != 0 && "Network interface not configured");
6872
Expects(not config_.url.empty());
6973

7074
if(liu::LiveUpdate::is_resumable(UPDATE_LOC))
@@ -179,6 +183,8 @@ namespace uplink {
179183

180184
MYINFO("Websocket established");
181185

186+
flush_log();
187+
182188
send_ident();
183189

184190
send_uplink();
@@ -440,6 +446,20 @@ namespace uplink {
440446
{
441447
send_message(Transport_code::LOG, data, len);
442448
}
449+
else
450+
{
451+
// buffer for later
452+
logbuf_.insert(logbuf_.end(), data, data+len);
453+
}
454+
}
455+
456+
void WS_uplink::flush_log()
457+
{
458+
if(not logbuf_.empty())
459+
{
460+
send_message(Transport_code::LOG, logbuf_.data(), logbuf_.size());
461+
logbuf_.clear();
462+
}
443463
}
444464

445465
void WS_uplink::panic(const char* why){

ws_uplink.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <net/ws/websocket.hpp>
2727
#include <liveupdate.hpp>
2828
#include <util/timer.hpp>
29+
#include <util/logger.hpp>
2930

3031
namespace uplink {
3132

@@ -53,6 +54,8 @@ class WS_uplink {
5354

5455
void send_log(const char*, size_t);
5556

57+
void flush_log();
58+
5659
void send_uplink();
5760

5861
void update(const std::vector<char>& buffer);
@@ -83,6 +86,8 @@ class WS_uplink {
8386
Timer retry_timer;
8487
uint8_t retry_backoff = 0;
8588

89+
std::vector<char> logbuf_;
90+
8691
void inject_token(http::Request& req, http::Client::Options&, const http::Client::Host)
8792
{
8893
if (not token_.empty())

0 commit comments

Comments
 (0)