summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/impl/ndds/Getter_T.cpp
blob: 5fa9c34fd367e11d125cf09f436b63f153c29156 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// $Id$
#include "dds4ccm/impl/ndds/DataReader.h"
#include "dds4ccm/impl/ndds/Utils.h"
#include "dds4ccm/impl/ndds/Duration_t.h"
#include "dds4ccm/impl/ndds/SampleInfo.h"

#include "dds4ccm/impl/logger/Log_Macros.h"

template <typename DDS_TYPE, typename CCM_TYPE>
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::Getter_Base_T (void) :
   impl_ (0),
   condition_(0),
   time_out_ (),
   max_delivered_data_ (0),
   ws_ (0),
   rd_condition_ (0)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::RTI::Getter_Base_T::Getter_Base_T");
}

template <typename DDS_TYPE, typename CCM_TYPE>
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::~Getter_Base_T (void)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::RTI::Getter_Base_T::~Getter_Base_T");
  delete ws_;
}

template <typename DDS_TYPE, typename CCM_TYPE>
typename DDS_TYPE::data_reader *
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::impl (void)
{
  if (this->impl_)
    {
      return this->impl_;
    }
  else
    {
      throw ::CORBA::BAD_INV_ORDER ();
    }
}

template <typename DDS_TYPE, typename CCM_TYPE>
bool
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::wait (
          DDSConditionSeq& active_conditions)
{
  DDS_Duration_t timeout;
  timeout <<= this->time_out_;
  DDS_ReturnCode_t const retcode = this->ws_->wait (active_conditions, timeout);
  if (retcode == DDS_RETCODE_TIMEOUT)
    {
      DDS4CCM_DEBUG (6, (LM_DEBUG, ACE_TEXT ("Getter: No data available after timeout.\n")));
      return false;
    }
  return true;
}

template <typename DDS_TYPE, typename CCM_TYPE>
bool
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::get_many (
          typename CCM_TYPE::seq_type::_out_type instances,
          ::CCM_DDS::ReadInfoSeq_out infos)
{
  instances = new typename CCM_TYPE::seq_type;
  infos = new ::CCM_DDS::ReadInfoSeq;

  DDSConditionSeq active_conditions;
  if (!this->wait (active_conditions))
    {
      return false;
    }

  ::DDS_Long max_samples = this->max_delivered_data_;
  if (max_samples == 0)
    {
      max_samples = DDS_LENGTH_UNLIMITED;
    }

  DDS_SampleInfoSeq sample_info;
  typename DDS_TYPE::dds_seq_type data;
  for (::DDS_Long i = 0; i < active_conditions.length(); i++)
    {
      if (active_conditions[i] == this->rd_condition_)
        {
          // Check trigger
          active_conditions[i]->get_trigger_value ();

          // Take read condition
          DDS_ReturnCode_t retcode = this->impl ()->read_w_condition (
                                    data,
                                    sample_info,
                                    max_samples,
                                    this->rd_condition_);

          if (retcode == DDS_RETCODE_OK && data.length () >= 1)
            {
              ::CORBA::ULong number_read = 0;
              for (::DDS_Long index = 0; index < sample_info.length (); index ++)
                {
                  if (sample_info[index].valid_data)
                    {
                      ++number_read;
                    }
                }
              DDS4CCM_DEBUG (6, (LM_DEBUG, ACE_TEXT ("Getter_Base_T::get_many: ")
                                ACE_TEXT ("read <%d> - valid <%d>\n"),
                                sample_info.length (),
                                number_read));
              infos->length (number_read);
              instances->length (number_read);
              number_read = 0;
              for (::DDS_Long j = 0; j < data.length (); j ++)
                {
                  if (sample_info[j].valid_data)
                    {
                      infos->operator[](number_read) <<= sample_info[j];
                      instances->operator[](number_read) = data[j];
                      ++number_read;
                    }
                }
            }
          else
            {
              // RETCODE_NO_DATA should be an error
              // because after a timeout there should be
              // data.
              DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
                    "CIAO::DDS4CCM::RTI::Getter_Base_T::Getter_Base_T::get_many - "
                    "Error while reading from DDS: <%C>\n",
                    translate_retcode (retcode)));
              this->impl ()->return_loan(data,sample_info);
              throw CCM_DDS::InternalError (retcode, 1);
            }

          retcode = this->impl ()->return_loan(data,sample_info);
          if (retcode != DDS_RETCODE_OK)
            {
              DDS4CCM_ERROR (1, (LM_ERROR, ACE_TEXT ("return loan error %C\n"),
                          translate_retcode (retcode)));
            }
        }
    }
  return true;
}

