summaryrefslogtreecommitdiff
path: root/TAO/tests/Time_Policy_Custom/main.cpp
blob: 7b51ce8546b041acbbb6ff901d839402c6b9d09b (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
//=============================================================================
/**
 *  @file    main.cpp
 *
 *  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/Service_Config.h"
#include "ace/ARGV.h"

#if (TAO_HAS_TIME_POLICY == 1)

#include "Custom_Time_Policy_Strategy.h"

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
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
    {
      ACE_ARGV my_argv;
      if (argc>1 && argv[1][0]=='-' && argv[1][1]=='s')
        {
          ACE_DEBUG ((LM_DEBUG, "loading static service\n"));

          ACE_Service_Config::process_directive (ace_svc_desc_Custom_Time_Policy_Strategy);
          my_argv.add(argv[0]);
          for (int i=2; i<argc ;++i)
            my_argv.add (argv[i]);

          my_argv.add (ACE_TEXT ("-ORBSvcConfDirective"));
          my_argv.add (ACE_TEXT ("static Time_Policy_Manager \"-ORBTimePolicyStrategy CUSTOM_TIME_POLICY\""), true);
        }
      else
        {
          for (int i=0; i<argc ;++i)
            my_argv.add (argv[i]);
        }

      int my_argc = my_argv.argc ();
      CORBA::ORB_var orb =
        CORBA::ORB_init (my_argc, my_argv.argv ());

      // check if custom policy installed in ORB
      ACE_Time_Value tv_hr = ACE_High_Res_Timer::gettimeofday_hr ();
      ACE_Time_Value tv_orb = orb->orb_core ()->reactor ()->timer_queue ()->gettimeofday ();
      ACE_Time_Value tv_diff = tv_orb - tv_hr;
      // The custom policy gives an offset of +10s
      if (tv_diff.sec () != 10)
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Incorrect time offset (%us). Time policy doesn't seem to be loaded\n", tv_diff.sec ()),
                          1);
      }

      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 ();

      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);

      // 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"));

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

  return 0;
}

#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 */