summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/IOR_Multicast.cpp
blob: 408d61ef141da89e879f6b8801046e1272415c6f (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
// $Id$

#include "IOR_Multicast.h"

ACE_RCSID(orbsvcs, IOR_Multicast, "$Id$")

ACE_HANDLE
TAO_IOR_Multicast::get_handle (void) const
{
  return this->mcast_dgram_.get_handle ();
}

TAO_IOR_Multicast::TAO_IOR_Multicast (void)
  : service_id_ ((TAO_Service_ID) 0),
    ior_ (0)
{
}

TAO_IOR_Multicast::TAO_IOR_Multicast (const char *ior,
                                      u_short port,
                                      const char *mcast_addr,
                                      TAO_Service_ID service_id)
{
  if (this->init (ior,
                  port,
                  mcast_addr,
                  service_id) == -1)
    ACE_ERROR ((LM_ERROR,
                ASYS_TEXT ("%p\n"),
                ASYS_TEXT ("TAO_IOR_Multicast")));
}

// destructor

TAO_IOR_Multicast::~TAO_IOR_Multicast (void)
{
  this->mcast_dgram_.unsubscribe ();
}

int
TAO_IOR_Multicast::init (const char *ior,
                         u_short port,
                         const char *mcast_addr,
                         TAO_Service_ID service_id)
{
  this->service_id_ = service_id;
  this->mcast_addr_.set (port, mcast_addr);
  this->ior_ = ior;
  this->response_addr_.set ((u_short) 0);
  this->response_.open (this->response_addr_);

  // Use ACE_SOCK_Dgram_Mcast factory to subscribe to multicast group.
  if (this->mcast_dgram_.subscribe (this->mcast_addr_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "subscribe"),
                      -1);
  return 0;
}

int
TAO_IOR_Multicast::handle_timeout (const ACE_Time_Value &,
                                   const void *)
{
  return 0;
}

int
TAO_IOR_Multicast::handle_input (ACE_HANDLE)
{
  // mcast_info[0] == port.
  // mcast_info[1] == service id.
  ACE_UINT16 mcast_info[2];

  ACE_INET_Addr remote_addr;

  ssize_t result =
    // This call fills in the address of the sender.
    this->mcast_dgram_.recv (mcast_info,
                             sizeof (mcast_info),
                             remote_addr);

  if (result == -1)
    return -1;

  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) Received multicast.\n"));

  // Validate data string received is from a valid client here.

  if (result != sizeof (mcast_info))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Reply to multicast not sent. Received %d bytes, expected %d.",
                       result,
                       sizeof (mcast_info)),
                      -1);

  // Confirm that we were meant to respond to this request.
  mcast_info[1] = ACE_NTOHS (mcast_info[1]);

  if (mcast_info[1] == this->service_id_)
    {
      remote_addr.set_port_number (mcast_info[0], 0);

      // Send the object reference for the naming service

      // Wait to give client a chance to start receiving,
      // Send two times to provide better protection against packet
      // loss.
      // @@Vishal, a better (not hardcoded) 'policy' solution for this should be provided.
      for (int i = 0; i < 5; ++i)
        {
          ACE_OS::sleep (ACE_Time_Value (0, 10000));
          result = response_.send (this->ior_,
                                   ACE_OS::strlen (this->ior_),
                                   remote_addr,
                                   0);
        }

      if (TAO_debug_level > 0)
	ACE_DEBUG ((LM_DEBUG,
		    "(%P|%t) ior_: <%s>\n"
		    " sent through port %u to host %s.\n"
		    "result = %d\n",
		    this->ior_,
		    remote_addr.get_port_number (),
		    remote_addr.get_host_name (),
		    result));

      if (result == -1)
        return -1;
    }
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) Multicast was not for us."));

  return 0;
}