Class ILLIXR::switchboard
ClassList > ILLIXR > switchboard
A manager for typesafe, threadsafe, named event-streams (called topics). More...
#include <switchboard.hpp>
Inherits the following classes: ILLIXR::phonebook::service
Classes
Type | Name |
---|---|
class | buffered_reader <typename specific_event> |
class | event Virtual class for event types. |
class | event_wrapper <typename underlying_type> Helper class for making event types. |
class | reader <typename specific_event> A handle which can read the latest event on a topic. |
class | writer <typename specific_event> A handle which can publish events to a topic. |
Public Types
Type | Name |
---|---|
typedef std::shared_ptr< specific_event > | ptr The type of shared pointer returned by switchboard. |
Public Functions
Type | Name |
---|---|
buffered_reader< specific_event > | get_buffered_reader (const std::string & topic_name) |
reader< specific_event > | get_reader (const std::string & topic_name) Gets a handle to read to the latest value from the topic topic_name . |
writer< specific_event > | get_writer (const std::string & topic_name) Gets a handle to publish to the topic topic_name . |
void | schedule (plugin_id_t plugin_id, std::string topic_name, std::function< void(ptr< const specific_event > &&, std::size_t)> fn) Schedules the callback fn every time an event is published totopic_name . |
void | stop () Stops calling switchboard callbacks. |
switchboard (const phonebook * pb) |
Public Functions inherited from ILLIXR::phonebook::service
See ILLIXR::phonebook::service
Type | Name |
---|---|
virtual | ~service () = default |
Detailed Description
- Writing: One can write to a topic (in any thread) through the object returned by
get_writer()
. - There are two ways of reading: asynchronous reading and synchronous reading:
- Asynchronous reading returns the most-recent event on the topic (idempotently). One can do this through (in any thread) the object returned by
get_reader()
. - Synchronous reading schedules a callback to be executed on every event which gets published. One can schedule computation by
schedule()
, which will run the computation in a thread managed by switchboard.
// Get a reader on topic1
switchboard::reader<topic1_type> topic1 = switchboard.get_reader<topic1_type>("topic1");
// Get a writer on topic2
switchboard::writer<topic2_type> topic2 = switchboard.get_writer<topic2_type>("topic2");
while (true) {
// Read topic 1
switchboard::ptr<topic1_type> event1 = topic1.get_rw();
// Write to topic 2 using topic 1 input
topic2.put(topic2.allocate<topic2_type>( do_something(event1->foo) ));
}
// Read topic 3 synchronously
switchboard.schedule<topic3_type>(plugin_id, "topic3", [&](switchboard::ptr<topic3_type> event3, std::size_t it) {
// This is a lambda expression
// https://en.cppreference.com/w/cpp/language/lambda
std::cout << "Got a new event on topic3: " << event3->foo << " for iteration " << it << std::endl;
});
Public Types Documentation
typedef ptr
The type of shared pointer returned by switchboard.
using ILLIXR::switchboard::ptr = std::shared_ptr<specific_event>;
TODO: Make this agnostic to the type of ptr
Currently, it depends on ptr
== shared_ptr
Public Functions Documentation
function get_buffered_reader
template<typename specific_event>
inline buffered_reader< specific_event > ILLIXR::switchboard::get_buffered_reader (
const std::string & topic_name
)
function get_reader
Gets a handle to read to the latest value from the topic topic_name
.
template<typename specific_event>
inline reader < specific_event > ILLIXR::switchboard::get_reader (
const std::string & topic_name
)
This is safe to be called from any thread.
Exception:
If
topic already exists, and its type does not match theevent
.
function get_writer
Gets a handle to publish to the topic topic_name
.
template<typename specific_event>
inline writer < specific_event > ILLIXR::switchboard::get_writer (
const std::string & topic_name
)
This is safe to be called from any thread.
Exception:
If
topic already exists, and its type does not match theevent
.
function schedule
Schedules the callback fn
every time an event is published totopic_name
.
template<typename specific_event>
inline void ILLIXR::switchboard::schedule (
plugin_id_t plugin_id,
std::string topic_name,
std::function< void( ptr < const specific_event > &&, std::size_t)> fn
)
Switchboard maintains a threadpool to call fn
.
This is safe to be called from any thread.
Exception:
if
topic already exists and its type does not match theevent
.
function stop
Stops calling switchboard callbacks.
inline void ILLIXR::switchboard::stop ()
This is safe to be called from any thread.
Leave topics in place, so existing reader/writer handles will not crash.
function switchboard
inline ILLIXR::switchboard::switchboard (
const phonebook * pb
)
If pb
is null, then logging is disabled.
The documentation for this class was generated from the following file include/illixr/switchboard.hpp