summaryrefslogtreecommitdiff
path: root/CIAO/connectors/dds4ccm/impl/Getter_T.cpp
blob: 85ee254e6ca63bb425963c613431362a67fafc27 (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// #include "dds4ccm/impl/Utils.h"

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

namespace CIAO
{
  namespace DDS4CCM
  {
    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::Getter_Base_T (void)
      : condition_manager_ (0),
        time_out_ (),
        max_delivered_data_ (0)
    {
      DDS4CCM_TRACE ("Getter_Base_T::Getter_Base_T");
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::~Getter_Base_T (void)
    {
      DDS4CCM_TRACE ("Getter_Base_T::~Getter_Base_T");
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    typename TYPED_DDS_READER::_ptr_type
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::dds_reader (void)
    {
      if (this->dds_reader_)
        {
          return this->dds_reader_;
        }
      else
        {
          DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (LM_DEBUG,
                        "CIAO::DDS4CCM::Getter_Base_T::impl - "
                        "Throwing BAD_INV_ORDER.\n"));
          throw ::CORBA::BAD_INV_ORDER ();
        }
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    ::DDS::ReturnCode_t
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::get (
      SEQ_VALUE_TYPE & data,
      ::DDS::SampleInfoSeq & sample_info,
      const ::CORBA::Long & max_samples)
    {
      DDS4CCM_TRACE ("Getter_Base_T::get");

      ::DDS::ReadCondition_var rc = this->condition_manager_->get_readcondition ();
      if (! ::CORBA::is_nil (rc.in ()))
        {
          // Read, using the read condition
          return this->get (data,
                            sample_info,
                            max_samples,
                            rc.in ());
        }
      else
        {
          // A filter has been set. Use the appropriate query condition
          // to read data from DDS.
          ::DDS::QueryCondition_var qc = this->condition_manager_->get_querycondition_getter ();
          if (::CORBA::is_nil (qc.in ()))
            {
              DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                            "Getter_Base_T::get - "
                            "Unable to retrieve QueryCondition from "
                            "Condition manager\n"));
              return ::DDS::RETCODE_ERROR;
            }
          return this->get (data,
                            sample_info,
                            max_samples,
                            qc.in ());
        }
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    ::DDS::ReturnCode_t
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::get (
      SEQ_VALUE_TYPE & data,
      ::DDS::SampleInfoSeq & sample_info,
      const ::CORBA::Long & max_samples,
      ::DDS::QueryCondition_ptr qc)
    {
      DDS4CCM_TRACE ("CIAO::DDS4CCM::Getter_Base_T::get");

      if (! ::CORBA::is_nil (qc))
        {
          return this->dds_reader ()->read_w_condition (data,
                                                  sample_info,
                                                  max_samples,
                                                  qc);
      }
      return ::DDS::RETCODE_ERROR;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    ::DDS::ReturnCode_t
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::get (
      SEQ_VALUE_TYPE & data,
      ::DDS::SampleInfoSeq & sample_info,
      const ::CORBA::Long & max_samples,
      ::DDS::ReadCondition_ptr rd)
    {
      DDS4CCM_TRACE ("CIAO::DDS4CCM::Getter_Base_T::get");

      if (! ::CORBA::is_nil (rd))
        {
          return this->dds_reader ()->read_w_condition (data,
                                                  sample_info,
                                                  max_samples,
                                                  rd);
        }
      return ::DDS::RETCODE_ERROR;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    bool
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::get_many (
      SEQ_VALUE_TYPE& instances,
      ::CCM_DDS::ReadInfoSeq& infos)
    {
      DDS4CCM_TRACE ("Getter_Base_T::get_many");

      if (!this->condition_manager_->wait (this->time_out_))
        {
          // Wait hasn't been triggered (no samples which match the attached
          // conditions are received).
          return false;
        }

      ::CORBA::Long max_samples = this->max_delivered_data_;
      if (max_samples == 0)
        {
          max_samples = ::DDS::LENGTH_UNLIMITED;
        }

      ::DDS::SampleInfoSeq sample_info;
      SEQ_VALUE_TYPE data;

      ::DDS::ReturnCode_t const retcode = this->get (data,
                                                     sample_info,
                                                     max_samples);

      if (retcode == ::DDS::RETCODE_OK && data.length () >= 1)
        {
          // Determine which samples are valid and return these to
          // the caller.
          ::CORBA::ULong number_read = 0;
          for (::CORBA::ULong index = 0; index < sample_info.length (); index ++)
            {
              if (sample_info[index].valid_data)
                {
                  ++number_read;
                }
            }
          DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (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 (::CORBA::ULong 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 (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                "Getter_Base_T::get_many - "
                "Error while reading from DDS: <%C>\n",
                translate_retcode (retcode)));

          ::DDS::ReturnCode_t const retval =
            this->dds_reader ()->return_loan (data, sample_info);
          if (retval != ::DDS::RETCODE_OK)
            {
              DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                "Getter_Base_T::get_many - "
                "Error returning loan to DDS - <%C>\n",
                translate_retcode (retval)));
            }
          throw ::CCM_DDS::InternalError (retcode, 0);
        }

      ::DDS::ReturnCode_t const retval =
        this->dds_reader ()->return_loan (data, sample_info);
      if (retval != ::DDS::RETCODE_OK)
        {
          DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
            "Getter_Base_T::get_many - "
            "Error returning loan to DDS - <%C>\n",
            translate_retcode (retval)));

          throw ::CCM_DDS::InternalError (retcode, 0);
        }

      return true;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    ::DDS::Duration_t
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::time_out (void)
    {
      DDS4CCM_TRACE ("Getter_Base_T::time_out");

      return this->time_out_;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    void
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::time_out (
      const ::DDS::Duration_t & time_out)
    {
      this->time_out_ = time_out;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    ::CCM_DDS::DataNumber_t
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::max_delivered_data (void)
    {
      return this->max_delivered_data_;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    void
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::max_delivered_data (
      ::CCM_DDS::DataNumber_t max_delivered_data)
    {
      this->max_delivered_data_ = max_delivered_data;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    void
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::set_dds_reader (
      ::DDS::DataReader_ptr dr,
      ::CIAO::DDS4CCM::ConditionManager * condition_manager)
    {
      DDS4CCM_TRACE ("Getter_Base_T::set_dds_reader");

      this->dds_reader_ = TYPED_DDS_READER::_narrow (dr);
      if (!::CORBA::is_nil (dr) && ::CORBA::is_nil (this->dds_reader_.in ()))
        {
          DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                        "Getter_Base_T::set_dds_reader - "
                        "narrow failed.\n"));
          throw ::CORBA::INTERNAL ();
        }
      this->condition_manager_ = condition_manager;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    ::DDS::DataReader_ptr
    Getter_Base_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE>::get_dds_reader (void)
    {
      return ::DDS::DataReader::_duplicate (this->dds_reader_.in ());
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    bool
    Getter_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE, true>::get_one (
      typename VALUE_TYPE::_out_type an_instance,
      ::CCM_DDS::ReadInfo_out info)
    {
      DDS4CCM_TRACE ("Getter_T::get_one");

      if (!this->condition_manager_->wait (this->time_out_))
        {
          // None of the attached conditions have triggered wait.
          return false;
        }

      bool valid_data_read = false;

      // Read the samples one by one until a valid sample
      // has been found.
      while (!valid_data_read)
        {
          ::DDS::SampleInfoSeq sample_info;
          SEQ_VALUE_TYPE data;

          ::DDS::ReturnCode_t const retcode = this->get (data, sample_info, 1);

          if (retcode == ::DDS::RETCODE_NO_DATA)
            {
              DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (LM_DEBUG, DDS4CCM_INFO
                            "Getter_T::get_one - "
                            "DDS returned <%C>. No data available in DDS.\n",
                            translate_retcode (retcode)));
              return false;
            }
          else if (retcode != ::DDS::RETCODE_OK)
            {
              // Something went wrong.
              DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "Error while reading from DDS: <%C>\n",
                    translate_retcode (retcode)));

              ::DDS::ReturnCode_t const retval =
                this->dds_reader ()->return_loan (data, sample_info);
              if (retval != ::DDS::RETCODE_OK)
                {
                  DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                    "Getter_Base_T::get_one - "
                    "Error returning loan to DDS - <%C>\n",
                    translate_retcode (retval)));
                }

              throw ::CCM_DDS::InternalError (retcode, 0);
            }
          else if (data.length () == 1 && sample_info[0].valid_data)
            {
              DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (LM_DEBUG, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "Read one valid sample from DDS.\n"));

              // Add the valid sample to the list which will be returned
              // to the caller
              info <<= sample_info[0];
              an_instance = data[0];
              valid_data_read = true;
            }
          else
            {
              DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (LM_DEBUG, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "No valid data available in DDS.\n"));
            }

          // Return the loan of each read.
          ::DDS::ReturnCode_t const retval =
            this->dds_reader ()->return_loan (data, sample_info);
          if (retval != ::DDS::RETCODE_OK)
            {
              DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                "Getter_T::get_one - "
                "Error returning loan to DDS - <%C>\n",
                translate_retcode (retval)));

              throw ::CCM_DDS::InternalError (retcode, 0);
            }
        }

      return valid_data_read;
    }

    template <typename GETTER_TYPE, typename TYPED_DDS_READER, typename VALUE_TYPE, typename SEQ_VALUE_TYPE>
    bool
    Getter_T<GETTER_TYPE, TYPED_DDS_READER, VALUE_TYPE, SEQ_VALUE_TYPE, false>::get_one (
      typename VALUE_TYPE::_out_type an_instance,
      ::CCM_DDS::ReadInfo_out info)
    {
      DDS4CCM_TRACE ("Getter_T::get_one");

      an_instance = 0;
      ACE_NEW_THROW_EX (an_instance,
                        VALUE_TYPE,
                        ::CORBA::NO_MEMORY ());
      if (!this->condition_manager_->wait (this->time_out_))
        {
          // None of the attached conditions have triggered wait.
          return false;
        }

      bool valid_data_read = false;
      ::DDS::SampleInfoSeq sample_info;
      SEQ_VALUE_TYPE data;

      // Read the samples one by one until a valid sample
      // has been found.
      while (!valid_data_read)
        {
          ::DDS::SampleInfoSeq sample_info;
          SEQ_VALUE_TYPE data;
          ::DDS::ReturnCode_t const retcode = this->get (data,
                                                         sample_info,
                                                         1);
          if (retcode == ::DDS::RETCODE_NO_DATA)
            {
              DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (LM_DEBUG, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "DDS returned <%C>. No data available in DDS.\n",
                    translate_retcode (retcode)));
              return false;
            }
          else if (retcode != ::DDS::RETCODE_OK)
            {
              DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "Error while reading from DDS: <%C>\n",
                    translate_retcode (retcode)));

              ::DDS::ReturnCode_t const retval =
                this->dds_reader ()->return_loan (data, sample_info);
              if (retval != ::DDS::RETCODE_OK)
                {
                  DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "Error returning loan to DDS - <%C>\n",
                    translate_retcode (retval)));
                }
              throw ::CCM_DDS::InternalError (retcode, 0);
            }
          else if (data.length () == 1 && sample_info[0].valid_data)
            {
              // Add the valid sample to the list which will be returned
              // to the caller
              info <<= sample_info[0];
              *an_instance = data[0];
              valid_data_read = true;
            }
          else
            {
              DDS4CCM_DEBUG (DDS4CCM_LOG_LEVEL_ACTION, (LM_DEBUG, DDS4CCM_INFO
                    "Getter_T::get_one - "
                    "No valid available in DDS.\n"));
            }
          // Return the loan of each read.
          ::DDS::ReturnCode_t const retval = this->dds_reader ()->return_loan (data, sample_info);
          if (retval != ::DDS::RETCODE_OK)
            {
              DDS4CCM_ERROR (DDS4CCM_LOG_LEVEL_ERROR, (LM_ERROR, DDS4CCM_INFO
                "Getter_T::get_one - "
                "Error returning loan to DDS - <%C>\n",
                translate_retcode (retval)));
              throw ::CCM_DDS::InternalError (retval, 0);
            }
        }

      return valid_data_read;
    }
  }
}