summaryrefslogtreecommitdiff
path: root/modules/CIAO/connectors/dds4ccm/examples/Hello/Receiver/Hello_Receiver_Connector_exec.cpp
blob: 8440156b8f782a52be8f69a5bb040e5790b77b03 (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
// -*- C++ -*-
//
// $Id$

// ****  Code generated by the The ACE ORB (TAO) IDL Compiler v1.6.9 ****
// TAO and the TAO IDL Compiler have been developed by:
//       Center for Distributed Object Computing
//       Washington University
//       St. Louis, MO
//       USA
//       http://www.cs.wustl.edu/~schmidt/doc-center.html
// and
//       Distributed Object Computing Laboratory
//       University of California at Irvine
//       Irvine, CA
//       USA
//       http://doc.ece.uci.edu/
// and
//       Institute for Software Integrated Systems
//       Vanderbilt University
//       Nashville, TN
//       USA
//       http://www.isis.vanderbilt.edu/
//
// Information about TAO is available at:
//     http://www.cs.wustl.edu/~schmidt/TAO.html

// TAO_IDL - Generated from
// be/be_codegen.cpp:1278

#define MAX_STRING_SIZE         1024

#include "Hello_Receiver_Connector_exec.h"
#include "ciao/CIAO_common.h"

#include "dds4ccm/impl/ndds/DomainParticipantFactory.h"
#include "dds4ccm/impl/ndds/DataReader.h"
#include "dds4ccm/impl/ndds/Time_t.h"

/* The listener of events and data from the middleware */
class HelloListener : public ::DDS::DataReaderListener
{
  public:
    HelloListener ( ::CCM_DDS::string_RawListener_ptr listener,
                    ::CCM_DDS::PortStatusListener_ptr statuslistener);
    virtual void on_data_available( ::DDS::DataReader *reader);
    virtual void
    on_requested_deadline_missed (
      ::DDS::DataReader_ptr the_reader,
      const ::DDS::RequestedDeadlineMissedStatus & status);
    virtual void
    on_sample_lost (
      ::DDS::DataReader_ptr the_reader,
      const ::DDS::SampleLostStatus & status);
    bool enabled () const;
    void enabled (bool enable);
  private:
    ACE_Atomic_Op <TAO_SYNCH_MUTEX, bool> enabled_;
    ::CCM_DDS::string_RawListener_var listener_;
    ::CCM_DDS::PortStatusListener_var statuslistener_;
};

/* This method gets called back by DDS when one or more data samples have been
 * received.
 */

HelloListener::HelloListener ( ::CCM_DDS::string_RawListener_ptr listener, ::CCM_DDS::PortStatusListener_ptr statuslistener) :
  enabled_ (true), // @todo should be false later on
  listener_ ( ::CCM_DDS::string_RawListener::_duplicate (listener)),
  statuslistener_ (::CCM_DDS::PortStatusListener::_duplicate (statuslistener))
{
}

bool
HelloListener::enabled () const
{
  return this->enabled_.value ();
}

void
HelloListener::enabled (bool enable)
{
  this->enabled_ = enable;
}

void
HelloListener::on_requested_deadline_missed (
      ::DDS::DataReader_ptr the_reader,
      const ::DDS::RequestedDeadlineMissedStatus & status)
{
  this->statuslistener_->on_requested_deadline_missed (the_reader, status);
}

void
HelloListener::on_sample_lost (
      ::DDS::DataReader_ptr the_reader,
      const ::DDS::SampleLostStatus & status)
{
  this->statuslistener_->on_sample_lost (the_reader, status);
}

void
HelloListener::on_data_available( ::DDS::DataReader *reader)
{
  if (this->enabled_.value ())
    {
    /* Perform a safe type-cast from a generic data reader into a
     * specific data reader for the type "DDS::String"
     */
    ::CIAO::DDS4CCM::RTI::RTI_DataReader_i* rd = dynamic_cast < ::CIAO::DDS4CCM::RTI::RTI_DataReader_i*>(reader);
    DDSStringDataReader * string_reader = DDSStringDataReader::narrow(rd->get_datareader ());
    if (!string_reader) {
        /* In this specific case, this will never fail */
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("DDSStringDataReader::narrow failed.\n")));
        return;
    }

    /* Loop until there are messages available in the queue */
    for(;;) {
        char sample[MAX_STRING_SIZE];
        char *ptr_sample  = &sample[0];
        DDS_SampleInfo        info;
        DDS_ReturnCode_t retcode = string_reader->take_next_sample(
                            ptr_sample,
                            info);
        if (retcode == DDS_RETCODE_NO_DATA) {
            /* No more samples */
            break;
        } else if (retcode != DDS_RETCODE_OK) {
            ACE_ERROR ((LM_ERROR, ACE_TEXT ("Unable to take data from data reader, error %d.\n"), retcode));
            return;
        }
        if (info.valid_data) {
            ::CCM_DDS::ReadInfo empty;
            listener_->on_data (sample, empty);
    }
  }
  }
}