template <typename DDS_TYPE, typename CCM_TYPE>
::DDS::Duration_t
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::time_out (void)
{
  return this->time_out_;
}

template <typename DDS_TYPE, typename CCM_TYPE>
void
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::time_out (
  const ::DDS::Duration_t & time_out)
{
  this->time_out_ = time_out;
}

template <typename DDS_TYPE, typename CCM_TYPE>
::CCM_DDS::DataNumber_t
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::max_delivered_data (void)
{
  return this->max_delivered_data_;
}

template <typename DDS_TYPE, typename CCM_TYPE>
void
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::max_delivered_data (
  ::CCM_DDS::DataNumber_t max_delivered_data)
{
  this->max_delivered_data_ = max_delivered_data;
}

template <typename DDS_TYPE, typename CCM_TYPE>
void
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::passivate ()
{
  DDS_ReturnCode_t retcode = this->ws_->detach_condition (this->rd_condition_);
  if (retcode != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "Getter_Base_T::passivate - "
                      "Unable to detach read condition from waitset.\n"));
    }
  else
    {
      DDS4CCM_DEBUG (6, (LM_INFO, CLINFO "Getter_Base_T::passivate - "
                      "Read condition succesfully detached from waitset.\n"));
    }
  if (retcode != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "Getter_Base_T::passivate - "
                      "Unable to detach guard condition from waitset.\n"));
    }
  else
    {
      DDS4CCM_DEBUG (6, (LM_INFO, CLINFO "Getter_Base_T::passivate - "
                      "Guard condition succesfully detached from waitset.\n"));
    }
  retcode = this->impl ()->delete_readcondition (this->rd_condition_);
  if (retcode != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "Getter_Base_T::passivate - "
                      "Unable to delete read condition from DDSDataReader.\n"));
    }
  else
    {
      DDS4CCM_DEBUG (6, (LM_INFO, CLINFO "Getter_Base_T::passivate - "
                      "Read condition succesfully deleted from DDSDataReader.\n"));
    }
}

template <typename DDS_TYPE, typename CCM_TYPE>
void
CIAO::DDS4CCM::RTI::Getter_Base_T<DDS_TYPE, CCM_TYPE>::set_impl (
  ::DDS::DataReader_ptr reader)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::RTI::Getter_Base_T::set_impl");

  if (::CORBA::is_nil (reader))
    {
      impl_ = 0;
      delete this->ws_;
      this->ws_ = 0;
    }
  else
    {
      CCM_DDS_DataReader_i *rdr = dynamic_cast <CCM_DDS_DataReader_i *> (reader);
      if (!rdr)
        {
          DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::RTI::Getter_Base_T::data_reader - "
                       "Unable to cast provided DataReader to servant\n"));
          throw CORBA::INTERNAL ();
        }

      this->impl_ =  DDS_TYPE::data_reader::narrow (rdr->get_impl ());

      if (!this->impl_)
        {
          DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::RTI::Getter_Base_T::data_reader - "
                       "Unable to narrow the provided writer entity to the specific "
                       "type necessary to publish messages\n"));
          throw CORBA::INTERNAL ();
        }

      // Now create the waitset conditions
      this->ws_ = new DDSWaitSet ();
      this->rd_condition_ = this->impl ()->create_readcondition (DDS_NOT_READ_SAMPLE_STATE,
                                                       DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,
                                                       DDS_ALIVE_INSTANCE_STATE | DDS_NOT_ALIVE_INSTANCE_STATE);
      DDS_ReturnCode_t retcode = this->ws_->attach_condition (this->rd_condition_);
      if (retcode != DDS_RETCODE_OK)
        {
          DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "GETTER: Unable to attach read condition to waitset.\n"));
          throw CCM_DDS::InternalError (retcode, 1);
        }
    }
}

