00001 //////////////////////////////////////////////////////////////////////////////// 00002 /*! \file AddressServer.h 00003 * \brief Declares the class that keeps track of all the networked processes. 00004 * \author $Author: rsharo $ 00005 * \version $Revision: 1.1 $ 00006 * \date $Date: 2004/04/07 13:45:04 $ 00007 */////////////////////////////////////////////////////////////////////////////// 00008 #ifndef ADDRESS_SERVER_H 00009 #define ADDRESS_SERVER_H 00010 00011 #include "ace/INET_Addr.h" 00012 #include "ace/SOCK_Dgram_Mcast.h" 00013 #include "ace/Reactor.h" 00014 00015 #include <map> 00016 00017 // 00018 // A reactor-based class that stores and reports message ID bindings via UDP. 00019 // 00020 // This class is a fundamental part of the MessageClient/MessageServer scheme. 00021 // Message clients are user-derived classes that send and receive messages 00022 // on the network. Message clients carry an integer identifier that 00023 // determines which messages they are to receive. Since clients can be 00024 // distributed over a network, a means is needed to keep track of which 00025 // message clients are at which IP addresses -- or more specifically which 00026 // IP addresses are interested in which messages. 00027 // 00028 // An address server is an object that holds the mappings from message ID 00029 // to IP address (called bindings). The address server will typically 00030 // be located at a multicast address so it can be relocated anywhere on 00031 // the network without changing its IP address. 00032 // 00033 // The AddressServer class is a subclass of ACE_Event_Handler, and must 00034 // be registered with an ACE_Reactor to function properly. 00035 class AddressServer : public ACE_Event_Handler 00036 { 00037 public: 00038 // 00039 // Constructor. 00040 // Constructs an AddressServer and binds it to the specified 00041 // multicast address and port. 00042 // If an interface is specified, then the server will listen 00043 // on that ethernet interface only. 00044 // The server must be registered with a reactor before it will be 00045 // useful, however. 00046 explicit AddressServer (u_short port = ACE_DEFAULT_MULTICAST_PORT, 00047 const char *mcast_addr = ACE_DEFAULT_MULTICAST_ADDR, 00048 const char *mcast_iface=NULL); 00049 00050 // 00051 // Constructor. 00052 // Constructs an AddressServer and binds it to the specified 00053 // multicast IP address. 00054 // If an interface is specified, then the server will listen 00055 // on that ethernet interface only. 00056 // The server must be registered with a reactor before it will be 00057 // useful, however. 00058 explicit AddressServer (const ACE_INET_Addr &mcast_addr, 00059 const char *mcast_iface=NULL); 00060 00061 // 00062 // Destroys the AddressServer. The server should have been 00063 // deregistered from its reactor (if any) before this method 00064 // is called. 00065 ~AddressServer (void); 00066 00067 // 00068 // Enables/disables loopback of multicast messages. 00069 // AddressClients on the same host will not be able to communicate 00070 // with the AddressServer unless loopback is enabled. 00071 // Returns 0 on success and -1 on failure. 00072 int SetLoopbackEnable(bool enabled); 00073 00074 // 00075 // Sets the multicast message time to live (TTL). 00076 // If TTL is set to 1, then messages will only be sent on the local 00077 // subnet. Setting this number higher determines how many routers through 00078 // which multicast messages may travel. Set this higher than one only 00079 // if you have clients on different subnets than the server. 00080 // Returns 0 on success or -1 on failure. 00081 int SetTimeToLive(u_char ttl); 00082 00083 // 00084 // Connect this object to a reactor. 00085 // The server will not receive network communication until this is called. 00086 // Returns zero on success and -1 on failure. 00087 int RegisterWithReactor(ACE_Reactor &theReactor); 00088 00089 // 00090 // Disconnect this object from a reactor. 00091 // The server will no longer receive network communication after 00092 // this is called. This method should be called prior to destroying the 00093 // AddressServer. 00094 // Returns zero on success and -1 on failure. 00095 int RemoveFromReactor(ACE_Reactor &theReactor); 00096 00097 // 00098 // Implementation of the handle_input() method derived from 00099 // ACE_Event_Handler. This method processes messages sent to the address 00100 // server's multicast UDP socket. 00101 virtual int handle_input (ACE_HANDLE fd); 00102 00103 // 00104 // Implementation of the get_handle() method derived from 00105 // ACE_Event_Handler. This method returns a handle to the AddressServer's 00106 // multicast UDP socket. 00107 virtual ACE_HANDLE get_handle (void) const; 00108 00109 protected: 00110 // 00111 // This method sends an ASCII string to the specified destination 00112 // address via UDP. 00113 // Returns the number of bytes sent on success and -1 on failure. 00114 int SendString(const char *str, const ACE_INET_Addr &dest); 00115 00116 // 00117 // Notifies all registered AddressClients that a global change 00118 // to the bindings has occured (such as a "CLEAR_BINDINGS" command). 00119 // Returns 0 if all clients were successfully notified, or -1 00120 // if any failures occurred. 00121 int NotifyChange(); 00122 00123 // 00124 // Notifies all registered AddressClients that the bindings for a given 00125 // message ID have changed (such as a "ADD" or "REMOVE" command). 00126 // Returns 0 if all clients were successfully notified, or -1 00127 // if any failures occurred. 00128 int NotifyChange(u_int id); 00129 00130 private: 00131 // 00132 // The data structure type used to hold ID/address bindings. 00133 typedef std::multimap<u_int, std::string> BindMap_t; 00134 00135 // 00136 // The internal data type of a single binding. 00137 typedef BindMap_t::value_type BindValue_t; 00138 00139 // 00140 // The data structure type used to record the AddressClient 00141 // notification list. 00142 typedef std::map<u_int,ACE_INET_Addr> NotificationMap_t; 00143 00144 // 00145 // The internal data type of a single notification entry. 00146 typedef NotificationMap_t::value_type NotificationValue_t; 00147 00148 ACE_INET_Addr m_remoteAddr; 00149 char m_buf[BUFSIZ]; 00150 00151 ACE_SOCK_Dgram m_replySocket; 00152 ACE_SOCK_Dgram_Mcast m_mcastDgram; 00153 ACE_INET_Addr m_mcastAddr; 00154 00155 BindMap_t m_bindings; 00156 00157 NotificationMap_t m_observers; 00158 u_int m_nextObserverID; 00159 }; 00160 00161 00162 00163 #endif // ADDRESS_SERVER_H 00164