summaryrefslogtreecommitdiff
path: root/TAO/tests/Oneway_Timeouts/server.cpp
blob: c84c0745c280e9ac30707cd98b71dbb054b5c2b3 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// $Id$

#include "TestS.h"

#include "tao/Strategies/advanced_resource.h"
#include "tao/Messaging/Messaging.h"
#include "tao/AnyTypeCode/TAOA.h"
#include "tao/AnyTypeCode/Any.h"

#include "ace/streams.h"
#include "ace/High_Res_Timer.h"
#include "ace/Reactor.h"

const int TIME_THRESHOLD = 50; //ms

int activate_delay = 0000;
int run_delay = 00;
int request_delay = 00;
int abort_after = 0;
int num_expected = 0;
int elapsed_max = 0;
int elapsed_min = 0;
int first_min = 0;
int first_max = 0;

class Tester_i
  : public virtual POA_Tester
  , public virtual ACE_Event_Handler
{
public:
  Tester_i (CORBA::ORB_ptr orb)
    : orb_ (orb)
    , id1_ (0)
    , id2_ (0)
    , count_ (0)
    , failed_ (false)
  {
    this->start_ = ACE_High_Res_Timer::gettimeofday_hr ();
  }

  virtual ~Tester_i ()
  {
  }

  virtual void test (CORBA::Long id)
    ACE_THROW_SPEC ((::CORBA::SystemException))
  {
    testShared (id);
  }

  virtual CORBA::Long test2 (CORBA::Long id)
    ACE_THROW_SPEC ((::CORBA::SystemException))
  {
    if (id == -2)
      {
        // Special id used to force a connect. Ignore.
        this->start_ = ACE_High_Res_Timer::gettimeofday_hr ();
        return id;
      }
    return testShared (id);
  }

  int testShared (CORBA::Long id)
  {
    ACE_Time_Value now = ACE_High_Res_Timer::gettimeofday_hr ();
    if (id == -1)
      {
        // Signals the end of a test run
        if (num_expected > 0 && count_ != num_expected)
          {
            cerr << "Error: Expected " << num_expected
                 << ", but received " << count_ << endl;
            this->failed_ = true;
          }
        long ms = (last_ - first_).msec ();
        if (elapsed_max > 0 && ms > elapsed_max)
          {
            cerr << "Error: Expected  < " << elapsed_max
                 << "ms, but was " << ms << "ms" << endl;
            this->failed_ = true;
          }
        if (elapsed_min > 0 && ms < elapsed_min)
          {
            cerr << "Error: Expected  > " << elapsed_min
                 << "ms, but was " << ms << "ms" << endl;
            this->failed_ = true;
          }
        ms = (first_ - start_).msec ();
        if (first_max > 0 && ms > first_max)
          {
            cerr << "Error: Expected first < " << first_max
                 << "ms, but was " << ms << "ms" << endl;
            this->failed_ = true;
          }
        if (first_min > 0 && ms < first_min)
          {
            cerr << "Error: Expected first > " << first_min
                 << "ms, but was " << ms << "ms" << endl;
            this->failed_ = true;
          }
        ACE_Time_Value timeout (0, 50 * 1000);
        this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0, timeout);
        return id;
      }
    this->last_ = now;
    if (id == 0)
      {
        this->first_ = now;
      }
    ++this->count_;
    cout << 's' << id << endl;
    if (abort_after > 0 && this->count_ >= abort_after)
      {
        cout << "\nAborting..." << endl;
        ACE_OS::abort ();
      }
    if (request_delay > 0 && id == 0)
      {
        ACE_OS::sleep (ACE_Time_Value (0, 1000 * request_delay));
      }
    return id;
  }

  int handle_timeout (const ACE_Time_Value &, const void *)
  {
    this->orb_->shutdown (0);
    return 0;
  }

  bool failed () const {
    return this->failed_;
  }

private:
  CORBA::ORB_ptr orb_;
  CORBA::Long id1_;
  CORBA::Long id2_;
  int count_;
  bool failed_;
  ACE_Time_Value start_;
  ACE_Time_Value first_;
  ACE_Time_Value last_;
};

