Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / ForwardUponObjectNotExist / client.cpp
blobf795552f1521694550f242c11f36c3e3215cdaf7
1 #include "testC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
5 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
6 int nthreads = 1;
7 bool do_shutdown = 0;
8 static const ACE_TCHAR corbaloc_prefix[] = ACE_TEXT("corbaloc:");
9 bool expected_object_not_exist = false;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:e:"));
15 int c;
17 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'x':
22 do_shutdown = true;
23 break;
25 case 'k':
26 ior = get_opts.opt_arg ();
27 break;
29 case 'e':
30 expected_object_not_exist = ACE_OS::atoi (get_opts.opt_arg ());
31 break;
33 case '?':
34 default:
35 ACE_ERROR_RETURN ((LM_ERROR,
36 "usage: %s "
37 "-k <ior> -x "
38 "-e <expected_object_not_exist> "
39 "\n",
40 argv [0]),
41 -1);
45 if (ACE_OS::strncmp (ior,
46 corbaloc_prefix,
47 ACE_OS::strlen (corbaloc_prefix)) != 0)
48 return 1;
50 // Indicates successful parsing of the command line
51 return 0;
54 class Worker : public ACE_Task_Base
56 public:
57 /// Constructor
58 Worker (CORBA::ORB_ptr orb);
60 /// The actual implementation of the test
61 virtual void run_test ();
63 virtual int svc ();
65 /// Caught OBJECT_NOT_EXIST exception ?
66 bool got_object_not_exist () const;
68 private:
69 CORBA::ORB_var orb_;
70 // The ORB reference
71 bool got_object_not_exist_;
74 int
75 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
77 try
79 CORBA::ORB_var orb =
80 CORBA::ORB_init (argc, argv);
82 if (parse_args (argc, argv) != 0)
83 return 1;
85 Worker worker (orb.in ());
87 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
88 nthreads) != 0)
89 ACE_ERROR_RETURN ((LM_ERROR,
90 "(%P|%t) Cannot activate worker threads\n"),
91 1);
93 int timeout = 30;
94 int now = 0;
95 while (now < timeout && worker.got_object_not_exist () != expected_object_not_exist)
97 now += 5;
98 ACE_Time_Value tv (5, 0);
99 orb->run (tv);
102 if (do_shutdown)
104 CORBA::Object_var object =
105 orb->string_to_object (ior);
107 Simple_Server_var server =
108 Simple_Server::_narrow (object.in ());
110 server->shutdown ();
113 orb->shutdown ();
115 worker.thr_mgr ()->wait ();
117 orb->destroy ();
119 if (worker.got_object_not_exist () != expected_object_not_exist)
121 ACE_ERROR_RETURN ((LM_ERROR,
122 "(%P|%t)client: test failed.\n"),
125 else
127 ACE_DEBUG ((LM_DEBUG, "(%P|%t)client: test passed.\n"));
130 catch (const CORBA::Exception& ex)
132 ex._tao_print_exception ("Exception caught in main:");
133 return 1;
136 return 0;
139 // ****************************************************************
141 Worker::Worker (CORBA::ORB_ptr orb)
142 : orb_ (CORBA::ORB::_duplicate (orb)),
143 got_object_not_exist_ (false)
148 Worker::svc ()
152 this->run_test ();
154 catch (const CORBA::OBJECT_NOT_EXIST &)
156 got_object_not_exist_ = true;
158 catch (...)
162 return 0;
165 void
166 Worker::run_test ()
168 CORBA::Object_var object =
169 this->orb_->string_to_object (ior);
171 Simple_Server_var server =
172 Simple_Server::_narrow (object.in ());
174 if (CORBA::is_nil (server.in ()))
176 ACE_ERROR ((LM_ERROR,
177 "Object reference <%s> is nil.\n",
178 ior));
179 return;
182 CORBA::Boolean r =
183 server->test_is_a ("IDL:Foo:1.0");
185 if (r != 0)
186 ACE_DEBUG ((LM_DEBUG,
187 "(%P|%t) unexpected result = %d\n",
188 r));
191 bool
192 Worker::got_object_not_exist () const
194 return got_object_not_exist_;