Merge pull request #2306 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / scale_clients / server.cpp
blob5e7e9a53b83771753023680e4922628d73184b8a
1 // This version uses the Implementation Repository.
3 #include "Test_i.h"
5 #include "tao/IORTable/IORTable.h"
6 #include "tao/PortableServer/Root_POA.h"
7 #include "tao/ImR_Client/ImR_Client.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/streams.h"
11 #include "ace/OS_NS_unistd.h"
13 PortableServer::POA_ptr
14 createPOA(PortableServer::POA_ptr root_poa, const char* poa_name)
16 PortableServer::LifespanPolicy_var life =
17 root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
19 PortableServer::IdAssignmentPolicy_var assign =
20 root_poa->create_id_assignment_policy(PortableServer::USER_ID);
22 CORBA::PolicyList pols;
23 pols.length(2);
24 pols[0] = PortableServer::LifespanPolicy::_duplicate(life.in());
25 pols[1] = PortableServer::IdAssignmentPolicy::_duplicate(assign.in());
27 PortableServer::POAManager_var mgr = root_poa->the_POAManager();
28 PortableServer::POA_var poa =
29 root_poa->create_POA(poa_name, mgr.in(), pols);
31 life->destroy();
32 assign->destroy();
34 return poa._retn();
37 int
38 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
40 try {
41 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
43 int init_delay_secs = 0;
44 int num_requests_expected = 0;
46 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("d:n:?"));
47 int c;
49 while ((c = get_opts ()) != -1)
50 switch (c)
52 case 'd':
53 init_delay_secs = ACE_OS::atoi (get_opts.opt_arg ());
54 break;
55 case 'n':
56 num_requests_expected = ACE_OS::atoi (get_opts.opt_arg ());
57 break;
58 case '?':
59 ACE_DEBUG ((LM_DEBUG,
60 "Server: usage: %s "
61 "-d <seconds to delay before initializing POA> ",
62 "-n <number of expected requests> \n",
63 argv[0]));
64 return 1;
65 break;
68 ACE_DEBUG ((LM_DEBUG,
69 "(%P|%t) Server: delaying in initialization for <%d> seconds\n",
70 init_delay_secs));
71 ACE_OS::sleep (init_delay_secs);
72 ACE_DEBUG ((LM_DEBUG,
73 "(%P|%t) Server: done with delay\n"));
75 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
76 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj.in());
78 PortableServer::POAManager_var mgr = root_poa->the_POAManager();
80 ACE_CString poa_name("TestObject");
82 PortableServer::POA_var test_poa = createPOA(root_poa.in(),
83 poa_name.c_str ());
85 PortableServer::Servant_var<Test_i> test_servant =
86 new Test_i(orb.in(), num_requests_expected);
88 PortableServer::ObjectId_var object_id =
89 PortableServer::string_to_ObjectId("test_object");
91 // Activate the servant with the test POA,
92 // obtain its object reference, and get a
93 // stringified IOR.
94 test_poa->activate_object_with_id(object_id.in(), test_servant.in());
96 // Create binding between "TestService" and
97 // the test object reference in the IOR Table.
98 // Use a TAO extension to get the non imrified poa
99 // to avoid forwarding requests back to the ImR.
100 TAO_Root_POA* tpoa = dynamic_cast<TAO_Root_POA*>(test_poa.in());
101 if (!tpoa)
103 ACE_ERROR ((LM_ERROR,
104 ACE_TEXT ("(%P|%t) Server: Could not cast POA to root POA")
106 return -1;
109 obj = tpoa->id_to_reference_i(object_id.in(), false);
110 CORBA::String_var test_ior = orb->object_to_string(obj.in());
111 obj = orb->resolve_initial_references("IORTable");
112 IORTable::Table_var table = IORTable::Table::_narrow(obj.in());
113 table->bind(poa_name.c_str (), test_ior.in());
115 // This server is now ready to run.
116 // This version does not create an IOR
117 // file as demonstrated in the
118 // Developer's Guide. It assumes that
119 // users create IORs for the client using
120 // the tao_imr utility.
122 // Stop discarding requests.
123 mgr->activate();
125 ACE_DEBUG ((LM_DEBUG,
126 "(%P|%t) Server: started <%C>\n",
127 poa_name.c_str()));
130 ACE_CString status_file = poa_name + ACE_CString(".status");
131 ofstream out(status_file.c_str ());
132 out << "started" << endl;
135 orb->run();
137 root_poa->destroy(1,1);
138 orb->destroy();
140 ACE_DEBUG ((LM_DEBUG,
141 "(%P|%t) Server: ended <%C>\n",
142 poa_name.c_str()));
144 catch(const CORBA::Exception& ex) {
145 ex._tao_print_exception ("Server main()");
146 return 1;
149 bool const expected_requests_made = Test_i::expected_requests_made ();
150 if (!expected_requests_made)
152 ACE_ERROR ((LM_ERROR,
153 "(%P|%t) Server: ERROR: Expected number of requests were not made\n"));
156 int const status = expected_requests_made ? 0 : -1;
158 return status;