summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/EC_Config/TimeoutConsumer.cpp
blob: 7546364855c0a91ed765262aa734a0e743a33742 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// $Id$

#include "TimeoutConsumer.h"

#include <sstream> //for ostringstream

#include "orbsvcs/Event_Utilities.h" //for ACE_Supplier/ConsumerQOS_Factory
#include "orbsvcs/Event_Service_Constants.h"
#include "orbsvcs/RtecSchedulerC.h"
#include "orbsvcs/RtecEventCommC.h"
#include "ace/RW_Mutex.h"

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

TimeoutConsumer::TimeoutConsumer (void)
  : _to_send(0)
  , _num_sent(0)
  , _hold_mtx(0)
  , _done(0)
  , _supplier(this)
  , _consumer(this)
  , _events(0)
{
}

TimeoutConsumer::~TimeoutConsumer (void)
{
  // TODO this->disconnect() ???

  if (this->_hold_mtx && this->_done!=0)
    {
      this->_done->release();
      this->_hold_mtx = 0;
    }
}

void
TimeoutConsumer::connect (ACE_RW_Mutex* done,
                          RtecScheduler::Scheduler_ptr scheduler,
                          const char *entry_prefix,
                          TimeBase::TimeT period,
                          RtecScheduler::Importance_t importance,
                          RtecScheduler::Criticality_t criticality,
                          RtecEventComm::EventSourceID supplier_id,
                          size_t to_send,
                          const RtecEventComm::EventSet& events,
                          RtecEventChannelAdmin::EventChannel_ptr ec
                          ACE_ENV_ARG_DECL)
{
  this->_supplier_id = supplier_id;
  this->_to_send = to_send;
  this->_num_sent = 0;
  this->_hold_mtx = 0;
  this->_done = done;
  if (this->_done!= 0 && this->_num_sent<this->_to_send)
    {
      int ret = done->acquire_read();
      if (ret == -1)
        {
          ACE_DEBUG((LM_DEBUG,"ERROR: Could not acquire read lock for TimeoutConsumer: %s\n",
                     ACE_OS::strerror(errno)));
        } else
        {
          ACE_DEBUG((LM_DEBUG,"ACQUIRED read lock for TimeoutConsumer %d\n",this->_supplier_id));
          this->_hold_mtx = 1;
        }
    } else
    {
      ACE_DEBUG((LM_DEBUG,"Already done; did not grab read lock for TimeoutConsumer\n"));
    }

  this->_events.length(events.length());
  for (size_t i=0; i<events.length(); ++i)
    {
      this->_events[i] = events[i]; //copy event to local set
      this->_events[i].header.source = this->_supplier_id; //make sure event source is this
    }

  //create supplier RT_Info
  std::ostringstream supp_entry_pt;
  supp_entry_pt << entry_prefix << " Supplier " << this->_supplier_id; //unique RT_Info entry point
  ACE_DEBUG((LM_DEBUG,"Creating %s\n",supp_entry_pt.str().c_str()));
  RtecScheduler::handle_t rt_info = scheduler->create (supp_entry_pt.str().c_str()
                                                       ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
  scheduler->set (rt_info,
                  criticality,
                  period, period, period,
                  period,
                  importance,
                  period,
                  0,
                  RtecScheduler::OPERATION
                  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  // Register as supplier of events
  ACE_SupplierQOS_Factory supplierQOS;
  for (size_t i=0; i<events.length(); ++i)
    {
      //insert type for each event
      supplierQOS.insert (this->_supplier_id,
                          events[i].header.type,
                          rt_info,
                          1);
    }

  RtecEventChannelAdmin::SupplierAdmin_var supplier_admin =
    ec->for_suppliers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->_consumer_proxy =
    supplier_admin->obtain_push_consumer (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  RtecEventComm::PushSupplier_var supplierv =
    this->_supplier._this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->_consumer_proxy->connect_push_supplier (supplierv.in (),
                                                supplierQOS.get_SupplierQOS ()
                                                ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  ACE_DEBUG((LM_DEBUG,"TimeoutConsumer connected as event supplier\n"));
  for (size_t i=0; i<events.length(); ++i)
    {
      ACE_DEBUG((LM_DEBUG,"\tEvent Type: %d\n",events[i].header.type));
    }

  //create consumer RT_Info
  std::ostringstream cons_entry_pt;
  cons_entry_pt << entry_prefix << " Consumer"; //unique RT_Info entry point
  ACE_DEBUG((LM_DEBUG,"Creating %s\n",cons_entry_pt.str().c_str()));
  rt_info = scheduler->create (cons_entry_pt.str().c_str() 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
                         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()));
}

void
TimeoutConsumer::disconnect (ACE_ENV_SINGLE_ARG_DECL)
{
  if (! CORBA::is_nil (this->_consumer_proxy.in ()))
    {
      this->_consumer_proxy->disconnect_push_consumer (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      this->_consumer_proxy =
        RtecEventChannelAdmin::ProxyPushConsumer::_nil ();

      // Deactivate the servant
      PortableServer::POA_var poa =
        this->_supplier._default_POA (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      PortableServer::ObjectId_var id =
        poa->servant_to_id (&this->_supplier ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      poa->deactivate_object (id.in () ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }

  //disconnect consumer ???
  if (! CORBA::is_nil (this->_supplier_proxy.in()))
    {
      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;
    }

  if (this->_hold_mtx && this->_done!=0)
    {
      this->_done->release();
      this->_hold_mtx = 0;
    }
}

void
TimeoutConsumer::push (const RtecEventComm::EventSet& events
                ACE_ENV_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  if (events.length () == 0)
    {
      ACE_DEBUG ((LM_DEBUG,"TimeoutConsumer (%P|%t) push but no events\n"));
      return;
    }

  ACE_DEBUG((LM_DEBUG,"TimeoutConsumer %d (%P|%t) received %d events:\n",this->_supplier_id,
             events.length()));
  if (this->_num_sent < this->_to_send)
    {
      for (size_t i=0; i<events.length(); ++i)
        {
          if (ACE_ES_EVENT_INTERVAL_TIMEOUT == events[i].header.type)
            {
              ACE_DEBUG((LM_DEBUG,"TimeoutConsumer %d (%P|%t) received timeout event\n",this->_supplier_id));

              //TODO send this->_events
              ++this->_num_sent;
              ACE_DEBUG((LM_DEBUG,"Sent events; %d sent\t%d total\n",this->_num_sent,this->_to_send));
              if (this->_num_sent >= this->_to_send)
                {
                  //done
                  ACE_DEBUG((LM_DEBUG,"RELEASE read lock from TimeoutConsumer %d\n",
                             this->_supplier_id));
                  this->_done->release();
                  this->_hold_mtx = 0;
                }
            }
          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 @%d (%P|%t) we received event type %d\n",
                          prio,events[0].header.type));
            }
        }
    } else
    {
      //do nothing
    }
}

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

void
TimeoutConsumer::disconnect_push_supplier (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 */