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;
    }
 public override void on_requested_incompatible_qos(
     DDS.DataReader reader,
     DDS.RequestedIncompatibleQosStatus status)
 {
     Console.WriteLine("ParticipantListener: on_requested_incompatible_qos()");
 }
 public override void on_data_available(
     DDS.DataReader reader)
 {
     Console.WriteLine("SubscriberListener: on_data_available()");
 }
 public override void on_sample_lost(
     DDS.DataReader reader,
     ref DDS.SampleLostStatus status)
 {
     Console.WriteLine("ReaderListener: on_sample_lost()\n");
 }
 public override void on_sample_lost(
     DDS.DataReader reader,
     ref DDS.SampleLostStatus status)
 {
 }
 public override void on_data_available(
     DDS.DataReader reader)
 {
     Console.WriteLine("ParticipantListener: on_data_available()");
 }
Пример #7
0
    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);
    }
 public override void on_sample_rejected(
     DDS.DataReader reader,
     ref DDS.SampleRejectedStatus status)
 {
 }
Пример #9
0
    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");
        }

        // --- 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);
    }
Пример #11
0
    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);
    }
Пример #12
0
    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;
    }
Пример #13
0
    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;
    }
Пример #14
0
        public override void on_data_available(DDS.DataReader reader)
        {
            if (reader.GetType() == typeof(PositionDataReader))
            {
                PositionDataReader Position_reader =
                    (PositionDataReader)reader;

                try
                {
                    Position_reader.take(
                        pdata_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)
                {
                    return;
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("take error {0}", e);
                    return;
                }
                //Console.WriteLine("MessageType\t Route\t\t Vehicle\t Traffic\t Stop#\t #Stops\t TimeBetweenStops\t Fill%\t TimeStamp");
                System.Int32 data_length = pdata_seq.length;
                for (int i = 0; i < data_length; ++i)
                {
                    if (info_seq.get_at(i).valid_data)
                    {
                        Position p = pdata_seq.get_at(i);

                        if (string.IsNullOrEmpty(bus))
                        {
                            if (p.route == route)
                            {
                                if (p.stopNumber == start)
                                {
                                    Console.WriteLine("Getting on bus" + p.vehicle);
                                    bus = p.vehicle;
                                }
                            }
                        }
                        else
                        {
                            if (p.route == route && p.vehicle == bus)
                            {
                                Console.WriteLine("At {2} bus{0} arrived at stop {1}", p.vehicle, p.stopNumber, p.timestamp);

                                if (p.stopNumber == end)
                                {
                                    Console.WriteLine("GET OFF THE BUS. At stop " + p.stopNumber);
                                    sample_count = 1;
                                    bus          = "99999999999999999";
                                }
                            }
                        }

                        /*
                         * Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}",
                         *  "Position", p.route, p.vehicle, p.trafficConditions, p.stopNumber, p.numStops, p.timeBetweenStops, p.fillInRatio, p.timestamp);
                         * Accident a = adata_seq.get_at(i);
                         * Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}",
                         *  "Accident", a.route, a.vehicle, string.Empty, a.stopNumber, string.Empty, string.Empty, string.Empty, a.timestamp);
                         */
                        //          PositionTypeSupport.print_data(pdata_seq.get_at(i));
                    }
                }

                try
                {
                    Position_reader.return_loan(pdata_seq, info_seq);
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("return loan error {0}", e);
                }
            }
            else
            {
                AccidentDataReader Accident_reader =
                    (AccidentDataReader)reader;

                try
                {
                    Accident_reader.take(
                        adata_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)
                {
                    return;
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("take error {0}", e);
                    return;
                }

                System.Int32 data_length = adata_seq.length;
                for (int i = 0; i < data_length; ++i)
                {
                    if (info_seq.get_at(i).valid_data)
                    {
                        AccidentTypeSupport.print_data(adata_seq.get_at(i));
                    }
                }

                try
                {
                    Accident_reader.return_loan(adata_seq, info_seq);
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("return loan error {0}", e);
                }
            }
        }
Пример #15
0
    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;
    }
 public override void on_requested_incompatible_qos(
     DDS.DataReader reader,
     DDS.RequestedIncompatibleQosStatus status)
 {
 }
Пример #17
0
    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;
    }
 public override void on_liveliness_changed(
     DDS.DataReader reader,
     ref DDS.LivelinessChangedStatus status)
 {
 }
Пример #19
0
    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);
    }
 public override void on_subscription_matched(
     DDS.DataReader reader,
     ref DDS.SubscriptionMatchedStatus status)
 {
 }
