00001 //////////////////////////////////////////////////////////////////////////////// 00002 /*! \file MessageServer.h 00003 * \brief Declares the class that routes messages around the system. 00004 * \author $Author: rsharo $ 00005 * \version $Revision: 1.1 $ 00006 * \date $Date: 2004/04/07 13:45:04 $ 00007 */////////////////////////////////////////////////////////////////////////////// 00008 #ifndef MESSAGE_SERVER_H 00009 #define MESSAGE_SERVER_H 00010 00011 #include "ace/INET_Addr.h" 00012 #include "ace/SOCK_Dgram.h" 00013 #include "ace/Reactor.h" 00014 00015 #include <map> 00016 00017 #include "Comm/AddressClient.h" 00018 00019 class MessageClient; 00020 00021 00022 // 00023 // This class manages incoming and outgoing network messages. Message clients 00024 // send outgoing messages through this class, and incoming messages 00025 // are read in this class and dispatched to the respective message clients. 00026 class MessageServer : public ACE_Event_Handler 00027 { 00028 public: 00029 // 00030 // MessageClients may call RegisterClient() and RemoveClient() 00031 // even though those methods are not public. 00032 friend class MessageClient; 00033 00034 // 00035 // Constructs a Message server. Given the IP address of the 00036 // AddressServer, this constructor instantiates 00037 // an AddressClient as a child of this MessageServer as well. 00038 // The address client will be registered with the reactor 00039 // when RegisterWithReactor() is called for this object. 00040 // The message server's UDP port will be opened with the 00041 // specified local IP address. 00042 // Note that the message server will not be useful until 00043 // RegisterWithReactor() is called. 00044 explicit MessageServer (const ACE_INET_Addr &addrServer, 00045 const ACE_INET_Addr &localAddr=ACE_INET_Addr()); 00046 00047 // 00048 // Constructs a Message server. This constructor instantiates 00049 // a MessageServer and connects it to the supplied AddressClient. 00050 // If the "takeOwnership" flag is set, it is assumed that 00051 // the supplied client is to be registered and deregistered 00052 // to/from the reactor, and deleted from the MessageServer's 00053 // destructor as if it were allocated by the MessageServer itself. 00054 // If the "take ownership" flag is false, then the AddressClient 00055 // will simply be used by the MessageServer and never registered, 00056 // deregistered, or destroyed. 00057 // The message server's UDP port will be opened with the 00058 // specified local IP address. 00059 // Note that the message server will not be useful until 00060 // RegisterWithReactor() is called for the MessageServer and, 00061 // if the AddressClient is not owned, for the AddressClient as well. 00062 explicit MessageServer (AddressClient *addrClient, 00063 bool takeOwnership = false, 00064 const ACE_INET_Addr &localAddr=ACE_INET_Addr()); 00065 00066 // 00067 // Destroys the message server. RemoveFromReactor() should have 00068 // been called before destruction. If the MessageServer owns 00069 // its AddressClient, then the AddressClient will be destroyed 00070 // as well. 00071 ~MessageServer (void); 00072 00073 // 00074 // Registers the MessageServer with a reactor. If the MessageServer 00075 // owns its AddressClient, the address client will be registered with 00076 // the reactor as well. Once registered, the message server can be used 00077 // to route messages among MessageClients. 00078 // Returns 0 on success or -1 on failure. 00079 int RegisterWithReactor(ACE_Reactor &theReactor); 00080 00081 // 00082 // Removes the MessageServer from a reactor. If the MessageServer 00083 // owns its AddressClient, the address client will be removed from 00084 // the reactor as well. Once removed, the message server can no longer be 00085 // used to route messages among MessageClients. 00086 // Returns 0 on success or -1 on failure. 00087 int RemoveFromReactor(ACE_Reactor &theReactor, bool immediate = false); 00088 00089 // 00090 // Provides access to the MessageServer's AddressClient. 00091 // Returns a pointer to the AddressClient. 00092 AddressClient * GetAddressClient() { return m_pAddrClient; } 00093 00094 00095 // 00096 // Implementation of the handle_input() method derived from 00097 // ACE_Event_Handler. This method routes incoming messages to the 00098 // MessageServer's clients. 00099 virtual int handle_input (ACE_HANDLE fd); 00100 00101 // 00102 // Implementation of the get_handle() method derived from 00103 // ACE_Event_Handler. This method returns a handle to the MessageServer's 00104 // UDP socket. 00105 virtual ACE_HANDLE get_handle (void) const; 00106 00107 // 00108 // Routes a message to other MessageServers. MessageClients call this 00109 // method when they want to transmit a message. The IP addresses 00110 // bound to the destination ID are retrieved from the AddressServer and 00111 // the message is sent to each of those IP addresses with a header. 00112 // Returns 0 if all message transmissions succeed, -1 otherwise. 00113 int SendMessage(u_int originator, const void *msg, size_t len, u_int dest); 00114 00115 protected: 00116 // 00117 // This is the header that is pre-pended to all outgoing messages. 00118 // It contains a magic number that allows determination of endianness 00119 // and fields to route and verify the integrity of the message. 00120 typedef struct MsgHdr 00121 { 00122 // 00123 // This is the big-endian interpretation of the characters 00124 // "MESG" If the sender and receiver don't have the same endianness, 00125 // this magic number will be reversed on the receiver side. 00126 enum { MAGIC_NUMBER = 0x4D455347 }; 00127 00128 // 00129 // Constructs a message header and initializes the magic number. 00130 MsgHdr() : m_magicNumber(MAGIC_NUMBER) {} 00131 00132 // 00133 // The magic number that identifies this block as a message and 00134 // shows its endianness. 00135 u_int m_magicNumber; 00136 // 00137 // The client ID to which this message is destined. 00138 u_int m_destination; 00139 // 00140 // The length of the data following this header. 00141 u_int m_msgLength; 00142 } MsgHdr_t; 00143 00144 // 00145 // The data structure used to keep track of the clients and 00146 // their IDs. 00147 typedef std::multimap<u_int, MessageClient *> ClientMap_t; 00148 // 00149 // The internal structure that holds a single client/ID pair. 00150 typedef ClientMap_t::value_type ClientValue_t; 00151 00152 protected: 00153 // 00154 // Maps a client to receive messages with a given ID. 00155 // Note that this method will fail if the client is already 00156 // registered for the given ID. 00157 // This method is normally only called from MessageClient::Register(). 00158 // Returns 0 on success and -1 on failure. 00159 int RegisterClient(u_int id, MessageClient *client); 00160 00161 // 00162 // Removes a client mapping for a given ID. 00163 // The client will no longer receive messages with this ID. 00164 // Note that this method will fail if the client is not registered 00165 // for the given ID. 00166 // This method is normally only called from MessageClient::Deregister(). 00167 // Returns 0 on success and -1 on failure. 00168 int RemoveClient(u_int id, MessageClient *client); 00169 00170 // 00171 // Implementation method that removes a MessageClient from 00172 // the client map. It is called as part of RemoveClient() 00173 // or any time a MessageClient returns -1 from 00174 // MessageClient::ReceiveMessage(). 00175 // Note that this method is not synchronized (it acquires no mutexes). 00176 // It depends on the calling method to ensure synchronicity. 00177 // Returns an iterator pointing to the element immediately following 00178 // the removed one in the client map. 00179 ClientMap_t::iterator RemoveClient_i(ClientMap_t::iterator it); 00180 00181 protected: 00182 typedef AddressClient::AddrList_t AddrList_t; 00183 00184 ACE_Thread_Mutex m_clientMutex; 00185 ClientMap_t m_clients; 00186 00187 AddressClient *m_pAddrClient; 00188 bool m_addrClientOwned; 00189 00190 ACE_INET_Addr m_localAddr; 00191 00192 private: 00193 ACE_SOCK_Dgram m_socket; 00194 char m_buf[BUFSIZ]; 00195 }; 00196 00197 00198 00199 #endif // MESSAGE_SERVER_H 00200