Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / TAO / orbsvcs / ImplRepo_Service / Locator_Repository.cpp
blob29b7d010aaf8f20d079c32da38129e82f71d8227
1 #include "orbsvcs/Log_Macros.h"
2 #include "Locator_Repository.h"
3 #include "ImR_Locator_i.h"
5 #include "utils.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,
16 CORBA::ORB_ptr orb)
17 : opts_ (opts),
18 orb_(CORBA::ORB::_duplicate(orb)),
19 registered_(false)
23 Locator_Repository::~Locator_Repository ()
25 teardown_multicast();
28 void
29 Locator_Repository::shutdown ()
31 // default impl - no op
34 int
35 Locator_Repository::init (PortableServer::POA_ptr root_poa,
36 PortableServer::POA_ptr imr_poa,
37 const char* this_ior)
39 this->imr_ior_ = this_ior;
40 int err = init_repo(imr_poa);
41 if (err != 0)
43 return err;
46 // Activate the two poa managers
47 PortableServer::POAManager_var poaman =
48 root_poa->the_POAManager ();
49 poaman->activate ();
50 poaman = imr_poa->the_POAManager ();
51 poaman->activate ();
53 return err;
56 int
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)
84 return -1;
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(),
91 ACE_TEXT("r"));
93 bool write_data = true;
94 if (orig_fp != 0)
96 ACE_Read_Buffer reader (orig_fp, false);
98 char* string = reader.read ();
100 if (string != 0)
102 write_data =
103 (ACE_OS::strcasecmp (string, this->imr_ior_.in ()) != 0);
104 reader.alloc ()->free (string);
106 ACE_OS::fclose (orig_fp);
109 if (write_data)
111 FILE* fp = ACE_OS::fopen (this->opts_.ior_filename ().c_str (),
112 ACE_TEXT("w"));
113 if (fp == 0)
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 ());
120 ACE_OS::fclose (fp);
124 registered_ = true;
126 return 0;
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)
151 return -1;
153 try {
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");
169 return -1;
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)
185 return -1;
188 registered_ = true;
190 return 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)
208 return -1;
211 else
213 // Port can be specified as param, env var, or default
214 CORBA::UShort port =
215 core->orb_params ()->service_port (TAO::MCAST_IMPLREPOSERVICE);
216 if (port == 0)
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));
224 if (port == 0)
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)
230 return -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"));
240 return -1;
242 #else /* ACE_HAS_IP_MULTICAST*/
243 ACE_UNUSED_ARG (reactor);
244 ACE_UNUSED_ARG (ior);
245 #endif /* ACE_HAS_IP_MULTICAST*/
246 return 0;
249 void
250 Locator_Repository::teardown_multicast ()
252 ACE_Reactor* r = ior_multicast_.reactor ();
253 if (r != 0) {
254 r->remove_handler (&ior_multicast_, ACE_Event_Handler::READ_MASK);
255 ior_multicast_.reactor (0);
259 bool
260 Locator_Repository::multicast() const
262 return this->ior_multicast_.reactor () != 0;
265 ACE_CString
266 Locator_Repository::lcase (const ACE_CString& s)
268 ACE_CString ret(s);
269 for (size_t i = 0; i < ret.length (); ++i)
271 ret[i] = static_cast<char> (ACE_OS::ace_tolower (s[i]));
273 return ret;
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"),
287 fqname.c_str(),
288 partial_ior));
291 ACE_CString key;
292 Server_Info_Ptr si;
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;
298 if (si.null())
300 Server_Info::parse_id (fqname.c_str(), server_id, poa_name);
302 else
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 ());
336 if (!aam.is_nil())
338 aam->server_is_shutting_down ();
340 info->reset_runtime ();
344 return 0;
348 Locator_Repository::add_server
349 (const ACE_CString& fqname,
350 const ImplementationRepository::StartupOptions & options)
352 Server_Info *si = 0;
353 ACE_NEW_RETURN (si,
354 Server_Info (fqname,
355 options.activator.in (),
356 options.command_line.in (),
357 options.environment,
358 options.working_directory.in (),
359 options.activation,
360 options.start_limit),
361 -1);
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)
372 Server_Info *si = 0;
373 ACE_NEW_RETURN (si,
374 Server_Info (fqname, "", "",
375 ImplementationRepository::EnvironmentList (),
376 "", ImplementationRepository::NORMAL,
377 1, partial_ior, ior, srvobj),
378 -1);
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 ();
388 if (err != 0)
390 return err;
392 err = servers ().bind (si->key_name_, info);
393 if (err != 0)
395 return err;
398 this->persistent_update(info, true);
399 return 0;
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 ();
409 if (err != 0)
411 return err;
414 Activator_Info_Ptr info (new Activator_Info (name, token, ior, act));
416 err = activators ().bind (lcase (name), info);
417 if (err != 0)
419 return err;
421 this->persistent_update(info, true);
422 return 0;
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);
437 void
438 Locator_Repository::notify_remote_access (const char *,
439 ImplementationRepository::AAM_Status)
443 Server_Info_Ptr
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)
453 return info;
456 return Server_Info_Ptr();
459 Server_Info_Ptr
460 Locator_Repository::get_active_server (const ACE_CString& name, int pid)
462 sync_load ();
463 ACE_CString key;
464 Server_Info_Ptr si;
465 if (name.length() == 0)
467 return si;
469 Server_Info::fqname_to_key (name.c_str(), key);
470 servers ().find (key, si);
471 if (si.null())
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"),
477 name.c_str()));
479 si = find_by_poa (key);
480 if (si.null())
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)
488 jo_key += name;
490 else
492 jo_key += name.substring (0, pos);
493 jo_key += '/';
494 jo_key += name.substring (pos+1);
496 return this->get_active_server (jo_key, pid);
498 else
500 return si;
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));
514 si.reset ();
516 return si;
520 Locator_Repository::remove_server (const ACE_CString& name,
521 ImR_Locator_i* imr_locator)
523 int const err = sync_load ();
524 if (err != 0)
526 return err;
528 Server_Info_Ptr si;
529 this->servers().find (name, si);
530 int const ret = this->servers().unbind (name);
531 if (ret != 0)
533 return ret;
536 if (!si->alt_info_.null ())
538 // name is a peer to another and must be removed from other list
539 bool found = false;
540 for (CORBA::ULong i = 0; i < si->alt_info_->peers.length(); i++)
542 if (!found && si->poa_name == si->alt_info_->peers[i])
544 found = true;
545 continue;
547 if (found)
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++)
558 ACE_CString key;
559 ACE_CString const peer (si->peers[i]);
560 Server_Info::gen_key (si->server_id, peer, key);
562 Server_Info_Ptr si2;
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)
576 sync_load ();
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];
582 Server_Info *si = 0;
583 ACE_CString peer(p[i]);
584 ACE_NEW_RETURN (si,
585 Server_Info (base->server_id, peer, base->is_jacorb, base),
586 -1);
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);
594 return 0;
597 // -------------------------------------------------------------------------------------
599 Activator_Info_Ptr
600 Locator_Repository::get_activator (const ACE_CString& name)
602 sync_load ();
604 Activator_Info_Ptr activator (0);
605 activators ().find (lcase (name), activator);
606 return activator;
609 bool
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 ();
620 if (err != 0)
622 return err;
625 int const ret = activators().unbind (lcase(name));
626 if (ret != 0)
628 return ret;
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
674 return 0;
677 bool
678 Locator_Repository::registered () const
680 return this->registered_;
683 //--------------------------------------------------------------------------
685 No_Backing_Store::No_Backing_Store (const Options& opts,
686 CORBA::ORB_ptr orb)
687 : Locator_Repository(opts, orb)
691 No_Backing_Store::~No_Backing_Store ()
695 const ACE_TCHAR*
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
705 return 0;
709 No_Backing_Store::persistent_update (const Server_Info_Ptr& , bool )
711 // nothing more to do for no backing store update
712 return 0;
716 No_Backing_Store::persistent_update (const Activator_Info_Ptr& , bool )
718 // nothing more to do for no backing store update
719 return 0;
723 No_Backing_Store::persistent_remove (const ACE_CString& , bool )
725 // nothing more to do for no backing store remove
726 return 0;