summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Notify_StructuredEvents.cpp
blob: f7a3864d8e33841f1c04e7ed0afb21515c609a2f (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
// $Id$
#include "Notify_StructuredEvents.h"
#include "Notify_QoSAdmin_i.h"
#include "tao/debug.h"

ACE_RCSID (Notify, Notify_Event, "$Id$")


TAO_Notify_StructuredEvents::TAO_Notify_StructuredEvents (
                          const CosNotification::EventBatch& notifications)
  : data_ (notifications),
    match_called_ (0)
{
  if (this->data_.length () > 0)
    {
      this->event_type_ = this->data_[0].header.fixed_header.event_type;
    }
  this->init_QoS ();
}

TAO_Notify_StructuredEvents::~TAO_Notify_StructuredEvents ()
{
}

void
TAO_Notify_StructuredEvents::init_QoS (void)
{
  if (this->data_.length () > 0)
    {
      CosNotification::PropertySeq& qos = this->data_[0].header.variable_header;

      for (CORBA::ULong index = 0; index < qos.length (); ++index)
        {
          ACE_CString property_name (qos[index].name);

          if (property_name.compare (CosNotification::Priority) == 0)
            {
              qos[index].value >>= this->priority_;
            }
          else if (property_name.compare (CosNotification::StartTime) == 0)
            {
              // qos[index].value >>= this->start_time_;
            }
          else if (property_name.compare (CosNotification::StopTime) == 0)
            {
              // qos[index].value >>= this->stop_time_;
            }
          else if (property_name.compare (CosNotification::Timeout) == 0)
            {
              qos[index].value >>= this->timeout_;
            }
        }
    }
}

TAO_Notify_Event*
TAO_Notify_StructuredEvents::clone (void)
{
  TAO_Notify_StructuredEvents* clone;

  ACE_NEW_RETURN (clone, TAO_Notify_StructuredEvents (this->data_), 0);

  return clone;
}

void
TAO_Notify_StructuredEvents::operator=(const TAO_Notify_StructuredEvents& structured_events)
{
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "In TAO_Notify_StructuredEvents::operator=\n"));

  if (this != &structured_events)
    {
      this->data_ = structured_events.data_;
      this->matching_ = structured_events.matching_;
      this->event_type_ = structured_events.event_type_;
    }
}

CORBA::Boolean
TAO_Notify_StructuredEvents::is_special_event_type (void) const
{
  return this->event_type_.is_special ();
}

const TAO_Notify_EventType&
TAO_Notify_StructuredEvents::event_type (void) const
{
  return this->event_type_;
}

CORBA::Boolean
TAO_Notify_StructuredEvents::do_match (CosNotifyFilter::Filter_ptr filter
                                       ACE_ENV_ARG_DECL)
{
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "Notify (%P|%t) - "
                          "TAO_Notify_StructuredEvents::do_match ()\n"));

  CORBA::Boolean status = 0;
  CORBA::ULong index = this->matching_.length ();
  CORBA::ULong length = this->data_.length ();

  for (CORBA::ULong i = 0; i < length; i++)
    {
      CORBA::Boolean matched = filter->match_structured (this->data_[i]
                                                         ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);

      if (matched)
        {
          // Return TRUE if any event in the sequence matches
          status = 1;

          // Keep track of the position within the data sequence
          this->matching_.length (index + 1);
          this->matching_[index++] = i;
        }
    }
  this->match_called_ = 1;

  return status;
}

