Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3384_Regression / client.cpp
blobea37462912c3ca4a6f9368908f5aa00d7578ed11
1 #include "Client_i.h"
2 #include "tao/Messaging/Messaging.h"
3 #include "tao/AnyTypeCode/Any.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_sys_time.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/Task.h"
9 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'k':
21 ior = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-k <ior> "
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 /**
38 * Run a server thread
40 * Use the ACE_Task_Base class to run server threads
42 class Worker : public ACE_Task_Base
44 public:
45 Worker (CORBA::ORB_ptr orb);
46 // ctor
48 virtual int svc (void);
49 // The thread entry point.
51 private:
52 CORBA::ORB_var orb_;
53 // The orb
56 int
57 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
59 int result = 1;
61 try
63 CORBA::ORB_var orb =
64 CORBA::ORB_init (argc, argv);
66 if (parse_args (argc, argv) != 0)
67 return 1;
69 CORBA::Object_var tmp =
70 orb->string_to_object(ior);
72 Test::Server_var test_server =
73 Test::Server::_narrow(tmp.in ());
75 if (CORBA::is_nil (test_server.in ()))
76 ACE_ERROR_RETURN ((LM_DEBUG,
77 "ERROR: Nil reference in Test::Server reference <%s>\n",
78 ior),
79 1);
81 CORBA::Any scope_as_any;
82 scope_as_any <<= Messaging::SYNC_NONE;
84 CORBA::PolicyList policies(1); policies.length (1);
85 policies[0] =
86 orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
87 scope_as_any);
89 tmp = test_server->_set_policy_overrides (policies, CORBA::ADD_OVERRIDE);
91 policies[0]->destroy ();
93 Test::Server_var test_server_no_sync =
94 Test::Server::_narrow(tmp.in ());
96 if (CORBA::is_nil (test_server_no_sync.in ()))
97 ACE_ERROR_RETURN ((LM_DEBUG,
98 "ERROR: Nil reference in Test::Server reference <%s>\n",
99 ior),
102 CORBA::Object_var poa_object =
103 orb->resolve_initial_references("RootPOA");
105 if (CORBA::is_nil (poa_object.in ()))
106 ACE_ERROR_RETURN ((LM_ERROR,
107 " (%P|%t) Unable to initialize the POA.\n"),
110 PortableServer::POA_var root_poa =
111 PortableServer::POA::_narrow (poa_object.in ());
113 PortableServer::POAManager_var poa_manager =
114 root_poa->the_POAManager ();
116 poa_manager->activate ();
118 Worker worker (orb.in ());
119 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
120 1) != 0)
121 ACE_ERROR_RETURN ((LM_ERROR,
122 "Cannot activate client server thread\n"),
125 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Test starting . . .\n"));
127 Client client_impl;
129 PortableServer::ObjectId_var id =
130 root_poa->activate_object (&client_impl);
132 tmp = root_poa->id_to_reference (id.in ());
134 Test::Client_var test_client =
135 Test::Client::_narrow (tmp.in ());
137 // setup client callback at server
138 test_server_no_sync->setup (test_client.in ());
140 // send oneway request to server
141 test_server_no_sync->request (1);
143 // sleep 2 sec to give ample opportunity for oneway reply to be received by worker
144 ACE_OS::sleep (2);
146 // check if reply received
147 if (client_impl.reply_count () > 0)
149 ACE_DEBUG ((LM_INFO, "(%P|%t) Oneway reply correctly received\n"));
151 result = 0; // test OK
153 else
155 ACE_ERROR ((LM_ERROR, "(%P|%t) ERROR: Oneway reply not received\n"));
157 // send second request to trigger reception of first and second reply
158 test_server_no_sync->request (2);
160 // sleep 2 sec to give ample opportunity for oneway reply to be received by worker
161 ACE_OS::sleep (2);
163 if (client_impl.reply_count () > 1)
165 ACE_DEBUG ((LM_INFO, "(%P|%t) Received both replies after second request\n"));
167 else
169 ACE_ERROR ((LM_ERROR, "(%P|%t) FATAL ERROR: Still no replies received\n"));
173 // shutdown server (use original synchronous reference to be sure to deliver message)
174 test_server->shutdown ();
176 // shutdown worker
177 orb->shutdown (1);
179 worker.thr_mgr ()->wait ();
181 orb->destroy ();
183 catch (const CORBA::Exception& ex)
185 ex._tao_print_exception ("Exception caught:");
186 return 1;
189 return result;
192 // ****************************************************************
194 Worker::Worker (CORBA::ORB_ptr orb)
195 : orb_ (CORBA::ORB::_duplicate (orb))
200 Worker::svc (void)
204 this->orb_->run ();
206 catch (const CORBA::Exception&)
209 return 0;