summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/RtEC/Federated_Kokyu/dynamic_topology_test/Consumer.cpp
blob: 97da36d9be24a2df0b4b625aa88c76ae33095c7b (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
// $Id$

#include "Consumer.h"
#include "Supplier.h"
#include "Service_Handler.h"

#include "ace/Timeprobe.h"
#include "ace/High_Res_Timer.h"
#include "ace/Time_Value.h"
#include "ace/ACE.h" //for is_prime()
#include "orbsvcs/orbsvcs/Time_Utilities.h" //ORBSVCS_Time
#include <ace/Counter.h>
#include <ace/OS_NS_sys_time.h>

#if defined (ACE_HAS_DSUI)
#include <dsui.h>
#include "federated_config.h"
#include "federated_dsui_families.h"
#endif /* ACE_HAS_DSUI */

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

Consumer::Consumer (SourceID normal_type,
                    SourceID ft_type,
                    Supplier *fwddest, Service_Handler * handler)
  : worktime_(0,0)
  , fwddest_(fwddest)
  , handler_(handler)
  , norm_type_(normal_type)
  , ft_type_(ft_type)
{
}

Consumer::Consumer (SourceID normal_type,
                    SourceID ft_type,
                    ACE_Time_Value& worktime,
                    Supplier *fwddest, Service_Handler *handler)
  : worktime_(worktime)
  , fwddest_(fwddest)
  , handler_(handler)
  , norm_type_(normal_type)
  , ft_type_(ft_type)
{
}

Consumer::~Consumer(void)
{
}

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

  ACE_DEBUG ((LM_DEBUG, "Consumer (%P|%t) we received event type %d\n",
              events[0].header.type));

  Supplier::mode_t mode;
  if (events[0].header.type == this->ft_type_)
    {
      mode = Supplier::FAULT_TOLERANT;
    }
  else if (events[0].header.type == this->norm_type_)
    {
      mode = Supplier::NORMAL;
    }
  else
    {
      //default
      ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) received unknown type %d; ignoring\n",events[0].header.type));
      return;
    }

  if (this->handler_ != 0)
    {
      ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) calling handle_service_start()\n"));
      this->handler_->handle_service_start(ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) handle_service_start() DONE\n"));
    }

  //@BT INSTRUMENT with event ID: EVENT_WORK_START Measure time
  //when work triggered by event starts.
  //DSTRM_EVENT (TEST_ONE_FAM, START_SERVICE, guid, 0, NULL);
  ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) START_SERVICE at %u\n",ACE_OS::gettimeofday().msec()));

  Object_ID oid;
  oid.id = events[0].header.eid.id;
  oid.tid = events[0].header.eid.tid;
  oid.pid = events[0].header.eid.pid;
  oid.queue_id = events[0].header.eid.queue_id;
  oid.type = events[0].header.type;

  ACE_TIMEPROBE("START_SERVICE");
  DSTRM_EVENT (TEST_ONE_FAM, START_SERVICE, 0, sizeof(Object_ID), (char*)&oid);

  ACE_High_Res_Timer timer;
  ACE_Time_Value elapsed_time;

  static CORBA::ULong prime_number = 9619899;

  ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) worktime is %isec %iusec\n",
             this->worktime_.sec(),this->worktime_.usec()));

  //There seems to be something wrong with using ACE_Countdown_Time, so we don't use it.
  //ACE_Time_Value compute_count_down_time (this->worktime_);
  //ACE_Countdown_Time compute_count_down (&compute_count_down_time); //auto-starts

  ACE_Time_Value start_time(ACE_OS::gettimeofday());
  timer.start();
  int j=0;
  while (elapsed_time <= this->worktime_)
    {
      //ACE_DEBUG((LM_DEBUG,"%isec %iusec elapsed\n",elapsed_time.sec(),elapsed_time.usec()));

      ACE::is_prime (prime_number,
                     2,
                     prime_number / 2);

      ++j;
      elapsed_time = ACE_OS::gettimeofday() - start_time;
    }

  //ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) elapsed %isec %iusec\n",elapsed_time.sec(),elapsed_time.usec()));

  timer.stop ();
  timer.elapsed_time (elapsed_time); //total elapsed time

  TimeBase::TimeT now;
  ORBSVCS_Time::Time_Value_to_TimeT (now, ACE_OS::gettimeofday ());
  if(now > events[0].header.deadline )
    {
      this->deadline_missed_++;

      //@BT INSTRUMENT with event ID: EVENT_WORK_DEADLINE_MISSED Measure time when
      //work triggered by event finishes and deadline missed.
      //DSTRM_EVENT (TEST_ONE_FAM, DEADLINE_MISSED, guid, strlen(extra_info), extra_info);
      ACE_DEBUG((LM_DEBUG,"Consumer in thread %t STOP_SERVICE (DEADLINE_MISSED) at %u\n",ACE_OS::gettimeofday().msec()));
      DSTRM_EVENT (TEST_ONE_FAM, DEADLINE_MISSED, 0, sizeof(Object_ID), (char*)&oid);
    }

  //@BT INSTRUMENT with event ID: EVENT_WORK_END Measure time when
  //work triggered by event finishes.
  //DSTRM_EVENT (TEST_ONE_FAM, STOP_SERVICE, guid,0,NULL);
  ACE_DEBUG((LM_DEBUG,"Consumer in thread %t STOP_SERVICE at %u\n",ACE_OS::gettimeofday().msec()));
  DSTRM_EVENT (TEST_ONE_FAM, STOP_SERVICE, 0, sizeof(Object_ID), (char*)&oid);

  ACE_TIMEPROBE("STOP_SERVICE");

  //now print timeprobe values
  ACE_TIMEPROBE_PRINT;
  ACE_TIMEPROBE_RESET;

  ACE_DEBUG ((LM_DEBUG, "Consumer (%P|%t) request processing for %d done, "
              "elapsed time = %isec %iusec, deadline_missed_=%d\n",
              events[0].header.type,elapsed_time.sec(),elapsed_time.usec(),
              this->deadline_missed_));
  ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) processing took %d iterations\n",j));
  ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) event had deadline %i\n",
             events[0].header.deadline));

//   ACE_DEBUG ((LM_DEBUG,
//               "Request processing in thread %t done, "
//               "prio = %d, load = %d, elapsed time = %umsec, deadline_missed = %d\n",
//               prio, exec_duration, elapsed_time.msec (),Deadline_missed ));

  //now, trigger the next subtask if any
  if (this->fwddest_ != 0)
    {
      //trigger next subtask; we assume we are the only ones who set the Supplier's mode!
      ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) triggering next subtask\n"));
      this->fwddest_->mode(mode);
      this->fwddest_->timeout_occured(ACE_ENV_SINGLE_ARG_PARAMETER);
    }

  if (this->handler_ != 0)
    {
      ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) calling handle_service_stop()\n"));
      this->handler_->handle_service_stop(ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_DEBUG((LM_DEBUG,"Consumer (%P|%t) handle_service_stop() DONE\n"));
    }
}

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

void
Consumer::setWorkTime(ACE_Time_Value& worktime)
{
  this->worktime_.set(worktime.sec(),worktime.usec());
}

void
Consumer::rt_info(InfoHandle consumer_rt_info)
{
  rt_info_ = consumer_rt_info;
}

Consumer::InfoHandle
Consumer::rt_info(void)
{
  return rt_info_;
}

void
Consumer::handler(Service_Handler * handler)
{
  this->handler_ = handler;
}

Service_Handler *
Consumer::handler(void) const
{
  return this->handler_;
}

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

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