template <typename DDS_TYPE, typename CCM_TYPE>
bool
CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one (
  typename DDS_TYPE::value_type::_out_type an_instance,
  ::CCM_DDS::ReadInfo_out info)
{
  DDSConditionSeq active_conditions;
  if (!this->wait (active_conditions))
    {
      return false;
    }

  for (::DDS_Long i = 0; i < active_conditions.length(); i++)
    {
      if (active_conditions[i] == this->rd_condition_)
        {
          bool valid_data_read = false;

          while (!valid_data_read)
            {
              DDS_SampleInfoSeq sample_info;
              typename DDS_TYPE::dds_seq_type data;
              DDS_ReturnCode_t retcode = this->impl ()->read_w_condition (
                                                                  data,
                                                                  sample_info,
                                                                  1,
                                                                  this->rd_condition_);
              if (retcode == DDS_RETCODE_NO_DATA)
                {
                  DDS4CCM_DEBUG (6, (LM_DEBUG, CLINFO
                        "CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one - "
                        "DDS returned DDS_RETCODE_NO_DATA. No data available in DDS.\n"));
                  return false;
                }
              else if (retcode != DDS_RETCODE_OK)
                {
                  DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
                        "CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one - "
                        "Error while reading from DDS: <%C>\n",
                        translate_retcode (retcode)));
                  if (this->impl ()->return_loan (data, sample_info) !=
                      DDS_RETCODE_OK)
                    {
                      DDS4CCM_ERROR (1, (LM_ERROR,
                                  ACE_TEXT ("CIAO::DDS4CCM::RTI::Getter_T")
                                  ACE_TEXT ("<DDS_TYPE, CCM_TYPE, true>::get_one - ")
                                  ACE_TEXT ("return loan error\n")));
                    }
                  throw CCM_DDS::InternalError (retcode, 1);
                }
              else if (data.length () == 1 &&
                       sample_info[0].valid_data)
                {
                  info <<= sample_info[0];
                  an_instance = data[0];
                  valid_data_read = true;
                }
              else
                {
                  DDS4CCM_DEBUG (6, (LM_DEBUG, CLINFO
                        "CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one - "
                        "No valid available in DDS.\n"));
                }
              //return the loan of each read.
              if (this->impl ()->return_loan (data, sample_info) !=
                  DDS_RETCODE_OK)
                {
                  DDS4CCM_ERROR (1, (LM_ERROR,
                              ACE_TEXT ("CIAO::DDS4CCM::RTI::Getter_T")
                              ACE_TEXT ("<DDS_TYPE, CCM_TYPE, true>::get_one - ")
                              ACE_TEXT ("return loan error\n")));
                }
            }
        }
    }

  return true;
}

template <typename DDS_TYPE, typename CCM_TYPE>
bool
CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, false>::get_one (
  typename DDS_TYPE::value_type::_out_type an_instance,
  ::CCM_DDS::ReadInfo_out info)
{
  an_instance = new typename DDS_TYPE::value_type;
  DDSConditionSeq active_conditions;
  if (!this->wait (active_conditions))
    {
      return false;
    }

  DDS_SampleInfoSeq sample_info;
  typename DDS_TYPE::dds_seq_type data;
  for (::DDS_Long i = 0; i < active_conditions.length(); i++)
    {
      if (active_conditions[i] == this->rd_condition_)
        {
          bool valid_data_read = false;

          while (!valid_data_read)
            {
              DDS_SampleInfoSeq sample_info;
              typename DDS_TYPE::dds_seq_type data;
              DDS_ReturnCode_t retcode = this->impl ()->read_w_condition (
                                                                  data,
                                                                  sample_info,
                                                                  1,
                                                                  this->rd_condition_);
              if (retcode == DDS_RETCODE_NO_DATA)
                {
                  DDS4CCM_DEBUG (6, (LM_DEBUG, CLINFO
                        "CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one - "
                        "DDS returned DDS_RETCODE_NO_DATA. No data available in DDS.\n"));
                  return false;
                }
              else if (retcode != DDS_RETCODE_OK)
                {
                  DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
                        "CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one - "
                        "Error while reading from DDS: <%C>\n",
                        translate_retcode (retcode)));
                  if (this->impl ()->return_loan (data, sample_info) !=
                      DDS_RETCODE_OK)
                    {
                      DDS4CCM_ERROR (1, (LM_ERROR,
                                  ACE_TEXT ("CIAO::DDS4CCM::RTI::Getter_T")
                                  ACE_TEXT ("<DDS_TYPE, CCM_TYPE, true>::get_one - ")
                                  ACE_TEXT ("return loan error\n")));
                    }
                  throw CCM_DDS::InternalError (retcode, 1);
                }
              else if (data.length () == 1 &&
                       sample_info[0].valid_data)
                {
                  info <<= sample_info[0];
                  *an_instance = data[0];
                  valid_data_read = true;
                }
              else
                {
                  DDS4CCM_DEBUG (6, (LM_DEBUG, CLINFO
                        "CIAO::DDS4CCM::RTI::Getter_T<DDS_TYPE, CCM_TYPE, true>::get_one - "
                        "No valid available in DDS.\n"));
                }
              //return the loan of each read.
              if (this->impl ()->return_loan (data, sample_info) !=
                  DDS_RETCODE_OK)
                {
                  DDS4CCM_ERROR (1, (LM_ERROR,
                              ACE_TEXT ("CIAO::DDS4CCM::RTI::Getter_T")
                              ACE_TEXT ("<DDS_TYPE, CCM_TYPE, true>::get_one - ")
                              ACE_TEXT ("return loan error\n")));
                }
            }
        }
    }

  return true;
}