1 #include "orbsvcs/Log_Macros.h"
2 #include "Locator_Repository.h"
3 #include "ImR_Locator_i.h"
6 #include "tao/ORB_Core.h"
7 #include "tao/default_ports.h"
8 #include "ace/Read_Buffer.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_strings.h"
11 #include "ace/OS_NS_ctype.h"
12 #include "ace/OS_NS_unistd.h"
13 #include "ace/Vector_T.h"
15 Locator_Repository::Locator_Repository (const Options
& opts
,
18 orb_(CORBA::ORB::_duplicate(orb
)),
23 Locator_Repository::~Locator_Repository ()
29 Locator_Repository::shutdown ()
31 // default impl - no op
35 Locator_Repository::init (PortableServer::POA_ptr root_poa
,
36 PortableServer::POA_ptr imr_poa
,
39 this->imr_ior_
= this_ior
;
40 int err
= init_repo(imr_poa
);
46 // Activate the two poa managers
47 PortableServer::POAManager_var poaman
=
48 root_poa
->the_POAManager ();
50 poaman
= imr_poa
->the_POAManager ();
57 Locator_Repository::report_ior (PortableServer::POA_ptr
)
59 if (this->registered_
)
61 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
62 ACE_TEXT ("ERROR: Repository already reported IOR\n")), -1);
65 if (this->opts_
.debug () > 0)
67 ORBSVCS_DEBUG ((LM_INFO
, ACE_TEXT ("(%P|%t) ImR: report_ior <%C>\n"),
68 this->imr_ior_
.in ()));
71 // Register the ImR for use with INS
72 CORBA::Object_var obj
= this->orb_
->resolve_initial_references ("AsyncIORTable");
73 IORTable::Table_var ior_table
= IORTable::Table::_narrow (obj
.in ());
74 ACE_ASSERT (! CORBA::is_nil (ior_table
.in ()));
76 ior_table
->rebind ("ImplRepoService", this->imr_ior_
.in());
77 ior_table
->rebind ("ImR", this->imr_ior_
.in());
79 // Set up multicast support (if enabled)
80 if (this->opts_
.multicast ())
82 ACE_Reactor
* reactor
= this->orb_
->orb_core ()->reactor ();
83 if (this->setup_multicast (reactor
, this->imr_ior_
.in ()) != 0)
87 // We write the ior file last so that the tests can know we are ready.
88 if (this->opts_
.ior_filename ().length () > 0)
90 FILE* orig_fp
= ACE_OS::fopen(this->opts_
.ior_filename ().c_str(),
93 bool write_data
= true;
96 ACE_Read_Buffer
reader (orig_fp
, false);
98 char* string
= reader
.read ();
103 (ACE_OS::strcasecmp (string
, this->imr_ior_
.in ()) != 0);
104 reader
.alloc ()->free (string
);
106 ACE_OS::fclose (orig_fp
);
111 FILE* fp
= ACE_OS::fopen (this->opts_
.ior_filename ().c_str (),
115 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
116 ACE_TEXT ("(%P|%t) ImR: Could not open file: %s\n"),
117 this->opts_
.ior_filename ().c_str ()), -1);
119 ACE_OS::fprintf (fp
, "%s", this->imr_ior_
.in ());
130 Locator_Repository::recover_ior ()
132 if (this->registered_
)
134 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
135 ACE_TEXT ("ERROR: Repository already registered IOR. ")
136 ACE_TEXT ("recover_ior should not be called.\n")), -1);
139 if (this->opts_
.debug () > 0)
141 ORBSVCS_DEBUG ((LM_INFO
, ACE_TEXT ("recover_ior <%C>\n"),
142 this->opts_
.ior_filename ().c_str()));
145 // Load the IOR from the specified file if it is available.
146 const ACE_TString
& combined_ior_file
= this->opts_
.ior_filename ();
148 // Check if the file exists. If not, then return 1 indicating
149 // we cannot recover our state.
150 if (ACE_OS::access (combined_ior_file
.c_str (), F_OK
) != 0)
154 ACE_TString combined_ior
= ACE_TEXT ("file://") + combined_ior_file
;
156 CORBA::Object_var combined_obj
=
157 this->orb_
->string_to_object (combined_ior
.c_str());
159 if (!CORBA::is_nil (combined_obj
.in ()))
161 // Convert the object back into an IOR string to store in the
162 // imr_ior_ attribute.
163 this->imr_ior_
= this->orb_
->object_to_string (combined_obj
.in ());
166 catch (const CORBA::Exception
& ex
)
168 ex
._tao_print_exception ("ImR: Attempting to read combined_ior for ImR_Locator\n");
172 // Register the ImR for use with INS
173 CORBA::Object_var obj
= this->orb_
->resolve_initial_references ("AsyncIORTable");
174 IORTable::Table_var ior_table
= IORTable::Table::_narrow (obj
.in ());
175 ACE_ASSERT (! CORBA::is_nil (ior_table
.in ()));
177 ior_table
->bind ("ImplRepoService", this->imr_ior_
.in());
178 ior_table
->bind ("ImR", this->imr_ior_
.in());
180 // Set up multicast support (if enabled)
181 if (this->opts_
.multicast ())
183 ACE_Reactor
* reactor
= this->orb_
->orb_core ()->reactor ();
184 if (this->setup_multicast (reactor
, this->imr_ior_
.in ()) != 0)
194 Locator_Repository::setup_multicast (ACE_Reactor
* reactor
, const char* ior
)
196 ACE_ASSERT (reactor
!= 0);
197 #if defined (ACE_HAS_IP_MULTICAST)
199 TAO_ORB_Core
* core
= TAO_ORB_Core_instance ();
200 // See if the -ORBMulticastDiscoveryEndpoint option was specified.
201 ACE_CString
mde (core
->orb_params ()->mcast_discovery_endpoint ());
203 if (mde
.length () != 0)
205 if (this->ior_multicast_
.init (ior
,
206 mde
.c_str (), TAO_SERVICEID_IMPLREPOSERVICE
) == -1)
213 // Port can be specified as param, env var, or default
215 core
->orb_params ()->service_port (TAO::MCAST_IMPLREPOSERVICE
);
218 // Check environment var. for multicast port.
219 const char* port_number
= ACE_OS::getenv ("ImplRepoServicePort");
221 if (port_number
!= 0)
222 port
= static_cast<CORBA::UShort
> (ACE_OS::atoi (port_number
));
225 port
= TAO_DEFAULT_IMPLREPO_SERVER_REQUEST_PORT
;
227 if (this->ior_multicast_
.init (ior
, port
,
228 ACE_DEFAULT_MULTICAST_ADDR
, TAO_SERVICEID_IMPLREPOSERVICE
) == -1)
234 // Register event handler for the ior multicast.
235 if (reactor
->register_handler (&this->ior_multicast_
,
236 ACE_Event_Handler::READ_MASK
) == -1)
238 if (this->opts_
.debug() > 0)
239 ORBSVCS_DEBUG ((LM_DEBUG
, "ImR: cannot register Event handler\n"));
242 #else /* ACE_HAS_IP_MULTICAST*/
243 ACE_UNUSED_ARG (reactor
);
244 ACE_UNUSED_ARG (ior
);
245 #endif /* ACE_HAS_IP_MULTICAST*/
250 Locator_Repository::teardown_multicast ()
252 ACE_Reactor
* r
= ior_multicast_
.reactor ();
254 r
->remove_handler (&ior_multicast_
, ACE_Event_Handler::READ_MASK
);
255 ior_multicast_
.reactor (0);
260 Locator_Repository::multicast() const
262 return this->ior_multicast_
.reactor () != 0;
266 Locator_Repository::lcase (const ACE_CString
& s
)
269 for (size_t i
= 0; i
< ret
.length (); ++i
)
271 ret
[i
] = static_cast<char> (ACE_OS::ace_tolower (s
[i
]));
277 Locator_Repository::unregister_if_address_reused (const ACE_CString
& fqname
,
278 const char* partial_ior
,
279 ImR_Locator_i
* imr_locator
)
282 if (this->opts_
.debug() > 0)
284 ORBSVCS_DEBUG ((LM_DEBUG
,
285 ACE_TEXT ("(%P|%t) ImR: checking reuse address ")
286 ACE_TEXT ("for server <%C> ior <%C>\n"),
294 Server_Info::fqname_to_key (fqname
.c_str(), key
);
295 servers ().find (key
, si
);
296 ACE_CString poa_name
;
297 ACE_CString server_id
;
300 Server_Info::parse_id (fqname
.c_str(), server_id
, poa_name
);
304 server_id
= si
->active_info ()->server_id
;
305 poa_name
= si
->active_info ()->poa_name
;
308 Locator_Repository::SIMap::ENTRY
* sientry
= 0;
309 Locator_Repository::SIMap::ITERATOR
siit (servers ());
310 for (; siit
.next (sientry
); siit
.advance() )
312 Server_Info
*info
= sientry
->int_id_
->active_info ();
314 if (this->opts_
.debug() > 0)
316 ORBSVCS_DEBUG ((LM_DEBUG
,
317 ACE_TEXT ("(%P|%t) ImR: iterating - registered server")
318 ACE_TEXT ("<%C:%C> key <%C> ior <%C>\n"), info
->server_id
.c_str(),
319 info
->poa_name
.c_str (), info
->key_name_
.c_str(), info
->partial_ior
.c_str ()));
321 bool same_server
= info
->server_id
== server_id
;
322 if (same_server
&& (server_id
.length () == 0))
324 same_server
= info
->poa_name
== poa_name
;
326 if (info
->partial_ior
== partial_ior
&& !same_server
)
328 if (this->opts_
.debug() > 0)
330 ORBSVCS_DEBUG ((LM_DEBUG
,
331 ACE_TEXT ("(%P|%t) ImR: reuse address <%C> so remove server <%C>\n"),
332 info
->partial_ior
.c_str (), info
->poa_name
.c_str ()));
334 imr_locator
->pinger ().remove_server (info
->key_name_
.c_str(), info
->pid
);
335 AsyncAccessManager_ptr aam
= imr_locator
->find_aam (info
->key_name_
.c_str ());
338 aam
->server_is_shutting_down ();
340 info
->reset_runtime ();
348 Locator_Repository::add_server
349 (const ACE_CString
& fqname
,
350 const ImplementationRepository::StartupOptions
& options
)
355 options
.activator
.in (),
356 options
.command_line
.in (),
358 options
.working_directory
.in (),
360 options
.start_limit
),
362 return this->add_server_i (si
);
366 Locator_Repository::add_server
367 (const ACE_CString
& fqname
,
368 const ACE_CString
& partial_ior
,
369 const ACE_CString
& ior
,
370 ImplementationRepository::ServerObject_ptr srvobj
)
374 Server_Info (fqname
, "", "",
375 ImplementationRepository::EnvironmentList (),
376 "", ImplementationRepository::NORMAL
,
377 1, partial_ior
, ior
, srvobj
),
379 return this->add_server_i (si
);
383 Locator_Repository::add_server_i (Server_Info
*si
)
385 Server_Info_Ptr
info(si
);
387 int err
= sync_load ();
392 err
= servers ().bind (si
->key_name_
, info
);
398 this->persistent_update(info
, true);
403 Locator_Repository::add_activator (const ACE_CString
& name
,
404 const CORBA::Long token
,
405 const ACE_CString
& ior
,
406 ImplementationRepository::Activator_ptr act
)
408 int err
= sync_load ();
414 Activator_Info_Ptr
info (new Activator_Info (name
, token
, ior
, act
));
416 err
= activators ().bind (lcase (name
), info
);
421 this->persistent_update(info
, true);
426 Locator_Repository::update_server (const Server_Info_Ptr
& info
)
428 return this->persistent_update(info
, false);
432 Locator_Repository::update_activator (const Activator_Info_Ptr
& info
)
434 return this->persistent_update(info
, false);
438 Locator_Repository::notify_remote_access (const char *,
439 ImplementationRepository::AAM_Status
)
444 Locator_Repository::find_by_poa (const ACE_CString
& name
)
446 Locator_Repository::SIMap::ENTRY
* sientry
= 0;
447 Locator_Repository::SIMap::ITERATOR
siit (servers ());
448 for (; siit
.next (sientry
); siit
.advance() )
450 Server_Info_Ptr
& info
= sientry
->int_id_
;
451 if (info
->poa_name
== name
)
456 return Server_Info_Ptr();
460 Locator_Repository::get_active_server (const ACE_CString
& name
, int pid
)
465 if (name
.length() == 0)
469 Server_Info::fqname_to_key (name
.c_str(), key
);
470 servers ().find (key
, si
);
473 if (this->opts_
.debug() > 5)
475 ORBSVCS_DEBUG ((LM_DEBUG
,
476 ACE_TEXT ("(%P|%t) ImR: get_active_server could not find <%C>\n"),
479 si
= find_by_poa (key
);
482 if (name
.find ("JACORB:") == ACE_CString::npos
)
484 ACE_CString
jo_key ("JACORB:");
485 ACE_CString::size_type
const pos
= name
.find (':');
486 if (pos
== ACE_CString::npos
)
492 jo_key
+= name
.substring (0, pos
);
494 jo_key
+= name
.substring (pos
+1);
496 return this->get_active_server (jo_key
, pid
);
505 if (pid
!= 0 && si
->pid
!= 0 && si
->pid
!= pid
)
507 if (this->opts_
.debug() > 5)
509 ORBSVCS_DEBUG ((LM_DEBUG
,
510 ACE_TEXT ("(%P|%t) ImR: get_active_server could not")
511 ACE_TEXT (" find <%C> pid <%d> != <%d>\n"),
512 name
.c_str(), pid
, si
->pid
));
520 Locator_Repository::remove_server (const ACE_CString
& name
,
521 ImR_Locator_i
* imr_locator
)
523 int const err
= sync_load ();
529 this->servers().find (name
, si
);
530 int const ret
= this->servers().unbind (name
);
536 if (!si
->alt_info_
.null ())
538 // name is a peer to another and must be removed from other list
540 for (CORBA::ULong i
= 0; i
< si
->alt_info_
->peers
.length(); i
++)
542 if (!found
&& si
->poa_name
== si
->alt_info_
->peers
[i
])
549 si
->alt_info_
->peers
[i
-1] = si
->alt_info_
->peers
[i
];
552 si
->alt_info_
->peers
.length (si
->alt_info_
->peers
.length() - 1);
554 else if (si
->peers
.length () > 0)
556 for (CORBA::ULong i
= 0; i
< si
->peers
.length(); i
++)
559 ACE_CString
const peer (si
->peers
[i
]);
560 Server_Info::gen_key (si
->server_id
, peer
, key
);
563 this->servers().find (key
, si2
);
564 imr_locator
->destroy_poa (si2
->poa_name
);
565 this->servers ().unbind (key
);
566 this->persistent_remove (key
, false);
569 return persistent_remove (name
, false);
573 Locator_Repository::link_peers (Server_Info_Ptr base
,
574 const CORBA::StringSeq p
)
577 CORBA::ULong
const len
= base
->peers
.length();
578 base
->peers
.length (len
+ p
.length());
579 for (CORBA::ULong i
= 0; i
< p
.length(); i
++)
581 base
->peers
[len
+ i
] = p
[i
];
583 ACE_CString
peer(p
[i
]);
585 Server_Info (base
->server_id
, peer
, base
->is_jacorb
, base
),
587 Server_Info_Ptr
sip(si
);
588 servers ().bind (si
->key_name_
, sip
);
589 this->persistent_update (sip
, true);
592 this->persistent_update (base
, true);
597 // -------------------------------------------------------------------------------------
600 Locator_Repository::get_activator (const ACE_CString
& name
)
604 Activator_Info_Ptr
activator (0);
605 activators ().find (lcase (name
), activator
);
610 Locator_Repository::has_activator (const ACE_CString
& name
)
612 Activator_Info_Ptr
activator (0);
613 return activators().find (lcase (name
), activator
) == 0;
617 Locator_Repository::remove_activator (const ACE_CString
& name
)
619 int const err
= sync_load ();
625 int const ret
= activators().unbind (lcase(name
));
631 Locator_Repository::SIMap::ENTRY
* sientry
= 0;
632 Locator_Repository::SIMap::ITERATOR
siit (servers ());
633 for (; siit
.next (sientry
); siit
.advance() )
635 Server_Info
*info
= sientry
->int_id_
->active_info ();
637 if (info
->death_notify
&& info
->activator
== name
)
639 info
->death_notify
= false;
643 return persistent_remove(name
, true);
646 Locator_Repository::SIMap
&
647 Locator_Repository::servers ()
649 return server_infos_
;
652 const Locator_Repository::SIMap
&
653 Locator_Repository::servers () const
655 return server_infos_
;
658 Locator_Repository::AIMap
&
659 Locator_Repository::activators ()
661 return activator_infos_
;
664 const Locator_Repository::AIMap
&
665 Locator_Repository::activators () const
667 return activator_infos_
;
671 Locator_Repository::sync_load ()
673 // nothing more to do for default server/activator load
678 Locator_Repository::registered () const
680 return this->registered_
;
683 //--------------------------------------------------------------------------
685 No_Backing_Store::No_Backing_Store (const Options
& opts
,
687 : Locator_Repository(opts
, orb
)
691 No_Backing_Store::~No_Backing_Store ()
696 No_Backing_Store::repo_mode () const
698 return ACE_TEXT ("Disabled");
702 No_Backing_Store::init_repo (PortableServer::POA_ptr
)
704 // nothing more to do for no backing store init
709 No_Backing_Store::persistent_update (const Server_Info_Ptr
& , bool )
711 // nothing more to do for no backing store update
716 No_Backing_Store::persistent_update (const Activator_Info_Ptr
& , bool )
718 // nothing more to do for no backing store update
723 No_Backing_Store::persistent_remove (const ACE_CString
& , bool )
725 // nothing more to do for no backing store remove