GHS
Leader Election Based on GHS Minimum Spanning Tree
|
a message passing class that uses nng, suitable for testing GhsState More...
#include <ghs-demo-comms.h>
Public Member Functions | |
bool | ok () |
Comms & | with_config (Config &) |
Errno | send (demo::WireMessage &, demo::OptMask=0) |
void | start_receiver () |
void | stop_receiver () |
bool | has_msg () |
bool | get_next (demo::WireMessage &) |
void | little_iperf () |
void | exchange_iperf () |
void | print_iperf () |
Kbps | kbps_to (const uint16_t agent_id) const |
CommsEdgeMetric | unique_link_metric_to (const uint16_t agent_id) const |
This function is really important for the working of your system More... | |
Static Public Member Functions | |
static Comms & | inst () |
a message passing class that uses nng, suitable for testing GhsState
This class encapsulates the network layer for a demonstration of GHS over a real network. It is initialized by populating a DemoConfig struct and calling with_config()
After that, you can use send() and call get_next() at will to exchanges messages using nng_socket of type req-rep.
void demo::Comms::exchange_iperf | ( | ) |
This will block for a while, during which time it will synchronize with all known endpoints so that everyone uses the same link metric values.
However, the actual link metrics that are used are calculated by unique_link_metric_to()
bool demo::Comms::get_next | ( | demo::WireMessage & | m | ) |
If has_msg returns true, then this will copy the next message out of the buffer for you to process.
bool demo::Comms::has_msg | ( | ) |
Returns true if there is a message waiting in the incoming buffer.
This will always return false until you call start_receiver()
|
static |
Keep one static instance on hand. You're not required to use it, but we have it nonetheless An usual way to initialize is to call Comms::inst().with_config()
Kbps demo::Comms::kbps_to | ( | const uint16_t | agent_id | ) | const |
Returns the calculated and observed throughput to the given agent
void demo::Comms::little_iperf | ( | ) |
This will block for a while, during which time it will repeatedly send and receive messages from all known endpoints to guage the throughput of the links. This information is used to populate the GhsState::mwoe() and Edge metric_t information. However, the actual link metrics that are used are calculated by unique_link_metric_to()
void demo::Comms::print_iperf | ( | ) |
Will dump link metric information to stdout
Errno demo::Comms::send | ( | demo::WireMessage & | msg, |
demo::OptMask | mask = 0 |
||
) |
Sends a demo::WireMessage. The demo::WireMessage contains all the destination and routing information in it, so this is a singular call that blocks then returns a status message.
void demo::Comms::start_receiver | ( | ) |
Starts a background process to copy incoming message into a buffer for later processing.
This is implementation specific, I use plain old threads here. Note, ZMQ does this for you, but requires a lot of std:: and platform assumptions.
So, we avoid some of that with nng, and one use of std::thread.
void demo::Comms::stop_receiver | ( | ) |
Stops the background process by interrupting it after it's next timeout, then join
ing the thread until it exits cleanly. This will block for a few seconds, but shouldn't fail.
uint64_t demo::Comms::unique_link_metric_to | ( | const uint16_t | agent_id | ) | const |
This function is really important for the working of your system
Calculates and returns a globally unique metric value that is suitable for use as a metric_t inside GhsState. The metric, once little_iperf() and exchange_iperf() complete and agent ids are globally unique, satisfies:
The implementation I used is sym_metric() but that's not required, compile in your own if you want.
Initialie all the internal data and communication structures with the given config information
The intent is that you would extend this class with other with_config()
options.