Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / PortableInterceptors / PICurrent_NameService / ClientInitializer.cpp
blob26400a9741a39728445757f51e1b21455e0241b0
1 #include "ClientInitializer.h"
2 #include "ClientInterceptor.h"
3 #include "MessengerC.h"
4 #include "orbsvcs/CosNamingC.h"
5 #include "ace/OS_NS_unistd.h"
6 #include <iostream>
8 ClientInitializer::ClientInitializer (void)
9 : slot_ (0),
10 current_ (PortableInterceptor::Current::_nil())
14 void
15 ClientInitializer::pre_init (PortableInterceptor::ORBInitInfo_ptr)
19 void
20 ClientInitializer::post_init (PortableInterceptor::ORBInitInfo_ptr info)
22 // Find the Naming Service
23 CORBA::Object_var naming_obj =
24 info->resolve_initial_references("NameService");
25 CosNaming::NamingContext_var root =
26 CosNaming::NamingContext::_narrow(naming_obj.in());
27 if( CORBA::is_nil(root.in())) {
28 std::cerr << "Nil Naming Context reference" << std::endl;
29 ACE_ASSERT(false);
32 // Resolve the Messenger object
33 CosNaming::Name name;
34 name.length( 1 );
35 name[0].id = CORBA::string_dup( "Messenger" );
36 CORBA::Object_var obj = CORBA::Object::_nil();
37 while ( CORBA::is_nil( obj.in() ) ) {
38 try {
39 obj = root->resolve( name );
40 } catch (const CosNaming::NamingContext::NotFound&) {
41 // Sleep for a second and try again
42 ACE_OS::sleep(1);
46 Messenger_var messenger = Messenger::_narrow( obj.in() );
47 if( CORBA::is_nil( messenger.in() ) ) {
48 std::cerr << "Not a Messenger reference" << std::endl;
49 ACE_ASSERT(false);
52 // allocate slot
53 slot_ = info->allocate_slot_id();
55 // get PICurrent
56 CORBA::Object_var current_obj = info->resolve_initial_references("PICurrent");
58 current_ =
59 PortableInterceptor::Current::_narrow(current_obj.in());
61 // Create and register the request interceptors.
62 PortableInterceptor::ClientRequestInterceptor_var ci =
63 new ClientInterceptor(messenger, current_.in(), slot_);
64 info->add_client_request_interceptor (ci.in());
67 void
68 ClientInitializer::set_slot_data (void)
70 // Set the recursion flag
71 CORBA::Any flag;
72 CORBA::Boolean x = 0;
73 flag <<= CORBA::Any::from_boolean(x);
74 current_->set_slot(slot_, flag);
76 // Now that we're done with the PICurrent, we will release
77 // our reference to it.
78 current_ = PortableInterceptor::Current::_nil();