msgbroker

Index Home MAE > MAE Architecture > MAE Daemons > msgbroker

MsgBroker Daemon

This daemon routes messages within MAE amongst the daemons and applications.  Typically, a daemon registers to listen to a channel.  Then, whenever a message is being sent to that channel, MsgBroker knows which other daemon(s) to send it to. There is also a broadcast channel that shares messages to all subscribers of it.

Use the tmesg utility to send messages from the command line.

Programming API to MsgBroker

Although you can directly use the Messenger class to write code to send and receive messages, it is best practice to have genmae create that code. You create a .mreg file that specifies what messages your program receives and what API calls other programs can make to send your program messages; genmae will write the code for you.

For example, if your app uses the xyz channel, then you create xyz.mreg and then use genmae to create the logic to handle inbound messages using

genmae xyz.mreg -msg msgXyz.cpp

and then create the API interface using

genmae xyz.mreg -apih -apicpp

to create XyzAPI.h and XyzAPI.cpp.

Receiving a Message Example

There's plenty of detail in the genmae documentation, but here is an example of receiving a message:

#msg "key" userKey(const string * key, int keycode)

/** Process a user keystroke - send it to the connected resource.

 * @param key - a representation of the keystroke, e.g. ctrl-a or j

 * @param keycode - the ASCII code for the key, if printable, 0 otherwise.

 */

#param key=key

#param keycode=code

which declares the keyword for this inbound message is key.  It has two message parameters, key and code, which are mapped to the parameters key and keycode in the method userKey(). The method parameter types are specified; genmae will convert the parameter values to those types and then call userKey().

By automatically generating all this code, you can focus on creating the method rather than spending your time managing message mechanics.

API Interface to Program Example

If you have a program that other programs will send messages to, you can create an API for those programs to use.  The API will properly encode the message and send it. Your program then receives the message and processes it.

For example, if your program uses the xyz channel, you would enter the following into xyz.mreg to declare the API call XyzAPI::chat() which expects three parameters of the appropriate type.  genmae will generate the code to encode the parameters and send them to your program.  The #msg block after the #api block defines how your program receives and processes the message.

#api chat(const UserDevice & device, const string & from, const string & text)

/** Send a chat message to the end-user's device.

 * @param device - the end-user's device

 * @param from - name of user whom the message is from

 * @param text - the message to display

 * @return true if sent successfully

 */

#param device=device

#param from=from

#param text=text

#msg "chat" cmdChat(const string & from, const string & msg);

/** Send a chat message to the end-user's device.

 * @param from - name of user whom the message is from

 * @param msg - the message to display

 */

#param from=from

#param msg=text

You may have noticed that the #msg block did not have a mapping for device.  This is because the program handles multiple users which have connected via different devices.  The .mreg file needs a #idcode block (not shown here) that processes device (because, presumably, all the messages include a device parameter) to determine which data structure instance to use to process the call.  Then instance.cmdChat(from,msg) is called.

Database Tables

msgbroker uses these database tables:

Table Name

Description

MsgListeners

Used to track message channels used by which apps.

PendingMsgs

If an app can accept queued messages when it starts, those messages are stored here.


Messages Sent/Received

Command

Parameters

Description

MsgListen

appid appname src persist user

An app sends this message to msgbroker to declare it is now listening on the channel, src.  commhub prepends the appid and appname.  persist is Y is messages should be queued when the app is not connected.

MsgClose

appid appname src

This message tells msgbroker that channel src is no longer listening for messages.  Routing of messages to that channel stop.

MsgCease

appid appname src

This message tells msgbroker that channel src is no longer listening for messages.  Routing of messages to that channel stop.

MsgRoute

appid appname src dest ttl

An app, src, sends this message to msgbroker so msgbroker can route it to the appropriate app, dest.

MsgSystem

appid appname src dest command message

commhub sends this message to msgbroker, which msgbroker then broadcasts to all apps.

MsgSend

appid src dest message

msgbroker sends this to commhub to relay a message to an app; this is sent by an app for msgbroker to process.  The appid was provided by commhub to identify app routing information.  The 'src is the name of the source app sending the message.  The dest is the task name of the destination app.  Note that dest may be broadcast for messages sent to all apps.


Message Channels

Message broker clients registered services:

Channel

Daemon

Description

display

usergw

Channel for controlling output to PC's and DM

input

guibroker

Channel for processing input from PC's and DM.  Often sent from usergw.

gui

guibroker

Channel for interactive I/O with PC or DM.

imager

imager

Used by imager to receive image requests and send images to usergw.

iogw

iogw

Used by IoGW to receive resource requests, receive data for resources, and send resource data to apps.

noticegw

noticegw

Used by noticegw to receive notice requests.

termgw

termgw

Used by termgw to receive communications for virtual terminals and transmit virtual terminal updates to usergw.

maerest

maerest

Used by maerest to receive REST API requests and transmit results back to usergw.

userman

userman

Used by userman to variously communicate with MAE.

supervisor

supervisor

Used by supervisor to variously communicate with MAE.

maeterm

maeterm

Used by maeterm to variously communicate with MAE.