static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domain_id, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } // --- Create subscriber --- // DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } /* Create DynamicData using TypeCode from Shapes.cxx * If you are NOT using a type generated with rtiddsgen, you * need to create this TypeCode from scratch. */ DDS.TypeCode type_code = ShapeType.get_typecode(); if (type_code == null) { shutdown(participant); throw new ApplicationException("create_typecode error"); } /* Create the Dynamic data type support object */ DDS.DynamicDataTypeSupport type_support = new DDS.DynamicDataTypeSupport(type_code, new DDS.DynamicDataTypeProperty_t()); if (type_support == null) { shutdown(participant); throw new ApplicationException("create_type_support error"); } // --- Create topic --- // /* Register type before creating topic */ System.String type_name = EXAMPLE_TYPE_NAME; try { type_support.register_type(participant, EXAMPLE_TYPE_NAME); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* Make sure both publisher and subscriber share the same topic name. * In the Shapes example: we are subscribing to a Square, wich is the * topic name. If you want to publish other shapes (Triangle or Circle), * you just need to update the topic name. */ DDS.Topic topic = participant.create_topic( "Square", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } // --- Create reader --- // /* Create a data reader listener */ ShapeTypeListener reader_listener = new ShapeTypeListener(); /* First, we create a generic DataReader for our topic */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); if (reader == null) { shutdown(participant); reader_listener = null; throw new ApplicationException("create_datareader error"); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 4000; // milliseconds for (int count=0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "ShapeType subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_listener = null; }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domain_id, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } // --- Create subscriber --- // DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } /* Create DynamicData using TypeCode from Shapes.cxx * If you are NOT using a type generated with rtiddsgen, you * need to create this TypeCode from scratch. */ DDS.TypeCode type_code = ShapeType.get_typecode(); if (type_code == null) { shutdown(participant); throw new ApplicationException("create_typecode error"); } /* Create the Dynamic data type support object */ DDS.DynamicDataTypeSupport type_support = new DDS.DynamicDataTypeSupport(type_code, new DDS.DynamicDataTypeProperty_t()); if (type_support == null) { shutdown(participant); throw new ApplicationException("create_type_support error"); } // --- Create topic --- // /* Register type before creating topic */ System.String type_name = EXAMPLE_TYPE_NAME; try { type_support.register_type(participant, EXAMPLE_TYPE_NAME); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* Make sure both publisher and subscriber share the same topic name. * In the Shapes example: we are subscribing to a Square, wich is the * topic name. If you want to publish other shapes (Triangle or Circle), * you just need to update the topic name. */ DDS.Topic topic = participant.create_topic( "Square", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } // --- Create reader --- // /* Create a data reader listener */ ShapeTypeListener reader_listener = new ShapeTypeListener(); /* First, we create a generic DataReader for our topic */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); if (reader == null) { shutdown(participant); reader_listener = null; throw new ApplicationException("create_datareader error"); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "ShapeType subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_listener = null; }