*Edit*: Placed on the Ogre3D wiki under PeerOS
I hope I'm not reinventing too many wheels. It works for me, though I suspect there is a bug or two hanging out in the code.
Here is a sample of the peeros_main.cpp. It shows how to set up multiple handlers to process messages of various class types. I hope this is easy to read:
Code: Select all
#include <iostream>
#include "peeros.h"
#include "peeros_main.h"
using namespace std;
// 1853 is an arbitrary number:
#define TOPIC_EXAMPLE_INITIALIZE 1853
// ======================================================================
// MAIN
// ======================================================================
int main(void)
{
// We only have one publisher:
CMPublisher_Keyboard keyb; // Announces keyboard events
// But there are three handlers who read the publisher:
CMHandler_Camera h_cam; // Handles camera messages
CMHandler_CameraAndWidgets h_camwid; // Handles cameras and widgets
CMHandler_Widget h_wid; // Handles only widgets
// Two example messages. One about a camera, one about a widget.
// Notice how they both are talking about topic TOPIC_EXAMPLE_INITIALIZE
CM_Camera msg_cam; // An example msg about a camera
CM_Widget msg_widget; // An example msg about a widget
msg_cam.m_topic = TOPIC_EXAMPLE_INITIALIZE;
msg_widget.m_topic = TOPIC_EXAMPLE_INITIALIZE;
// Setup relationships. Because each of the message instances, CM_Camera
// and CM_Widget, have their topics set to TOPIC_EXAMPLE_INITIALIZE,
// all three handlers will *only* answer messages about
// TOPIC_EXAMPLE_INITIALIZE. See cm_defs.h for a couple examples.
keyb.Subscribe(h_cam, msg_cam);
keyb.Subscribe(h_camwid, msg_cam);
keyb.Subscribe(h_wid, msg_cam);
// Publish the first message. It will be handled by any subscribers who
// can receive a CM_Camera message. Note: It will only be delivered to
// subscribers whose subscribed topic matches msg_cam.m_topic!
keyb.Publish(msg_cam);
// Now publish a widget message.
keyb.Publish(msg_widget);
// Now change the camera topic to CM_TOPIC_NONE, so all listeners
// who can receive *any* CM_Camera message will be published to.
// It's up to them if they want to do anything with it:
msg_cam.m_topic = CM_TOPIC_NONE;
keyb.Publish(msg_cam);
return 0;
}
Here is the output from the example program. It shows one publisher (CMPublisher_Keyboard) sending messages of varying types (CM_Camera and CM_Widget) to all subscribers.
Code: Select all
PeerPublisher::Subscribe(ob, msg topic:1853)
PeerPublisher::Subscribe(ob, msg topic:1853)
PeerPublisher::Subscribe(ob, msg topic:1853)
CMPublisher_Keyboard::Publish(1853, 0)
CMHandler_CameraAndWidgets::process(CM_Camera *): Widgetizing at '0.111, 0.511, 0.666)!
CMHandler_Camera::process(CM_Camera *): XYZ (0.111, 0.511, 0.666) successfully processed!
CMPublisher_Keyboard::Publish(1853, 0)
CMHandler_Widget::process(CM_Widget *): Cheese 'meunster' dispensed!
CMHandler_CameraAndWidgets::process(CM_Widget *): Placing 'meunster' cheese onto pizza.
CMPublisher_Keyboard::Publish(0, 0)
CMHandler_CameraAndWidgets::process(CM_Camera *): Widgetizing at '0.111, 0.511, 0.666)!
CMHandler_Camera::process(CM_Camera *): XYZ (0.111, 0.511, 0.666) successfully processed!
It's easy to write very complex relationships with this setup, or leave it very simple. Hope this helps someone.
There is some good documentation for it, but it's on my team's wiki which is private. I'll convert it to plain HTML if a couple people express interest.
Note: The Makefile has only been tested under Debian, but I use PeerOS under my Win32 projects as well.