Loading...
Searching...
No Matches
ml::NetworkDispatcher Class Reference

Routes incoming JSON messages to typed message-bus subscribers. More...

#include <Malena/Providers/NetworkDispatcher.h>

Inheritance diagram for ml::NetworkDispatcher:
[legend]

Public Member Functions

 NetworkDispatcher (const std::string &keyField="type")
void dispatch (const json &msg)
 Dispatch a JSON object to the matching subscriber.
void offAllMessages ()
 Unsubscribe from all messages registered by this object.
template<typename DataType, typename Enum>
void offMessage (Enum event)
 Unsubscribe from a specific typed message.
void on (const std::string &value, std::function< void(const json &)> callback)
 Subscribe a callback for a specific message value.
template<typename DataType, typename Enum>
void onMessage (Enum event, std::function< void(const DataType &)> callback)
 Register a callback to receive a typed message.
template<typename DataType, typename Enum>
void sendMessage (Enum event, const DataType &data)
 Publish a typed message to all current subscribers.

Detailed Description

Routes incoming JSON messages to typed message-bus subscribers.

NetworkDispatcher reads a configurable key field (default "type") from each incoming JSON object and publishes its value on the global message bus. Subscribers call on() with the value they care about:

// Default key field ("type")
NetworkDispatcher dispatcher;
dispatcher.on("student_connected", [](const ml::json& msg) { ... });
// Custom key field — server uses "event" instead of "type"
NetworkDispatcher dispatcher{"event"};
dispatcher.on("courses", [](const ml::json& msg) { ... });
Routes incoming JSON messages to typed message-bus subscribers.
void on(const std::string &value, std::function< void(const json &)> callback)
Subscribe a callback for a specific message value.
NetworkDispatcher(const std::string &keyField="type")
nlohmann::json json
Alias for nlohmann::json.
Definition Json.h:32
See also
WebSocketProvider

Definition at line 37 of file NetworkDispatcher.h.

Constructor & Destructor Documentation

◆ NetworkDispatcher()

ml::NetworkDispatcher::NetworkDispatcher ( const std::string & keyField = "type")
explicit

Member Function Documentation

◆ dispatch()

void ml::NetworkDispatcher::dispatch ( const json & msg)

Dispatch a JSON object to the matching subscriber.

Reads _keyField from the message and publishes its value on the message bus. No-op if the field is missing or empty.

Parameters
msgParsed JSON message from the network.

◆ offAllMessages()

void ml::Messenger::offAllMessages ( )
inherited

Unsubscribe from all messages registered by this object.

Called automatically by the destructor. Only call manually if you need to reset all subscriptions while the object is still alive.

◆ offMessage()

template<typename DataType, typename Enum>
void ml::Messenger::offMessage ( Enum event)
inherited

Unsubscribe from a specific typed message.

After this call, the callback previously registered for event and DataType will no longer be invoked. Safe to call from within the callback itself.

Template Parameters
DataTypeThe payload type of the subscription to remove.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to unsubscribe from.

◆ on()

void ml::NetworkDispatcher::on ( const std::string & value,
std::function< void(const json &)> callback )

Subscribe a callback for a specific message value.

Parameters
valueThe value of the key field to listen for.
callbackCalled with the full JSON object when the value matches.

◆ onMessage()

template<typename DataType, typename Enum>
void ml::Messenger::onMessage ( Enum event,
std::function< void(const DataType &)> callback )
inherited

Register a callback to receive a typed message.

The callback is invoked each time sendMessage is called with the same event and DataType. Multiple calls with the same event replace the previous callback for that event.

Template Parameters
DataTypeThe expected payload type.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to listen for.
callbackFunction called with a const reference to the payload when the event fires.

◆ sendMessage()

template<typename DataType, typename Enum>
void ml::Messenger::sendMessage ( Enum event,
const DataType & data )
inherited

Publish a typed message to all current subscribers.

All objects that have called onMessage for the same event and DataType will have their callbacks invoked with data. Delivery is immediate unless MessageManager is currently iterating, in which case it is deferred.

Template Parameters
DataTypeThe payload type. Must match the type used in the corresponding onMessage call.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to send.
dataThe payload to deliver to subscribers.

The documentation for this class was generated from the following file: