Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

Ars::MessageServer Class Reference

A server which queues and dispatches messages. More...

#include <arsmessages.h>

Collaboration diagram for Ars::MessageServer:

Collaboration graph
[legend]

Public Member Functions

virtual ~MessageServer (void)
void RegisterMessageClient (MessageClient *pClient, const Message::EMessageType eMessageType)
void DeregisterMessageClient (MessageClient *pClient, const Message::EMessageType eMessageType)
void DeregisterMessageClient (MessageClient *pClient)
void ProcessMessage (void)
 Takes the next message in the queue and dispatches it to any registered clients in priority order.

void QueueMessage (Message *msg, const int aLine, const std::string aFile, void *aFrom)
void LaunchMessage (Message *msg)
 Sends immediately a message - do not put it in queue.

const bool IsRegistered (const Message::EMessageType &type, const MessageClient *target)

Static Public Member Functions

MessageServerInstance (void)
 The single valid instance of the message server, or create one if it doesn't already exist.


Protected Member Functions

 MessageServer (void)
 The CMessageServer class cannot be directly instantiated, it must be access through Instance().


Protected Attributes

std::map< Message::EMessageType,
std::vector< std::pair< MessageClient *,
bool > > > 
messageClients
 A map of all the registered clients.


Static Protected Attributes

MessageServerinstance = 0
 A pointer to the single instande of the message server.


Private Member Functions

void HandleTillRoot (Message &msg)

Friends

class Form

Detailed Description

A server which queues and dispatches messages.

CMessageServer is a singeton (only one instance of it is allowed to exist at any time) Clients must register to get messages sent to them

See also:
CMessage CMessageClient

Definition at line 242 of file arsmessages.h.


Constructor & Destructor Documentation

Ars::MessageServer::MessageServer void   )  [protected]
 

The CMessageServer class cannot be directly instantiated, it must be access through Instance().

Definition at line 113 of file arsmessage.cpp.

00114 {
00115 }

Ars::MessageServer::~MessageServer void   )  [virtual]
 

Definition at line 118 of file arsmessage.cpp.

References messageClients.

00119 {
00120         while( !empty() )
00121         {
00122                 delete front();
00123                 pop_front();
00124         }
00125         messageClients.clear();
00126         clear();
00127 }


Member Function Documentation

void Ars::MessageServer::DeregisterMessageClient MessageClient pClient  ) 
 

Deregister a client for all message types

Parameters:
pClient A pointer to the message client to be deregistered

Definition at line 165 of file arsmessage.cpp.

References DeregisterMessageClient(), and messageClients.

00166 {
00167         for( std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator it = messageClients.begin() ; it != messageClients.end() ; ++it )
00168                 if( it->first != Message::DEREG_CHILD )
00169                         DeregisterMessageClient( pClient , it->first );
00170 }

void Ars::MessageServer::DeregisterMessageClient MessageClient pClient,
const Message::EMessageType  eMessageType
 

Deregister a client for a certain message type

Parameters:
pClient A pointer to the message client to be deregistered
eMessageType The message type for which the client no longer should recieve messages

Definition at line 151 of file arsmessage.cpp.

References messageClients.

Referenced by DeregisterMessageClient().

00152 {
00153         std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator at = messageClients.find( eMessageType );
00154         if( at != messageClients.end() )
00155         {
00156                 for( std::vector< std::pair<MessageClient *, bool> >::iterator it = at->second.begin() ; it != at->second.end() ; ++it )
00157                         if( it->first == pClient )
00158                         {
00159                                 it->second = false;
00160                                 break;
00161                         }
00162         }
00163 }

void Ars::MessageServer::HandleTillRoot Message msg  )  [private]
 

Definition at line 207 of file arsmessage.cpp.

References Ars::Message::Destination(), Ars::Window::GetParent(), Ars::MessageClient::HandleMessage(), IsRegistered(), Ars::Message::MessageType(), and Ars::Message::SetTarget().

Referenced by LaunchMessage(), and ProcessMessage().

00208 {
00209         bool stopLoop = false;
00210 
00211         while( !stopLoop )
00212         {
00213                 if( IsRegistered( msg.MessageType() , msg.Destination() ) )
00214                         stopLoop = msg.Destination()->HandleMessage( msg );
00215 
00216                 if( !stopLoop )
00217                 {
00218                         Window *wnd = dynamic_cast<Window *>( msg.Destination() );
00219 
00220                         if( wnd && wnd->GetParent() )
00221                                 msg.SetTarget( wnd->GetParent() );
00222                         else
00223                                 stopLoop = true;
00224                 }
00225         }
00226 }

MessageServer & Ars::MessageServer::Instance void   )  [static]
 

The single valid instance of the message server, or create one if it doesn't already exist.

Definition at line 130 of file arsmessage.cpp.

References instance.

00131 {
00132         if( !instance )
00133                 instance = new MessageServer;
00134 
00135         return *instance;
00136 }

const bool Ars::MessageServer::IsRegistered const Message::EMessageType type,
const MessageClient target
 