#include "tao/Messaging/Messaging.h"
#include "tao/Strategies/advanced_resource.h"

#include "ace/streams.h"
#include "ace/Log_Msg.h"
#include "ace/Arg_Shifter.h"

using namespace CORBA;
using namespace PortableServer;

namespace {

  void print_usage ()
  {
    cout << "server [-activate_delay ms] [-run_delay ms] [-request_delay ms] "
      "[-abort_after n]\n"
      "\t[-expected n=0] [-elapsed_max ms=0] [-elapsed_min ms=0] "
      "[-first_min ms=0]\n"
      "\t[-first_max ms=0]\n"
      "\tactivate_delay Millisecond delay before POAManager::activate.\n"
      "\trun_delay Millisecond delay before ORB::run ().\n"
      "\trequest_delay Millisecond delay within each servant request.\n"
      "\tabort_after abort () after N requests.\n" << endl;
  }

  bool parse_command_line (int ac, char *av[])
  {
    ACE_Arg_Shifter args (ac, av);
    args.consume_arg ();
    while (args.is_anything_left ())
      {
        if (args.cur_arg_strncasecmp ("-activate_delay") == 0)
          {
            args.consume_arg ();
            activate_delay = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-run_delay") == 0)
          {
            args.consume_arg ();
            run_delay = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-request_delay") == 0)
          {
            args.consume_arg ();
            request_delay = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-expected") == 0)
          {
            args.consume_arg ();
            num_expected = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-elapsed_max") == 0)
          {
            args.consume_arg ();
            elapsed_max = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-elapsed_min") == 0)
          {
            args.consume_arg ();
            elapsed_min = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-first_min") == 0)
          {
            args.consume_arg ();
            first_min = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-first_max") == 0)
          {
            args.consume_arg ();
            first_max = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else if (args.cur_arg_strncasecmp ("-abort_after") == 0)
          {
            args.consume_arg ();
            abort_after = ACE_OS::atoi (args.get_current ());
            args.consume_arg ();
          }
        else 
          {
            cerr << "Error: Unknown argument \"" 
                 << args.get_current () << "\"" << endl;
            print_usage ();
            return false;
          }
      }
    return true;
  }

  void WriteIOR (const char *ior)
  {
    ofstream out ("server.ior");
    out << ior;
  }

  POA_ptr create_poa (ORB_ptr orb)
  {
    PolicyList pols (2);
    Object_var obj = orb->resolve_initial_references ("RootPOA");
    POA_var root = POA::_narrow (obj.in ());
    ACE_ASSERT (! is_nil (root.in ()));
    pols.length (2);
    pols[0] = root->create_id_assignment_policy (PortableServer::USER_ID);
    pols[1] = root->create_lifespan_policy (PortableServer::PERSISTENT);
    POAManager_var man = root->the_POAManager ();
    POA_var poa = root->create_POA ("X", man.in (), pols); 
    return poa._retn ();
  }

}

int main (int ac, char *av[])
{
  try
    {
      ORB_var orb = ORB_init (ac, av);

      if (!parse_command_line (ac, av))
        {
          return 1;
        }

      POA_var poa = create_poa (orb.in ());
      ACE_ASSERT (! is_nil (poa.in ()));

      Tester_i svt (orb.in ());

      ObjectId_var id = string_to_ObjectId ("tester");

      poa->activate_object_with_id (id.in (), &svt);
      Object_var obj = poa->id_to_reference (id.in ());
      String_var ior = orb->object_to_string (obj.in ());
      WriteIOR (ior.in ());

      cout << "Servants registered and activated." << endl;

      if (activate_delay > 0)
        {
          ACE_OS::sleep (ACE_Time_Value (0, activate_delay * 1000));
        }
      POAManager_var man = poa->the_POAManager ();
      man->activate ();

      cout << "POAManager activated." << endl;

      if (run_delay > 0)
        {
          ACE_OS::sleep (ACE_Time_Value (0, run_delay * 1000));
        }
      cout << "Running orb..." << endl;

      orb->run ();

      if (svt.failed ())
        {
          return 1;
        }

      return 0;

    } 
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("server:");
    }

  return 1;

}