00001
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.i"
00009 #endif
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
00030 const char * data = arg;
00031
00032
00033 while (*data != '\0')
00034 {
00035
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
00052
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
00064 len = ACE_OS::strlen (data);
00065 mcast_string.set (data, len, 1);
00066 data += len;
00067 }
00068
00069
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
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
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 ACE_ENV_ARG_DECL_NOT_USED)
00129 ACE_THROW_SPEC ((CORBA::SystemException))
00130 {
00131 CORBA::Long key;
00132 if (this->is_source_mapping_)
00133 key = header.source;
00134 else
00135 key = header.type;
00136
00137 MAP::ENTRY * mapping_entry = 0;
00138 if (this->mcast_mapping_.find (key, mapping_entry) == -1)
00139 {
00140
00141 addr.ipaddr = this->default_addr_.get_ip_address ();
00142 addr.port = this->default_addr_.get_port_number ();
00143 }
00144 else
00145 {
00146 addr.ipaddr = mapping_entry->int_id_.get_ip_address ();
00147 addr.port = mapping_entry->int_id_.get_port_number ();
00148 }
00149 }
00150
00151 void
00152 TAO_ECG_Complex_Address_Server::dump_content (void)
00153 {
00154 ACE_DEBUG ((LM_DEBUG, "Default address: %s:%d\n",
00155 this->default_addr_.get_host_addr (),
00156 this->default_addr_.get_port_number ()));
00157
00158 for (MAP::iterator iter = this->mcast_mapping_.begin ();
00159 iter != this->mcast_mapping_.end ();
00160 iter++)
00161 {
00162 MAP::ENTRY & entry = *iter;
00163 ACE_DEBUG ((LM_DEBUG, "%d --> %s:%d\n",
00164 entry.ext_id_,
00165 this->default_addr_.get_host_addr (),
00166 this->default_addr_.get_port_number ()));
00167 }
00168 }
00169
00170 TAO_END_VERSIONED_NAMESPACE_DECL