Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / MT_NoUpcall_Client_Leader / client.cpp
blobb5595adf0eb08083802a72654cfa2242719880af
1 #include "SharedIntf_i.h"
2 #include "worker.h"
3 #include "chatter.h"
4 #include "police.h"
6 #include "ace/SString.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/OS_NS_unistd.h"
10 const ACE_TCHAR *ior_output_file = ACE_TEXT ("client.ior");
11 int nr_threads = 1;
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("t:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 't':
23 nr_threads = ACE_OS::atoi(get_opts.opt_arg ());
24 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-t threads "
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 int
40 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
42 CORBA::ORB_var orb_;
43 int result = 0;
45 try
47 ACE_DEBUG((LM_INFO,"(%P|%t) START OF CLIENT TEST\n"));
49 orb_ = CORBA::ORB_init (argc, argv, "myorb-client");
51 if (parse_args (argc, argv) != 0)
52 return 1;
54 CORBA::Object_var poa_object =
55 orb_->resolve_initial_references ("RootPOA");
57 PortableServer::POA_var root_poa =
58 PortableServer::POA::_narrow (poa_object.in());
60 PortableServer::POAManager_var poa_manager =
61 root_poa->the_POAManager ();
62 PortableServer::POA_var poa = root_poa;
64 poa_manager->activate ();
66 // Creating the monitorable servant and activating it
68 Test_Idl_SharedIntf_i* intf_i = new Test_Idl_SharedIntf_i (orb_.in());
70 PortableServer::ServantBase_var base_var = intf_i;
71 PortableServer::ObjectId_var intfId_var =
72 poa->activate_object (base_var.in());
74 CORBA::Object_var obj_var =
75 poa->id_to_reference (intfId_var.in());
77 Test_Idl::SharedIntf_var intf_var =
78 Test_Idl::SharedIntf::_narrow (obj_var.in());
80 // Creating stringified IOR of the servant and writing it to a file.
82 CORBA::String_var intfString_var =
83 orb_->object_to_string (intf_var.in());
85 // Output the IOR to the <ior_output_file>
86 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
87 if (output_file == 0)
88 ACE_ERROR_RETURN ((LM_ERROR,
89 "Cannot open output file for writing IOR: %s\n",
90 ior_output_file),
91 1);
92 ACE_OS::fprintf (output_file, "%s", intfString_var.in ());
93 ACE_OS::fclose (output_file);
95 ACE_DEBUG ((LM_INFO,"(%P|%t) client IOR to %s\n", ior_output_file));
97 // Running ORB in separate thread
98 Worker worker (orb_.in ());
99 if (worker.activate (THR_NEW_LWP | THR_JOINABLE, nr_threads) != 0)
100 ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n",
101 "Cannot activate client threads"), -1);
103 ACE_DEBUG((LM_INFO,"(%P|%t) Await server initialization\n"));
104 poll ("./server.ior");
105 ACE_DEBUG((LM_INFO,"(%P|%t) Server IOR file was detected\n"));
107 ACE_Mutex mutex;
108 ACE_Condition<ACE_Mutex> stop_condition (mutex);
110 ACE_GUARD_RETURN (ACE_Mutex, guard, mutex, -1);
112 const ACE_TCHAR* serverior = ACE_TEXT("file://server.ior");
113 Chatter worker2 (orb_.in (), serverior, stop_condition);
115 if (worker2.activate (THR_NEW_LWP | THR_JOINABLE, 2) != 0)
116 ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n",
117 "Cannot activate chatty client threads"), -1);
119 do {
120 stop_condition.wait ();
121 ACE_DEBUG((LM_INFO,"(%P|%t) So far, %d/%d requests/replies have been processed\n",
122 worker2.nrequests (), worker2.nreplies ()));
123 } while (worker2.nreplies () < 2);
124 ACE_OS::sleep (8);
126 // Kill the peer
128 CORBA::Object_var rawObject = orb_->string_to_object( serverior);
130 Test_Idl::SharedIntf_var intf_var =
131 Test_Idl::SharedIntf::_narrow(rawObject.in());
133 if (CORBA::is_nil (intf_var.in ()))
134 ACE_ERROR_RETURN ((LM_ERROR, "Nil reference <%s>\n", serverior), -1);
136 // make call on server
137 ACE_DEBUG((LM_INFO,"(%P|%t) farewell START for %s\n", serverior));
139 intf_var->farewell();
141 ACE_DEBUG((LM_INFO,"(%P|%t) farewell COMPLETE for %s\n", serverior));
143 ACE_OS::sleep (8);
145 ACE_DEBUG((LM_INFO,"(%P|%t) END OF CLIENT TEST\n"));
147 orb_.in()->shutdown ();
149 worker.thr_mgr()->wait ();
151 root_poa->destroy(1,1);
153 orb_->destroy();
155 ACE_DEBUG((LM_INFO,"(%P|%t) Client Test %C\n",
156 (worker2.nrequests() == worker2.nreplies())?"succeeded":"failed"));
158 result = (worker2.nrequests_ == worker2.nreplies_)? 0 : -1;
161 catch (const CORBA::Exception& ex)
163 ex._tao_print_exception ("Error: Exception caught:");
166 ACE_OS::unlink ("client.ior");
167 return result;