Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Bug_3853_Regression / client.cpp
blob9c8ac01825cff8b4d13b4b2ada2ebbfce3076d79
1 #include "Hello_i.h"
2 #include "HelloC.h"
3 #include "Client_ORBInitializer.h"
5 #include "tao/ORBInitializer_Registry.h"
6 #include "tao/PI_Server/PI_Server.h"
7 #include "tao/debug.h"
8 #include "ace/Task.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/OS_NS_unistd.h"
11 #include <iostream>
13 const ACE_TCHAR *ior_output_file = ACE_TEXT("client.ior");
14 const ACE_TCHAR *server1_ior = ACE_TEXT("file://server1.ior");
15 const ACE_TCHAR *server2_ior = ACE_TEXT("file://server2.ior");
16 const ACE_TCHAR *server1_shutdown_ior = ACE_TEXT("file://server1_shutdown.ior");
17 const ACE_TCHAR *server2_shutdown_ior = ACE_TEXT("file://server2_shutdown.ior");
19 CORBA::ORB_var orb;
20 Demo::HelloWorld_var server1_shutdownObj;
21 Demo::HelloWorld_var server2_shutdownObj;
22 int test_duration = 30;
23 bool reconnected = false;
24 bool caught_exception = false;
26 class ClientTask : public ACE_Task_Base
28 public:
29 ClientTask () = default;
30 ~ ClientTask () = default;
32 virtual int svc ()
34 CORBA::Object_var helloObj = orb->string_to_object(server1_ior);
36 Demo::HelloWorld_var hello = Demo::HelloWorld::_narrow(helloObj.in ());
38 if (CORBA::is_nil(hello.in ()))
40 ACE_ERROR ((LM_ERROR, ACE_TEXT ("hello reference is nil\n")));
41 return 0;
43 else
45 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ClientTask start.\n")));
46 int i = 0;
47 ACE_Time_Value due = ACE_OS::gettimeofday () + ACE_Time_Value (test_duration);
49 while (ACE_OS::gettimeofday () < due)
51 try
53 ++ i;
54 ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t)======client calling server1 sayHello %d\n"), i));
55 const char* pMsg = " server1 say Hello";
56 hello->sayHello(pMsg);
57 ACE_OS::sleep(2);
59 if (caught_exception) {
60 // Reconncted to server 1
61 ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) client reconnected to server 1\n")));
62 reconnected = true;
65 catch (const CORBA::TRANSIENT &)
67 caught_exception = true;
68 if (TAO_debug_level > 0) {
69 ACE_DEBUG((LM_DEBUG, ACE_TEXT ("sayHello() expected TRANSIENT exceptions.\n")));
72 catch (...) {
73 ACE_ERROR((LM_ERROR, ACE_TEXT ("sayHello() caught unknown exception\n")));
77 return 0;
82 int
83 parse_args (int argc, ACE_TCHAR *argv[])
85 ACE_Get_Opt get_opts (argc, argv, "t:");
86 int c;
88 while ((c = get_opts ()) != -1)
89 switch (c)
91 case 't':
92 test_duration = ACE_OS::atoi(get_opts.opt_arg());
93 break;
95 case '?':
96 default:
97 ACE_ERROR_RETURN ((LM_ERROR,
98 "usage: %s"
99 " -t <test duration>"
100 "\n",
101 argv [0]),
102 -1);
104 // Indicates successful parsing of the command line
105 return 0;
110 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
114 PortableInterceptor::ORBInitializer_ptr temp_initializer;
116 ACE_NEW_RETURN (temp_initializer,
117 Client_ORBInitializer(),
118 -1);
119 PortableInterceptor::ORBInitializer_var initializer =
120 temp_initializer;
122 PortableInterceptor::register_orb_initializer (initializer.in ());
124 orb = CORBA::ORB_init (argc, argv);
126 // Initialize options based on command-line arguments.
127 if (parse_args (argc, argv))
129 return -1;
132 CORBA::Object_var shutdownObj = orb->string_to_object(server1_shutdown_ior);
134 server1_shutdownObj = Demo::HelloWorld::_narrow(shutdownObj.in ());
136 if (CORBA::is_nil(server1_shutdownObj.in ()))
138 ACE_ERROR ((LM_ERROR, ACE_TEXT ("server1 shutdown object reference is nil\n")));
139 return 1;
142 shutdownObj = orb->string_to_object(server2_shutdown_ior);
144 server2_shutdownObj = Demo::HelloWorld::_narrow(shutdownObj.in ());
146 if (CORBA::is_nil(server2_shutdownObj.in ()))
148 ACE_ERROR ((LM_ERROR, ACE_TEXT ("server2 shutdown object reference is nil\n")));
149 return 1;
152 ClientTask task;
153 if (task.activate(THR_NEW_LWP | THR_JOINABLE, 1) != 0)
155 ACE_ERROR_RETURN ((LM_ERROR,
156 "Cannot activate client threads\n"),
160 task.wait ();
162 server1_shutdownObj->shutdown ();
163 server2_shutdownObj->shutdown ();
165 server1_shutdownObj = Demo::HelloWorld::_nil ();
166 server2_shutdownObj = Demo::HelloWorld::_nil ();
168 orb->destroy ();
170 orb = CORBA::ORB::_nil ();
172 if (!reconnected) {
173 ACE_ERROR_RETURN ((LM_ERROR,
174 "Client was not able to reconnect to server 1.\n"),
176 return 1;
179 catch (const CORBA::Exception &e)
181 std::cerr << "Unexpected exception: " << e << std::endl;
182 return 1;
185 return 0;