Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / MT_Client / client.cpp
blobc4aef141512e49927f7b2bca3e89e8d857ccd2c2
1 #include "testC.h"
2 #include "tao/debug.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/Task.h"
6 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
7 int nthreads = 5;
8 int niterations = 5;
9 bool server_shutdown = false;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:n:i:x"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'k':
21 ior = get_opts.opt_arg ();
22 break;
23 case 'n':
24 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
25 break;
26 case 'i':
27 niterations = ACE_OS::atoi (get_opts.opt_arg ());
28 break;
29 case 'x':
30 server_shutdown = true;
31 break;
32 case '?':
33 default:
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "usage: %s "
36 "-k <ior> "
37 "-n <nthreads> "
38 "-i <niterations> "
39 "\n",
40 argv [0]),
41 -1);
43 // Indicates successful parsing of the command line
44 return 0;
47 class Client : public ACE_Task_Base
49 // = TITLE
50 // Run the client thread
52 // = DESCRIPTION
53 // Use the ACE_Task_Base class to run the client threads.
55 public:
56 Client (Simple_Server_ptr server, int niterations);
57 // ctor
59 virtual int svc (void);
60 // The thread entry point.
62 private:
63 void validate_connection (void);
64 // Validate the connection
66 private:
67 Simple_Server_var server_;
68 // The server.
70 int niterations_;
71 // The number of iterations on each client thread.
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 CORBA::Object_var object =
86 orb->string_to_object (ior);
88 Simple_Server_var server =
89 Simple_Server::_narrow (object.in ());
91 if (CORBA::is_nil (server.in ()))
93 ACE_ERROR_RETURN ((LM_ERROR,
94 "Object reference <%s> is nil.\n",
95 ior),
96 1);
99 Client client (server.in (), niterations);
100 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
101 nthreads) != 0)
102 ACE_ERROR_RETURN ((LM_ERROR,
103 "Cannot activate client threads\n"),
106 client.thr_mgr ()->wait ();
108 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
110 if (server_shutdown)
112 server->shutdown ();
115 orb->destroy ();
117 catch (const CORBA::Exception& ex)
119 ex._tao_print_exception ("Caught exception:");
120 return 1;
123 return 0;
126 // ****************************************************************
128 Client::Client (Simple_Server_ptr server,
129 int niterations)
130 : server_ (Simple_Server::_duplicate (server)),
131 niterations_ (niterations)
135 void
136 Client::validate_connection (void)
138 // Ping the object 100 times, ignoring all exceptions.
139 // It would be better to use validate_connection() but the test must
140 // run on minimum CORBA builds too!
141 for (int j = 0; j != 100; ++j)
145 this->server_->test_method ();
147 catch (const CORBA::Exception&){}
152 Client::svc (void)
156 this->validate_connection ();
158 for (int i = 0; i < this->niterations_; ++i)
160 this->server_->test_method ();
162 if (TAO_debug_level > 0 && i % 100 == 0)
163 ACE_DEBUG ((LM_DEBUG, "(%P|%t) iteration = %d\n",
164 i));
167 catch (const CORBA::Exception& ex)
169 ex._tao_print_exception ("MT_Client: exception raised");
171 return 0;