2 // This version uses the Implementation Repository.
5 #include "Server_ORBInitializer.h"
7 #include "tao/IORTable/IORTable.h"
8 #include "tao/PortableServer/Root_POA.h"
9 #include "tao/ImR_Client/ImR_Client.h"
10 #include "tao/ORBInitializer_Registry.h"
12 #include "ace/streams.h"
13 #include "ace/OS_NS_strings.h"
14 #include "ace/OS_NS_unistd.h"
18 ACE_CString
toStr(int n
)
21 return ACE_OS::itoa(n
, buf
, 10);
25 PortableServer::POA_ptr
26 createPOA (PortableServer::POA_ptr root_poa
,
30 PortableServer::LifespanPolicy_var life
=
31 root_poa
->create_lifespan_policy(PortableServer::PERSISTENT
);
33 PortableServer::IdAssignmentPolicy_var assign
=
34 root_poa
->create_id_assignment_policy(PortableServer::USER_ID
);
36 CORBA::PolicyList pols
;
38 pols
[0] = PortableServer::LifespanPolicy::_duplicate(life
.in());
39 pols
[1] = PortableServer::IdAssignmentPolicy::_duplicate(assign
.in());
41 PortableServer::POAManager_var mgr
= PortableServer::POAManager::_nil();
44 mgr
= root_poa
->the_POAManager();
46 PortableServer::POA_var poa
=
47 root_poa
->create_POA(poa_name
, mgr
.in(), pols
);
56 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
64 for (int i
= 1; i
< argc
; i
++)
66 ACE_TCHAR
*c
= argv
[i
];
67 if (ACE_OS::strcasecmp (ACE_TEXT ("-n"), c
) == 0)
69 server_num
= ACE_OS::atoi (argv
[++i
]);
71 else if (ACE_OS::strcasecmp (ACE_TEXT ("-?"),c
) == 0)
74 ACE_TEXT ("usage: %C ")
75 ACE_TEXT ("-n Number of the server\n"),
81 Server_ORBInitializer
* temp_initializer
;
83 ACE_NEW_RETURN (temp_initializer
,
84 Server_ORBInitializer (&ping_count
),
85 -1); // No exceptions yet!
86 PortableInterceptor::ORBInitializer_var initializer
=
89 PortableInterceptor::register_orb_initializer (initializer
.in ());
91 CORBA::ORB_var orb
= CORBA::ORB_init(argc
, argv
);
93 CORBA::Object_var obj
= orb
->resolve_initial_references("RootPOA");
94 PortableServer::POA_var root_poa
= PortableServer::POA::_narrow(obj
.in());
96 PortableServer::POAManager_var mgr
= root_poa
->the_POAManager();
98 ACE_CString poa_name_base
= ACE_CString("TestObject_") + toStr (server_num
);
99 PortableServer::POA_var test_poa
;
100 test_poa
= createPOA(root_poa
.in (), true, poa_name_base
.c_str ());
101 temp_initializer
->set_poa (test_poa
.in());
105 PortableServer::Servant_var
<Test_i
> test_servant
=
106 new Test_i(server_num
);
108 PortableServer::ObjectId_var object_id
=
109 PortableServer::string_to_ObjectId("test_object");
112 // Activate the servant with the test POA,
113 // obtain its object reference, and get a
116 test_poa
->activate_object_with_id(object_id
.in(), test_servant
.in());
119 // Create binding between "TestService" and
120 // the test object reference in the IOR Table.
121 // Use a TAO extension to get the non imrified poa
122 // to avoid forwarding requests back to the ImR.
124 TAO_Root_POA
* tpoa
= dynamic_cast<TAO_Root_POA
*>(test_poa
.in());
125 obj
= tpoa
->id_to_reference_i(object_id
.in(), false);
126 CORBA::String_var test_ior
= orb
->object_to_string(obj
.in());
127 obj
= orb
->resolve_initial_references("IORTable");
128 IORTable::Table_var table
= IORTable::Table::_narrow(obj
.in());
129 table
->bind(poa_name_base
.c_str (), test_ior
.in());
131 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Started Server %C \n"),
132 poa_name_base
.c_str()));
135 ACE_CString status_file
= poa_name_base
+ ACE_CString(".status");
136 ofstream
out(status_file
.c_str ());
137 out
<< "started" << endl
;
140 while (ping_count
< die_on_ping
)
142 orb
->perform_work ();
145 root_poa
->destroy(1,1);
148 catch(const CORBA::Exception
& ex
) {
149 ex
._tao_print_exception (ACE_TEXT("Server main()"));