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 --- // int count = 0; DDS.Duration_t timeout; timeout.nanosec = (uint)500000000; timeout.sec = 1; /* Main loop */ while (sample_count == 0 || count < sample_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); 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(); try { /* 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 */ 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); } catch (DDS.Retcode_NoData e) { shutdown(participant); throw e; } catch (DDS.Exception e) { shutdown(participant); throw e; } /* Iterate over returned data. Print the data * values if it is not metadata. */ 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)); } try { /* Return the loaned data */ waitset_reader.return_loan(data_seq, info_seq); } catch (DDS.Exception e) { Console.WriteLine("return loan error {0}", e); } } } } } catch (DDS.Retcode_Timeout) { Console.WriteLine("wait timed out"); count += 2; continue; } } // --- 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 = 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 --- // int count = 0; DDS.Duration_t timeout; timeout.nanosec = (uint)500000000; timeout.sec = 1; /* Main loop */ while (sample_count == 0 || count < sample_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); 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(); try { /* 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 */ 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); } catch (DDS.Retcode_NoData e) { shutdown(participant); throw e; } catch (DDS.Exception e) { shutdown(participant); throw e; } /* Iterate over returned data. Print the data * values if it is not metadata. */ 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)); } try { /* Return the loaned data */ waitset_reader.return_loan(data_seq, info_seq); } catch (DDS.Exception e) { Console.WriteLine("return loan error {0}", e); } } } } } catch (DDS.Retcode_Timeout) { Console.WriteLine("wait timed out"); count += 2; continue; } } // --- Shutdown --- // /* Delete all entities */ shutdown(participant); }