1 #include "SharedIntf_i.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");
14 parse_args (int argc
, ACE_TCHAR
*argv
[])
16 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("t:"));
19 while ((c
= get_opts ()) != -1)
23 nr_threads
= ACE_OS::atoi(get_opts
.opt_arg ());
28 ACE_ERROR_RETURN ((LM_ERROR
,
35 // Indicates successful parsing of the command line
40 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
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)
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");
88 ACE_ERROR_RETURN ((LM_ERROR
,
89 "Cannot open output file for writing IOR: %s\n",
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"));
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);
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);
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
));
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);
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");