=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / Oneway_Send_Timeouts / Server.cpp
blob86caa6f0a56c8dd21721f32980c454cf1b031394
1 #include "Server.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/ARGV.h"
5 #include "ace/OS_NS_strings.h"
7 void replace (ACE_TCHAR** a, ACE_TCHAR** b)
9 ACE_TCHAR* tmp = *a;
11 *a = *b;
12 *b = tmp;
15 Server::Server (int , ACE_TCHAR* argv[])
16 : init_ (false), shutdown_ (false)
18 try {
19 ACE_ARGV args_1 (argv);
20 ACE_ARGV args_2 (argv);
23 for (int my_count = 0; my_count < args_1.argc(); my_count++) {
24 ACE_DEBUG ((LM_DEBUG, "<%d, %s> ", my_count, (args_1.argv())[my_count]));
26 ACE_DEBUG ((LM_DEBUG, "\n"));
29 ACE_TCHAR** my_argv = args_1.argv ();
30 int my_argc = args_1.argc ();
31 int count = 0;
32 for (count = my_argc-1; count > -1; count--) {
33 if (ACE_OS::strcasecmp (ACE_TEXT("-ORBEndpoint"), my_argv[count]) == 0) {
34 break;
37 if (count > my_argc-2){
38 // check if less than 2-last position.
39 return;
41 replace (&(my_argv[count]), &(my_argv[my_argc-2]));
42 replace (&(my_argv[count+1]), &(my_argv[my_argc-1]));
44 my_argc--; my_argc--;
45 orb_ = CORBA::ORB_init (my_argc, my_argv, "Server");
46 if (CORBA::is_nil (orb_.in())) {
47 ACE_ERROR ((LM_ERROR, "Server::Server> ORB initialization failed.\n"));
48 return;
51 my_argv = args_2.argv();
52 my_argc = args_2.argc();
53 for (count = 0; count < my_argc; count++) {
54 if (ACE_OS::strcasecmp (ACE_TEXT("-ORBEndpoint"), my_argv[count]) == 0) {
55 break;
58 if (count > my_argc-2) {
59 // check if less than 2-last position.
60 return;
62 replace (&(my_argv[count]), &(my_argv[my_argc-2]));
63 replace (&(my_argv[count+1]), &(my_argv[my_argc-1]));
65 // initialize management ORB
66 my_argc--; my_argc--;
67 management_orb_ = CORBA::ORB_init (my_argc, my_argv, "Management");
68 if (CORBA::is_nil (orb_.in())) {
69 ACE_ERROR ((LM_ERROR, "Server::Server> Mgmt ORB initialization failed.\n"));
70 return;
73 if (!this->parse_args (my_argc, my_argv))
74 return;
76 CORBA::Object_var obj = orb_->resolve_initial_references ("RootPOA");
77 PortableServer::POA_var root_poa =
78 PortableServer::POA::_narrow (obj.in ());
79 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
81 test_i_.reset (new Test_i (orb_.in()));
82 PortableServer::ObjectId_var oid =
83 root_poa->activate_object (test_i_.get());
84 obj = root_poa->id_to_reference (oid.in());
85 Test_var test_obj = Test::_narrow (obj.in());
87 CORBA::String_var ior = orb_->object_to_string (test_obj.in ());
88 obj = orb_->resolve_initial_references("IORTable");
89 IORTable::Table_var table = IORTable::Table::_narrow (obj.in());
90 table->bind ("Test", ior.in());
92 poa_manager->activate ();
94 // initialize management objects
96 obj = management_orb_->resolve_initial_references ("RootPOA");
97 PortableServer::POA_var management_root_poa =
98 PortableServer::POA::_narrow (obj.in ());
99 PortableServer::POAManager_var management_poa_manager =
100 management_root_poa->the_POAManager ();
102 oid = management_root_poa->activate_object (test_i_.get());
103 obj = management_root_poa->id_to_reference (oid.in());
104 test_obj = Test::_narrow (obj.in());
106 ior = orb_->object_to_string (test_obj.in ());
107 obj = management_orb_->resolve_initial_references("IORTable");
108 table = IORTable::Table::_narrow (obj.in());
109 table->bind ("Management", ior.in());
111 management_poa_manager->activate ();
113 catch( CORBA::Exception& ex) {
114 ACE_ERROR ((LM_ERROR, "(%P|%t) Server> Caught CORBA::Exception %s"
115 , ex._info().c_str()));
116 return;
119 init_ = true;
122 Server::~Server ()
124 this->shutdown ();
127 bool
128 Server::parse_args (int , ACE_TCHAR* [])
131 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT(""));
132 int c;
134 while ((c = get_opts ()) != -1) {
135 switch (c)
137 default:
138 ACE_ERROR_RETURN ((LM_ERROR, "Invalid option \'-%c\'\n", c)
139 , false);
144 return true;
147 bool
148 Server::run (bool management)
150 bool status = true;
152 try {
153 if (!management && !CORBA::is_nil (orb_.in())) {
154 orb_->run ();
157 catch( CORBA::Exception& ex) {
158 ACE_ERROR ((LM_ERROR, "(%P|%t) Server::run> Caught CORBA::Exception %s in worker ORB\n"
159 , ex._info().c_str()));
160 status = false;
163 try {
164 if (management && !CORBA::is_nil (management_orb_.in())) {
165 management_orb_->run();
168 catch( CORBA::Exception& ex) {
169 ACE_ERROR ((LM_ERROR, "(%P|%t) Server::run> Caught CORBA::Exception %s in management ORB\n"
170 , ex._info().c_str()));
171 status = false;
174 return status;
177 void
178 Server::shutdown ()
180 ACE_GUARD (TAO_SYNCH_MUTEX, ace_mon, this->mutex_);
181 if (shutdown_) {
182 return;
185 try {
186 if (!CORBA::is_nil (management_orb_.in())) {
187 management_orb_->shutdown (true);
188 ACE_OS::sleep (1); // Let management thread clear out
190 test_i_.reset ((Test_i*)0);
192 if (!CORBA::is_nil (orb_.in())) {
193 orb_->destroy ();
195 orb_ = CORBA::ORB::_nil();
196 if (!CORBA::is_nil (management_orb_.in())) {
197 management_orb_->destroy ();
199 management_orb_ = CORBA::ORB::_nil();
201 catch( CORBA::Exception& ex) {
202 ACE_ERROR ((LM_ERROR, "(%P|%t) Server::run> Caught CORBA::Exception %s"
203 , ex._info().c_str()));
206 shutdown_ = true;