summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/FaultTolerance/FLARe/Worker/Client_Timer_Handler.cpp
blob: 6e1ffafb619273bdb7c0b85375831f45dd06d175 (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
// $Id$

#include <fstream>
#include <ace/High_Res_Timer.h>
#include "Client_Timer_Handler.h"
#include "WorkerC.h"
#include "ace/Reactor.h"
#include "tao/ORB_Core.h"

extern double execution_time;

Client_Timer_Handler::Client_Timer_Handler (unsigned long iterations,
                                            unsigned long log_start,
                                            const std::string & filename,
                                            const ACE_Time_Value & period,
                                            bool logging)
  : period_ (period),
    invocations_ (0),
    logfile_ (filename),
    max_iterations_ (iterations),
    log_start_ (log_start),
    logging_ (logging)
{
  timer_.calibrate ();
}

Client_Timer_Handler::~Client_Timer_Handler ()
{
  if (logging_)
    {
      std::ofstream out (logfile_.c_str ());

      for (TimingList::iterator it = history_.begin ();
           it != history_.end ();
           ++it)
        {
          out << *it << std::endl;
        }

      out.close ();
    }
}

void
Client_Timer_Handler::set_orb (CORBA::ORB_ptr orb)
{
  orb_ = CORBA::ORB::_duplicate (orb);
}

void
Client_Timer_Handler::set_worker (DeCoRAM::Worker_ptr worker)
{
  worker_ = DeCoRAM::Worker::_duplicate (worker);
}

int
Client_Timer_Handler::handle_timeout (const ACE_Time_Value &,
                                      const void *)
{
  try
    {
      CORBA::ULong server_processing_time;

      timer_.start ();

      // we have to do some profiling first to see how we can achieve
      // the correct execution time.
      server_processing_time = worker_->run_task (execution_time);

      timer_.stop ();

      if (logging_ && (invocations_ >= log_start_))
        {
          ACE_Time_Value rt;
          timer_.elapsed_time (rt);

          history_.push_back (rt.msec () - server_processing_time);
        }
    }
  catch (CORBA::SystemException & ex)
    {
      ACE_DEBUG ((LM_WARNING, 
                   ACE_TEXT ("Client_Timer_Handler::handle_timeout () -"
                             "caught: %s"), ex._info ().c_str ()));

      orb_->shutdown ();

      return 1;
    }

  try
    {
      if ((max_iterations_ > 0) && (++invocations_ >= max_iterations_))
        {
          worker_->stop ();
     
          orb_->orb_core ()->reactor ()->cancel_timer (this);

          orb_->shutdown ();
        }
    }
  catch (CORBA::Exception & ex)
    {
      ACE_DEBUG ((LM_WARNING, 
                  "Client_Timer_Handler::handle_timeout () after run_task - "
                  "caught: %s", ex._info ().c_str ()));
    }

  return 0;
}

int
Client_Timer_Handler::handle_signal (int, siginfo_t *, ucontext_t *)
{
  orb_->shutdown ();

  return 0;
}