namespace CIAO_Hello_DDS_Hello_receiver_Connector_Impl
{
  //============================================================
  // Facet Executor Implementation Class: string_Reader_exec_i
  //============================================================

  string_Reader_exec_i::string_Reader_exec_i (DDSStringDataReader *reader)
    : reader_ (reader),
      condition_ (0)
  {
  }

  string_Reader_exec_i::~string_Reader_exec_i (void)
  {
  }

  // Operations from ::CCM_DDS::string_Reader

  void
  string_Reader_exec_i::read_all (
    ::CCM_DDS::string_Reader::stringSeq_out /* instances */,
    ::CCM_DDS::ReadInfoSeq_out /* infos */)
  {
    /* Your code here. */
  }

  void
  string_Reader_exec_i::read_all_history (
    ::CCM_DDS::string_Reader::stringSeq_out /* instances */,
    ::CCM_DDS::ReadInfoSeq_out /* infos */)
  {
    /*     DDS_StringSeq data;
    DDS_SampleInfoSeq sample_info;
    DDS_ReturnCode_t retval = this->reader_->read (data, sample_info);
 */
  }

  void
  string_Reader_exec_i::read_one (
    char *& an_instance,
    ::CCM_DDS::ReadInfo_out info)
  {
    // This has to be reworked using a reader template and traits. 
    DDS_StringSeq data;
    DDS_SampleInfoSeq sample_info;
    DDS_ReturnCode_t retval;
    if (this->condition_)
      {
        retval = this->reader_->read_w_condition (data, sample_info, 1, this->condition_);
      }
    else
      {
        retval = this->reader_->read (data,
                           sample_info,
                           1,
                           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)
      {
        an_instance = data[0];
        info.timestamp <<= sample_info[0].reception_timestamp;
      }
    else
      {
        //printf ("failed %d", retval);
        throw ::CCM_DDS::InternalError (retval, 0);
      }
  }

  void
  string_Reader_exec_i::read_one_history (
    const char * an_instance,
    ::CCM_DDS::string_Reader::stringSeq_out /* instances */,
    ::CCM_DDS::ReadInfoSeq_out /* infos */)
  {
    ::DDS_InstanceHandle_t instance_handle = this->reader_->lookup_instance (an_instance);
    if (!DDS_InstanceHandle_equals (&instance_handle, & ::DDS_HANDLE_NIL))
      {/*
        DDS_StringSeq data;
        DDS_SampleInfoSeq sample_info;
        DDS_ReturnCode_t retval;
        retval = this->reader_->read_instance (data, sample_info, DDS_LENGTH_UNLIMITED, instance_handle);
        if (retval == DDS_RETCODE_OK)
          {
            // todo
          }
        */
      }
    else
      {
        ::CCM_DDS::InstanceNumberSeq seq;
        throw ::CCM_DDS::NonExistent (seq);
      }
  }

  ::CCM_DDS::QueryFilter *
  string_Reader_exec_i::filter (void)
  {
    ::CCM_DDS::QueryFilter *qf = new ::CCM_DDS::QueryFilter;
    qf->query = CORBA::string_dup (this->condition_->get_query_expression());
    ::DDS_StringSeq seq;
    ::DDS_ReturnCode_t const retval = this->condition_->get_query_parameters (seq);
    if (retval == DDS_RETCODE_OK)
      {
        qf->query_parameters.length (seq.length ());
        for (::DDS_Long index = 0; index < seq.length (); index++)
          {
            qf->query_parameters[index] = CORBA::string_dup (seq[index]);
          }
      }

    return qf;
  }

  void
  string_Reader_exec_i::filter (
    const ::CCM_DDS::QueryFilter &filter)
  {
    if (this->condition_)
      {
        DDS_ReturnCode_t const retval = this->reader_->delete_readcondition (this->condition_);
        ACE_UNUSED_ARG (retval);
      }
    DDS_StringSeq params;
    for (::DDS::StringSeq::size_type index = 0; index < filter.query_parameters.length (); index++)
      {
        //params[index] = filter.query_parameters[index].in ();
      }
    this->condition_ = this->reader_->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,
      params);
    if (!this->condition_)
      {
        throw ::CCM_DDS::BadParameter ();
      }
  }
  //============================================================
  // Facet Executor Implementation Class: ListenerControl_exec_i
  //============================================================