Definition at line 228 of file arsmessage.cpp.

References messageClients.

Referenced by HandleTillRoot().

00229 {
00230         std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator at = messageClients.find( type );
00231         if( at != messageClients.end() )
00232                 for( std::vector< std::pair<MessageClient *, bool> >::iterator it = at->second.begin() ; it != at->second.end() ; ++it )
00233                         if( it->first == target )
00234                                 return it->second;
00235         return false;
00236 }

void Ars::MessageServer::LaunchMessage Message msg  ) 
 

Sends immediately a message - do not put it in queue.

Definition at line 246 of file arsmessage.cpp.

References Ars::Message::Destination(), HandleTillRoot(), messageClients, and Ars::Message::MessageType().

00247 {
00248         if( msg->Destination() != 0 )
00249                 HandleTillRoot( *msg );
00250         else
00251         {
00252                 std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator at = messageClients.find( msg->MessageType() );
00253                 std::vector< std::pair<MessageClient *, bool> > tmp = at->second;
00254 
00255                 for( std::vector< std::pair<MessageClient *, bool> >::iterator it = tmp.begin() ; it != tmp.end() ; ++it )
00256                         if( it->second )
00257                                 it->first->HandleMessage( *msg );
00258         }
00259         delete msg;
00260 }

void Ars::MessageServer::ProcessMessage void   ) 
 

Takes the next message in the queue and dispatches it to any registered clients in priority order.

Broadcast to all if no specific targets....

Definition at line 172 of file arsmessage.cpp.

References Ars::Message::Destination(), HandleTillRoot(), messageClients, and Ars::Message::MessageType().

00173 {
00174         if( !empty() )
00175         {
00176                 Message *msg2 = front();
00177                 Message &msg = *msg2;
00178                 pop_front();
00179                 std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator at = messageClients.find( msg.MessageType() );
00181                 if( at != messageClients.end() )
00182                 {
00183                         if( msg.Destination() == 0 )
00184                         {
00185                                 std::vector< std::pair<MessageClient *, bool> > tmp = at->second;
00186                                 for( std::vector< std::pair<MessageClient *, bool> >::iterator it = tmp.begin() ; it != tmp.end() ; ++it )
00187                                         if( it->second && it->first->HandleMessage( msg ) )
00188                                                 break;
00189                         }
00190                         else 
00191                                 HandleTillRoot( msg );
00192                 }
00193                 delete msg2;
00194         }
00195 
00196         for( std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator at = messageClients.begin() ; at != messageClients.end() ; ++at )
00197                 for( std::vector< std::pair<MessageClient *, bool> >::iterator it = at->second.begin() ; it != at->second.end() ; )
00198                         if( !it->second )
00199                         {
00200                                 at->second.erase( it );
00201                                 it = at->second.begin();
00202                         }
00203                         else
00204                                 ++it;
00205 }

void Ars::MessageServer::QueueMessage Message msg,
const int  aLine,
const std::string  aFile,
void *  aFrom
 

Adds a message to the message queue

Parameters:
pMessage A pointer to the message to be queued

Definition at line 238 of file arsmessage.cpp.

References Ars::Message::file, Ars::Message::from, and Ars::Message::line.

00239 {
00240         msg->file = aFile;
00241         msg->line = aLine;
00242         msg->from = aFrom;
00243         push_back( msg );
00244 }

void Ars::MessageServer::RegisterMessageClient MessageClient pClient,
const Message::EMessageType  eMessageType
 

Register a client to recieve messages

Parameters:
pClient A pointer to the client which should recieve the messages
eMessageType The message type the client wishes to recieve
Priority The priority of the client for recieving the message

Definition at line 139 of file arsmessage.cpp.

References messageClients.

00140 {
00141         std::map<Message::EMessageType,std::vector< std::pair<MessageClient *, bool> > >::iterator at = messageClients.find( eMessageType );
00142         if( at == messageClients.end() )
00143         {
00144                 std::vector< std::pair<MessageClient *, bool> > tmp;
00145                 at = messageClients.insert( std::make_pair( eMessageType , tmp ) ).first;
00146         }
00147         at->second.push_back( std::make_pair( pClient , true ) );
00148 }


Friends And Related Function Documentation

friend class Form [friend]
 

Definition at line 245 of file arsmessages.h.


Field Documentation

MessageServer * Ars::MessageServer::instance = 0 [static, protected]
 

A pointer to the single instande of the message server.

Definition at line 111 of file arsmessage.cpp.

Referenced by Instance().

std::map< Message::EMessageType , std::vector< std::pair<MessageClient *, bool> > > Ars::MessageServer::messageClients [protected]
 

A map of all the registered clients.

Definition at line 248 of file arsmessages.h.

Referenced by DeregisterMessageClient(), IsRegistered(), LaunchMessage(), ProcessMessage(), RegisterMessageClient(), and ~MessageServer().


The documentation for this class was generated from the following files:
Generated on Fri Dec 5 04:06:46 2003 for Borqueror by doxygen 1.3.3