Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / MT_BiDir / server.cpp
blobd69309da73e0bf7e0cdb988f2011217a2b544dea
1 #include "ace/Get_Opt.h"
2 #include "Sender_i.h"
3 #include "tao/BiDir_GIOP/BiDirGIOP.h"
4 #include "tao/AnyTypeCode/Any.h"
5 #include "Server_Task.h"
6 #include "tao/ORB_Core.h"
7 #include "tao/Thread_Lane_Resources.h"
8 #include "tao/Transport_Cache_Manager.h"
9 #include "ace/Manual_Event.h"
11 const ACE_TCHAR *ior_output_file = 0;
12 static int no_iterations = 10;
13 static int no_clients = 2;
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:i:c:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'o':
25 ior_output_file = get_opts.optarg;
26 break;
27 case 'c':
28 no_clients = ACE_OS::atoi (get_opts.optarg);
29 break;
30 case 'i':
31 no_iterations = ACE_OS::atoi (get_opts.optarg);
32 break;
33 case '?':
34 default:
35 ACE_ERROR_RETURN ((LM_ERROR,
36 "usage: %s "
37 "-o <iorfile>"
38 "-c <no_clients>"
39 "-i <no_iterations>"
40 "\n",
41 argv [0]),
42 -1);
44 // Indicates successful parsing of the command line
45 return 0;
49 int
50 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
52 try
54 CORBA::ORB_var orb =
55 CORBA::ORB_init (argc, argv);
57 if (parse_args (argc, argv) != 0)
58 return 1;
60 CORBA::Object_var poa_object =
61 orb->resolve_initial_references ("RootPOA");
63 if (CORBA::is_nil (poa_object.in ()))
64 ACE_ERROR_RETURN ((LM_ERROR,
65 " (%P|%t) Unable to initialize the POA.\n"),
66 1);
68 PortableServer::POA_var root_poa =
69 PortableServer::POA::_narrow (poa_object.in ());
71 PortableServer::POAManager_var poa_manager =
72 root_poa->the_POAManager ();
74 // Policies for the childPOA to be created.
75 CORBA::PolicyList policies (1);
76 policies.length (1);
78 CORBA::Any pol;
79 pol <<= BiDirPolicy::BOTH;
80 policies[0] =
81 orb->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
82 pol);
84 // Create POA as child of RootPOA with the above policies. This POA
85 // will receive request in the same connection in which it sent
86 // the request
87 PortableServer::POA_var child_poa =
88 root_poa->create_POA ("childPOA",
89 poa_manager.in (),
90 policies);
92 // Creation of childPOA is over. Destroy the Policy objects.
93 for (CORBA::ULong i = 0;
94 i < policies.length ();
95 ++i)
97 policies[i]->destroy ();
100 poa_manager->activate ();
102 ACE_Manual_Event manual_event;
104 Sender_i *sender = 0;
105 ACE_NEW_RETURN (sender,
106 Sender_i (no_clients,
107 manual_event),
108 -1);
110 PortableServer::ServantBase_var owner_transfer (sender);
112 PortableServer::ObjectId_var id =
113 child_poa->activate_object (sender);
115 CORBA::Object_var obj =
116 child_poa->id_to_reference (id.in ());
118 CORBA::String_var ior =
119 orb->object_to_string (obj.in ());
121 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
123 // If the ior_output_file exists, output the ior to it
124 if (ior_output_file != 0)
126 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
127 if (output_file == 0)
128 ACE_ERROR_RETURN ((LM_ERROR,
129 "Cannot open output file for writing IOR: %s",
130 ior_output_file),
132 ACE_OS::fprintf (output_file, "%s", ior.in ());
133 ACE_OS::fclose (output_file);
137 // Get the main thread id..
138 ACE_thread_t thr_id = ACE_Thread::self ();
140 Server_Task server_task (sender,
141 manual_event,
142 no_iterations,
143 orb.in (),
144 ACE_Thread_Manager::instance ());
147 if (server_task.activate (THR_NEW_LWP | THR_JOINABLE, 4, 1) == -1)
149 ACE_ERROR ((LM_ERROR, "Error activating server task\n"));
152 // Only the main thread gets to run the ORB
153 if (thr_id == ACE_Thread::self ())
155 // run the ORB for at most 60 seconds...
156 ACE_Time_Value tv (60, 0);
158 // Call the ORB run from the main thread
159 orb->run (tv);
161 ACE_DEBUG ((LM_DEBUG, "(%P|%t) event loop finished\n"));
164 ACE_Thread_Manager::instance ()->wait ();
166 root_poa->destroy (true, true);
168 catch (const CORBA::Exception& ex)
170 ex._tao_print_exception ("Caught exception:");
171 return 1;
174 return 0;