  ListenerControl_exec_i::ListenerControl_exec_i (void)
  {
  }

  ListenerControl_exec_i::~ListenerControl_exec_i (void)
  {
  }

  // Operations from ::CCM_DDS::ListenerControl

  ::CORBA::Boolean
  ListenerControl_exec_i::enabled (void)
  {
    // Need to control the data listener
    /* Your code here. */
    return false;
  }

  void
  ListenerControl_exec_i::enabled (
    ::CORBA::Boolean enabled)
  {
    if (enabled)
      {
        // @todo
      }
  }
  //============================================================
  // Facet Executor Implementation Class: DataReader_exec_i
  //============================================================

  //============================================================
  // Component Executor Implementation Class: Hello_receiver_Connector_exec_i
  //============================================================

  Hello_receiver_Connector_exec_i::Hello_receiver_Connector_exec_i (void)
    : dds_configured_ (false)
  {
  }

  Hello_receiver_Connector_exec_i::~Hello_receiver_Connector_exec_i (void)
  {
  }

  // Supported operations and attributes.

  // Component attributes.

  // Port operations.

  ::CCM_DDS::CCM_string_Reader_ptr
  Hello_receiver_Connector_exec_i::get_receiver_data (void)
  {
    this->configure_dds ();
    ::CIAO::DDS4CCM::RTI::RTI_DataReader_i* rd = dynamic_cast < ::CIAO::DDS4CCM::RTI::RTI_DataReader_i*>(this->dr_.in ());
    DDSStringDataReader * string_reader = DDSStringDataReader::narrow(rd->get_datareader ());
    if (string_reader)
      return new string_Reader_exec_i (string_reader);
      
    throw ::CORBA::INTERNAL ();
  }

  ::CCM_DDS::CCM_ListenerControl_ptr
  Hello_receiver_Connector_exec_i::get_receiver_control (void)
  {
    this->configure_dds ();
//    return ::DDS::CCM_DataWriter::_duplicate (this->dw_.in ());

    /* Your code here. */
    // @todo
    return new ListenerControl_exec_i ();
  }

  ::DDS::CCM_DataReader_ptr
  Hello_receiver_Connector_exec_i::get_receiver_dds_entity (void)
  {
    this->configure_dds ();
    /* Your code here. */
    // @todo
    return ::DDS::CCM_DataReader::_duplicate (this->dr_.in ());
  }

  // Operations from Components::SessionComponent.

  void
  Hello_receiver_Connector_exec_i::set_session_context (
    ::Components::SessionContext_ptr ctx)
  {
    ::Hello_DDS::CCM_Hello_receiver_Connector_Context_var lctx =
      ::Hello_DDS::CCM_Hello_receiver_Connector_Context::_narrow (ctx);

    if ( ::CORBA::is_nil (lctx.in ()))
      {
        throw ::CORBA::INTERNAL ();
      }
      
    this->context_ = lctx;
  }

  void
  Hello_receiver_Connector_exec_i::configuration_complete (void)
  {
    this->configure_dds ();
    /* Your code here. */
  }

  void
  Hello_receiver_Connector_exec_i::ccm_activate (void)
  {
    /* Your code here. */
    /* Test code to test the reader interface
    ::CCM_DDS::CCM_string_Reader_var reader = this->get_receiver_data ();
    CORBA::String_var string;
    CCM_DDS::ReadInfo_var ri;
    reader->read_one (string.out (),  ri.out ());
    printf ("read <%s>\n", string.in ());
    */
  }

  void
  Hello_receiver_Connector_exec_i::ccm_passivate (void)
  {
    /* Your code here. */
  }

