summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/RtEC/test_driver/TimeoutConsumer.cpp
blob: 37e5af7ccd97819f16cb3039a66f0a07cfe1d78b (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
// $Id$

#include "TimeoutConsumer.h"

#include <sstream> //for ostringstream

#include "orbsvcs/Event_Utilities.h" //for ACE_ConsumerQOS_Factory
//#include "orbsvcs/RtecSchedulerC.h"
//#include "orbsvcs/RtecEventCommC.h"
//REACTOR CHANGE
#include "Timer_Event_Handler.h"
#include "orbsvcs/Event_Service_Constants.h" //for ORBSVCS_Time
#include "tao/ORB_Core.h"
#include "ace/Reactor.h"

ACE_RCSID(EC_Examples, Consumer, "$Id$")

TimeoutConsumer::TimeoutConsumer (Timeout_Observer* obs)
  : _observer(obs)
  , _consumer(this)
  //REACTOR CHANGE
  , _handler(0)
  //REACTOR CHANGE END
{
}

TimeoutConsumer::~TimeoutConsumer (void)
{
  this->disconnect();
}
/*
RtecScheduler::handle_t
TimeoutConsumer::get_RT_Info(void)
{
  return this->_rt_info;
}
*/
void
TimeoutConsumer::connect (RtecScheduler::Scheduler_ptr scheduler,
                          const char *entry_prefix,
                          TimeBase::TimeT period,
                          RtecScheduler::Importance_t importance,
                          RtecScheduler::Criticality_t criticality,
                          RtecEventChannelAdmin::EventChannel_ptr ec
                          ACE_ENV_ARG_DECL)
{
  /*
  this->_scheduler = scheduler;

  //create consumer RT_Info
  std::ostringstream cons_entry_pt;
  cons_entry_pt << entry_prefix << " TimeoutConsumer"; //unique RT_Info entry point
  ACE_DEBUG((LM_DEBUG,"Creating %s\n",cons_entry_pt.str().c_str()));
  this->_rt_info = this->_scheduler->create (cons_entry_pt.str().c_str() ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
  this->_scheduler->set (this->_rt_info,
                         criticality,
                         period, period, period,
                         period,
                         importance,
                         period,
                         0,
                         RtecScheduler::OPERATION
                         ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  // Register as consumer of timeout events
  ACE_ConsumerQOS_Factory timeoutQOS;
  timeoutQOS.insert_time(ACE_ES_EVENT_INTERVAL_TIMEOUT,
                         period, //TimeBase::TimeT
                         this->_rt_info);

  RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
    ec->for_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->_supplier_proxy =
    consumer_admin->obtain_push_supplier (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  RtecEventComm::PushConsumer_var consumerv =
    this->_consumer._this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->_supplier_proxy->connect_push_consumer (consumerv.in (),
                                                timeoutQOS.get_ConsumerQOS ()
                                                ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  ACE_DEBUG((LM_DEBUG,"TimeoutConsumer connected as timeout consumer\n"));
  std::ostringstream prd;
  prd << period;
  ACE_DEBUG((LM_DEBUG,"\tTimeout period: %s\n",prd.str().c_str()));
  */

  //REACTOR CHANGE
  ACE_UNUSED_ARG(entry_prefix);
  ACE_UNUSED_ARG(importance);
  ACE_UNUSED_ARG(criticality);
  ACE_UNUSED_ARG(scheduler);
  ACE_UNUSED_ARG(ec);

  this->entry_pt << entry_prefix << " TimeoutConsumer"; //unique RT_Info entry point
/*
  ACE_DEBUG((LM_DEBUG,"Creating %s\n",this->entry_pt.str().c_str()));
*/

  this->_handler = new Timer_Event_Handler(this);
  ACE_Time_Value interval;
  ORBSVCS_Time::TimeT_to_Time_Value(interval,period);
  TAO_ORB_Core *core = ec->orb_core();
  ACE_Reactor *reactor = core->reactor();
  //ACE_Reactor *reactor = ACE_Reactor::instance();
  this->_timer_id = reactor->schedule_timer(this->_handler,
                                           0,
                                           interval,
                                           interval);
  //REACTOR CHANGE END
}

void
TimeoutConsumer::disconnect (ACE_ENV_SINGLE_ARG_DECL)
{
  //disconnect consumer
  if (! CORBA::is_nil (this->_supplier_proxy.in()))
    {
      //REACTOR CHANGE
      /*
      this->_supplier_proxy->disconnect_push_supplier (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      this->_supplier_proxy = RtecEventChannelAdmin::ProxyPushSupplier::_nil();
      //Deactivate the servant
      PortableServer::POA_var poa =
        this->_consumer._default_POA(ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      PortableServer::ObjectId_var id =
        poa->servant_to_id (&this->_consumer ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      poa->deactivate_object(id.in() ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      */
      //REACTOR CHANGE END
    }

  //REACTOR CHANGE
  //ACE_Reactor *reactor = ACE_Reactor::instance();
  ACE_Reactor *reactor = this->_supplier_proxy->orb_core()->reactor();
  reactor->cancel_timer(this->_timer_id);
  delete this->_handler;
  this->_handler = 0;
  //REACTOR CHANGE END
}

void
TimeoutConsumer::push (const RtecEventComm::EventSet& events
                ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  /*
  RtecScheduler::RT_Info* info = this->_scheduler->get(this->_rt_info ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
  */

  if (events.length () == 0)
    {
      ACE_DEBUG ((LM_DEBUG,"TimeoutConsumer %s (%P|%t) push but no events\n",this->entry_pt.str().c_str()));
      return;
    }


/*
  ACE_DEBUG((LM_DEBUG,"TimeoutConsumer %s (%P|%t) received %d events:\n",this->entry_pt.str().c_str(),
            events.length()));
*/
  for (size_t i=0; i<events.length(); ++i)
    {
      if (ACE_ES_EVENT_INTERVAL_TIMEOUT == events[i].header.type)
        {
  	//  ACE_DEBUG((LM_DEBUG,"TimeoutConsumer %s (%P|%t) received timeout event\n",this->entry_pt.str().c_str()));
          if (this->_observer != 0)
            {
        //      ACE_DEBUG((LM_DEBUG,"TimeoutConsumer %s (%P|%t) updating observer\n",this->entry_pt.str().c_str()));
              this->_observer->update();
            }
        }
      else
        {
          int prio = -1;
          ACE_hthread_t handle;
          ACE_Thread::self(handle);
          ACE_Thread::getprio(handle,prio);
          //ACE_thread_t tid = ACE_Thread::self();
//          ACE_DEBUG ((LM_DEBUG, "TimeoutConsumer %s @%d (%P|%t) we received event type %d\n",
//                      this->entry_pt.str().c_str(),prio,events[0].header.type));
        }
    }
}

void
TimeoutConsumer::disconnect_push_consumer (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
}

// ****************************************************************

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */