summaryrefslogtreecommitdiff
path: root/TAO/tests/Bug_3853_Regression/client.cpp
blob: 765f8677cfde78581d12b63300da49a61eaf87e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// $Id$
#include "Hello_i.h"
#include "HelloC.h"
#include "Client_ORBInitializer.h"

#include "tao/ORBInitializer_Registry.h"
#include "tao/PI_Server/PI_Server.h"
#include "tao/debug.h"
#include "ace/Task.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_unistd.h"
#include <iostream>

const ACE_TCHAR *ior_output_file = ACE_TEXT("client.ior");
const ACE_TCHAR *server1_ior = ACE_TEXT("file://server1.ior");
const ACE_TCHAR *server2_ior = ACE_TEXT("file://server2.ior");
const ACE_TCHAR *server1_shutdown_ior = ACE_TEXT("file://server1_shutdown.ior");
const ACE_TCHAR *server2_shutdown_ior = ACE_TEXT("file://server2_shutdown.ior");

CORBA::ORB_var orb;
Demo::HelloWorld_var server1_shutdownObj;
Demo::HelloWorld_var server2_shutdownObj;
int test_duration = 30;
bool reconnected = false;
bool caught_exception = false;

class ClientTask : public ACE_Task_Base
{
public:
  ClientTask () {};
  ~ ClientTask () {};

  virtual int svc (void)
  {
    CORBA::Object_var helloObj = orb->string_to_object(server1_ior);

    Demo::HelloWorld_var hello = Demo::HelloWorld::_narrow(helloObj.in ());

    if (CORBA::is_nil(hello.in ()))
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("hello reference is nil\n")));
      return 0;
    }
    else
    {

      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ClientTask start.\n")));
      int i = 0;
      ACE_Time_Value due = ACE_OS::gettimeofday () + ACE_Time_Value (test_duration);

      while (ACE_OS::gettimeofday () < due)
      {
        try
        {
          ++ i;
          ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t)======client calling server1 sayHello %d\n"), i));
          const char* pMsg = " server1 say Hello";
          hello->sayHello(pMsg) ;
          ACE_OS::sleep(2);

          if (caught_exception) {
            // Reconncted to server 1
            ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) client reconnected to server 1\n")));
            reconnected = true;
          }
        }
        catch (const CORBA::TRANSIENT &)
        {
          caught_exception = true;
          if (TAO_debug_level > 0) {
            ACE_DEBUG((LM_DEBUG, ACE_TEXT ("sayHello() expected TRANSIENT exceptions.\n")));
          }
        }
        catch (...) {
          ACE_ERROR((LM_ERROR, ACE_TEXT ("sayHello() caught unknown exception\n")));
        }
      }
    }
    return 0;
  }
};


int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "t:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 't':
        test_duration = ACE_OS::atoi(get_opts.opt_arg());
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                              "usage:  %s"
                              " -t <test duration>"
                              "\n",
                              argv [0]),
            -1);
      }
  // Indicates successful parsing of the command line
  return 0;
}


int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
  {
    PortableInterceptor::ORBInitializer_ptr temp_initializer;

    ACE_NEW_RETURN (temp_initializer,
                    Client_ORBInitializer(),
                    -1);
    PortableInterceptor::ORBInitializer_var initializer =
      temp_initializer;

    PortableInterceptor::register_orb_initializer (initializer.in ());

    orb = CORBA::ORB_init (argc, argv);

    // Initialize options based on command-line arguments.
    if (parse_args (argc, argv))
      {
        return -1;
      }

    CORBA::Object_var shutdownObj = orb->string_to_object(server1_shutdown_ior);

    server1_shutdownObj = Demo::HelloWorld::_narrow(shutdownObj.in ());

    if (CORBA::is_nil(server1_shutdownObj.in ()))
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("server1 shutdown object reference is nil\n")));
      return 1;
    }

    shutdownObj = orb->string_to_object(server2_shutdown_ior);

    server2_shutdownObj = Demo::HelloWorld::_narrow(shutdownObj.in ());

    if (CORBA::is_nil(server2_shutdownObj.in ()))
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("server2 shutdown object reference is nil\n")));
      return 1;
    }

    ClientTask task;
    if (task.activate(THR_NEW_LWP | THR_JOINABLE, 1) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "Cannot activate client threads\n"),
                        1);
    }

    task.wait ();

    server1_shutdownObj->shutdown ();
    server2_shutdownObj->shutdown ();

    server1_shutdownObj = Demo::HelloWorld::_nil ();
    server2_shutdownObj = Demo::HelloWorld::_nil ();

    orb->destroy ();

    orb = CORBA::ORB::_nil ();

    if (!reconnected) {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "Client was not able to reconnect to server 1.\n"),
                        1);
      return 1;
    }
  }
  catch (const CORBA::Exception &e)
  {
    std::cerr << "Unexpected exception: " << e << std::endl;
    return 1;
  }

  return 0;
}