Пример #21
0
    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 = keysTypeSupport.get_type_name();
        try {
            keysTypeSupport.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 keys",
            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 */
        keysListener reader_listener =
            new keysListener();

        /* 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)
        {
            //Console.WriteLine("keys subscriber sleeping for {0} sec...",receive_period / 1000);

            System.Threading.Thread.Sleep(receive_period);
        }

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
 public override void on_liveliness_changed(
     DDS.DataReader reader,
     ref DDS.LivelinessChangedStatus status)
 {
     Console.WriteLine("SubscriberListener: on_liveliness_changed()\n");
 }
Пример #23
0
        public override void on_data_available(DDS.DataReader reader)
        {
            keysDataReader keys_reader =
                (keysDataReader)reader;

            try {
                keys_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) {
                return;
            }
            catch (DDS.Exception e) {
                Console.WriteLine("take error {0}", e);
                return;
            }

            System.Int32 data_length = data_seq.length;
            for (int i = 0; i < data_length; ++i)
            {
                /* Start changes for Keyed_Data */
                /* We first check if the sample includes valid data */
                if (info_seq.get_at(i).valid_data)
                {
                    if (info_seq.get_at(i).view_state ==
                        DDS.ViewStateKind.NEW_VIEW_STATE)
                    {
                        Console.WriteLine("Found new instance; code = {0}",
                                          data_seq.get_at(i).code);
                    }

                    Console.WriteLine("Instance {0}: x: {1}, y: {2}",
                                      data_seq.get_at(i).code, data_seq.get_at(i).x,
                                      data_seq.get_at(i).y);
                }
                else
                {
                    /* Since there is not valid data, it may include metadata */
                    keys dummy = new keys();
                    try
                    {
                        DDS.InstanceHandle_t temp =
                            info_seq.get_at(i).instance_handle;
                        keys_reader.get_key_value(dummy, ref temp);
                    }
                    catch (DDS.Exception e)
                    {
                        Console.WriteLine("get_key_value error {0}", e);
                    }

                    /* Here we print a message if the instance state is ALIVE_NO_WRITERS or ALIVE_DISPOSED */
                    if (info_seq.get_at(i).instance_state ==
                        DDS.InstanceStateKind.NOT_ALIVE_NO_WRITERS_INSTANCE_STATE)
                    {
                        Console.WriteLine("Instance {0} has no writers",
                                          dummy.code);
                    }
                    else if (info_seq.get_at(i).instance_state ==
                             DDS.InstanceStateKind.NOT_ALIVE_DISPOSED_INSTANCE_STATE)
                    {
                        Console.WriteLine("Instance {0} disposed",
                                          dummy.code);
                    }
                }
                /* End changes for Keyed_Data */
            }

            try {
                keys_reader.return_loan(data_seq, info_seq);
            }
            catch (DDS.Exception e) {
                Console.WriteLine("return loan error {0}", e);
            }
        }
 public override void on_sample_rejected(
     DDS.DataReader reader,
     ref DDS.SampleRejectedStatus status)
 {
     Console.WriteLine("ReaderListener: on_sample_rejected()");
 }
    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 --- //

        DDS.SubscriberQos subscriber_qos = new DDS.SubscriberQos();
        participant.get_default_subscriber_qos(subscriber_qos);

        /* If you want to change the Partition name programmatically rather than
         * using the XML, you will need to add the following lines to your code
         * and comment out the create_subscriber() call bellow.
         */

        /*
         * String[] partitions = { "ABC", "X*Z" };
         *
         * subscriber_qos.partition.name.ensure_length(2, 2);
         * subscriber_qos.partition.name.from_array(partitions);
         *
         * DDS.Subscriber subscriber = participant.create_subscriber(
         *  subscriber_qos,
         *  null,
         *  DDS.StatusMask.STATUS_MASK_NONE);
         */

        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 = partitionsTypeSupport.get_type_name();
        try
        {
            partitionsTypeSupport.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 partitions",
            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 */
        partitionsListener reader_listener =
            new partitionsListener();


        /* If you want to change the Datareader QoS programmatically rather than
         * using the XML, you will need to add the following lines to your code
         * and comment out the create_datareader() call bellow.
         */

        /*
         * DDS.DataReaderQos readerQos = new DDS.DataReaderQos();
         * subscriber.get_default_datareader_qos(readerQos);
         *
         * readerQos.reliability.kind = DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS;
         * readerQos.history.kind = DDS.HistoryQosPolicyKind.KEEP_ALL_HISTORY_QOS;
         * readerQos.durability.kind = DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS;
         * DDS.DataReader reader = subscriber.create_datareader(
         *  topic,
         *  readerQos,
         *  reader_listener,
         *  DDS.StatusMask.STATUS_MASK_ALL);
         */

        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)
        {
            System.Threading.Thread.Sleep(receive_period);
        }

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
 public override void on_subscription_matched(
     DDS.DataReader reader,
     ref DDS.SubscriptionMatchedStatus status)
 {
     Console.WriteLine("ReaderListener: on_subscription_matched()");
 }
    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;
    }
 public override void on_requested_deadline_missed(
     DDS.DataReader reader,
     ref DDS.RequestedDeadlineMissedStatus status)
 {
     Console.WriteLine("ParticipantListener: on_requested_deadline_missed()");
 }
 public override void on_requested_deadline_missed(
     DDS.DataReader reader,
     ref DDS.RequestedDeadlineMissedStatus status)
 {
 }
    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);
    }
Пример #31
0
    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;
    }