Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / servers_list / server.cpp
bloba0508df2a31ea7a94e232292859a9317eca23a6d
1 // server.cpp
2 // This version uses the Implementation Repository.
4 #include "Test_i.h"
5 #include "Terminator.h"
7 #include "tao/IORTable/IORTable.h"
8 #include "tao/PortableServer/Root_POA.h"
9 #include "tao/ImR_Client/ImR_Client.h"
11 #include "ace/Get_Opt.h"
12 #include "ace/streams.h"
13 #include "ace/OS_NS_unistd.h"
15 namespace
17 ACE_CString toStr(int n)
19 char buf[20];
20 return ACE_OS::itoa(n, buf, 10);
24 PortableServer::POA_ptr
25 createPOA(PortableServer::POA_ptr root_poa, const char* poa_name)
27 PortableServer::LifespanPolicy_var life =
28 root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
30 PortableServer::IdAssignmentPolicy_var assign =
31 root_poa->create_id_assignment_policy(PortableServer::USER_ID);
33 CORBA::PolicyList pols;
34 pols.length(2);
35 pols[0] = PortableServer::LifespanPolicy::_duplicate(life.in());
36 pols[1] = PortableServer::IdAssignmentPolicy::_duplicate(assign.in());
38 PortableServer::POAManager_var mgr = root_poa->the_POAManager();
39 PortableServer::POA_var poa =
40 root_poa->create_POA(poa_name, mgr.in(), pols);
42 life->destroy();
43 assign->destroy();
45 return poa._retn();
48 int
49 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
51 try {
52 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
54 int server_num = 0;
55 int init_delay_secs = 0;
57 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("d:n:?"));
58 int c;
60 while ((c = get_opts ()) != -1)
61 switch (c)
63 case 'd':
64 init_delay_secs = ACE_OS::atoi (get_opts.opt_arg ());
65 break;
66 case 'n':
67 server_num = ACE_OS::atoi (get_opts.opt_arg ());
68 break;
69 case '?':
70 ACE_DEBUG ((LM_DEBUG,
71 "usage: %s "
72 "-d <seconds to delay before initializing POA> "
73 "-n Number of the server\n",
74 argv[0]));
75 return 1;
76 break;
79 ACE_OS::sleep (init_delay_secs);
81 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
82 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj.in());
84 PortableServer::POAManager_var mgr = root_poa->the_POAManager();
86 ACE_CString poa_name = ACE_CString("TestObject_") + toStr (server_num);
88 PortableServer::POA_var test_poa = createPOA(root_poa.in(), poa_name.c_str ());
90 Terminator terminator;
91 if (terminator.open (0) == -1)
92 ACE_ERROR_RETURN((LM_ERROR,
93 ACE_TEXT ("main Error opening terminator\n")),-1);
95 PortableServer::Servant_var<Test_i> test_servant =
96 new Test_i(server_num, terminator);
98 PortableServer::ObjectId_var object_id =
99 PortableServer::string_to_ObjectId("test_object");
102 // Activate the servant with the test POA,
103 // obtain its object reference, and get a
104 // stringified IOR.
106 test_poa->activate_object_with_id(object_id.in(), test_servant.in());
109 // Create binding between "TestService" and
110 // the test object reference in the IOR Table.
111 // Use a TAO extension to get the non imrified poa
112 // to avoid forwarding requests back to the ImR.
114 TAO_Root_POA* tpoa = dynamic_cast<TAO_Root_POA*>(test_poa.in());
115 ACE_ASSERT (tpoa != 0);
117 obj = tpoa->id_to_reference_i(object_id.in(), false);
118 CORBA::String_var test_ior = orb->object_to_string(obj.in());
119 obj = orb->resolve_initial_references("IORTable");
120 IORTable::Table_var table = IORTable::Table::_narrow(obj.in());
121 table->bind(poa_name.c_str (), test_ior.in());
124 // This server is now ready to run.
125 // This version does not create an IOR
126 // file as demonstrated in the
127 // Developer's Guide. It assumes that
128 // users create IORs for the client using
129 // the tao_imr utility.
132 // Stop discarding requests.
134 mgr->activate();
136 ACE_DEBUG ((LM_DEBUG,
137 "Started Server %s \n",
138 poa_name.c_str()));
141 ACE_CString status_file = poa_name + ACE_CString(".status");
142 ofstream out(status_file.c_str ());
143 out << "started" << endl;
146 orb->run();
148 root_poa->destroy(1,1);
149 orb->destroy();
151 catch(const CORBA::Exception& ex) {
152 ex._tao_print_exception ("Server main()");
153 return 1;
156 return 0;