Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

MessageClient.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 /*! \file MessageClient.h
00003 *  \brief Declares the base classes used by the application to communicate.
00004 *  \author $Author: rsharo $
00005 *  \version $Revision: 1.1 $
00006 *  \date    $Date: 2004/04/07 13:45:04 $
00007 *///////////////////////////////////////////////////////////////////////////////
00008 #ifndef MESSAGE_CLIENT_H
00009 #define MESSAGE_CLIENT_H
00010 
00011 #include "Comm/MessageServer.h"
00012 
00013 //
00014 // This is the base class for consumers of network messages.
00015 //
00016 // Users of this class must perform the following:
00017 // 1) Implement the ReceiveMessage() method in their derived class.
00018 // 2) Supply a client ID at construction time.
00019 // 3) Call Register() with a pointer to a valid MessageServer.
00020 //
00021 // As long as the message server keeps running, any messages sent
00022 // to the specified client ID will be presented to the client.
00023 // If the client returns (-1) from ReceiveMessage(), the server
00024 // will deregister the client and it will no longer receive messages.
00025 class MessageClient
00026 {
00027 public:
00028         //
00029         // Note that the MessageServer class has control of the m_pServer
00030         // member.
00031         friend class MessageServer;
00032 
00033         //
00034         // Constructs a MessageClient.  The derived class must supply
00035         // a client identifier at the time of construction.
00036         MessageClient(u_int id) : m_id(id), m_pServer(NULL) {}
00037 
00038         //
00039         // Virtual destructor. Default behavior is to deregister
00040         // with the message server (if any).
00041         virtual ~MessageClient()
00042         {
00043                 if (m_pServer != NULL)
00044                         Deregister();
00045         }
00046 
00047 
00048         //
00049         // Returns the identifier for this client.  The identifier was
00050         // specified at construction time.
00051         u_int id() const { return m_id; }
00052 
00053 
00054         //
00055         // Handles an incoming message. The derived class must implement
00056         // this pure-virtual method.
00057         // Note that if this method returns a -1, the server will
00058         // automatically deregister this client for the given ID.
00059         virtual int ReceiveMessage(const char *msg, size_t len) = 0;
00060 
00061         //
00062         // Registers this client with a message server.
00063         // Returns 0 on success and -1 on failure.
00064         virtual int Register(MessageServer *pServer);
00065 
00066         //
00067         // Deregisters this client with its message server.
00068         // Returns 0 on success and -1 on failure.
00069         virtual int Deregister();
00070 
00071 protected:
00072         //
00073         // If registered with a message server, this method
00074         // sends a message to another client. 
00075         // It returns -1 on error and the number of bytes sent on success.
00076         int SendMessage(const void *msg, size_t len, u_int dest)
00077         {
00078                 if (m_pServer == NULL)
00079                 {
00080                         ACE_ERROR_RETURN((LM_ERROR,
00081                                 "(%N:%l) Attempt to send a message from an unregistered "
00082                                 "message client.\n"),-1);
00083                 }
00084 
00085                 return m_pServer->SendMessage(m_id, msg, len, dest);
00086         }
00087 
00088         //
00089         // Returns a pointer to the client's message server.
00090         // If not registered, this method returns NULL.
00091         MessageServer * Server() { return m_pServer; }
00092 
00093 private:
00094         //
00095         // The identifier by which this client is known.
00096         const u_int             m_id;
00097 
00098         //
00099         // The server with which this client is registered.
00100         // NOTE: This member is set and cleared exclusively by the
00101         // server class itself.  This class should not change this member.
00102         MessageServer * m_pServer;
00103 };
00104 
00105 
00106 
00107 //
00108 // This is a base class for clients that wish to handle messages
00109 // to many client IDs from a single object.
00110 class MultiMessageClient
00111 {
00112 public:
00113         //
00114         // Constructs a multi-message client.
00115         MultiMessageClient(u_int *id, u_int numIDs);
00116 
00117         //
00118         // Virtual destructor for the multi-message client.
00119         virtual ~MultiMessageClient();
00120 
00121         //
00122         // This pure virtual method is called any time a message is received
00123         // for any of the registered IDs.
00124         // NOTE: if this method returns -1, then the client is deregistered
00125         // for the specific ID for which this method was called.
00126         virtual int ReceiveMessage(u_int destinationID,
00127                 const char *msg, size_t len) = 0;
00128 
00129         //
00130         // If registered with a message server with the given origination ID,
00131         // this method sends a message to another client.
00132         // It returns -1 on error and the number of bytes sent on success.
00133         int SendMessage(u_int originator, const void *msg, size_t len, u_int dest);
00134 
00135         //
00136         // Registers this client with a message server.
00137         // Returns 0 if all IDs successfully register and -1 on any failure.
00138         virtual int Register(MessageServer *pServer);
00139 
00140         //
00141         // Deregisters this client with its message server.
00142         // Returns 0 if all IDs successfully deregister and -1 on any failure.
00143         virtual int Deregister();
00144 
00145 private:
00146         //
00147         // This inner class routes its messages to the parent MultiMessageClient.
00148         class MC : public MessageClient
00149         {
00150         public:
00151                 // Constructs this specialized message client.
00152                 MC(u_int id, MultiMessageClient *pParent)
00153                         : MessageClient(id), m_pParent(pParent) {}
00154 
00155                 // Makes SendMessage public to the MultiMessageClient can call it.
00156                 int SendMessage(const void *msg, size_t len, u_int dest)
00157                         { return MessageClient::SendMessage(msg, len, dest); }
00158 
00159                 // Forwards a message to the MultiMessageClient.
00160                 virtual int ReceiveMessage(const char *msg, size_t len)
00161                         { return m_pParent->ReceiveMessage(id(), msg, len); }
00162 
00163         private:
00164                 MultiMessageClient *m_pParent;
00165 
00166         };
00167 
00168 private:
00169         //
00170         // The array of MessageClients -- one per ID.
00171         MC **m_ppChildren;
00172 
00173         //
00174         // The number of MessageClients in the array.
00175         u_int m_numChildren;
00176 
00177 };
00178 
00179 #endif // MESSAGE_CLIENT_H
00180 

Generated on Wed Sep 5 12:54:22 2007 for DSACSS Operational Code by  doxygen 1.3.9.1