static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = orderedTypeSupport.get_type_name(); try { orderedTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example ordered", 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"); } /* Start changes for Ordered_Presentation */ /* Create two subscribers to illustrate different presentation QoS * This is a publisher/subscriber level QoS, so we have to * do it here instead of just making two datareaders */ DDS.Subscriber[] subscriber = new DDS.Subscriber[MAX_SUBSCRIBERS]; DDS.DataReader[] reader = new DDS.DataReader[MAX_SUBSCRIBERS]; /* Subscriber[0], reader[0] and ordered_reader[0] is getting * the profile "ordered_Profile_subscriber_instance" */ /* Subscriber[1], reader[1] and ordered_reader[1] is getting * the profile "ordered_Profile_subscriber_topic" */ String[] profile_name = new String[MAX_SUBSCRIBERS] { "ordered_Profile_subscriber_instance", "ordered_Profile_subscriber_topic" }; for (int i = 0; i < MAX_SUBSCRIBERS; ++i) { Console.WriteLine("Subscriber {0} using {1}", i, profile_name[i]); subscriber[i] = participant.create_subscriber_with_profile( "ordered_Library", profile_name[i], null, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber[i] == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } reader[i] = subscriber[i].create_datareader_with_profile(topic, "ordered_Library", profile_name[i], null, DDS.StatusMask.STATUS_MASK_ALL); if (reader[i] == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } } /* If you want to change the Publisher's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and comment out the above 'for' loop. */ /* Get default subscriber QoS to customize */ /* DDS.SubscriberQos subscriber_qos = new DDS.SubscriberQos(); * participant.get_default_subscriber_qos(subscriber_qos); * */ /* Set this for both subscribers */ /* subscriber_qos.presentation.ordered_access = true; * */ /* No listener needed, but we do need to increase history depth */ /* Get default datareader QoS to customize */ /* DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); * * for (int i = 0; i < MAX_SUBSCRIBERS; ++i) { * if (i == 0) { * Console.WriteLine("Subscriber 0 using Instance access scope"); * subscriber_qos.presentation.access_scope = * DDS.PresentationQosPolicyAccessScopeKind * .INSTANCE_PRESENTATION_QOS; * } else { * Console.WriteLine("Subscriber 1 using Topic access scope"); * subscriber_qos.presentation.access_scope = * DDS.PresentationQosPolicyAccessScopeKind * .TOPIC_PRESENTATION_QOS; * } * */ /* To create subscriber with default QoS, use * DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULTinstead of * subscriber_qos */ /* subscriber[i] = participant.create_subscriber( * subscriber_qos, null, DDS.StatusMask.STATUS_MASK_NONE); * if (subscriber == null) { * shutdown(participant); * throw new ApplicationException("create_subscriber error"); * } * subscriber[i].get_default_datareader_qos(datareader_qos); * datareader_qos.history.depth = 10; * */ /* To create datareader with default QoS, use * DDS.Subscriber.DATAREADER_QOS_DEFAULT instead of datareader_qos */ /* reader[i] = subscriber[i].create_datareader( * topic, * datareader_qos, * null, * DDS.StatusMask.STATUS_MASK_NONE); * if (reader[i] == null) { * shutdown(participant); * 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( "\nSleeping for {0} sec...\n-----", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); poll_data(reader, 2); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = orderedTypeSupport.get_type_name(); try { orderedTypeSupport.register_type( participant, type_name); } catch(DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example ordered", 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"); } /* Start changes for Ordered_Presentation */ /* Create two subscribers to illustrate different presentation QoS * This is a publisher/subscriber level QoS, so we have to * do it here instead of just making two datareaders */ DDS.Subscriber[] subscriber = new DDS.Subscriber[MAX_SUBSCRIBERS]; DDS.DataReader[] reader = new DDS.DataReader[MAX_SUBSCRIBERS]; /* Subscriber[0], reader[0] and ordered_reader[0] is getting * the profile "ordered_Profile_subscriber_instance" */ /* Subscriber[1], reader[1] and ordered_reader[1] is getting * the profile "ordered_Profile_subscriber_topic" */ String[] profile_name = new String[MAX_SUBSCRIBERS] {"ordered_Profile_subscriber_instance", "ordered_Profile_subscriber_topic" }; for (int i = 0; i < MAX_SUBSCRIBERS; ++i) { Console.WriteLine("Subscriber {0} using {1}", i, profile_name[i]); subscriber[i] = participant.create_subscriber_with_profile( "ordered_Library", profile_name[i], null, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber[i] == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } reader[i] = subscriber[i].create_datareader_with_profile(topic, "ordered_Library", profile_name[i], null, DDS.StatusMask.STATUS_MASK_ALL); if (reader[i] == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } } /* If you want to change the Publisher's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and comment out the above 'for' loop. */ /* Get default subscriber QoS to customize */ /* DDS.SubscriberQos subscriber_qos = new DDS.SubscriberQos(); participant.get_default_subscriber_qos(subscriber_qos); */ /* Set this for both subscribers */ /* subscriber_qos.presentation.ordered_access = true; */ /* No listener needed, but we do need to increase history depth */ /* Get default datareader QoS to customize */ /* DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); for (int i = 0; i < MAX_SUBSCRIBERS; ++i) { if (i == 0) { Console.WriteLine("Subscriber 0 using Instance access scope"); subscriber_qos.presentation.access_scope = DDS.PresentationQosPolicyAccessScopeKind .INSTANCE_PRESENTATION_QOS; } else { Console.WriteLine("Subscriber 1 using Topic access scope"); subscriber_qos.presentation.access_scope = DDS.PresentationQosPolicyAccessScopeKind .TOPIC_PRESENTATION_QOS; } */ /* To create subscriber with default QoS, use * DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULTinstead of * subscriber_qos */ /* subscriber[i] = participant.create_subscriber( subscriber_qos, null, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } subscriber[i].get_default_datareader_qos(datareader_qos); datareader_qos.history.depth = 10; */ /* To create datareader with default QoS, use * DDS.Subscriber.DATAREADER_QOS_DEFAULT instead of datareader_qos */ /* reader[i] = subscriber[i].create_datareader( topic, datareader_qos, null, DDS.StatusMask.STATUS_MASK_NONE); if (reader[i] == null) { shutdown(participant); 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( "\nSleeping for {0} sec...\n-----", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); poll_data(reader, 2); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ /* The listener has been modified */ /* Start changes for Ordered Presentation Example */ /* Note that the StatusKind is DATA_ON_READERS_STATUS in order * to the incoming data can be read by the SubscriberListener */ ordered_groupSubscriberListener subscriber_listener = new ordered_groupSubscriberListener(); DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, subscriber_listener, (DDS.StatusMask)DDS.StatusKind.DATA_ON_READERS_STATUS); if (subscriber == null) { shutdown(participant); subscriber_listener = null; throw new ApplicationException("create_subscriber error"); } /* End changes for Ordered Presentation Example */ // --- Create topics --- // /* Register the type before creating the topic */ System.String type_name = ordered_groupTypeSupport.get_type_name(); try { ordered_groupTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* Start changes for Ordered Presentation Example */ /* TOPICS */ /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic1 = participant.create_topic( "Topic1", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic1 == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } DDS.Topic topic2 = participant.create_topic( "Topic2", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic2 == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } DDS.Topic topic3 = participant.create_topic( "Topic3", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic3 == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } /* DATAREADERS */ /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader reader1 = subscriber.create_datareader( topic1, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_ALL); if (reader1 == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } DDS.DataReader reader2 = subscriber.create_datareader( topic2, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_ALL); if (reader2 == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } DDS.DataReader reader3 = subscriber.create_datareader( topic3, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_ALL); if (reader3 == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } /* End changes for Ordered Presentation Example */ // --- 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( "ordered_group subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); subscriber_listener = null; }
static void subscribe(int domain_id, int sample_count) { /* There are several different approaches for loading QoS profiles from * XML files (see Configuring QoS with XML chapter in the RTI Connext * Core Libraries and Utilities User's Manual). In this example we * illustrate two of them: * * 1) Creating a file named USER_QOS_PROFILES.xml, which is loaded, * automatically by the DomainParticipantFactory. In this case, the file * defines a QoS profile named volatile_profile that configures reliable, * volatile DataWriters and DataReaders. * * 2) Adding XML documents to the DomainParticipantFactory using its * Profile QoSPolicy (DDS Extension). In this case, we add * my_custom_qos_profiles.xml to the url_profile sequence, which stores * the URLs of all the XML documents with QoS policies that are loaded * by the DomainParticipantFactory aside from the ones that are * automatically loaded. * my_custom_qos_profiles.xml defines a QoS profile named * transient_local_profile that configures reliable, transient local * DataWriters and DataReaders. */ /* To load my_custom_qos_profiles.xml, as explained above, we need to * modify the DDSTheParticipantFactory Profile QoSPolicy */ DDS.DomainParticipantFactoryQos factory_qos = new DDS.DomainParticipantFactoryQos(); DDS.DomainParticipantFactory.get_instance().get_qos(factory_qos); /* We are only going to add one XML file to the url_profile sequence, so * we ensure a length of 1,1. */ factory_qos.profile.url_profile.ensure_length(1, 1); /* The XML file will be loaded from the working directory. That means, * you need to run the example like this: * ./objs/<architecture>/profiles_publisher * (see README.txt for more information on how to run the example). * * Note that you can specify the absolute path of the XML QoS file to * avoid this problem. */ factory_qos.profile.url_profile.set_at(0, "file://my_custom_qos_profiles.xml"); DDS.DomainParticipantFactory.get_instance().set_qos(factory_qos); // --- Create participant --- // /* Our default Qos profile, volatile_profile, sets the participant name. * This is the only participant_qos policy that we change in our * example. As this is done in the default QoS profile, we don't need * to specify its name, so we can create the participant using the * create_participant() method rather than using * create_participant_with_profile(). */ 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 --- // /* We haven't changed the subscriber_qos in any of QoS profiles we use * in this example, so we can just use the create_topic() method. If you * want to load an specific profile in which you may have changed the * publisher_qos, use the create_publisher_with_profile() method. */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = profilesTypeSupport.get_type_name(); try { profilesTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* We haven't changed the topic_qos in any of QoS profiles we use in * this example, so we can just use the create_topic() method. If you * want to load an specific profile in which you may have changed the * topic_qos, use the create_topic_with_profile() method. */ DDS.Topic topic = participant.create_topic( "Example profiles", 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 */ profilesListener reader_volatile_listener = new profilesListener("volatile_profile"); profilesListener reader_transient_local_listener = new profilesListener("transient_local_profile"); /* Volatile reader -- As volatile_profile is the default qos profile * we don't need to specify the profile we are going to use, we can * just call create_datareader passing DDS_DATAWRITER_QOS_DEFAULT. */ DDS.DataReader reader_volatile = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_volatile_listener, DDS.StatusMask.STATUS_MASK_ALL); if (reader_volatile == null) { shutdown(participant); reader_volatile = null; throw new ApplicationException("create_datareader_volatile error"); } /* Transient Local writer -- In this case we use * create_datareader_with_profile, because we have to use a profile * other than the default one. This profile has been defined in * my_custom_qos_profiles.xml, but since we already loaded the XML file * we don't need to specify anything else. */ DDS.DataReader reader_transient_local = subscriber.create_datareader_with_profile( topic, "profiles_Library", "transient_local_profile", reader_transient_local_listener, DDS.StatusMask.STATUS_MASK_ALL); if (reader_transient_local == null) { shutdown(participant); reader_transient_local = null; throw new ApplicationException("create_datareader_transient_local" + "error"); } Console.WriteLine("Created reader_transient_local"); // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "profiles subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_volatile_listener = null; reader_transient_local_listener = null; }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = waitset_statuscondTypeSupport.get_type_name(); try { waitset_statuscondTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example waitset_statuscond", 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 --- // /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_ALL); if (reader == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } // Get narrowed datareader waitset_statuscondDataReader waitset_reader = (waitset_statuscondDataReader)reader; /* Get status conditions * --------------------- * Each entity may have an attached Status Condition. To modify the * statuses we need to get the reader's Status Conditions first. */ DDS.StatusCondition status_condition = reader.get_statuscondition(); /* Set enabled statuses * -------------------- * Now that we have the Status Condition, we are going to enable the * status we are interested in: knowing that data is available */ try { status_condition.set_enabled_statuses( (DDS.StatusMask)DDS.StatusKind.DATA_AVAILABLE_STATUS); } catch (DDS.Exception e) { shutdown(participant); throw new ApplicationException("set_enabled_statuses error {0}", e); } /* Create and attach conditions to the WaitSet * ------------------------------------------- * Finally, we create the WaitSet and attach both the Read Conditions * and the Status Condition to it. */ DDS.WaitSet waitset = new DDS.WaitSet(); /* Attach Status Conditions */ try { waitset.attach_condition(status_condition); } catch (DDS.Exception e) { shutdown(participant); throw new ApplicationException("set_enabled_statuses error {0}", e); } // --- Wait for data --- // DDS.Duration_t timeout; timeout.nanosec = (uint)500000000; timeout.sec = 1; /* Main loop */ for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { DDS.ConditionSeq active_conditions = new DDS.ConditionSeq(); // The triggered condition(s) will be placed in active_conditions try { waitset.wait(active_conditions, timeout); Console.WriteLine("got {0} active conditions", active_conditions.length); } catch (DDS.Retcode_Timeout) { Console.WriteLine("Wait timed out!! No conditions were " + "triggered."); continue; } catch (DDS.Exception e) { Console.WriteLine("wait error {0}", e); break; } for (int i = 0; i < active_conditions.length; ++i) { /* In this case, we have only a single condition, but * if we had multiple, we would need to iterate over * them and check which one is true. Leaving the logic * for the more complex case. */ if (active_conditions.get_at(i) == status_condition) { DDS.StatusMask triggeredmask = waitset_reader.get_status_changes(); if ((triggeredmask & (DDS.StatusMask) DDS.StatusKind.DATA_AVAILABLE_STATUS) != 0) { /* Current conditions match our conditions to read data, so * we can read data just like we would do in any other * example. */ waitset_statuscondSeq data_seq = new waitset_statuscondSeq(); DDS.SampleInfoSeq info_seq = new DDS.SampleInfoSeq(); /* Access data using read(), take(), etc. If * you fail to do this the condition will * remain true, and the WaitSet will wake up * immediately - causing high CPU usage when it * does not sleep in the loop */ bool follow = true; while (follow) { try { waitset_reader.take( data_seq, info_seq, DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED, DDS.SampleStateKind.ANY_SAMPLE_STATE, DDS.ViewStateKind.ANY_VIEW_STATE, DDS.InstanceStateKind.ANY_INSTANCE_STATE); System.Int32 data_length = data_seq.length; for (int j = 0; i < data_length; ++i) { if (!info_seq.get_at(j).valid_data) { Console.WriteLine("Got metadata"); continue; } waitset_statuscondTypeSupport.print_data(data_seq.get_at(j)); } } catch (DDS.Retcode_NoData) { /* When there isn't data, the subscriber stop to * take samples */ follow = false; } finally { /* Return the loaned data */ waitset_reader.return_loan(data_seq, info_seq); } } } } } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = pollTypeSupport.get_type_name(); try { pollTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example poll", 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 --- // /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_NONE); if (reader == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } /* If you want to change datareader_qos.history.kind programmatically rather * than using the XML file, you will need to add the following lines to your * code and comment out the create_datareader call above. */ /*DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); * * try * { * subscriber.get_default_datareader_qos(datareader_qos); * } * catch (DDS.Exception e) * { * Console.WriteLine("get_default_datareader_qos error {0}", e); * shutdown(participant); * throw e; * } * * datareader_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS; * * DDS.DataReader reader = subscriber.create_datareader( * topic, datareader_qos, null, * DDS.StatusMask.STATUS_MASK_ALL); * if (reader == null) { * shutdown(participant); * throw new ApplicationException("create_datareader error"); * } */ pollDataReader poll_reader = (pollDataReader)reader; // --- Wait for data --- // DDS.SampleInfoSeq info_seq = new DDS.SampleInfoSeq(); pollSeq data_seq = new pollSeq(); /* Main loop */ const System.Int32 receive_period = 5000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "poll subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); // --- Polling for data --- /// /* Check for new data calling the DataReader's take() method */ try { poll_reader.take( data_seq, info_seq, DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED, DDS.SampleStateKind.ANY_SAMPLE_STATE, DDS.ViewStateKind.ANY_VIEW_STATE, DDS. InstanceStateKind.ANY_INSTANCE_STATE); } catch (DDS.Retcode_NoData) { // Not an error return; } catch (DDS.Exception e) { Console.WriteLine("take error {0}", e); return; } int len = 0; double sum = 0; /* Iterate through the samples read using the take() method, getting * the number of samples got and, adding the value of x on each of * them to calculate the average afterwards. */ for (int i = 0; i < data_seq.length; ++i) { if (!info_seq.get_at(i).valid_data) { continue; } len++; sum += data_seq.get_at(i).x; } if (len > 0) { Console.WriteLine("Got {0} samples. Avg = {1}", len, sum / len); } try { poll_reader.return_loan(data_seq, info_seq); } catch (DDS.Exception e) { Console.WriteLine("return loan error {0}", e); } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = queryconditionTypeSupport.get_type_name(); try { queryconditionTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example querycondition", 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 --- // /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_ALL); if (reader == null) { shutdown(participant); throw new ApplicationException("create_datareader error"); } /* If you want to change datareader_qos.history programmatically rather * than using the XML file, you will need to add the following lines to your * code and comment out the create_datareader call above. */ /*DDS.DataReaderQos reader_qos = new DDS.DataReaderQos(); * subscriber.get_default_datareader_qos(reader_qos); * * reader_qos.history.kind = DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS; * reader_qos.history.depth = 6; * * DDS.DataReader reader = subscriber.create_datareader( * topic, * reader_qos, * reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * if (reader == null) * { * shutdown(participant); * reader_listener = null; * throw new ApplicationException("create_datareader error"); * } */ // --- Wait for data --- // /* NOTE: There must be single-quotes in the query parameters around * any strings! The single-quotes do NOT go in the query condition * itself. */ DDS.QueryCondition query_for_guid2; DDS.StringSeq query_parameters = new DDS.StringSeq(); query_parameters.ensure_length(1, 1); query_parameters.set_at(0, "'GUID2'"); queryconditionDataReader querycondition_reader = (queryconditionDataReader)reader; query_for_guid2 = querycondition_reader.create_querycondition( DDS.SampleStateKind.ANY_SAMPLE_STATE, DDS.ViewStateKind.ANY_VIEW_STATE, DDS.InstanceStateKind.ALIVE_INSTANCE_STATE, "id MATCH %0", query_parameters); /* Main loop */ const System.Int32 receive_period = 4000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(receive_period); queryconditionSeq data_seq = new queryconditionSeq(); DDS.SampleInfoSeq info_seq = new DDS.SampleInfoSeq(); try { querycondition_reader.read_w_condition( data_seq, info_seq, DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED, query_for_guid2); } catch (DDS.Retcode_NoData e) { continue; } catch (DDS.Exception e) { shutdown(participant); throw e; } int len = 0; double sum = 0; /* Iterate through the samples read using the read_w_condition() method, * accessing only the samples of GUID2. Then, show the number of samples * received and, adding the value of x on each of them to calculate the * average afterwards. */ for (int i = 0; i < data_seq.length; ++i) { if (!info_seq.get_at(i).valid_data) { continue; } len++; sum += data_seq.get_at(i).value; Console.WriteLine("Guid = {0}\n", data_seq.get_at(i).id); } if (len > 0) { Console.WriteLine("Got {0} samples. Avg = {1}\n", len, sum / len); } try { querycondition_reader.return_loan(data_seq, info_seq); } catch (DDS.Exception e) { Console.WriteLine("return loan error {0}", e); } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }
static void subscribe(int domain_id, int sample_count, int turbo_mode_on) { System.String profile_name = null; System.String library_name = "batching_Library"; /* We pick the profile name if the turbo_mode is selected or not. * If Turbo_mode is not selected, the batching profile will be used. */ if (turbo_mode_on == 1) { profile_name = "turbo_mode_profile"; Console.WriteLine("Turbo Mode enable"); } else { profile_name = "batch_profile"; Console.WriteLine("Manual batching enable"); } // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance(). create_participant_with_profile( domain_id, library_name, profile_name, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } // --- Create subscriber --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Subscriber subscriber = participant.create_subscriber_with_profile( library_name, profile_name, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { shutdown(participant); throw new ApplicationException("create_subscriber error"); } // --- Create topic --- // /* Register the type before creating the topic */ System.String type_name = batch_dataTypeSupport.get_type_name(); try { batch_dataTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example batch_data", 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 */ batch_dataListener reader_listener = new batch_dataListener(); /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader reader = subscriber.create_datareader_with_profile( topic, library_name, profile_name, 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( "batch_data 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 --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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"); } /* If you want to change the Participant's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and comment out the create_participant call above. */ /* By default, discovery will communicate via shared memory for * platforms that support it. Because we disable shared memory on the * publishing side, we do so here to ensure the reader and writer * discover each other. */ /* Get default participant QoS to customize */ /* * DDS.DomainParticipantQos participant_qos = * new DDS.DomainParticipantQos(); * try { * DDS.DomainParticipantFactory.get_instance(). * get_default_participant_qos(participant_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_participant_qos error {0}", e); * throw e; * } * * // By default, data will be sent via shared memory _and_ UDPv4. Because * // the flowcontroller limits writes across all interfaces, this halves * // the effective send rate. To avoid this, we enable only the UDPv4 * // transport * * participant_qos.transport_builtin.mask = * (int) DDS.TransportBuiltinKind.TRANSPORTBUILTIN_UDPv4; * * // To create participant with default QoS, use * // DDS_PARTICIPANT_QOS_DEFAULT instead of participant_qos * * DDS.DomainParticipant participant = * DDS.DomainParticipantFactory.get_instance().create_participant( * domain_id, * participant_qos, * null, * DDS.StatusMask.STATUS_MASK_NONE); * if (participant == null) { * shutdown(participant); * throw new ApplicationException("create_participant error"); * } */ /* End changes for custom_flowcontroller */ // --- Create subscriber --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = cfcTypeSupport.get_type_name(); try { cfcTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example cfc", 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 */ cfcListener reader_listener = new cfcListener(); /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 = 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); reader_listener = null; }
static void subscribe(int domain_id, int sample_count, int sel_cft) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = cftTypeSupport.get_type_name(); try { cftTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example cft", 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"); } /* For this filter we only allow 1 parameter */ DDS.StringSeq parameters = new DDS.StringSeq(1); /* The default parameter list that we will include in the * sequence of parameters will be "SOME_STRING" */ DDS.StringWrapper[] param_list = new DDS.StringWrapper[1] { "SOME_STRING" }; parameters.from_array(param_list); DDS.ContentFilteredTopic cft = null; if (sel_cft == 1) { /* create_contentfilteredtopic_with_filter */ cft = participant.create_contentfilteredtopic_with_filter( "ContentFilteredTopic", topic, "name MATCH %0", parameters, DDS.DomainParticipant.STRINGMATCHFILTER_NAME); if (cft == null) { shutdown(participant); throw new ApplicationException( "create_contentfilteredtopic_with_filter error"); } } // --- Create reader --- // /* Create a data reader listener */ cftListener reader_listener = new cftListener(); DDS.DataReader reader = null; if (sel_cft != 0) { Console.WriteLine("Using ContentFiltered Topic"); reader = subscriber.create_datareader(cft, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); } else { Console.WriteLine("Using Normal Topic"); 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"); } /* If you want to set the reliability and history QoS settings * programmatically rather than using the XML, you will need to add * the following lines to your code and comment out the * create_datareader calls above. */ /* * DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); * try { * subscriber.get_default_datareader_qos(datareader_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_datareader_qos error {0}", e); * shutdown(participant); * throw e; * } * * datareader_qos.reliability.kind = * DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * datareader_qos.durability.kind = * DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS; * datareader_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS; * datareader_qos.history.depth = 20; * * if (sel_cft != 0) { * Console.WriteLine("Using ContentFiltered Topic"); * reader = subscriber.create_datareader(cft, * datareader_qos, reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * } else { * Console.WriteLine("Using Normal Topic"); * reader = subscriber.create_datareader(topic, * datareader_qos, reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * } * * if (reader == null) { * shutdown(participant); * reader_listener = null; * throw new ApplicationException("create_datareader error"); * } * */ /* Change the filter */ if (sel_cft == 1) { Console.WriteLine(">>> Now setting a new filter: name MATCH \"EVEN\""); try { cft.append_to_expression_parameter(0, "EVEN"); } catch (DDS.Exception e) { Console.WriteLine("append_to_expression_parameter error {0}", e); shutdown(participant); throw e; } } // --- 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); if (sel_cft == 0) { continue; } if (count == 10) { Console.WriteLine("\n==========================="); Console.WriteLine("Changing filter parameters"); Console.WriteLine("Append 'ODD' filter"); Console.WriteLine("==========================="); try { cft.append_to_expression_parameter(0, "ODD"); } catch (DDS.Exception e) { Console.WriteLine( "append_to_expression_parameter error {0}", e); shutdown(participant); throw e; } } if (count == 20) { Console.WriteLine("\n==========================="); Console.WriteLine("Changing filter parameters"); Console.WriteLine("Removing 'EVEN' filter"); Console.WriteLine("==========================="); try { cft.remove_from_expression_parameter(0, "EVEN"); } catch (DDS.Exception e) { Console.WriteLine( "append_to_expression_parameter error {0}", e); shutdown(participant); throw e; } } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_listener = null; }
static void subscribe(int domain_id, int sample_count) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DomainParticipant participant = DDS.DomainParticipantFactory.get_instance().create_participant( domain_id, DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); /* If you want to change the DomainParticipant's QoS programmatically * rather than using the XML file, you will need to add the following lines * to your code and comment out the create_participant call above. * * In this case, we set the transport settings in the XML by default, but * in the createParticipant call, we set up the transport * properties either using the Properties QoS in code. */ /* DDS.DomainParticipant participant = createParticipant(domain_id); */ if (participant == null) { shutdown(participant); throw new ApplicationException("create_participant error"); } if (checkParticipant(participant) == true) { Console.WriteLine("Ok, recv_socket_buffer_size....modified"); Console.WriteLine("Ok, send_socket_buffer_size....modified"); } // --- Create subscriber --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = numbersTypeSupport.get_type_name(); try { numbersTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example numbers", 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 */ numbersListener reader_listener = new numbersListener(); /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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( "numbers 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) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String atype_name = AccidentTypeSupport.get_type_name(); System.String ptype_name = PositionTypeSupport.get_type_name(); try { AccidentTypeSupport.register_type( participant, atype_name); PositionTypeSupport.register_type( participant, ptype_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic atopic = participant.create_topic( "P3464_BHANDERSON: PT/ALR/ACC", atype_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); DDS.Topic ptopic = participant.create_topic( "P3464_BHANDERSON: PT/POS", ptype_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (atopic == null || ptopic == null) { shutdown(participant); throw new ApplicationException("create_topic error"); } // --- Create reader --- // /* Create a data reader listener */ AccidentListener reader_listener = new AccidentListener(); /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataReader areader = subscriber.create_datareader( atopic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); DDS.DataReader preader = subscriber.create_datareader( ptopic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); if (areader == null || preader == 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( "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, int sel_cft) { // --- Create participant --- // /* To customize the participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 --- // /* To customize the subscriber QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 topic --- // /* Register the type before creating the topic */ System.String type_name = cftTypeSupport.get_type_name(); try { cftTypeSupport.register_type( participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize the topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example cft", 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"); } /* Sequence of parameters for the content filter expression */ DDS.StringSeq parameters = new DDS.StringSeq(2); /* The default parameter list that we will include in the * sequence of parameters will be "1","4" (i.e., 1 <= x <= 4). */ DDS.StringWrapper[] param_list = new DDS.StringWrapper[2] { "1", "4" }; parameters.from_array(param_list); /* Create the content filtered topic in case sel_cft * is true. * The Content Filter Expresion has two parameters: * - %0 -- x must be greater or equal than %0. * - %1 -- x must be less or equal than %1. */ DDS.ContentFilteredTopic cft = null; if (sel_cft != 0) { cft = participant.create_contentfilteredtopic( "ContentFilteredTopic", topic, "(x >= %0 and x <= %1)", parameters); if (cft == null) { shutdown(participant); throw new ApplicationException( "create_contentfilteredtopic error"); } } // --- Create reader --- // /* Create a data reader listener */ cftListener reader_listener = new cftListener(); /* Here we create the reader either using a Content Filtered Topic or * a normal topic */ DDS.DataReader reader = null; if (sel_cft != 0) { Console.WriteLine("Using ContentFiltered Topic"); reader = subscriber.create_datareader(cft, DDS.Subscriber.DATAREADER_QOS_DEFAULT, reader_listener, DDS.StatusMask.STATUS_MASK_ALL); } else { Console.WriteLine("Using Normal Topic"); 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"); } /* If you want to set the reliability and history QoS settings * programmatically rather than using the XML, you will need to add * the following lines to your code and comment out the * create_datareader calls above. */ /* * DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos(); * try { * subscriber.get_default_datareader_qos(datareader_qos); * } catch (DDS.Exception e) { * Console.WriteLine("get_default_datareader_qos error {0}", e); * shutdown(participant); * throw e; * } * * datareader_qos.reliability.kind = * DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS; * datareader_qos.durability.kind = * DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS; * datareader_qos.history.kind = * DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS; * datareader_qos.history.depth = 20; * * if (sel_cft != 0) { * Console.WriteLine("Using ContentFiltered Topic"); * reader = subscriber.create_datareader(cft, * datareader_qos, reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * } else { * Console.WriteLine("Using Normal Topic"); * reader = subscriber.create_datareader(topic, * datareader_qos, reader_listener, * DDS.StatusMask.STATUS_MASK_ALL); * } * * if (reader == null) { * shutdown(participant); * reader_listener = null; * throw new ApplicationException("create_datareader error"); * } * */ if (sel_cft != 0) { Console.WriteLine("\n=========================="); Console.WriteLine("Using CFT\nFilter: 1 <= x <= 4"); Console.WriteLine("=========================="); } // --- Wait for data --- // /* Main loop */ const System.Int32 receive_period = 1000; // milliseconds for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { Console.WriteLine( "cft subscriber sleeping for {0} sec...", receive_period / 1000); System.Threading.Thread.Sleep(receive_period); if (sel_cft == 0) { continue; } if (count == 10) { Console.WriteLine("\n=========================="); Console.WriteLine("Changing filter parameters"); Console.WriteLine("Filter: 5 <= x <= 9"); Console.WriteLine("==========================="); parameters.set_at(0, "5"); parameters.set_at(1, "9"); try { cft.set_expression_parameters(parameters); } catch (DDS.Exception e) { Console.WriteLine("set_expression_parameters error {0}", e); shutdown(participant); throw e; } } else if (count == 20) { Console.WriteLine("\n=========================="); Console.WriteLine("Changing filter parameters"); Console.WriteLine("Filter: 3 <= x <= 9"); Console.WriteLine("==========================="); DDS.StringSeq oldParameters = new DDS.StringSeq(); cft.get_expression_parameters(oldParameters); oldParameters.set_at(0, "3"); try { cft.set_expression_parameters(oldParameters); } catch (DDS.Exception e) { Console.WriteLine("set_expression_parameters error {0}", e); shutdown(participant); throw e; } } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); reader_listener = null; }
static void publish(int domain_id, int sample_count) { // --- Create participant --- // /* Start changes for Builtin_Topics */ /* If you want to change the Factory's QoS programmatically rather * than using the XML file, you will need to add the following lines to * your code and comment out the participant call above. */ /* By default, the participant is enabled upon construction. * At that time our listeners for the builtin topics have not * been installed, so we disable the participant until we * set up the listeners. */ /* * DDS.DomainParticipantFactoryQos factory_qos = * new DDS.DomainParticipantFactoryQos(); * DDS.DomainParticipantFactory.get_instance().get_qos(factory_qos); * factory_qos.entity_factory.autoenable_created_entities = false; * DDS.DomainParticipantFactory.get_instance().set_qos(factory_qos); * */ // If you want to change the Participant's QoS programmatically // rather than using the XML file, you will need to uncomment the // following lines and replace in create_participant the argument // DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT with // participant_qos //DDS.DomainParticipantQos participant_qos = // new DDS.DomainParticipantQos(); //DDS.DomainParticipantFactory.get_instance(). // get_default_participant_qos(participant_qos); //participant_qos.resource_limits.participant_user_data_max_length = 1024; /* To customize participant QoS, use * the configuration file USER_QOS_PROFILES.xml */ 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 Exception("create_participant error"); } // Installing listeners for the builting topics requires several steps // First get the builting subscriber DDS.Subscriber builtin_subscriber = participant.get_builtin_subscriber(); if (builtin_subscriber == null) { shutdown(participant); throw new Exception("create_builtin_subscriber error"); } /* Then get builtin subscriber's datareader for participants * The type name is a bit hairy, but can be read right to left: * DDS.ParticipantBuiltinTopicDataDataReader is a * DataReader for BuiltinTopicData concerning a discovered * Participant */ DDS.ParticipantBuiltinTopicDataDataReader builtin_participant_datareader = (DDS.ParticipantBuiltinTopicDataDataReader) builtin_subscriber.lookup_datareader( DDS.ParticipantBuiltinTopicDataTypeSupport. PARTICIPANT_TOPIC_NAME); // Install our listener BuiltinParticipantListener builtin_participant_listener = new BuiltinParticipantListener(); try { builtin_participant_datareader.set_listener( builtin_participant_listener, (DDS.StatusMask.STATUS_MASK_NONE | (DDS.StatusMask)DDS.StatusKind.DATA_AVAILABLE_STATUS)); } catch (DDS.Exception e) { shutdown(participant); Console.WriteLine("set_listener error: {0}", e); } // Get builtin subscriber's datareader for subscribers DDS.SubscriptionBuiltinTopicDataDataReader builtin_subscription_datareader = (DDS.SubscriptionBuiltinTopicDataDataReader) builtin_subscriber.lookup_datareader( DDS.SubscriptionBuiltinTopicDataTypeSupport. SUBSCRIPTION_TOPIC_NAME); if (builtin_participant_datareader == null) { shutdown(participant); throw new Exception("lookup_datareader error"); } // Install our listener BuiltinSubscriberListener builtin_subscriber_listener = new BuiltinSubscriberListener(); builtin_subscription_datareader.set_listener( builtin_subscriber_listener, (DDS.StatusMask.STATUS_MASK_NONE | (DDS.StatusMask)DDS.StatusKind.DATA_AVAILABLE_STATUS)); /* Done! All the listeners are installed, so we can enable the * participant now. */ participant.enable(); /* End changes for Builtin_Topics */ // --- Create publisher --- // /* To customize publisher QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Publisher publisher = participant.create_publisher( DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (publisher == null) { shutdown(participant); throw new Exception("create_publisher error"); } // --- Create topic --- // /* Register type before creating topic */ string type_name = msgTypeSupport.get_type_name(); try { msgTypeSupport.register_type(participant, type_name); } catch (DDS.Exception e) { Console.WriteLine("register_type error {0}", e); shutdown(participant); throw e; } /* To customize topic QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.Topic topic = participant.create_topic( "Example msg", type_name, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { shutdown(participant); throw new Exception("create_topic error"); } // --- Create writer --- // /* To customize data writer QoS, use * the configuration file USER_QOS_PROFILES.xml */ DDS.DataWriter writer = publisher.create_datawriter( topic, DDS.Publisher.DATAWRITER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_NONE); if (writer == null) { shutdown(participant); throw new Exception("create_datawriter error"); } msgDataWriter msg_writer = (msgDataWriter)writer; // --- Write --- // /* Create data sample for writing */ msg instance = msgTypeSupport.create_data(); if (instance == null) { shutdown(participant); throw new Exception("msgTypeSupport.create_data error"); } /* For a data type that has a key, if the same instance is going to be * written multiple times, initialize the key here * and register the keyed instance prior to writing */ DDS.InstanceHandle_t instance_handle = DDS.InstanceHandle_t.HANDLE_NIL; /* * instance_handle = msg_writer.register_instance(instance); */ /* Main loop */ const int send_period = 1000; // milliseconds /* Changes for Builtin_Topics */ for (short count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.Threading.Thread.Sleep(send_period); Console.WriteLine("Writing msg, count {0}", count); /* Modify the data to be sent here */ /* Changes for Builtin_Topics */ instance.x = count; try { msg_writer.write(instance, ref instance_handle); } catch (DDS.Exception e) { Console.WriteLine("write error {0}", e); } } /* * try { * msg_writer.unregister_instance( * instance, ref instance_handle); * } catch(DDS.Exception e) { * Console.WriteLine("unregister instance error: {0}", e); * } */ // --- Shutdown --- // /* Delete data sample */ try { msgTypeSupport.delete_data(instance); } catch (DDS.Exception e) { Console.WriteLine("msgTypeSupport.delete_data error: {0}", e); } /* Delete all entities */ shutdown(participant); }
private void initializeDataReader(DDS.DomainParticipant participant) { DDS.Subscriber subscriber = participant.create_subscriber( DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (subscriber == null) { throw new ApplicationException("create_subscriber error"); } DDS.Topic topic = participant.create_topic( _topicName, _typeName, DDS.DomainParticipant.TOPIC_QOS_DEFAULT, null /* listener */, DDS.StatusMask.STATUS_MASK_NONE); if (topic == null) { throw new ApplicationException("create_topic error"); } /* To customize the data reader QoS, use * the configuration file USER_QOS_PROFILES.xml */ _reader = subscriber.create_datareader( topic, DDS.Subscriber.DATAREADER_QOS_DEFAULT, null, DDS.StatusMask.STATUS_MASK_ALL); if (_reader == null) { throw new ApplicationException("create_datareader error"); } _statusCondition = _reader.get_statuscondition(); try { var mask = (int)DDS.StatusKind.DATA_AVAILABLE_STATUS | (int)DDS.StatusKind.SUBSCRIPTION_MATCHED_STATUS | (int)DDS.StatusKind.LIVELINESS_CHANGED_STATUS | (int)DDS.StatusKind.SAMPLE_LOST_STATUS | (int)DDS.StatusKind.SAMPLE_REJECTED_STATUS; _statusCondition.set_enabled_statuses((DDS.StatusMask)mask); } catch (DDS.Exception e) { throw new ApplicationException($"set_enabled_statuses error {e}"); } _waitset = new DDS.WaitSet(); try { _waitset.attach_condition(_statusCondition); } catch (DDS.Exception e) { throw new ApplicationException("attach_condition error {0}", e); } }