Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Forwarding / client.cpp
blobd4037881831fe728306ef3a04564d237a32676d4
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 niterations = 10;
7 int nthreads = 1;
8 int do_shutdown = 0;
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:i:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'x':
20 do_shutdown = 1;
21 break;
23 case 'k':
24 ior = get_opts.opt_arg ();
25 break;
27 case 'i':
28 niterations = ACE_OS::atoi (get_opts.opt_arg ());
29 break;
31 case '?':
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "-k <ior> "
36 "-i <niterations> "
37 "\n",
38 argv [0]),
39 -1);
41 // Indicates successful parsing of the command line
42 return 0;
45 class Worker : public ACE_Task_Base
47 public:
48 Worker (CORBA::ORB_ptr orb);
49 // Constructor
51 virtual void run_test (void);
52 // The actual implementation of the test
54 // = The Task_Base methods
55 virtual int svc (void);
57 private:
58 CORBA::ORB_var orb_;
59 // The ORB reference
62 int
63 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
65 try
67 CORBA::ORB_var orb =
68 CORBA::ORB_init (argc, argv);
70 if (parse_args (argc, argv) != 0)
71 return 1;
73 Worker worker (orb.in ());
75 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
76 nthreads) != 0)
77 ACE_ERROR_RETURN ((LM_ERROR,
78 "(%P|%t) Cannot activate worker threads\n"),
79 1);
81 ACE_Time_Value tv (5, 0);
83 orb->run (tv);
85 // Now run a test in the main thread, just to confuse matters a
86 // little more.
87 worker.run_test ();
89 worker.thr_mgr ()->wait ();
91 if (do_shutdown)
93 CORBA::Object_var object =
94 orb->string_to_object (ior);
96 Simple_Server_var server =
97 Simple_Server::_narrow (object.in ());
99 server->shutdown ();
102 orb->destroy ();
104 catch (const CORBA::Exception& ex)
106 ex._tao_print_exception ("Exception caught in main:");
107 return 1;
110 return 0;
113 // ****************************************************************
115 Worker::Worker (CORBA::ORB_ptr orb)
116 : orb_ (CORBA::ORB::_duplicate (orb))
121 Worker::svc (void)
125 this->run_test ();
127 catch (const CORBA::Exception& ex)
129 ex._tao_print_exception ("Exception caught in thread (%t)\n");
132 return 0;
135 void
136 Worker::run_test (void)
138 for (int j = 0; j != niterations; ++j)
140 CORBA::Object_var object =
141 this->orb_->string_to_object (ior);
143 CORBA::Boolean is_simple_server =
144 object->_is_a ("IDL:Simple_Server:1.0");
145 if (!is_simple_server)
146 ACE_DEBUG ((LM_DEBUG,
147 "(%P|%t) unexpected result from _is_a()\n"));
150 CORBA::Object_var object =
151 this->orb_->string_to_object (ior);
153 Simple_Server_var server =
154 Simple_Server::_narrow (object.in ());
156 if (CORBA::is_nil (server.in ()))
158 ACE_ERROR ((LM_ERROR,
159 "Object reference <%s> is nil.\n",
160 ior));
161 return;
164 for (int i = 0; i != niterations; ++i)
166 CORBA::Boolean r =
167 server->test_is_a ("IDL:Foo:1.0");
169 if (r != 0)
170 ACE_DEBUG ((LM_DEBUG,
171 "(%P|%t) unexpected result = %d\n",
172 r));