summaryrefslogtreecommitdiff
path: root/TAO/tests/Hang_Shutdown/client.cpp
blob: 066461f895ae6196caf2bf48047433fd370ccdfc (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// $Id$

#include "TestC.h"
#include "ace/Get_Opt.h"
#include "ace/OS.h"
#include "ace/Task.h"
#include "ace/Profile_Timer.h"

ACE_RCSID(Hello, client, "$Id$")

namespace Test
{
  const char *ior = "file://server.ior";

  ACE_Profile_Timer profile_timer;
  bool blocked = false;

  bool
  parse_args (int argc, char *argv[])
  {
    ACE_Get_Opt get_opts (argc, argv, "b:k:");
    int c;

    while ((c = get_opts ()) != -1)
      switch (c)
        {
        case 'b':
          {
            int tmp =
              ACE_OS::atoi (get_opts.opt_arg ());

            if (tmp)
              blocked = true;
            else
              blocked = false;
          }
          break;
        case 'k':
          {
            ior = get_opts.opt_arg ();
          }
          break;
        case '?':
        default:
          ACE_ERROR_RETURN ((LM_ERROR,
                             "usage:  %s "
                             "-k <ior> "
                             "\n",
                             argv [0]),
                            false);
        }

    // Indicates sucessful parsing of the command line
    return true;
  }

  class Client_Task : public ACE_Task_Base
  {
  public:
    Client_Task (Hang_ptr h)
      : h_ (Hang::_duplicate (h))
    {}

    virtual int svc (void)
    {
      ACE_DECLARE_NEW_CORBA_ENV;

      ACE_TRY
        {
          this->h_->send_stuff ("Testing",
                                false
                                ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          this->h_->send_stuff ("Testing",
                                false
                                ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          this->h_->send_stuff ("Testing",
                                true
                                ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;
        }
      ACE_CATCH (CORBA::COMM_FAILURE, f)
        {
          ACE_UNUSED_ARG (f);
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) Caught COMM_FAILURE Exception \n"));

          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) This is expected \n"));

          return 0;
        }
      ACE_CATCH (CORBA::Exception, ex)
        {
          ex._tao_print_exception ("Caught CORBA Exception \n");

          ACE_ERROR ((LM_ERROR,
                      "(%P|%t) Error in test \n"));

          return -1;
        }
      ACE_CATCHALL
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) Caught a C++ exception \n"));
          ACE_ERROR ((LM_ERROR,
                      "(%P|%t) Error in test \n"));

          return -1;
        }
      ACE_ENDTRY;

      return 0;
    }

  private:
    Hang_var h_;
  };

  class Shutdown_Task : public ACE_Task_Base
  {
  public:
    Shutdown_Task (CORBA::ORB_ptr o)
      : o_ (CORBA::ORB::_duplicate (o))
    {}

    virtual int svc (void)
    {
      ACE_DECLARE_NEW_CORBA_ENV;

      ACE_TRY
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) Calling shutdown \n"));

          // Just wait for the main thread to start sening out
          // messages
          ACE_OS::sleep (4);

          // Start the timer
          profile_timer.start ();

          this->o_->shutdown (blocked
                              ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          // Stop the timer
          profile_timer.stop ();

          // Get the elampsed time
          ACE_Profile_Timer::ACE_Elapsed_Time el;
          profile_timer.elapsed_time (el);

          // The elapsed time is in secs
          if (el.real_time > 1)
            {
              ACE_ERROR ((LM_ERROR,
                          "(%P|%t) ERROR: Too long to shutdown \n"));

              return 0;
            }
        }
      ACE_CATCHALL
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) Caught exception during shutdown \n"));

          ACE_ERROR ((LM_ERROR,
                      "(%P|%t) Error in test \n"));
          return -1;
        }
      ACE_ENDTRY;

      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) Returning from shutdown \n"));
      return 0;
    }
  private:
    CORBA::ORB_var o_;
  };

  static int
  try_main (int argc, char *argv[])
  {
    ACE_DECLARE_NEW_CORBA_ENV;

    ACE_TRY
      {
        CORBA::ORB_var orb =
          CORBA::ORB_init (argc,
                           argv,
                           ""
                           ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        if (parse_args (argc, argv) == false)
          return -1;

        CORBA::Object_var tmp =
          orb->string_to_object (ior
                                 ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        Hang_var test =
          Hang::_narrow (tmp.in ()
                         ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        if (CORBA::is_nil (test.in ()))
          {
            ACE_ERROR_RETURN ((LM_DEBUG,
                               "Nil test reference <%s>\n",
                               ior),
                              1);
          }

        Client_Task ct (test.in ());

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

        ACE_DEBUG ((LM_DEBUG,
                    "(%P|%t) Activating shutdown thread \n"));

        Shutdown_Task st (orb.in ());

        if (st.activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Cannot activate shutdown threads\n"),
                            1);

        ACE_Thread_Manager::instance ()->wait ();

        orb->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
        ACE_TRY_CHECK;

      }
    ACE_CATCHANY
      {
        ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                             "CORBA Exception caught \n");
        ACE_ERROR ((LM_ERROR,
                    "(%P|%t) Eror in test \n"));
        return -1;
      }
    ACE_CATCHALL
      {
        ACE_DEBUG ((LM_DEBUG,
                    "(%P|%t) Error in test \n"));
        return -1;
      }
    ACE_ENDTRY;

    return 0;
  }
}

int
main (int argc, char *argv[])
{
  return Test::try_main (argc, argv);
}