More tests update
[ACE_TAO.git] / TAO / tests / Bug_2683_Regression / server.cpp
bloba6104342c1c78b8eed13cf6b1151f17c611bf0f8
1 #include "test_i.h"
2 #include "tao/IORTable/IORTable.h"
3 #include "ace/OS_NS_strings.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/Thread_Manager.h"
6 #include "ace/Task.h"
7 #include "ace/Get_Opt.h"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-o <iorfile>"
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 class ORB_Run_Task : public ACE_Task_Base
39 private:
40 CORBA::ORB_var orb_;
42 public:
43 ORB_Run_Task (CORBA::ORB_ptr orb)
44 : orb_(CORBA::ORB::_duplicate(orb))
48 int svc (void)
50 try
52 this->orb_->run();
54 catch (const CORBA::Exception &ex)
56 ACE_DEBUG ((LM_DEBUG,
57 "(%P|%t) server orb run thread caught %s\n",ex._name()));
59 return 0;
64 int
65 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
67 try
69 bool endpoint_found = false;
70 for (int i = 0; i < argc; i++)
72 if ((ACE_OS::strcasecmp (argv[i],ACE_TEXT("-ORBEndpoint")) == 0) ||
73 (ACE_OS::strcasecmp (argv[i],ACE_TEXT("-ORBListenEndpoints")) == 0))
74 endpoint_found = 1;
77 if (!endpoint_found)
78 ACE_ERROR_RETURN ((LM_ERROR,
79 "ERROR: %s requires an explicit endpoint!\n",
80 argv[0]), 1);
81 ACE_DEBUG ((LM_DEBUG,"(%P|%t) server started\n"));
82 CORBA::ORB_var orb =
83 CORBA::ORB_init (argc, argv);
85 if (parse_args (argc, argv) != 0)
86 return 1;
88 CORBA::Object_var obj =
89 orb->resolve_initial_references("RootPOA");
91 PortableServer::POA_var root_poa =
92 PortableServer::POA::_narrow (obj.in ());
94 if (CORBA::is_nil (root_poa.in ()))
95 ACE_ERROR_RETURN ((LM_ERROR,
96 " (%P|%t) Panic: nil RootPOA\n"),
97 1);
99 obj = orb->resolve_initial_references("IORTable");
101 IORTable::Table_var iortable =
102 IORTable::Table::_narrow(obj.in());
104 if (CORBA::is_nil (root_poa.in ()))
105 ACE_ERROR_RETURN ((LM_ERROR,
106 " (%P|%t) Panic: nil IORTable\n"),
109 ORB_Killer killer (orb.in());
110 test_i *servant;
111 ACE_NEW_RETURN (servant,
112 test_i (&killer),
114 PortableServer::ServantBase_var owner_transfer(servant);
116 PortableServer::ObjectId_var id =
117 root_poa->activate_object (servant);
119 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
121 Test::IORTable_Shutdown_Race_var target =
122 Test::IORTable_Shutdown_Race::_narrow (object.in ());
124 CORBA::String_var ior =
125 orb->object_to_string (target.in ());
127 iortable->bind ("Racer",ior.in());
129 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
131 poa_manager->activate ();
133 ACE_DEBUG ((LM_DEBUG,
134 "(%P|%t) server - activating ORB threads\n"));
136 ORB_Run_Task ort (orb.in());
137 ort.activate (THR_NEW_LWP | THR_JOINABLE, 3);
138 ACE_DEBUG ((LM_DEBUG,"(%P|%t) server - ORB running\n"));
141 // Output the IOR to the <ior_output_file> - to signal readiness.
142 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
143 if (output_file == 0)
144 ACE_ERROR_RETURN ((LM_ERROR,
145 "Cannot open output file for writing IOR: %s\n",
146 ior_output_file),
148 ACE_OS::fprintf (output_file, "%s", ior.in ());
149 ACE_OS::fclose (output_file);
151 ACE_Thread_Manager *tm = ort.thr_mgr();
152 tm->wait();
153 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
155 orb->destroy ();
157 catch (const CORBA::Exception &ex)
159 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server main - caught %s\n",ex._name()));
160 return 1;
163 return 0;