Skip to content

Instantly share code, notes, and snippets.

@dublindan
Created June 24, 2010 05:44
Show Gist options
  • Save dublindan/451033 to your computer and use it in GitHub Desktop.
Save dublindan/451033 to your computer and use it in GitHub Desktop.
typedef unsigned int MessageID;
typedef unsigned int Conversation;
class Message
{
public:
virtual ~Message () {}
virtual const MessageID& messageID () const=0;
virtual const char* senderName () const=0;
};
class MessageFactory
{
public:
virtual ~MessageFactory () {}
virtual Message* create (const char* sender, const MessageID& id)=0;
virtual void destroy (Message* const message)=0;
};
class MessageFunctor
{
public:
virtual void operator()(const Message* const message, const Conversation conversation) const {}
};
class Mailbox
{
public:
virtual const Message* const next ()=0;
};
/**
* Flags to control how message handlers may be invoked.
* Message handlers can be invoked in a number of ways, depending on the threading model and thread safety of the handler.
*/
enum HandlerFlags
{
/* Threading Model Flags *******************/
/** Normal task which may be executed in the task pool */
USER_TASK = 0x00, // ..xxxx0000
/** Core task which must be executed by the main thread */
CORE_TASK = 0x01, // ..xxxx0001
/** Lightweight tasks are executed together as a single user task */
LIGHTWEIGHT_TASK = 0x03, // ..xxxx0011
/* Thread Safety Flags *********************/
/** Task may be dispatched multiple times concurrently */
THREAD_SAFE = 0x00, // ..0000xxxx
/** Task must only be executing once at any given time, multiple executions must be performed sequentially */
THREAD_UNSAFE = 0x10, // ..0001xxxx
/* Default Flags ***************************/
/** Default task model */
TASK_DEFAULT = USER_TASK,
/** Default thread safety */
THREAD_DEFAULT = THREAD_SAFE,
/** Default flags */
DEFAULT_FLAGS = TASK_DEFAULT | THREAD_DEFAULT
};
/**
* Application Programming Interface for messaging subsystem.
*
*/
class MessagingAPI
{
public:
/* Message type management *****************/
/** Register a new message type by name and retrieve message identifier */
virtual const MessageID& registerMessageType (const char* name, MessageFactory* factory)=0;
/** Lookup a registered message type by name and retrieve message identifier */
virtual const MessageID& lookupMessageIdByName (const char* name)=0;
/** Retrieve a list of all registered message type names */
virtual void enumerateMessageTypes (std::vector<const char*>& messageTypes)=0;
/* Mailbox management **********************/
/** Register a new mailbox by name and retrieve reference to the receiving end-point */
virtual const Mailbox& registerMailbox (const char* mailbox)=0;
/** Copy messages of specified type to specified mailbox */
virtual void copyToMailbox (const MessageID& id, const char* mailbox)=0;
/** Retrieve a list of all registered mailbox names */
virtual void enumerateMailboxes (std::vector<const char*>& mailboxes)=0;
/* Message handler management **************/
/** Register a message handler to be called for messages of specified id; default threading model and safety */
virtual void registerHandler (const MessageFunctor& functor, const MessageID& id)=0;
/** Register a message handler to be called for messages of specified id; specified threading model and safety */
virtual void registerHandler (const MessageFunctor& functor, const MessageID& id, const HandlerFlags flags)=0;
/* Conversation and message management *****/
/** Create a new message of specified type */
virtual Message* const newMessage (const MessageID& id, const char* sender)=0;
/** Start a new conversation */
virtual const Conversation newConversation ()=0;
/** Send a message outside of any conversations */
virtual void send (const Message* msg)=0;
/** Send a message as part of a conversation */
virtual void send (const Message* msg, const Conversation conv)=0;
/** Queue a message in a named mailbox for later processing */
virtual void sendToMailbox (const char* mailbox, const Message* msg)=0;
/* Background task management **************/
/** Start a background task */
virtual void startBackgroundTask (const MessageFunctor& functor)=0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment