summaryrefslogtreecommitdiff
path: root/modules/CIAO/connectors/dds4ccm/impl/dds/Reader_T.cpp
blob: ecfc048b600d28dbda33226ac0447b88179d106e (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// $Id$

#include "dds4ccm/impl/dds/DataReader.h"
#include "dds4ccm/impl/dds/DataReaderListener_T.h"
#include "dds4ccm/impl/dds/Utils.h"
#include "dds4ccm/impl/dds/Subscriber.h"
#include "dds4ccm/impl/dds/QueryCondition.h"

#include "dds4ccm/impl/dds/ndds/SampleInfo.h"
#include "dds4ccm/impl/dds/ndds/StringSeq.h"

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

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::Reader_T (void)
  : reader_ (0),
    dds_get_ (0),
    library_name_ (""),
    profile_name_ ("")
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::Reader_T");
  #if (DDS4CCM_USES_QUERY_CONDITION==1)
    this->qc_ = 0;
  #else
    this->cft_ = 0;
  #endif
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::~Reader_T (void)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::~Reader_T");
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
typename DDS_TYPE::data_reader *
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::impl (void)
{
  if (this->reader_)
    {
      return DDS_TYPE::data_reader::narrow (this->reader_->get_impl ());
    }
  else
    {
      throw ::CORBA::BAD_INV_ORDER ();
    }
}

// For the requirement : 'samples ordered by instances' the following settings are necessary:
// ordered_access -> true   and     DDS_INSTANCE_PRESENTATION_QOS (default) .
template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
CORBA::ULong
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::get_nr_valid_samples (
  const DDS_SampleInfoSeq& sample_info,
  bool determine_last)
{
  CORBA::ULong nr_of_samples = 0;
  for (::DDS_Long i = 0 ; i < sample_info.length(); ++i)
    {
      if (determine_last)
        {
          if (sample_info[i].sample_rank == 0 && sample_info[i].valid_data)
            {
              ++nr_of_samples;
            }
        }
      else if (sample_info[i].valid_data)
        {
          ++nr_of_samples;
        }
    }
  return nr_of_samples;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::read_without_instance (
  typename DDS_TYPE::dds_seq_type & data,
  DDS_SampleInfoSeq & sample_info)
{
  DDS_ReturnCode_t retval = DDS_RETCODE_ERROR;
#if (DDS4CCM_USES_QUERY_CONDITION==1)
  if (this->qc_)
    {
      retval = this->impl ()->read_w_condition (data,
                                       sample_info,
                                       DDS_LENGTH_UNLIMITED,
                                       this->qc_);
    }
  else
    {
#endif
      retval = this->impl ()->read (data,
                                    sample_info,
                                    DDS_LENGTH_UNLIMITED,
                                    DDS_READ_SAMPLE_STATE | DDS_NOT_READ_SAMPLE_STATE,
                                    DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,
                                    DDS_ALIVE_INSTANCE_STATE);
#if (DDS4CCM_USES_QUERY_CONDITION==1)
    }
#endif
  if (retval != DDS_RETCODE_OK && retval != DDS_RETCODE_NO_DATA)
    {
      DDS_ReturnCode_t const retval = this->impl ()->return_loan (data, sample_info);
      if (retval != DDS_RETCODE_OK)
        {
          DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
            "CIAO::DDS4CCM::DDS_CCM::Reader_T::read_without_instance - "
            "Error returning loan to DDS - <%C>\n",
            translate_retcode (retval)));
        }
      DDS4CCM_ERROR (1, (LM_ERROR, ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_without_instance - ")
                            ACE_TEXT ("retval is %C\n"), translate_retcode(retval)));
      throw ::CCM_DDS::InternalError (retval, 0);
    }
}


template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::read_last (
  typename CCM_TYPE::seq_type::_out_type instances,
  ::CCM_DDS::ReadInfoSeq_out infos)
{
  // This function has to return the last sample of all instances
  DDS_SampleInfoSeq sample_info;
  typename DDS_TYPE::dds_seq_type data;

  this->read_without_instance (data, sample_info);

  typename CCM_TYPE::seq_type * inst_seq = 0;
  ACE_NEW_THROW_EX (inst_seq,
                    typename CCM_TYPE::seq_type,
                    CORBA::NO_MEMORY ());
  ::CCM_DDS::ReadInfoSeq * infoseq = 0;
  ACE_NEW_THROW_EX (infoseq,
                    ::CCM_DDS::ReadInfoSeq,
                    CORBA::NO_MEMORY ());

  CORBA::ULong const nr_of_last_samples = this->get_nr_valid_samples (sample_info, true);

  DDS4CCM_DEBUG (6, (LM_DEBUG, ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_last - ")
                            ACE_TEXT ("total number of samples <%u> - ")
                            ACE_TEXT ("last number of samples <%u>\n"),
                            data.length(),
                            nr_of_last_samples));
  CORBA::ULong ix = 0;

  infoseq->length (nr_of_last_samples);
  inst_seq->length (nr_of_last_samples);

  // We need only the last sample of each instance
  for (::DDS_Long i = 0 ; i < sample_info.length(); ++i)
    {
      if((sample_info[i].sample_rank == 0) && (sample_info[i].valid_data))
        {
          (*infoseq)[ix] <<= sample_info[i];
          (*inst_seq)[ix] = data[i];
          ++ix;
        }
    }

  // Return the loan
  DDS_ReturnCode_t const retval = this->impl ()->return_loan (data, sample_info);
  if (retval != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
        "CIAO::DDS4CCM::DDS_CCM::Reader_T::read_last - "
        "Error returning loan to DDS - <%C>\n",
        translate_retcode (retval)));
    }
  infos = infoseq;
  instances = inst_seq;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::read_all (
          typename CCM_TYPE::seq_type::_out_type instances,
          ::CCM_DDS::ReadInfoSeq_out infos)
{
  // This function has to return all samples of all instances
  DDS_SampleInfoSeq sample_info;
  typename DDS_TYPE::dds_seq_type data;

  this->read_without_instance (data, sample_info);

  CORBA::ULong const nr_of_valid_samples = this->get_nr_valid_samples (sample_info);
  DDS4CCM_DEBUG (6, (LM_DEBUG, ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_all - ")
                            ACE_TEXT ("total number of samples <%u> - ")
                            ACE_TEXT ("valid number of samples <%u>\n"),
                            data.length (),
                            nr_of_valid_samples));

  typename CCM_TYPE::seq_type * inst_seq = 0;
  ACE_NEW_THROW_EX (inst_seq,
                    typename CCM_TYPE::seq_type,
                    CORBA::NO_MEMORY ());
  ::CCM_DDS::ReadInfoSeq * infoseq = 0;
  ACE_NEW_THROW_EX (infoseq,
                    ::CCM_DDS::ReadInfoSeq,
                    CORBA::NO_MEMORY ());

  infoseq->length (nr_of_valid_samples);
  inst_seq->length (nr_of_valid_samples);

  CORBA::ULong ix = 0;

  for (::DDS_Long i = 0 ; i < sample_info.length(); ++i)
    {
        if(sample_info[i].valid_data)
          {
            (*infoseq)[ix] <<= sample_info[i];
            (*inst_seq)[ix] = data[i];
            ++ix;
          }
    }

  // Return the loan
  DDS_ReturnCode_t const retval = this->impl ()->return_loan (data, sample_info);
  if (retval != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
        "CIAO::DDS4CCM::DDS_CCM::Reader_T::read_all - "
        "Error returning loan to DDS - <%C>\n",
        translate_retcode (retval)));
    }

  infos = infoseq;
  instances = inst_seq;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
DDS_InstanceHandle_t
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::check_handle (
  const typename DDS_TYPE::value_type& an_instance,
  const ::DDS::InstanceHandle_t & instance_handle)
{
  DDS_InstanceHandle_t hnd = ::DDS_HANDLE_NIL;
  hnd <<= instance_handle;

  DDS_InstanceHandle_t const lookup_hnd =
      this->impl ()->lookup_instance (an_instance);
  if (!DDS_InstanceHandle_equals (&hnd, &::DDS_HANDLE_NIL) &&
      !DDS_InstanceHandle_equals (&hnd, &lookup_hnd))
    {
      throw ::CCM_DDS::InternalError (0, 0);
    }
  if (DDS_InstanceHandle_equals (&lookup_hnd, &::DDS_HANDLE_NIL))
    {
      throw ::CCM_DDS::NonExistent ();
    }
  return lookup_hnd;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::read_with_instance (
  typename DDS_TYPE::dds_seq_type & data,
  const ::DDS_InstanceHandle_t & lookup_hnd,
  DDS_SampleInfoSeq & sample_info)
{
  DDS4CCM_DEBUG (6, (LM_INFO, ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_with_instance - ")
                           ACE_TEXT ("Reading with instance.\n")));
  DDS_ReturnCode_t const retval = this->impl ()->read_instance (
                                      data,
                                      sample_info,
                                      DDS_LENGTH_UNLIMITED,
                                      lookup_hnd,
                                      DDS_READ_SAMPLE_STATE | DDS_NOT_READ_SAMPLE_STATE ,
                                      DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,
                                      DDS_ALIVE_INSTANCE_STATE);
  if (retval != DDS_RETCODE_OK && retval != DDS_RETCODE_NO_DATA)
    {
      DDS_ReturnCode_t const retval = this->impl ()->return_loan (data, sample_info);
      if (retval != DDS_RETCODE_OK)
        {
          DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
            "CIAO::DDS4CCM::DDS_CCM::Reader_T::read_with_instance - "
            "Error returning loan to DDS - <%C>\n",
            translate_retcode (retval)));
        }
      DDS4CCM_ERROR (1, (LM_ERROR, ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_with_instance - ")
                            ACE_TEXT ("retval is %C\n"), translate_retcode(retval)));
      throw ::CCM_DDS::InternalError (retval, 0);
    }
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::read_one_last (
  typename DDS_TYPE::value_type& an_instance,
  ::CCM_DDS::ReadInfo_out info,
  const ::DDS::InstanceHandle_t & instance_handle)
{
  DDS_InstanceHandle_t const lookup_hnd = this->check_handle (an_instance, instance_handle);

  DDS_SampleInfoSeq sample_info;
  typename DDS_TYPE::dds_seq_type data;

  // For now, only read with instance...
  this->read_with_instance (data, lookup_hnd, sample_info);

  ::DDS_Long sample = data.length();
  DDS4CCM_DEBUG (6, (LM_INFO,
              ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_one_last - ")
              ACE_TEXT ("total number of samples <%u>\n"),
              sample));
  while (sample >= 0 && !sample_info[sample-1].valid_data)
    {
      --sample;
    }
  if (sample >= 0)
    {
      if(sample_info[sample-1].valid_data)
        {
          an_instance = data[sample-1];
          info <<= sample_info[sample-1];
        }
    }
  // Return the loan
  DDS_ReturnCode_t const retval = this->impl ()->return_loan (data, sample_info);
  if (retval != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
        "CIAO::DDS4CCM::DDS_CCM::Reader_T::read_one_last - "
        "Error returning loan to DDS - <%C>\n",
        translate_retcode (retval)));
    }
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::read_one_all (
  const typename DDS_TYPE::value_type& an_instance,
  typename CCM_TYPE::seq_type::_out_type instances,
  ::CCM_DDS::ReadInfoSeq_out infos,
  const ::DDS::InstanceHandle_t & instance_handle)
{
  DDS_InstanceHandle_t const lookup_hnd =
    this->check_handle (an_instance, instance_handle);

  DDS_SampleInfoSeq sample_info;
  typename DDS_TYPE::dds_seq_type data;

  this->read_with_instance (data, lookup_hnd, sample_info);

  // Count the number of valid samples
  CORBA::ULong const nr_of_valid_samples = this->get_nr_valid_samples (sample_info);
  DDS4CCM_DEBUG (6, (LM_DEBUG, ACE_TEXT ("CIAO::DDS4CCM::DDS_CCM::Reader_T::read_all - ")
                            ACE_TEXT ("total number of samples <%u> - ")
                            ACE_TEXT ("valid number of samples <%u>\n"),
                            data.length (),
                            nr_of_valid_samples));

  typename CCM_TYPE::seq_type * inst_seq = 0;
  ACE_NEW_THROW_EX (inst_seq,
                    typename CCM_TYPE::seq_type (nr_of_valid_samples),
                    CORBA::NO_MEMORY ());
  ::CCM_DDS::ReadInfoSeq * infoseq = 0;
  ACE_NEW_THROW_EX (infoseq,
                    ::CCM_DDS::ReadInfoSeq (nr_of_valid_samples),
                    CORBA::NO_MEMORY ());

  infoseq->length (nr_of_valid_samples);
  inst_seq->length (nr_of_valid_samples);

  // Copy the valid samples
  CORBA::ULong ix = 0;
  for (::DDS_Long i = 0 ; i < sample_info.length(); ++i)
    {
      if(sample_info[i].valid_data)
        {
          (*infoseq)[ix] <<= sample_info[i];
          (*inst_seq)[ix] = data[i];
          ++ix;
        }
    }

  // Return the loan
  DDS_ReturnCode_t const retval = this->impl ()->return_loan (data, sample_info);
  if (retval != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO
        "CIAO::DDS4CCM::DDS_CCM::Reader_T::read_one_all - "
        "Error returning loan to DDS - <%C>\n",
        translate_retcode (retval)));
    }

  infos = infoseq;
  instances = inst_seq;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::create_contentfilteredtopic (
  const ::CCM_DDS::QueryFilter & filter,
  ::DDSSubscriber * sub)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::create_contentfilteredtopic");

  // To create a ContentFilteredTopic we need a DDSTopic.
  // Since a ContentFilteredTopic is created on the DomainParticipant,
  // we need to obtain the DomainParticipant through the
  // Subscriber.
  ::DDSDomainParticipant * dp = sub->get_participant ();
  if (!dp)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::create_contentfilteredtopic - "
                        "Unable to get the Participant from the DDS Subscriber\n"));
      throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 1);
    }
  // Now, get the topic.
  ::DDSTopicDescription * td = this->reader_->get_impl ()->get_topicdescription ();
  if (!td)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::create_contentfilteredtopic - "
                        "Unable to get the TopicDescription from the DDS DomainParticipant\n"));
      throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 2);
    }
  ::DDSTopic * tp = ::DDSTopic::narrow (td);
  if (!tp)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::create_contentfilteredtopic - "
                        "Unable to narrow the DDS TopicDescription to a DDS Topic\n"));
      throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 3);
    }
  // Now create the ContentFilteredTopic
  DDS_StringSeq params;
  params <<= filter.query_parameters;
  this->cft_ = dp->create_contentfilteredtopic (
                        "DDS4CCMContentFilteredTopic",
                        tp,
                        filter.query,
                        params);
  if (!this->cft_)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::create_contentfilteredtopic - "
                    "Error: Unable to create ContentFilteredTopic.\n"));
      throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 4);
    }
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::delete_datareader (
  ::DDSSubscriber * sub)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::delete_datareader");
  DDS_ReturnCode_t const retval = sub->delete_datareader (this->impl ());
  if (retval != DDS_RETCODE_OK)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::delete_datareader - "
                                "Unable to delete original DataReader. "
                                "Retval is %C\n",
                                translate_retcode(retval)));
    }
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::create_filter (
  const ::CCM_DDS::QueryFilter & filter)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::create_filter");

  // we need to use the DDS entities direct since we're not allowed
  // to change the CORBA interfaces.  These are known to component
  // the end user has created.

  // To set a ContentFilteredTopic on a DataReader, the DataReader
  // should be recreated. Since the Getter uses the same DataReader,
  // the original DataReader should not be destroyed.
  ::DDSSubscriber * sub = this->impl ()->get_subscriber ();
  if (!sub)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::create_filter - "
                        "Unable to get the Subscriber from the type specific DDS DataReader\n"));
      throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 0);
    }
  this->create_contentfilteredtopic (filter, sub);

  // Now recreate the DataReader, using the ContentFilteredTopic.
  // After recreation, connect the original DataReaderListener to it.
  ::DDSDataReaderListener *drl = this->impl ()->get_listener ();

  ::DDSDataReader * dr = 0;
  if (this->library_name_.length () > 0 &&
      this->profile_name_.length () > 0)
    {
      dr = sub->create_datareader_with_profile (this->cft_,
                                                this->library_name_.c_str (),
                                                this->profile_name_.c_str (),
                                                drl,
                                                ::DDS::DATA_AVAILABLE_STATUS |
                                                ::DDS::REQUESTED_DEADLINE_MISSED_STATUS |
                                                ::DDS::SAMPLE_LOST_STATUS);
    }
  else
    {
      DDS_DataReaderQos const qos = DDS_DATAREADER_QOS_DEFAULT;
      dr = sub->create_datareader (this->cft_,
                                   qos,
                                   drl,
                                   ::DDS::DATA_AVAILABLE_STATUS |
                                   ::DDS::REQUESTED_DEADLINE_MISSED_STATUS |
                                   ::DDS::SAMPLE_LOST_STATUS);
    }
  if (!dr)
    {
      DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::create_filter - "
                    "Error: Unable to create a new DataReader.\n"));
      throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 1);
    }
  // Inform the Getter that there's a new DataReader created
  if (this->dds_get_)
    {
      this->dds_get_->remove_conditions ();
      this->delete_datareader (sub);
      // Now we need to set the new created DataReader in our proxy classes.
      this->reader_->set_impl (dr);
      this->dds_get_->set_impl (this->reader_);
    }
  else
    {
      this->delete_datareader (sub);
      this->reader_->set_impl (dr);
    }
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
 ::CCM_DDS::QueryFilter *
 CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::filter (void)
{
  #if (DDS4CCM_USES_QUERY_CONDITION==1)
    if (!this->qc_)
      {
        DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::filter - "
                      "Error: No QueryCondition set yet. First set a filter.\n"));
        throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 0);
      }
    ::CCM_DDS::QueryFilter_var filter = 0;
    ACE_NEW_THROW_EX (filter,
                      ::CCM_DDS::QueryFilter(),
                      CORBA::NO_MEMORY ());
    filter->query = this->qc_->get_query_expression ();
    ::DDS_StringSeq dds_qp;
    this->qc_->get_query_parameters (dds_qp);
    filter->query_parameters <<= dds_qp;
    return filter._retn ();
  #else
    if (!this->cft_)
      {
        DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::filter - "
                      "Error: No ContentFilter set yet. First set a filter.\n"));
        throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 0);
      }
    ::CCM_DDS::QueryFilter_var filter = 0;
    ACE_NEW_THROW_EX (filter,
                      ::CCM_DDS::QueryFilter(),
                      CORBA::NO_MEMORY ());
    filter->query = this->cft_->get_filter_expression ();
    DDS_StringSeq params;
    ::DDS::ReturnCode_t const retval = this->cft_->get_expression_parameters (
                                          params);
    filter->query_parameters <<= params;
    if (retval != DDS::RETCODE_OK)
      {
        DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::filter - "
                                  "Error getting expression_parameters. "
                                  "Retval is %C\n",
                                  translate_retcode(retval)));
        throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, retval);
      }
    return filter._retn ();
  #endif
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::filter (
  const ::CCM_DDS::QueryFilter & filter)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::filter");
  #if (DDS4CCM_USES_QUERY_CONDITION==1)
    if (!this->qc_)
      {
        ::DDS_StringSeq dds_qp;
        dds_qp <<= filter.query_parameters;
        this->qc_ = this->impl ()->create_querycondition (
                                DDS_READ_SAMPLE_STATE | DDS_NOT_READ_SAMPLE_STATE,
                                DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE,
                                DDS_ALIVE_INSTANCE_STATE,
                                filter.query,
                                dds_qp);
        if (!this->qc_)
          {
            DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::filter - "
                                      "Error creating query condition."));
            throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, 1);
          }
        if (this->dds_get_)
          {
            this->dds_get_->create_querycondition (filter.query, dds_qp);
          }
      }
    else
      {
        ::DDS_StringSeq dds_qp;
        dds_qp <<= filter.query_parameters;
        ::DDS::ReturnCode_t retval = this->qc_->set_query_parameters (
                                                  dds_qp);
        if (retval != ::DDS::RETCODE_OK)
          {
            DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::filter - "
                                      "Error setting expression_parameters. "
                                      "Retval is %C\n",
                                      translate_retcode(retval)));
            throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, retval);
          }
        if (this->dds_get_)
          {
            this->dds_get_->set_queryparameters (dds_qp);
          }
      }
  #else
    if (!this->cft_)
      {
        this->create_filter (filter);
      }
    else
      {
        DDS_StringSeq params;
        params <<= filter.query_parameters;
        ::DDS::ReturnCode_t retval = this->cft_->set_expression_parameters (
          params);
        if (retval != ::DDS::RETCODE_OK)
          {
            DDS4CCM_ERROR (1, (LM_ERROR, CLINFO "CIAO::DDS4CCM::DDS_CCM::Reader_T::filter - "
                                      "Error setting expression_parameters. "
                                      "Retval is %C\n",
                                      translate_retcode(retval)));
            throw CCM_DDS::InternalError (::DDS::RETCODE_ERROR, retval);
          }
      }
  #endif
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::set_contentfilteredtopic_data (
  const char * library_name,
  const char * profile_name,
  Getter_T<DDS_TYPE, CCM_TYPE, FIXED> * dds_get)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::set_contentfilteredtopic_data");
  this->library_name_ = library_name;
  this->profile_name_ = profile_name;
  this->dds_get_ = dds_get;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
DDS_ReturnCode_t
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::passivate ()
{
  #if (DDS4CCM_USES_QUERY_CONDITION==1)
    if (this->qc_)
      {
        return this->impl ()->delete_readcondition (this->qc_);
      }
  #endif
  return DDS_RETCODE_OK;
}

template <typename DDS_TYPE, typename CCM_TYPE, bool FIXED>
void
CIAO::DDS4CCM::DDS_CCM::Reader_T<DDS_TYPE, CCM_TYPE, FIXED>::set_impl (
  CCM_DDS_DataReader_i *reader)
{
  DDS4CCM_TRACE ("CIAO::DDS4CCM::DDS_CCM::Reader_T::set_impl");
  this->reader_ = reader;
}