  void
  Hello_receiver_Connector_exec_i::ccm_remove (void)
  {
    /* Your code here. */
  }

  void
  Hello_receiver_Connector_exec_i::configure_dds (void)
  {
    if (this->dds_configured_)
      return;

    try
      {
        //NDDSConfigLogger::get_instance()->set_verbosity_by_category(NDDS_CONFIG_LOG_CATEGORY_API,
          //                                                        NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL );

        dpf_ = new CIAO::DDS4CCM::RTI::RTI_DomainParticipantFactory_i ();
        ::DDS::DomainParticipantQos qos;
        dp_ = dpf_->create_participant (0, qos, 0, 0);

        ::DDS::TopicQos tqos;
        t_ = dp_->create_topic ("Hello, World",
                                CIAO::DDS4CCM::RTI::String_Traits::type_support::get_type_name (),
                                tqos,
                                0,
                                DDS_STATUS_MASK_NONE);

        ::DDS::SubscriberQos sqos;
        sub_ = dp_->create_subscriber (sqos,
                                      0,
                                      0);

        ::DDS::DataReaderQos drqos;
        ::CCM_DDS::string_RawListener_var rawlistener =
          this->context_->get_connection_receiver_listener ();

        ::CCM_DDS::PortStatusListener_var portstatuslistener =
          this->context_->get_connection_receiver_status ();

        ::DDS::DataReaderListener_ptr temp = new HelloListener (rawlistener.in (), portstatuslistener.in ());
        this->listener_ = temp;
        DDS::DataReader_var drv_tmp = sub_->create_datareader (t_.in (),
                                                               drqos,
                                                               listener_.in (),
                                                               DDS_DATA_AVAILABLE_STATUS);

        dr_ = DDS::CCM_DataReader::_narrow (drv_tmp);
        printf ("setup done\n");
      }
    catch (const CORBA::Exception &ex)
      {
        CIAO_ERROR ((LM_ERROR, CLINFO "Hello_receiver_Connector_exec_i::configure_dds - "
                     "Caught CORBA exception %C\n",
                     ex._info ().c_str ()));
      }
    catch (...)
      {
        CIAO_ERROR ((LM_ERROR, CLINFO "Hello_receiver_Connector_exec_i::configure_dds - "
                     "Error: caught unknown C++ exception while configuring DDS\n"));
      }

    this->dds_configured_ = true;
  }

  extern "C" HELLO_RECEIVER_CONNECTOR_EXEC_Export ::Components::EnterpriseComponent_ptr
  create_Hello_DDS_Hello_receiver_Connector_Impl (void)
  {
    ::Components::EnterpriseComponent_ptr retval =
      ::Components::EnterpriseComponent::_nil ();

    ACE_NEW_RETURN (
      retval,
      Hello_receiver_Connector_exec_i,
      ::Components::EnterpriseComponent::_nil ());

    return retval;
  }
}

namespace CIAO_Hello_DDS_Hello_receiver_Connector_Impl
{
  //============================================================
  // Home Executor Implementation Class: ReceiverConnectorHome_exec_i
  //============================================================

  ReceiverConnectorHome_exec_i::ReceiverConnectorHome_exec_i (void)
  {
  }

  ReceiverConnectorHome_exec_i::~ReceiverConnectorHome_exec_i (void)
  {
  }

  // All operations and attributes.

  // Factory operations.

  // Finder operations.

  // Implicit operations.

  ::Components::EnterpriseComponent_ptr
  ReceiverConnectorHome_exec_i::create (void)
  {
    ::Components::EnterpriseComponent_ptr retval =
      ::Components::EnterpriseComponent::_nil ();

    ACE_NEW_THROW_EX (
      retval,
      Hello_receiver_Connector_exec_i,
      ::CORBA::NO_MEMORY ());

    return retval;
  }

  extern "C" HELLO_RECEIVER_CONNECTOR_EXEC_Export ::Components::HomeExecutorBase_ptr
  create_Hello_DDS_ReceiverConnectorHome_Impl (void)
  {
    ::Components::HomeExecutorBase_ptr retval =
      ::Components::HomeExecutorBase::_nil ();

    ACE_NEW_RETURN (
      retval,
      ReceiverConnectorHome_exec_i,
      ::Components::HomeExecutorBase::_nil ());

    return retval;
  }
}