void
TAO_Notify_StructuredEvents::do_push (CosEventComm::PushConsumer_ptr consumer
                                      ACE_ENV_ARG_DECL) const
{
  CORBA::ULong sending_length  = (this->match_called_ ?
                                         this->matching_.length () :
                                         this->data_.length ());
  for (CORBA::ULong i = 0; i < sending_length; i++)
    {
      CORBA::Any any;
      any <<= this->data_[(this->match_called_ ?
                                  this->matching_[i] : i)];
      consumer->push (any ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }
}

void
TAO_Notify_StructuredEvents::do_push (CosNotifyComm::StructuredPushConsumer_ptr consumer
                                      ACE_ENV_ARG_DECL) const
{
  CORBA::ULong sending_length  = (this->match_called_ ?
                                         this->matching_.length () :
                                         this->data_.length ());
  for (CORBA::ULong i = 0; i < sending_length; i++)
    {
      consumer->push_structured_event (this->data_[(this->match_called_ ?
                                          this->matching_[i] : i)]
                                       ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }
}

void
TAO_Notify_StructuredEvents::do_push (
                            CosNotifyComm::SequencePushConsumer_ptr consumer,
                            const TAO_Notify_QoSAdmin_i& qos_admin,
                            CosNotification::EventBatch& unsent,
                            int flush_queue
                            ACE_ENV_ARG_DECL) const
{
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "Notify (%P|%t) - "
                          "TAO_Notify_StructuredEvents::do_push with "
                          "flush_queue = %d\n", flush_queue));

  CORBA::ULong unsent_length      = unsent.length ();
  CORBA::ULong maximum_batch_size = qos_admin.maximum_batch_size ();
  CORBA::ULong sending_length     = (this->match_called_ ?
                                         this->matching_.length () :
                                         this->data_.length ());
  CosNotification::EventBatch matched (maximum_batch_size);

  // Deal with the unsent events first
  CORBA::ULong queue_size = (flush_queue &&
                             unsent_length < maximum_batch_size ?
                                     unsent_length : maximum_batch_size);

  while (unsent_length > 0 && unsent_length >= queue_size)
    {
      // We can only send queue_size at a time!
      matched.length (queue_size);

      // Pack 'em up and send 'em out.
      for (CORBA::ULong i = 0; i < queue_size; i++)
        {
          matched[i] = unsent[i];
        }
      consumer->push_structured_events (matched ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      // Shift the rest of the unsent messages to the front.
      // We are reusing 'matched' for this purpose.
      matched.length (unsent_length - queue_size);
      for (CORBA::ULong j = queue_size; j < unsent_length; j++)
        {
          matched[j - queue_size] = unsent[j];
        }
      unsent = matched;
      unsent_length -= queue_size;

      // If we are flushing the queue and we don't have enough to
      // fill upto queue_size, then we need to reset queue_size to
      // the size of the rest of the unsent messages.
      if (flush_queue && unsent_length < queue_size)
        {
          queue_size = unsent_length;
        }
    }

    if (sending_length > 0)
      {
        CORBA::ULong start = 0;
        if (unsent_length + sending_length >= maximum_batch_size)
          {
            matched = unsent;
            matched.length (maximum_batch_size);

            // Add to the matched sequence until we are full
            CORBA::ULong index = 0;
            for (CORBA::ULong i = unsent_length; i < maximum_batch_size; i++)
              {
                index = i - unsent_length;
                matched[i] = this->data_[(this->match_called_ ?
                                          this->matching_[index] : index)];
                start++;
              }
            consumer->push_structured_events (matched ACE_ENV_ARG_PARAMETER);
            ACE_CHECK;

            unsent.length (0);
            unsent_length = 0;

            // Send some more events out
            while (start < sending_length &&
                   sending_length - start >= maximum_batch_size)
              {
                for (CORBA::ULong i = 0; i < maximum_batch_size; i++)
                  {
                    matched[i] = this->data_[(this->match_called_ ?
                                              this->matching_[start] : start)];
                    start++;
                  }
                consumer->push_structured_events (matched ACE_ENV_ARG_PARAMETER);
                ACE_CHECK;
              }
          }

          // append the rest to the unsent sequence
          unsent.length (unsent_length + (sending_length - start));
          for (CORBA::ULong j = start; j < sending_length; j++)
            {
              unsent[unsent_length + (j - start)] =
                               this->data_[(this->match_called_ ?
                                            this->matching_[j] : j)];
            }
      }
}


#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class TAO_Unbounded_Sequence<CORBA::ULong>;

#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate TAO_Unbounded_Sequence<CORBA::ULong>

#endif /*ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */