summaryrefslogtreecommitdiff
path: root/TAO/tests/Time_Policy/main.cpp
blob: 93fed1dcd7dd0a6a251881f6db880096df2591a3 (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
//=============================================================================
/**
 *  @file    main.cpp
 *
 *  $Id$
 *
 *  Implementation of the server.
 *
 *
 *  @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
 *  @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
 */
//=============================================================================

#include "tao/ORB.h"
#include "tao/ORB_Core.h"
#include "tao/PortableServer/PortableServer.h"
#include "ace/Reactor.h"
#include "ace/Timer_Queue.h"
#include "ace/Time_Value.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/High_Res_Timer.h"
#include "ace/Get_Opt.h"

#if (TAO_HAS_TIME_POLICY == 1)

#if defined(TAO_USE_HR_TIME_POLICY_STRATEGY)
bool uses_hr_time = true;
#else
bool uses_hr_time = false;
#endif

class TestHandler
  : public ACE_Event_Handler
{
public:
  TestHandler (CORBA::ORB_ptr orb)
    : orb_ (CORBA::ORB::_duplicate (orb)),
      timeout_triggered_ (false)
  {}

  virtual int handle_timeout (const ACE_Time_Value &tv,
                              const void *arg);

  bool trigger_in(const ACE_Time_Value &delay);

  bool timeout_triggered () { return this->timeout_triggered_; }

private:
  CORBA::ORB_var orb_;
  bool timeout_triggered_;
};

int TestHandler::handle_timeout (const ACE_Time_Value &,
                                 const void *)
{
  ACE_DEBUG ((LM_DEBUG, "TestHandler::handle_timeout - timeout triggered\n"));
  this->timeout_triggered_ = true;
  this->orb_->shutdown (false);
  return 0;
}

bool TestHandler::trigger_in(const ACE_Time_Value &delay)
{
  return -1 != this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0, delay, ACE_Time_Value (0));
}

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

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'h':
        uses_hr_time = true;
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-h "
                           "\n"
                           "\t-h\t: uses highres time policy\n",
                           argv [0]),
                          -1);
        break;
      }

  // Indicates successful parsing of the command line
  return 0;
}

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv);

      CORBA::Object_var poa_object =
        orb->resolve_initial_references("RootPOA");

      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to initialize the POA.\n"),
                          1);

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in ());

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager ();

      if (parse_args (argc, argv) != 0)
        return 1;

      poa_manager->activate ();

      TestHandler test_handler (orb.in ());

      // trigger in 2 seconds
      if (!test_handler.trigger_in (ACE_Time_Value (2, 0)))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to schedule trigger.\n"),
                          1);

      // reset system clock 4 seconds backwards
      timespec_t curts;
      ACE_Time_Value curtime = ACE_OS::gettimeofday ();
      curtime -= ACE_Time_Value (4, 0);
      curts = curtime;
      if (ACE_OS::clock_settime (CLOCK_REALTIME, &curts) != 0)
        {
          ACE_DEBUG((LM_INFO,
                     "Unable to reset OS time. Insufficient privileges or not supported.\n"));

          root_poa->destroy (1,  // ethernalize objects
                             0  // wait for completion
                            );

          orb->destroy ();

          return 0;
        }
      else
        {
          // run for max. 4 seconds
          ACE_Time_Value timeout (4, 0);
          orb->run (timeout);

          root_poa->destroy (1,  // ethernalize objects
                             0  // wait for completion
                            );

          orb->destroy ();

          ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));

          // reset system clock to correct time
          curtime = ACE_OS::gettimeofday ();
          curtime += ACE_Time_Value (4, 0);
          curts = curtime;
          ACE_OS::clock_settime (CLOCK_REALTIME, &curts);

          if (!test_handler.timeout_triggered ())
            {
              ACE_DEBUG ((LM_DEBUG, "timer handler did not trigger\n"));
              return uses_hr_time ? 1 : 0;
            }
          else
            {
              return uses_hr_time ? 0 : 1;
            }
        }
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Caught exception:");
      return 1;
    }
}

#else
int
ACE_TMAIN(int , ACE_TCHAR * [])
{
  ACE_DEBUG ((LM_INFO, "TAO built without Time Policy support\n"));
  return 0;
}
#endif /* TAO_HAS_TIME_POLICY != 1 */