summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Event/ECG_Complex_Address_Server.cpp
blob: 3091fb79acfe88da27adff1b53a1ccdb277943f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// $Id$

#include "orbsvcs/Event/ECG_Complex_Address_Server.h"
#include "ace/SString.h"
#include "ace/streams.h"

#if !defined(__ACE_INLINE__)
#include "orbsvcs/Event/ECG_Complex_Address_Server.i"
#endif /* __ACE_INLINE__ */

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_ECG_Complex_Address_Server::TAO_ECG_Complex_Address_Server (
                                              int is_source_mapping)
  : is_source_mapping_ (is_source_mapping)
{
}

TAO_ECG_Complex_Address_Server::~TAO_ECG_Complex_Address_Server (void)
{
}

int
TAO_ECG_Complex_Address_Server::init (const char *arg)
{
  ACE_CString key_string;
  ACE_CString mcast_string;

  // Our position in parsing initialization string.
  const char * data = arg;

  // Parse initialization string until we reach the end.
  while (*data != '\0')
    {
      // Extract lookup value (it is followed by '@').
      const char * location = 0;
      location = ACE_OS::strchr (data, '@');
      if (!location)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                                        "Unable to initialize address "
                                        "server: cannot find <@> separator "
                                        "in initialization string "
                                        "as expected\n"),
                            -1);
        }
      size_t len = location - data;
      key_string.set (data, len, 1);
      data += len + 1;

      // Extract mcast address to be mapped to just extracted lookup
      // value.
      location = 0;
      location = ACE_OS::strchr (data, ' ');
      if (location)
        {
          len = location - data;
          mcast_string.set (data, len, 1);
          data += len + 1;
        }
      else
        {
          // This must be the last entry in the mapping.
          len = ACE_OS::strlen (data);
          mcast_string.set (data, len, 1);
          data += len;
        }

      // Add new entry to the mapping.
      if (this->add_entry (key_string.c_str (),
                           mcast_string.c_str ()) == -1)
        return -1;
    }
  return 0;
}

int
TAO_ECG_Complex_Address_Server::add_entry (const char * key,
                                           const char * mcast_addr)
{
  // Check whether this is the default mcast address.
  if (ACE_OS::strlen (key) == 1
      && *key == '*')
    {
      if (this->default_addr_.set (mcast_addr) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: invalid "
                                     "mcast address specified: %s.\n",
                           mcast_addr),
                          -1);
      return 0;
    }

  // Convert strings to values.
  char * endptr = 0;
  CORBA::Long header_value = ACE_OS::strtol (key, &endptr, 0);
  if (*endptr != '\0')
    {
      ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: invalid "
                                   "source/type specified: %s.\n",
                         key),
                        -1);
    }

  ACE_INET_Addr addr;
  if (addr.set (mcast_addr) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: invalid "
                                   "mcast address specified: %s.\n",
                         mcast_addr),
                         -1);
    }

  if (this->mcast_mapping_.bind (header_value, addr) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR, "Unable to initialize: error adding "
                                   "new entry to the mapping.\n"),
                        -1);
    }

  return 0;
}


void
TAO_ECG_Complex_Address_Server::get_addr (
                         const RtecEventComm::EventHeader& header,
                         RtecUDPAdmin::UDP_Addr_out addr
                         ACE_ENV_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  CORBA::Long key;
  if (this->is_source_mapping_)
    key = header.source;
  else
    key = header.type;

  MAP::ENTRY * mapping_entry = 0;
  if (this->mcast_mapping_.find (key, mapping_entry) == -1)
    {
      // Key was not found in the mapping.  Use default.
      addr.ipaddr = this->default_addr_.get_ip_address ();
      addr.port = this->default_addr_.get_port_number ();
    }
  else
    {
      addr.ipaddr = mapping_entry->int_id_.get_ip_address ();
      addr.port = mapping_entry->int_id_.get_port_number ();
    }
}

void
TAO_ECG_Complex_Address_Server::dump_content (void)
{
  ACE_DEBUG ((LM_DEBUG, "Default address: %s:%d\n",
              this->default_addr_.get_host_addr (),
              this->default_addr_.get_port_number ()));

  for (MAP::iterator iter = this->mcast_mapping_.begin ();
       iter != this->mcast_mapping_.end ();
       iter++)
    {
      MAP::ENTRY & entry = *iter;
      ACE_DEBUG ((LM_DEBUG, "%d --> %s:%d\n",
                  entry.ext_id_,
                  this->default_addr_.get_host_addr (),
                  this->default_addr_.get_port_number ()));
    }
}

TAO_END_VERSIONED_NAMESPACE_DECL