1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#pragma once
#include <kel/lbm/webgl/probe.hpp>
#include <kel/lbm/swarm/message.hpp>
#include <kel/lbm/swarm/wire.hpp>
#include <cstdint>
#include <string>
/**
* What the browser worker does, separated from how JavaScript calls it.
*
* worker.cpp is a thin extern "C" shim over this; keeping the behaviour in a
* header means the self test can drive it directly under node, without a
* browser and without going through ccall. The GL probe is the only part that
* genuinely needs a browser, and it fails cleanly when there is no context --
* which is exactly the state node is in, so everything else stays exercisable.
*/
namespace kel {
namespace lbm {
namespace swarm {
class worker_protocol {
public:
struct config {
std::string worker_id = "browser";
std::string user_agent = "unknown";
};
/**
* Runs the capability probe.
*
* Returns false when this device cannot run a solver. That is not a reason
* to stay silent: the worker still registers, so the orchestrator learns
* the device exists and why it is unusable, rather than never hearing from
* it at all.
*/
bool init(config cfg, uint64_t now_ms){
cfg_ = std::move(cfg);
started_at_ms_ = now_ms;
webgl::probe_config pcfg;
probe_ok_ = webgl::probe_webgl2(pcfg, capability_);
return probe_ok_;
}
/// The hello to send once the control channel opens.
std::string hello(uint64_t now_ms) const {
auto message = make_message<sch::MsgHelloWebGl2>(
msg::hello, cfg_.worker_id, 0u);
auto &cap = message.get<"capability">();
cap.get<"worker_id">().set(cfg_.worker_id);
cap.get<"kind">().set(std::string{wkind::webgl2});
cap.get<"schema_version">().set(std::string{schema_version});
cap.get<"reported_at_ms">().set(now_ms);
cap.get<"user_agent">().set(cfg_.user_agent);
cap.get<"webgl2">() = capability_;
auto eo = encode_message<sch::MsgHelloWebGl2>(message);
if(eo.is_error()){
return {};
}
return eo.get_value();
}
/**
* Handles one control message, returning the reply or an empty string.
*
* The transport never has to know that a ping wants a pong -- only that a
* non-empty return goes back down the channel.
*/
std::string handle(const std::string &text, uint64_t now_ms){
auto eo_hdr = peek_header(text);
if(eo_hdr.is_error()){
return {};
}
const std::string type = eo_hdr.get_value().get<"type">().stl_string();
if(type == msg::ping){
return handle_ping(text, now_ms);
}
if(type == msg::welcome){
return handle_welcome(now_ms);
}
// Anything else is not ours to answer.
return {};
}
uint64_t pongs() const { return pongs_; }
bool probe_ok() const { return probe_ok_; }
const data<sch::WebGl2Capability> &capability() const { return capability_; }
private:
data<sch::WorkerLive> make_live(uint64_t now_ms) const {
data<sch::WorkerLive> live;
live.get<"state">().set(std::string{
probe_ok_ ? wstate::ready : wstate::error});
live.get<"uptime_ms">().set(
(now_ms > started_at_ms_) ? (now_ms - started_at_ms_) : 0u);
// Nothing is being simulated yet; the field exists so the monitor does
// not need a protocol change once something is.
live.get<"steps_done">().set(0u);
live.get<"last_error">().set(std::string{""});
return live;
}
std::string handle_ping(const std::string &text, uint64_t now_ms){
auto eo = decode_message<sch::MsgPing>(text);
if(eo.is_error()){
return {};
}
auto pong = make_message<sch::MsgPong>(
msg::pong, cfg_.worker_id, eo.get_value().get<"seq">().get());
// Echo the orchestrator's own timestamp so it can measure the round
// trip against its clock. The two machines never have to agree on what
// time it is.
pong.get<"sent_at_ms">().set(eo.get_value().get<"sent_at_ms">().get());
pong.get<"worker_now_ms">().set(now_ms);
pong.get<"live">() = make_live(now_ms);
auto eo_out = encode_message<sch::MsgPong>(pong);
if(eo_out.is_error()){
return {};
}
++pongs_;
return eo_out.get_value();
}
std::string handle_welcome(uint64_t now_ms){
// Registration accepted. Announce readiness, which is a claim only the
// worker can make -- the orchestrator will not assume it.
auto ready = make_message<sch::MsgReady>(msg::ready, cfg_.worker_id, 0u);
ready.get<"live">() = make_live(now_ms);
auto eo = encode_message<sch::MsgReady>(ready);
if(eo.is_error()){
return {};
}
return eo.get_value();
}
config cfg_;
data<sch::WebGl2Capability> capability_;
bool probe_ok_ = false;
uint64_t started_at_ms_ = 0u;
uint64_t pongs_ = 0u;
};
} // namespace swarm
} // namespace lbm
} // namespace kel
|