static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* Create participant listener */ ParticipantListener participant_listener = new ParticipantListener(); /* We associate the participant_listener to the participant and set the * status mask to get all the statuses */ DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domain_id, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, participant_listener /* listener */, DDS.StatusMask.STATUS_MASK_ALL /* get all statuses */); if (participant == null) { shutdown(participant); participant_listener = null; throw new ApplicationException("create_participant error"); } // --- Create subscriber --- // /* Create subscriber listener */ SubscriberListener subscriber_listener = new SubscriberListener(); /* Here we associate the subscriber listener to the subscriber and set the * status mask to get all the statuses */ DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, subscriber_listener /* listener */, DDS.StatusMask.STATUS_MASK_ALL /* get all statuses */); if (subscriber == null) { shutdown(participant); participant_listener = null; subscriber_listener = null; throw new ApplicationException("create_subscriber error"); } // --- Create topic --- // /* Register the type before creating the topic */ System.String type_name = listenersTypeSupport.get_type_name(); try { listenersTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); participant_listener = null; subscriber_listener = null; throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example listeners", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); participant_listener = null; subscriber_listener = null; throw new ApplicationException("create_topic error"); } // --- Create reader --- // /* Create a data reader listener */ ReaderListener reader_listener = new ReaderListener(); /* Create Status mask to listen just LIVELINESS_CHANGED_STATUS and * DATA_AVAILABLE_STATUS */ int liveliness_changed_mask = (int)DDS.StatusKind.LIVELINESS_CHANGED_STATUS; int data_available_changed_mask = (int)DDS.StatusKind.DATA_AVAILABLE_STATUS; data_available_changed_mask |= liveliness_changed_mask; DDS.StatusMask combined_status_mask = (DDS.StatusMask)data_available_changed_mask; /* Here we associate the data reader listener to the reader. * We just listen for liveliness changed and data available, * since most specific listeners will get called. */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, combined_status_mask); if (reader == null) { shutdown(participant); participant_listener = null; subscriber_listener = null; reader_listener = null; throw new ApplicationException("create_datareader error"); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); participant_listener = null; subscriber_listener = null; reader_listener = null; }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* Create participant listener */ ParticipantListener participant_listener = new ParticipantListener(); /* We associate the participant_listener to the participant and set the * status mask to get all the statuses */ DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domain_id, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, participant_listener /* listener */, DDS.StatusMask.STATUS_MASK_ALL /* get all statuses */); if (participant == null) { shutdown(participant); participant_listener = null; throw new ApplicationException("create_participant error"); } // --- Create subscriber --- // /* Create subscriber listener */ SubscriberListener subscriber_listener = new SubscriberListener(); /* Here we associate the subscriber listener to the subscriber and set the * status mask to get all the statuses */ DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, subscriber_listener /* listener */, DDS.StatusMask.STATUS_MASK_ALL /* get all statuses */); if (subscriber == null) { shutdown(participant); participant_listener = null; subscriber_listener = null; throw new ApplicationException("create_subscriber error"); } // --- Create topic --- // /* Register the type before creating the topic */ System.String type_name = listenersTypeSupport.get_type_name(); try { listenersTypeSupport.register_type( participant, type_name); } catch(DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); participant_listener = null; subscriber_listener = null; throw e; } /* To customize the topic QoS, use the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example listeners", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); participant_listener = null; subscriber_listener = null; throw new ApplicationException("create_topic error"); } // --- Create reader --- // /* Create a data reader listener */ ReaderListener reader_listener = new ReaderListener(); /* Create Status mask to listen just LIVELINESS_CHANGED_STATUS and * DATA_AVAILABLE_STATUS */ int liveliness_changed_mask = (int)DDS.StatusKind.LIVELINESS_CHANGED_STATUS; int data_available_changed_mask = (int)DDS.StatusKind.DATA_AVAILABLE_STATUS; data_available_changed_mask |= liveliness_changed_mask; DDS.StatusMask combined_status_mask = (DDS.StatusMask)data_available_changed_mask; /* Here we associate the data reader listener to the reader. * We just listen for liveliness changed and data available, * since most specific listeners will get called. */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, combined_status_mask); if (reader == null) { shutdown(participant); participant_listener = null; subscriber_listener = null; reader_listener = null; throw new ApplicationException("create_datareader error"); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 1000; // milliseconds for (int count=0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); participant_listener = null; subscriber_listener = null; reader_listener = null; }