4S Device Communication Module Collection  Version 0.6-SNAPSHOT
Full documentation of the modules in the 4SDC collection (aimed at 4SDC module developers)
FSYS::MsgSender< parentClass > Class Template Reference

Class enabling other classes to send messages. More...

#include <msgsender.h>

+ Inheritance diagram for FSYS::MsgSender< parentClass >:
+ Collaboration diagram for FSYS::MsgSender< parentClass >:

Public Member Functions

template<class T >
void broadcast (T &msg)
 Template function to broadcast messages. More...
 
template<class T >
void respond (T &msg, BaseMsg &received)
 Template function to respond to messages. More...
 
template<class T >
void send (T &msg, const MsgAddr &destination)
 
- Public Member Functions inherited from FSYS::MsgAddrGenerator< parentClass >
 MsgAddrGenerator (void)
 Constructor that creates an address.
 
 operator MsgAddr & ()
 MsgAddr operator for type conversion of MsgAddrGenerator to a MsgAddr. More...
 

Additional Inherited Members

- Private Member Functions inherited from FSYS::MsgSenderBase
void broadcast (std::shared_ptr< BaseMsg > &msg, const std::type_info &typeOfMsg, const MsgAddr &sender)
 Broadcast function, to broadcast messages. More...
 
void send (std::shared_ptr< BaseMsg > &msg, const std::type_info &typeOfMsg, const FSYS::MsgAddr &sender, const MsgAddr &destination)
 Type independent respond function. More...
 

Detailed Description

template<class parentClass>
class FSYS::MsgSender< parentClass >

When inheriting from this class it becomes possible for the class inheriting to send all types of messages.

There are two ways to send messages, either as broadcasts or as responses to other messages.

Broadcasts will be send to all instances of classes that listen to the specific type of message.

Responses will only go the the instance of the class that sended the message that is given in the argument to the respond function.

For a class to be considered as a receiver of a message, the class needs to be instantiated at the time when the message was sent.

Member Function Documentation

template<class parentClass>
template<class T >
void FSYS::MsgSender< parentClass >::broadcast ( T &  msg)
inline

When a message is broadcasted it will be received by all classes listening on the specific message type at the time the message was sent.

Warning
It is the type of the reference to the message (T in the template class) that is used by the system to identify the type of the message not the actuall message type. If you have a base class B, and inherit A from it: B <- A and you then instantiate an instance of A, but uses a B reference to hold it (like A a; B &b = a;) then the message that will be sent will be of type B.
class B : public BaseMsg {};
class A ; public B {};
A a;
B &b=a;
broadcast(b); // Even this is an A, it will be send as a B, so only
// classes listening for B will get the message
Parameters
msgThe message that should be sent
template<class parentClass>
template<class T >
void FSYS::MsgSender< parentClass >::respond ( T &  msg,
BaseMsg received 
)
inline

When a message is recevied it is possible for at class to send a message back to the sender and only the sender. To do this you use the respond function.

You can use the respond function and another message to send several messages to the sender of that message.

A common use case for a flow where the respond function is used is:

// Client searches for someone to handle a request
ServiceXRequest sXr(<arguments to request>);
broadcast(sXr);
...
// Server receives the request
receive(ServiceXRequest &sXr)
{
// Server sends ack to the client
ServiceXAck sXa
respond(sXa, sXr);
...
// At some later point the server can send data to the client
ServiceXProgressIndicator sXpi10(10);
respond(sXpi10, sXr);
---
// It can send multiple data
ServiceXProgressIndicator xXpi20(20);
respond(sXpi20, sXr);
// Client don't need the service of the server any more, so it closes
// the connection
receive(ServiceXAck sXa)
{
ServiceXTerminate sXt;
respond(sXt, sXa);
// The service the listsn for the ServiceXTerminate signal, and when
// received does what is needed to free internal resources ect.

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