summaryrefslogtreecommitdiff
path: root/Kokyu/Dispatch_Deferrer.cpp
blob: 84322b70c009716e4cc4bccb37cf6631bf92abb0 (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
// $Id$

#include "ace/Sched_Params.h"
#include "Dispatch_Deferrer.h"
#include "Dispatcher_Task.h"

#if ! defined (__ACE_INLINE__)
#include "Dispatch_Deferrer.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(Kokyu, Dispatch_Deferrer, "$Id$")

namespace Kokyu
{

int
Dispatch_Deferrer::init(const Dispatch_Deferrer_Attributes& attr)
{
  //set up reactor for timeouts
  this->react_.open(0); 
  //Don't need any handles, since we're only using it for timeouts

  this->timers_.open();

  this->task_ = attr.task_;

  return 0;
}

int
Dispatch_Deferrer::dispatch (Dispatch_Queue_Item *qitem)
{
  ACE_ASSERT(qitem != 0);

  //setup timout
  //For now, assume period = deadline
  ACE_Time_Value tv;
  tv = ACE_OS::gettimeofday() + qitem->qos_info().deadline_;
  long timer_id = this->react_.schedule_timer(this,
					      0, //NULL arg
					      tv);
  if (timer_id < 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
        ACE_TEXT ("EC (%P|%t) cannot schedule Release Guard timer.")
	ACE_TEXT ("ACE_Reactor.schedule_timer() returned -1\n")),
             -1);
    }
  //else valid timer_id
  this->timers_.bind(qitem,timer_id);

  //buffer until timer expires
  return this->rgq_.enqueue_deadline(qitem,&tv);
}


int 
Dispatch_Deferrer::handle_timeout (const ACE_Time_Value &,
                                   const void *)
{
  //get all expired Dispatch_Queue_Items
  ACE_Message_Block *begin,*end;
  this->rgq_.remove_messages(begin,end,
			     (u_int) (ACE_Dynamic_Message_Strategy::LATE
			     | ACE_Dynamic_Message_Strategy::BEYOND_LATE));

  //dispatch them back to Dispatcher_Impl
  while (begin <= end)
    {
      Dispatch_Queue_Item *qitem =
        ACE_dynamic_cast(Dispatch_Queue_Item*, begin);

      if (qitem == 0)
        {
          ACE_Message_Block::release (begin);
          continue;
        }


      //remove timer for each enqueued qitem from reactor
      long timer_id;
      if (this->timers_.find(qitem,timer_id) < 0)
	{
            ACE_ERROR_RETURN ((LM_ERROR,
             ACE_TEXT ("Could not cancel Release Guard timer.")
             ACE_TEXT ("Unknown timer ID\n")),
             -1);
	}
      //else got timer_id
      this->react_.cancel_timer(timer_id);
      
      this->task_->enqueue(qitem);

      ++begin;
    }

  return 0;
}

} //namespace Kokyu