ECG_Complex_Address_Server.cpp

Go to the documentation of this file.
00001 // $Id: ECG_Complex_Address_Server.cpp 78519 2007-05-29 14:54:16Z mesnier_p $
00002 
00003 #include "orbsvcs/Event/ECG_Complex_Address_Server.h"
00004 #include "ace/SString.h"
00005 #include "ace/streams.h"
00006 
00007 #if !defined(__ACE_INLINE__)
00008 #include "orbsvcs/Event/ECG_Complex_Address_Server.inl"
00009 #endif /* __ACE_INLINE__ */
00010 
00011 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 TAO_ECG_Complex_Address_Server::TAO_ECG_Complex_Address_Server (
00014                                               int is_source_mapping)
00015   : is_source_mapping_ (is_source_mapping)
00016 {
00017 }
00018 
00019 TAO_ECG_Complex_Address_Server::~TAO_ECG_Complex_Address_Server (void)
00020 {
00021 }
00022 
00023 int
00024 TAO_ECG_Complex_Address_Server::init (const char *arg)
00025 {
00026   ACE_CString key_string;
00027   ACE_CString mcast_string;
00028 
00029   // Our position in parsing initialization string.
00030   const char * data = arg;
00031 
00032   // Parse initialization string until we reach the end.
00033   while (*data != '\0')
00034     {
00035       // Extract lookup value (it is followed by '@').
00036       const char * location = 0;
00037       location = ACE_OS::strchr (data, '@');
00038       if (!location)
00039         {
00040           ACE_ERROR_RETURN ((LM_ERROR,
00041                                         "Unable to initialize address "
00042                                         "server: cannot find <@> separator "
00043                                         "in initialization string "
00044                                         "as expected\n"),
00045                             -1);
00046         }
00047       size_t len = location - data;
00048       key_string.set (data, len, 1);
00049       data += len + 1;
00050 
00051       // Extract mcast address to be mapped to just extracted lookup
00052       // value.
00053       location = 0;
00054       location = ACE_OS::strchr (data, ' ');
00055       if (location)
00056         {
00057           len = location - data;
00058           mcast_string.set (data, len, 1);
00059           data += len + 1;
00060         }
00061       else
00062         {
00063           // This must be the last entry in the mapping.
00064           len = ACE_OS::strlen (data);
00065           mcast_string.set (data, len, 1);
00066           data += len;
00067         }
00068 
00069       // Add new entry to the mapping.
00070       if (this->add_entry (key_string.c_str (),
00071                            mcast_string.c_str ()) == -1)
00072         return -1;
00073     }
00074   return 0;
00075 }
00076 
00077 int
00078 TAO_ECG_Complex_Address_Server::add_entry (const char * key,
00079                                            const char * mcast_addr)
00080 {
00081   // Check whether this is the default mcast address.
00082   if (ACE_OS::strlen (key) == 1
00083       && *key == '*')
00084     {
00085       if (this->default_addr_.set (mcast_addr) == -1)
00086         ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: invalid "
00087                                      "mcast address specified: %s.\n",
00088                            mcast_addr),
00089                           -1);
00090       return 0;
00091     }
00092 
00093   // Convert strings to values.
00094   char * endptr = 0;
00095   CORBA::Long header_value = ACE_OS::strtol (key, &endptr, 0);
00096   if (*endptr != '\0')
00097     {
00098       ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: invalid "
00099                                    "source/type specified: %s.\n",
00100                          key),
00101                         -1);
00102     }
00103 
00104   ACE_INET_Addr addr;
00105   if (addr.set (mcast_addr) == -1)
00106     {
00107       ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: invalid "
00108                                    "mcast address specified: %s.\n",
00109                          mcast_addr),
00110                          -1);
00111     }
00112 
00113   if (this->mcast_mapping_.bind (header_value, addr) == -1)
00114     {
00115       ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: error adding "
00116                                    "new entry to the mapping.\n"),
00117                         -1);
00118     }
00119 
00120   return 0;
00121 }
00122 
00123 
00124 void
00125 TAO_ECG_Complex_Address_Server::get_addr (
00126                          const RtecEventComm::EventHeader& header,
00127                          RtecUDPAdmin::UDP_Addr_out addr)
00128 {
00129   CORBA::Long key;
00130   if (this->is_source_mapping_)
00131     key = header.source;
00132   else
00133     key = header.type;
00134 
00135   MAP::ENTRY * mapping_entry = 0;
00136   if (this->mcast_mapping_.find (key, mapping_entry) == -1)
00137     {
00138       // Key was not found in the mapping.  Use default.
00139 #if defined (ACE_HAS_IPV6)
00140       if (this->default_addr_.get_type() == PF_INET6)
00141         throw CORBA::DATA_CONVERSION(0, CORBA::COMPLETED_YES);
00142 #endif /* ACE_HAS_IPV6 */
00143       addr.ipaddr = this->default_addr_.get_ip_address ();
00144       addr.port = this->default_addr_.get_port_number ();
00145     }
00146   else
00147     {
00148 #if defined (ACE_HAS_IPV6)
00149       if (mapping_entry->int_id_.get_type() == PF_INET6)
00150         throw CORBA::DATA_CONVERSION(0, CORBA::COMPLETED_YES);
00151 #endif /* ACE_HAS_IPV6 */
00152       addr.ipaddr = mapping_entry->int_id_.get_ip_address ();
00153       addr.port = mapping_entry->int_id_.get_port_number ();
00154     }
00155 }
00156 
00157 void
00158 TAO_ECG_Complex_Address_Server::get_address (
00159                          const RtecEventComm::EventHeader& header,
00160                          RtecUDPAdmin::UDP_Address_out addr)
00161 {
00162   CORBA::Long key;
00163   if (this->is_source_mapping_)
00164     key = header.source;
00165   else
00166     key = header.type;
00167 
00168   MAP::ENTRY * mapping_entry = 0;
00169   ACE_INET_Addr &src_addr =
00170     (this->mcast_mapping_.find (key, mapping_entry) == -1) ?
00171     this->default_addr_ : mapping_entry->int_id_;
00172 #if defined (ACE_HAS_IPV6)
00173   if (src_addr.get_type() == PF_INET6)
00174     {
00175       RtecUDPAdmin::UDP_Addr_v6 v6;
00176       sockaddr_in6 *in6 =
00177         reinterpret_cast<sockaddr_in6 *>(src_addr.get_addr());
00178       ACE_OS::memcpy (v6.ipaddr,&in6->sin6_addr,16);
00179       v6.port = src_addr.get_port_number();
00180       addr.v6_addr (v6);
00181       return;
00182     }
00183 #endif /* ACE_HAS_IPV6 */
00184   RtecUDPAdmin::UDP_Addr v4;
00185   v4.ipaddr = src_addr.get_ip_address();
00186   v4.port = src_addr.get_port_number();
00187   addr.v4_addr (v4);
00188 }
00189 
00190 
00191 void
00192 TAO_ECG_Complex_Address_Server::dump_content (void)
00193 {
00194   ACE_DEBUG ((LM_DEBUG, "Default address: %s:%d\n",
00195               this->default_addr_.get_host_addr (),
00196               this->default_addr_.get_port_number ()));
00197 
00198   for (MAP::iterator iter = this->mcast_mapping_.begin ();
00199        iter != this->mcast_mapping_.end ();
00200        iter++)
00201     {
00202       MAP::ENTRY & entry = *iter;
00203       ACE_DEBUG ((LM_DEBUG, "%d --> %s:%d\n",
00204                   entry.ext_id_,
00205                   this->default_addr_.get_host_addr (),
00206                   this->default_addr_.get_port_number ()));
00207     }
00208 }
00209 
00210 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:44:06 2010 for TAO_RTEvent by  doxygen 1.4.7