summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/bin/Naming_Service/svr.cpp
blob: 2921db2be049f540e43b98197bb87617c490bbdb (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
171
//
// $Id$
//
#include <iostream.h>
#include "CosNaming_i.h"
#include "svr.h"

// This is a startup for the naming server.
// This is used for testing of the Naming Service.

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

IOR_Multicast::IOR_Multicast (char * ior,
			      u_short port, 
			      const char *mcast_addr)
  : mcast_addr_ (port, mcast_addr),
    ior_ (ior),
    response_ (response_addr_)
{
  // Use ACE_SOCK_Dgram_Mcast factory to subscribe to multicast group.
  if (this->mcast_dgram_.subscribe (this->mcast_addr_) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "subscribe"));
}

// destructor 

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

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

int
IOR_Multicast::handle_input (ACE_HANDLE)
{
  ssize_t retcode = 
    this->mcast_dgram_.recv (this->buf_, 
			     BUFSIZ, 
			     this->remote_addr_);
  
  if (retcode == -1)
    return -1;

  ACE_DEBUG ((LM_DEBUG, "Received multicast.\n"));
  
  // @@ validate data string received is from a valid client here
  // @@ Probably not needed

  if (retcode != sizeof (CORBA::Short))
    {
      ACE_ERROR_RETURN ((LM_ERROR, 
			 "Reply to multicast not sent. Received %d bytes, expected %d.", 
			 retcode, 
			 sizeof(CORBA::Short)), -1);
    }

  // convert port number received to network byte order.
  CORBA::Short reply_port_number = ntohs (*(CORBA::Short *)this->buf_);

  // set port number to reply.
  this->remote_addr_.set_port_number (reply_port_number);
  
  // send the object reference for the naming service
  retcode = response_.send (this->ior_, 
			    ACE_OS::strlen (this->ior_) + 1, 
			    this->remote_addr_, 
			    0);

  ACE_DEBUG ((LM_DEBUG, 
	      "ior_ '%s' sent through port %u.\nretcode=%d\n", 
	      this->ior_, 
	      this->remote_addr_.get_port_number (), 
	      retcode));

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

  return 0;
}


int
main (int argc, char ** argv)
{
  CORBA::Environment env;
  char *orb_name = "internet";

  CORBA::ORB_ptr orb_ptr = CORBA::ORB_init (argc, 
					    argv, 
					    orb_name, 
					    env);

  if (env.exception () != 0)
    {
      env.print_exception ("ORB init");
      return 1;
    }

  // Initialize the Object Adapter
  CORBA::POA_ptr oa_ptr = orb_ptr->POA_init (argc, argv, "POA");

  if (oa_ptr == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
		       " (%P|%t) Unable to initialize the POA.\n"),
		      1);

  // Create a naming context object.
  NS_NamingContext *naming_context = new NS_NamingContext;
  
  // Stringify the objref we'll be implementing, and print it to
  // stdout.  Someone will take that string and give it to a
  // client.  Then release the object.
  CORBA::String str;
  str = orb_ptr->object_to_string (naming_context, env);

  if (env.exception () != 0)
    {
      env.print_exception ("object2string");
      return 1;
    }

  ACE_DEBUG ((LM_DEBUG, "listening as object '%s'\n", str));

#if defined (ACE_HAS_IP_MULTICAST)
  // get reactor instance from TAO
  ACE_Reactor *reactor = TAO_ORB_Core_instance ()->reactor ();
  
  // First, see if the user has given us a multicast port number
  // for the name service on the command-line;
  u_short port = TAO_ORB_Core_instance ()->orb_params ()->name_service_port ();
  if (port == 0)
    {
      const char *port_number = ACE_OS::getenv ("NameServicePort");

      if (port_number != 0)
	port = ACE_OS::atoi (port_number);
    }

  if (port == 0)
    port = TAO_DEFAULT_NAME_SERVER_REQUEST_PORT;

  // Instantiate a server which will receive requests for an ior
  IOR_Multicast ior_multicast (str,
			       port,
			       ACE_DEFAULT_MULTICAST_ADDR);

  // register event handler for the ior multicast.
  if (reactor->register_handler (&ior_multicast,
				 ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n%a", "register_handler", 1));
    
  ACE_DEBUG ((LM_DEBUG, "The multicast server setup is done.\n"));
#endif /* ACE_HAS_IP_MULTICAST */

  // Handle requests for this object until we're killed, or one of the
  // methods asks us to exit.
  if (orb_ptr->run () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "run"), -1);

  return 0;
}