private void initializeDataReader(DDS.DomainParticipant participant)
        {
            DDS.Subscriber subscriber = participant.create_subscriber(
                DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
                null,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (subscriber == null)
            {
                throw new ApplicationException("create_subscriber error");
            }

            DDS.Topic topic = participant.create_topic(
                _topicName,
                _typeName,
                DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
                null,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (topic == null)
            {
                throw new ApplicationException("create_topic error");
            }

            _listener = new InstanceDataReaderListener(
                _groupSubject, _keyedSubjectDict, _keySelector, _comparer, _handleKeyDict, _scheduler,
                _externalSubDict);

            DDS.DataReaderQos rQos = new DDS.DataReaderQos();
            participant.get_default_datareader_qos(rQos);
            //Console.WriteLine("LIB CODE DR QOS: " + r_qos.history.kind);
            //Console.WriteLine("LIB CODE DR QOS: " + r_qos.reliability.kind);

            DDS.DataReader reader = subscriber.create_datareader(
                topic,
                rQos, //DDS.Subscriber.DATAREADER_QOS_DEFAULT,
                _listener,
                DDS.StatusMask.STATUS_MASK_ALL);
            if (reader != null)
            {
                return;
            }
            _listener = null;
            throw new ApplicationException("create_datareader error");
        }
Esempio n. 2
0
        private void initializeDataReader(DDS.DomainParticipant participant)
        {
            DDS.Subscriber subscriber = participant.create_subscriber(
                DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (subscriber == null)
            {
                throw new ApplicationException("create_subscriber error");
            }

            DDS.Topic topic = participant.create_topic(
                _topicName,
                _typeName,
                DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (topic == null)
            {
                throw new ApplicationException("create_topic error");
            }

            _listener = new DataReaderListener(_subject, _scheduler);

            /* 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,
                _listener,
                DDS.StatusMask.STATUS_MASK_ALL);

            if (reader != null)
            {
                return;
            }
            _listener = null;
            throw new ApplicationException("create_datareader error");
        }
Esempio n. 3
0
        public void subscribe()
        {
            DDS.Subscriber subscriber = participant.create_subscriber(
                DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (subscriber == null)
            {
                throw new ApplicationException("create_subscriber error");
            }

            DDS.Topic topic = participant.create_topic(
                topicName,
                typeName,
                DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (topic == null)
            {
                throw new ApplicationException("create_topic error");
            }

            listener = new DataReaderListener(demultiplexer);


            DDS.DataReader reader = subscriber.create_datareader(
                topic,
                DDS.Subscriber.DATAREADER_QOS_DEFAULT,
                listener,
                DDS.StatusMask.STATUS_MASK_ALL);

            if (reader == null)
            {
                listener = null;
                throw new ApplicationException("create_datareader error");
            }
        }
Esempio n. 4
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;
    }
    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);
    }
        private void initializeDataReader(DDS.DomainParticipant participant)
        {
            DDS.Subscriber subscriber = participant.create_subscriber(
                DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (subscriber == null)
            {
                throw new ApplicationException("create_subscriber error");
            }

            DDS.Topic topic = participant.create_topic(
                _topicName,
                _typeName,
                DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (topic == null)
            {
                throw new ApplicationException("create_topic error");
            }

            /* To customize the data reader QoS, use
             * the configuration file USER_QOS_PROFILES.xml */
            _reader = subscriber.create_datareader(
                topic,
                DDS.Subscriber.DATAREADER_QOS_DEFAULT,
                null,
                DDS.StatusMask.STATUS_MASK_ALL);

            if (_reader == null)
            {
                throw new ApplicationException("create_datareader error");
            }

            _statusCondition = _reader.get_statuscondition();

            try
            {
                var mask =
                    (int)DDS.StatusKind.DATA_AVAILABLE_STATUS |
                    (int)DDS.StatusKind.SUBSCRIPTION_MATCHED_STATUS |
                    (int)DDS.StatusKind.LIVELINESS_CHANGED_STATUS |
                    (int)DDS.StatusKind.SAMPLE_LOST_STATUS |
                    (int)DDS.StatusKind.SAMPLE_REJECTED_STATUS;

                _statusCondition.set_enabled_statuses((DDS.StatusMask)mask);
            }
            catch (DDS.Exception e)
            {
                throw new ApplicationException("set_enabled_statuses error {0}", e);
            }

            _waitset = new DDS.WaitSet();

            try
            {
                _waitset.attach_condition(_statusCondition);
            }
            catch (DDS.Exception e)
            {
                throw new ApplicationException("attach_condition error {0}", e);
            }
        }
Esempio n. 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 = ccfTypeSupport.get_type_name();
        try {
            ccfTypeSupport.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 ccf",
            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 Custom Content Filter */
        /* Create and register custom filter */
        custom_filter_type custom_filter = new custom_filter_type();

        try {
            participant.register_contentfilter("CustomFilter", custom_filter);
        }
        catch (DDS.Exception e) {
            Console.WriteLine("write error {0}", e);
        }

        DDS.StringSeq       parameters = new DDS.StringSeq(2);
        DDS.StringWrapper[] param_list = { "2", "divides" };

        parameters.from_array(param_list);

        /* Create content filtered topic */
        DDS.ContentFilteredTopic cft =
            participant.create_contentfilteredtopic_with_filter(
                "ContentFilteredTopic", topic, "%0 %1 x", parameters,
                "CustomFilter"); // custom filter name
        if (cft == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "create_contentfilteredtopic_with_filter error");
        }

        Console.WriteLine("Filter: 2 divides x");

        /* Also note that we pass 'cft' rather than 'topic' to the
         * datareader below
         */

        /* End changes for Custom Contet Filter */

        // --- Create reader --- //

        /* Create a data reader listener */
        ccfListener reader_listener =
            new ccfListener();


        /*
         * NOTE THAT WE USE THE PREVIOUSLY CREATED CUSTOM FILTERED
         * TOPIC TO READ NEW SAMPLES
         */
        /* To customize the data reader QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataReader reader = subscriber.create_datareader(
            cft,
            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)
        {
            if (count == 10)
            {
                Console.WriteLine("changing filter parameters");
                Console.WriteLine("Filter: 15 greater-than x");
                parameters.set_at(0, "15");
                parameters.set_at(1, "greater-than");
                cft.set_expression_parameters(parameters);
            }
            else if (count == 20)
            {
                Console.WriteLine("changing filter parameters");
                Console.WriteLine("Filter: 3 divides x");
                DDS.StringSeq old_parameters = new DDS.StringSeq();
                cft.get_expression_parameters(old_parameters);

                old_parameters.set_at(0, "3");
                old_parameters.set_at(1, "divides");
                cft.set_expression_parameters(old_parameters);
            }

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Esempio n. 8
0
    static void subscribe(int domain_id, int sample_count,
                          String participant_auth)
    {
        // --- Create participant --- //

        /* Start changes for programmatically qos */

        /* Get default participant QoS to customize */
        DDS.DomainParticipantQos participant_qos =
            new DDS.DomainParticipantQos();
        DDS.DomainParticipantFactory.get_instance().
        get_default_participant_qos(participant_qos);

        // If you want to change the Participant's QoS programmatically
        // rather than using the XML file, you will need to uncomment the
        // following line.
        //participant_qos.resource_limits.participant_user_data_max_length = 1024;

        // user_data is opaque to DDS
        int len = participant_auth.Length;
        int max =
            participant_qos.resource_limits.participant_user_data_max_length;

        if (len > max)
        {
            Console.WriteLine(
                "error, participant user_data exceeds resource limits");
        }
        else
        {
            /* Byte type is defined to be 8 bits. If chars are not 8 bits
             * on your system, this will not work.
             */
            participant_qos.user_data.value.from_array(
                System.Text.Encoding.ASCII.GetBytes(participant_auth));
        }

        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 Exception("create_participant error");
        }

        /* End changes for programmatically qos */

        /* The participant is disabled by default. We enable it now */
        try {
            participant.enable();
        } catch (DDS.Exception e) {
            Console.WriteLine("failed to enable participant {0}", e);
            shutdown(participant);
            throw e;
        }

        // --- 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 Exception("create_subscriber error");
        }

        // --- Create topic --- //

        /* Register the type before creating the topic */
        System.String type_name = msgTypeSupport.get_type_name();
        try {
            msgTypeSupport.register_type(participant, type_name);
        }
        catch (DDS.Exception e) {
            Console.WriteLine("register_type error {0}", e);
            shutdown(participant);
            throw e;
        }

        /* To customize the topic QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Topic topic = participant.create_topic(
            "Example msg",
            type_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (topic == null)
        {
            shutdown(participant);
            throw new Exception("create_topic error");
        }

        // --- Create reader --- //

        /* Create a data reader listener */
        msgListener reader_listener = new msgListener();

        /* 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 Exception("create_datareader error");
        }

        // --- Wait for data --- //

        /* Main loop */
        const int 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;
    }
Esempio n. 9
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 you want to set the 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 call above.
         */
        /*
         * DDS.DataReaderQos readerQos = new DDS.DataReaderQos();
         * subscriber.get_default_datareader_qos(readerQos);
         *
         * readerQos.ownership.kind = DDS.OwnershipQosPolicyKind.EXCLUSIVE_OWNERSHIP_QOS;
         *
         * DDS.DataReader reader = subscriber.create_datareader(
         *  topic,
         *  readerQos,
         *  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;
    }
Esempio n. 10
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;
    }
Esempio n. 11
0
    static void subscribe(int domain_id, int sample_count)
    {
        /* Auxiliary variables */
        String odd_string  = "'ODD'";
        String even_string = "'EVEN'";

        // --- 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_query_condTypeSupport.get_type_name();
        try {
            waitset_query_condTypeSupport.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_query_cond",
            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");
        }
        waitset_query_condDataReader waitset_query_cond_reader =
            (waitset_query_condDataReader)reader;

        /* Create query condition */
        DDS.StringSeq query_parameters = new DDS.StringSeq(1);
        query_parameters.ensure_length(1, 1);

        /* The initial value of the parameters is EVEN string */
        query_parameters.set_at(0, even_string);

        String query_expression = "name MATCH %0";

        DDS.QueryCondition query_condition =
            waitset_query_cond_reader.create_querycondition(
                DDS.SampleStateKind.NOT_READ_SAMPLE_STATE,
                DDS.ViewStateKind.ANY_VIEW_STATE,
                DDS.InstanceStateKind.ANY_INSTANCE_STATE,
                query_expression,
                query_parameters);
        if (query_condition == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_querycondition error");
        }

        DDS.WaitSet waitset = new DDS.WaitSet();
        if (waitset == null)
        {
            shutdown(participant);
            throw new ApplicationException("create waitset error");
        }

        /* Attach Query Conditions */
        try {
            waitset.attach_condition((DDS.Condition)query_condition);
        } catch (DDS.Exception e) {
            Console.WriteLine("attach_condition error {0}", e);
            shutdown(participant);
            throw e;
        }

        DDS.Duration_t wait_timeout;
        wait_timeout.nanosec = (uint)500000000;
        wait_timeout.sec     = 1;

        Console.WriteLine("\n>>>Timeout: {0} sec",
                          wait_timeout.sec, wait_timeout.nanosec);
        Console.WriteLine(">>> Query conditions: name MATCH %0");
        Console.WriteLine("\t%0 = {0}", query_parameters.get_at(0));
        Console.WriteLine("---------------------------------\n");


        // --- Wait for data --- //

        /* Main loop */

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            DDS.ConditionSeq active_conditions_seq = new DDS.ConditionSeq();

            /* We set a new parameter in the Query Condition after 7 secs */
            if (count == 7)
            {
                query_parameters.set_at(0, odd_string);
                Console.WriteLine("CHANGING THE QUERY CONDITION");
                Console.WriteLine("\n>>> Query conditions: name MATCH %0");
                Console.WriteLine("\t%0 = {0}", query_parameters.get_at(0));
                Console.WriteLine(">>> We keep one sample in the history");
                Console.WriteLine("-------------------------------------\n");
                query_condition.set_query_parameters(query_parameters);
            }

            /* wait() blocks execution of the thread until one or more attached
             * Conditions become true, or until a user-specified timeout
             * expires.
             */
            try {
                waitset.wait(active_conditions_seq, wait_timeout);
            } 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;
            }

            waitset_query_condSeq data_seq = new waitset_query_condSeq();
            DDS.SampleInfoSeq     info_seq = new DDS.SampleInfoSeq();

            bool follow = true;
            while (follow)
            {
                try {
                    waitset_query_cond_reader.take_w_condition(
                        data_seq, info_seq,
                        DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
                        query_condition);

                    for (int i = 0; i < data_seq.length; ++i)
                    {
                        if (!info_seq.get_at(i).valid_data)
                        {
                            Console.WriteLine("Got metadata");
                            continue;
                        }
                        waitset_query_condTypeSupport.print_data(
                            data_seq.get_at(i));
                    }
                } catch (DDS.Retcode_NoData) {
                    /* When there isn't data, the subscriber stop to
                     * take samples
                     */
                    follow = false;
                } finally {
                    waitset_query_cond_reader.return_loan(data_seq, info_seq);
                }
            }
        }

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
    }
Esempio n. 12
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 = ChatObjectTypeSupport.get_type_name();
            try {
                ChatObjectTypeSupport.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(
                My.CHAT_TOPIC_NAME.VALUE, /*>>><<<*/
                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 ContentFiltered Topic */
            DDS.StringWrapper[] cft_param_list = new DDS.StringWrapper[] { "'Rajive'", "'Shannon'", "'Jaromy'" };
            DDS.StringSeq       cft_parameters = new DDS.StringSeq(3);
            cft_parameters.from_array(cft_param_list);

            DDS.ContentFilteredTopic cft = participant.create_contentfilteredtopic("Chat/filtered",
                                                                                   topic, "(id = %0 OR id = %1 OR id = %2)",
                                                                                   cft_parameters);

            if (cft == null)
            {
                Console.WriteLine("create_contentfilteredtopic error\n");
                shutdown(participant);
                throw new ApplicationException("create_contentfilteredtopic error");
            }
            /* <<< */


            // --- Create reader --- //

            /* Create a data reader listener */
            ChatObjectListener reader_listener =
                new ChatObjectListener();

            /* To customize the data reader QoS, use
             * the configuration file USER_QOS_PROFILES.xml */
            DDS.DataReader reader = subscriber.create_datareader(
                cft,                              /*>>><<<*/
                DDS.Subscriber.DATAREADER_QOS_DEFAULT,
                null,                             /*>>><<<*/
                DDS.StatusMask.STATUS_MASK_NONE); /*>>><<<*/
            if (reader == null)
            {
                shutdown(participant);
                reader_listener = null;
                throw new ApplicationException("create_datareader error");
            }

            /* >>> Setup StatusCondition */
            DDS.StatusCondition status_condition = reader.get_statuscondition();
            if (status_condition.Equals(null))
            {
                Console.WriteLine("get_statuscondition error\n");
                shutdown(participant);
                throw new ApplicationException("get_statuscondition error");
            }
            try {
                status_condition.set_enabled_statuses((DDS.StatusMask)DDS.StatusKind.DATA_AVAILABLE_STATUS);
            }
            catch {
                Console.WriteLine("set_enabled_statuses error\n");
                shutdown(participant);
                throw new ApplicationException("set_enabled_statuses error");
            }
            /* <<< */

            /* >>> Setup WaitSet */
            DDS.WaitSet waitset = new DDS.WaitSet();
            try {
                waitset.attach_condition(status_condition);
            }
            catch {
                // ... error
                waitset.Dispose(); waitset = null;
                shutdown(participant);
                reader_listener.Dispose(); reader_listener = null;
                return;
            }

            // holder for active conditions
            DDS.ConditionSeq active_conditions = new DDS.ConditionSeq();
            /* <<< */


            // --- 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(
                    "ChatObject subscriber sleeping ...",
                    receive_period / 1000);

                /* >>> Wait */
                /* Wait for condition to trigger */
                try {
                    waitset.wait(active_conditions, DDS.Duration_t.DURATION_INFINITE);
                    reader_listener.on_data_available(reader);
                }
                catch {
                }
                /* <<< */

                // System.Threading.Thread.Sleep(receive_period); /*>>><<<*/
            }

            // --- Shutdown --- //

            /* Delete all entities */
            waitset.Dispose(); waitset = null; /*>>><<<*/
            shutdown(participant);
            reader_listener = null;
        }
    static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //

        /* To customize the participant QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DomainParticipant participant =
            DDS.DomainParticipantFactory.get_instance().create_participant(
                domain_id,
                DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
        if (participant == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_participant error");
        }

        /* If you want to change the Participant's QoS programmatically rather
         * than using the XML file, you will need to add the following lines to
         * your code and comment out the create_participant call above.
         */
        /* By default, discovery will communicate via shared memory for
         * platforms that support it.  Because we disable shared memory on the
         * publishing side, we do so here to ensure the reader and writer
         * discover each other.
         */
        /* Get default participant QoS to customize */

/*
 *      DDS.DomainParticipantQos participant_qos =
 *              new DDS.DomainParticipantQos();
 *      try {
 *          DDS.DomainParticipantFactory.get_instance().
 *              get_default_participant_qos(participant_qos);
 *      } catch (DDS.Exception e) {
 *          Console.WriteLine("get_default_participant_qos error {0}", e);
 *            throw e;
 *      }
 *
 *      // By default, data will be sent via shared memory _and_ UDPv4.  Because
 *      // the flowcontroller limits writes across all interfaces, this halves
 *      // the effective send rate.  To avoid this, we enable only the UDPv4
 *      // transport
 *
 *      participant_qos.transport_builtin.mask =
 *              (int) DDS.TransportBuiltinKind.TRANSPORTBUILTIN_UDPv4;
 *
 *      // To create participant with default QoS, use
 *      // DDS_PARTICIPANT_QOS_DEFAULT instead of participant_qos
 *
 *      DDS.DomainParticipant participant =
 *          DDS.DomainParticipantFactory.get_instance().create_participant(
 *              domain_id,
 *              participant_qos,
 *              null,
 *              DDS.StatusMask.STATUS_MASK_NONE);
 *      if (participant == null) {
 *          shutdown(participant);
 *          throw new ApplicationException("create_participant error");
 *      }
 */
        /* End changes for custom_flowcontroller */
        // --- Create subscriber --- //

        /* To customize the subscriber QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Subscriber subscriber = participant.create_subscriber(
            DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (subscriber == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_subscriber error");
        }

        // --- Create topic --- //

        /* Register the type before creating the topic */
        System.String type_name = cfcTypeSupport.get_type_name();
        try {
            cfcTypeSupport.register_type(
                participant, type_name);
        }
        catch (DDS.Exception e) {
            Console.WriteLine("register_type error {0}", e);
            shutdown(participant);
            throw e;
        }

        /* To customize the topic QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Topic topic = participant.create_topic(
            "Example cfc",
            type_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (topic == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_topic error");
        }

        // --- Create reader --- //

        /* Create a data reader listener */
        cfcListener reader_listener =
            new cfcListener();

        /* To customize the data reader QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataReader reader = subscriber.create_datareader(
            topic,
            DDS.Subscriber.DATAREADER_QOS_DEFAULT,
            reader_listener,
            DDS.StatusMask.STATUS_MASK_ALL);
        if (reader == null)
        {
            shutdown(participant);
            reader_listener = null;
            throw new ApplicationException("create_datareader error");
        }

        // --- Wait for data --- //

        /* Main loop */
        const System.Int32 receive_period = 1000; // milliseconds

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            System.Threading.Thread.Sleep(receive_period);
        }

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Esempio n. 14
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;
    }
Esempio n. 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 */
        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);
    }
Esempio n. 16
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.
         *
         * This example uses a built-in QoS profile to enable
         * monitoring on the DomainParticipant.*/
        /*
         * DDS.DomainParticipant participant = DDS.DomainParticipantFactory.
         *  get_instance().create_participant_with_profile(domain_id,
         *      "BuiltinQosLib", "Generic.Monitoring.Common", null,
         *      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 = 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;
        }

        /* To customize the topic QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        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_listener =
            new profilesListener();

        /* 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 you want to change the DataWriter's QoS programatically rather
         * than using the XML file, you will need to add the following lines to
         * your code and comment out the create_datawriter call above.
         *
         * This example uses a built-in QoS profile to tune the QoS for
         * reliable streaming data. */
        /*
         * DDS.DataReader reader = subscriber.create_datareader_with_profile(
         *  topic, DDS.BuiltinQosProfiles.BUILTIN_QOS_LIB_EXP,
         *  DDS.BuiltinQosProfiles.PROFILE_PATTERN_RELIABLE_STREAMING,
         *  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(
                "profiles subscriber sleeping for {0} sec...",
                receive_period / 1000);

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Esempio n. 17
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 --- //

        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;
    }
Esempio n. 18
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 = tbfTypeSupport.get_type_name();
        try {
            tbfTypeSupport.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 tbf",
            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 */
        tbfListener reader_listener =
            new tbfListener();

        /* 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 --- //

        /* If you want to change the time-based filter programmatically (e.g.,
         * to 2 seconds) 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;
         * }
         *
         * DDS.Duration_t minsep = new DDS.Duration_t();
         * minsep.sec = 2; // 2 sec
         * datareader_qos.time_based_filter.minimum_separation = minsep;
         *
         * try
         * {
         *  subscriber.delete_datareader(ref reader);
         * }
         * catch (DDS.Exception e)
         * {
         *  Console.WriteLine("delete_datareader (default qos) error {0}", e);
         *  shutdown(participant);
         *  throw e;
         * }
         *
         * reader = subscriber.create_datareader(topic,
         *  datareader_qos, reader_listener, DDS.StatusMask.STATUS_MASK_ALL);
         * if (reader == null)
         * {
         *  shutdown(participant);
         *  throw new ApplicationException("create_datareader error");
         * }
         */

        /* Results table header: (1) source timestamp of the sample received;
         * (2) instance id (instance.code value); and (3) value of x
         * (instance.x).
         */
        Console.WriteLine("================================================");
        Console.WriteLine("Source timestamp\tInstance\tX");
        Console.WriteLine("================================================");

        /* Main loop */
        const System.Int32 receive_period = 4000; // milliseconds

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            /* Console.WriteLine(
             *  "tbf 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 --- //
        int count = 0;

        DDS.Duration_t timeout;
        timeout.nanosec = (uint)500000000;
        timeout.sec     = 1;

        /* Main loop */
        while (sample_count == 0 || count < sample_count)
        {
            DDS.ConditionSeq active_conditions = new DDS.ConditionSeq();

            // The triggered condition(s) will be placed in active_conditions
            try
            {
                waitset.wait(active_conditions, timeout);
                Console.WriteLine("got {0} active conditions", active_conditions.length);

                for (int i = 0; i < active_conditions.length; ++i)
                {
                    /* In this case, we have only a single condition, but
                     *          if we had multiple, we would need to iterate over
                     *          them and check which one is true.  Leaving the logic
                     *          for the more complex case. */
                    if (active_conditions.get_at(i) == status_condition)
                    {
                        DDS.StatusMask triggeredmask =
                            waitset_reader.get_status_changes();

                        if ((triggeredmask &
                             (DDS.StatusMask)
                             DDS.StatusKind.DATA_AVAILABLE_STATUS) != 0)
                        {
                            /* Current conditions match our conditions to read data, so
                             * we can read data just like we would do in any other
                             * example. */
                            waitset_statuscondSeq data_seq
                                = new waitset_statuscondSeq();
                            DDS.SampleInfoSeq info_seq
                                = new DDS.SampleInfoSeq();

                            try
                            {
                                /* Access data using read(), take(), etc.  If
                                 * you fail to do this the condition will
                                 * remain true, and the WaitSet will wake up
                                 * immediately - causing high CPU usage when it
                                 * does not sleep in the loop */
                                waitset_reader.take(
                                    data_seq,
                                    info_seq,
                                    DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
                                    DDS.SampleStateKind.ANY_SAMPLE_STATE,
                                    DDS.ViewStateKind.ANY_VIEW_STATE,
                                    DDS.InstanceStateKind.ANY_INSTANCE_STATE);
                            }
                            catch (DDS.Retcode_NoData e)
                            {
                                shutdown(participant);
                                throw e;
                            }
                            catch (DDS.Exception e)
                            {
                                shutdown(participant);
                                throw e;
                            }

                            /* Iterate over returned data.  Print the data
                             * values if it is not metadata.
                             */
                            System.Int32 data_length = data_seq.length;
                            for (int j = 0; i < data_length; ++i)
                            {
                                if (!info_seq.get_at(j).valid_data)
                                {
                                    Console.WriteLine("Got metadata");
                                    continue;
                                }
                                waitset_statuscondTypeSupport.print_data(data_seq.get_at(j));
                            }

                            try
                            {
                                /* Return the loaned data */
                                waitset_reader.return_loan(data_seq, info_seq);
                            }
                            catch (DDS.Exception e)
                            {
                                Console.WriteLine("return loan error {0}", e);
                            }
                        }
                    }
                }
            }
            catch (DDS.Retcode_Timeout)
            {
                Console.WriteLine("wait timed out");
                count += 2;
                continue;
            }
        }
        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
    }
Esempio n. 20
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 = market_dataTypeSupport.get_type_name();
        try {
            market_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 market_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");
        }

        /* Start changes for MultiChannel */
        DDS.StringSeq            expression_parameters = new DDS.StringSeq(1);
        DDS.ContentFilteredTopic filtered_topic        =
            participant.create_contentfilteredtopic_with_filter(
                "Example market_data",
                topic,
                "Symbol MATCH 'A'", expression_parameters,
                DDS.DomainParticipant.STRINGMATCHFILTER_NAME);

        // --- Create reader --- //

        /* Create a data reader listener */
        market_dataListener reader_listener =
            new market_dataListener();

        /* To customize the data reader QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataReader reader = subscriber.create_datareader(
            filtered_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(
             *    "market_data subscriber sleeping for {0} sec...",
             *    receive_period / 1000);
             *
             * System.Threading.Thread.Sleep(receive_period);
             */

            if (count == 3)
            {
                filtered_topic.append_to_expression_parameter(0, "D");
                Console.WriteLine("changed filter to Symbol MATCH 'AD'");
            }
            if (count == 6)
            {
                filtered_topic.remove_from_expression_parameter(0, "A");
                Console.WriteLine("changed filter to Symbol MATCH 'D'");
            }

            System.Threading.Thread.Sleep(receive_period);
        }
        /* End changes for MultiChannel */

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Esempio n. 21
0
        private void initialize()
        {
            DDS.Subscriber subscriber = participant.create_subscriber(
                DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (subscriber == null)
            {
                throw new ApplicationException("create_subscriber error");
            }

            DDS.Topic topic = participant.create_topic(
                topicName,
                typeName,
                DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (topic == null)
            {
                throw new ApplicationException("create_topic error");
            }

            reader = subscriber.create_datareader(
                topic,
                DDS.Subscriber.DATAREADER_QOS_DEFAULT,
                null,
                DDS.StatusMask.STATUS_MASK_ALL);

            if (reader == null)
            {
                throw new ApplicationException("create_datareader error");
            }
            status_condition = reader.get_statuscondition();

            try
            {
                int mask =
                    (int)DDS.StatusKind.DATA_AVAILABLE_STATUS |
                    (int)DDS.StatusKind.SUBSCRIPTION_MATCHED_STATUS |
                    (int)DDS.StatusKind.LIVELINESS_CHANGED_STATUS |
                    (int)DDS.StatusKind.SAMPLE_LOST_STATUS |
                    (int)DDS.StatusKind.SAMPLE_REJECTED_STATUS;

                status_condition.set_enabled_statuses((DDS.StatusMask)mask);
            }
            catch (DDS.Exception e)
            {
                throw new ApplicationException("set_enabled_statuses error {0}", e);
            }

            waitset = new DDS.WaitSet();

            try
            {
                waitset.attach_condition(status_condition);
            }
            catch (DDS.Exception e)
            {
                throw new ApplicationException("attach_condition error {0}", e);
            }
        }
Esempio n. 22
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;
    }
    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 = waitsetsTypeSupport.get_type_name();
        try {
            waitsetsTypeSupport.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 waitsets",
            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 the DataReader'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_datareader call above.
         *
         * In this case, we reduce the liveliness timeout period to trigger the
         * StatusCondition DDS.StatusKind.LIVELINESS_CHANGED_STATUS
         */
        /*
         * DDS.DataReaderQos datawriter_qos = new DDS.DataReaderQos();
         * try {
         *  subscriber.get_default_datareader_qos(datawriter_qos);
         * } catch (DDS.Exception e) {
         *  Console.WriteLine("get_default_datareader_qos error {0}", e);
         *  shutdown(participant);
         *  throw e;
         * }
         * datawriter_qos.liveliness.lease_duration.sec = 2;
         * datawriter_qos.liveliness.lease_duration.nanosec = 0;
         *
         * reader = subscriber.create_datareader(topic, datawriter_qos,
         *  null, DDS.StatusMask.STATUS_MASK_NONE);
         * if (reader == null) {
         *  shutdown(participant);
         *  throw new ApplicationException("create_datawriter_qos error");
         * }
         */

        /* Create read condition
         * ---------------------
         * Note that the Read Conditions are dependent on both incoming
         * data as well as sample state. Thus, this method has more
         * overhead than adding a DDS.StatusKind.DATA_AVAILABLE_STATUS
         * StatusCondition. We show it here purely for reference
         */
        DDS.ReadCondition read_condition = reader.create_readcondition(
            DDS.SampleStateKind.NOT_READ_SAMPLE_STATE,
            DDS.ViewStateKind.ANY_VIEW_STATE,
            DDS.InstanceStateKind.ANY_INSTANCE_STATE);
        if (read_condition == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_readcondition error");
        }

        /* 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();
        if (status_condition.get_entity() == null)
        {
            shutdown(participant);
            throw new ApplicationException("get_statuscondition error");
        }

        /* Set enabled statuses
         * --------------------
         * Now that we have the Status Condition, we are going to enable the
         * statuses we are interested in:
         * DDS.StatusKind.SUBSCRIPTION_MATCHED_STATUS
         * and DDS.StatusKind.LIVELINESS_CHANGED_STATUS.
         */
        try {
            status_condition.set_enabled_statuses(
                DDS.StatusMask.STATUS_MASK_NONE |
                (DDS.StatusMask)DDS.StatusKind.SUBSCRIPTION_MATCHED_STATUS |
                (DDS.StatusMask)DDS.StatusKind.LIVELINESS_CHANGED_STATUS);
        } catch (DDS.Exception e) {
            Console.WriteLine("set_enabled_statuses error {0}", e);
            shutdown(participant);
            throw 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 Read Conditions */
        try {
            waitset.attach_condition(read_condition);
        } catch (DDS.Exception e) {
            Console.WriteLine("attach_read_condition error {0}", e);
            shutdown(participant);
            throw e;
        }

        /* Attach Status Conditions */
        try {
            waitset.attach_condition(status_condition);
        } catch (DDS.Exception e) {
            Console.WriteLine("attach_status_condition error {0}", e);
            shutdown(participant);
            throw e;
        }

        /* Narrow the reader into your specific data type */
        waitsetsDataReader waitsets_reader =
            (waitsetsDataReader)reader;


        /* Main loop */
        for (int count = 0; (sample_count == 0) || (count < sample_count);
             ++count)
        {
            DDS.ConditionSeq active_conditions_seq = new DDS.ConditionSeq();
            DDS.Duration_t   timeout;
            timeout.nanosec = (uint)500000000;
            timeout.sec     = 1;

            /* wait() blocks execution of the thread until one or more attached
             * Conditions become true, or until a user-specified timeout expires
             */
            try {
                waitset.wait(active_conditions_seq, timeout);
            } 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;
            }

            /* Get the number of active conditions */
            int active_conditions = active_conditions_seq.length;
            Console.WriteLine("Got {0} active conditions", active_conditions);

            for (int i = 0; i < active_conditions; i++)
            {
                /* Now we compare the current condition with the Status
                 * Conditions and the Read Conditions previously defined. If
                 * they match, we print the condition that was triggered.*/

                /* Compare with Status Conditions */
                if (active_conditions_seq.get_at(i) == status_condition)
                {
                    /* Get the status changes so we can check which status
                     * condition triggered. */
                    DDS.StatusMask triggeredmask =
                        waitsets_reader.get_status_changes();

                    /* Liveliness changed */
                    DDS.StatusMask test = triggeredmask &
                                          (DDS.StatusMask)DDS.StatusKind.
                                          LIVELINESS_CHANGED_STATUS;
                    if (test != DDS.StatusMask.STATUS_MASK_NONE)
                    {
                        DDS.LivelinessChangedStatus st =
                            new DDS.LivelinessChangedStatus();
                        waitsets_reader.get_liveliness_changed_status(ref st);
                        Console.WriteLine("Liveliness changed => " +
                                          "Active writers = {0}", st.alive_count);
                    }

                    /* Subscription matched */
                    test = triggeredmask &
                           (DDS.StatusMask)DDS.StatusKind.
                           SUBSCRIPTION_MATCHED_STATUS;
                    if (test != DDS.StatusMask.STATUS_MASK_NONE)
                    {
                        DDS.SubscriptionMatchedStatus st =
                            new DDS.SubscriptionMatchedStatus();
                        waitsets_reader.get_subscription_matched_status(ref st);
                        Console.WriteLine("Subscription matched => " +
                                          "Cumulative matches = {0}", st.total_count);
                    }
                }

                /* Compare with Read Conditions */
                else if (active_conditions_seq.get_at(i) == read_condition)
                {
                    /* Current conditions match our conditions to read data, so
                     * we can read data just like we would do in any other
                     * example. */
                    waitsetsSeq       data_seq = new waitsetsSeq();
                    DDS.SampleInfoSeq info_seq = new DDS.SampleInfoSeq();

                    /* You may want to call take_w_condition() or
                     * read_w_condition() on the Data Reader. This way you will
                     * use the same status masks that were set on the Read
                     * Condition.
                     * This is just a suggestion, you can always use
                     * read() or take() like in any other example.
                     */
                    bool follow = true;
                    while (follow)
                    {
                        try {
                            waitsets_reader.take_w_condition(
                                data_seq, info_seq,
                                DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
                                read_condition);

                            for (int j = 0; j < data_seq.length; ++j)
                            {
                                if (!info_seq.get_at(j).valid_data)
                                {
                                    Console.WriteLine("Got metadata");
                                    continue;
                                }
                                waitsetsTypeSupport.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 {
                            waitsets_reader.return_loan(data_seq, info_seq);
                        }
                    }
                }
            }
        }

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
    }
    static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //

        /* To customize the participant QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DomainParticipant participant =
            DDS.DomainParticipantFactory.get_instance().create_participant(
                domain_id,
                DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);

        /* If 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;
    }
Esempio n. 25
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");
        }

        /* If you want to change the DataWriter'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_subscriber call above.
         */

        // Start changes for coherent_presentation
        /* Get default publisher QoS to customize */

/*        DDS.SubscriberQos subscriber_qos = new DDS.SubscriberQos();
 *      participant.get_default_subscriber_qos(subscriber_qos);
 *
 *      subscriber_qos.presentation.access_scope =
 *            DDS.PresentationQosPolicyAccessScopeKind.TOPIC_PRESENTATION_QOS;
 *      subscriber_qos.presentation.coherent_access = true;
 *
 *      DDS.Subscriber subscriber = participant.create_subscriber(
 *          subscriber_qos,
 *          null,
 *          DDS.StatusMask.STATUS_MASK_NONE);
 *      if (subscriber == null) {
 *          shutdown(participant);
 *          throw new ApplicationException("create_subscriber error");
 *      }
 *
 */     // End changes for coherent_presentation

        // --- Create topic --- //

        /* Register the type before creating the topic */
        System.String type_name = coherentTypeSupport.get_type_name();
        try {
            coherentTypeSupport.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 coherent",
            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 */
        coherentListener reader_listener =
            new coherentListener();

        /* 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");
        }

        // Start changes for coherent_presentation

        /* Get default datareader QoS to customize */

/*        DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos();
 *      subscriber.get_default_datareader_qos(datareader_qos);
 *
 *      datareader_qos.reliability.kind =
 *          DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS;
 *      datareader_qos.history.depth = 10;
 *
 *      DDS.DataReader 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");
 *      }
 */     // End changes for coherent_presentation

        // --- 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;
    }
Esempio n. 26
0
    static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //
        DDS.DomainParticipant participant =
            DDS.DomainParticipantFactory.get_instance().create_participant(
                domain_id,
                DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
        if (participant == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_participant error");
        }

        // --- Create subscriber --- //
        DDS.Subscriber subscriber = participant.create_subscriber(
            DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (subscriber == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_subscriber error");
        }

        /* Create DynamicData using TypeCode from Shapes.cxx
         * If you are NOT using a type generated with rtiddsgen, you
         * need to create this TypeCode from scratch.
         */
        DDS.TypeCode type_code = ShapeType.get_typecode();
        if (type_code == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_typecode error");
        }

        /* Create the Dynamic data type support object */
        DDS.DynamicDataTypeSupport type_support =
            new DDS.DynamicDataTypeSupport(type_code,
                                           new DDS.DynamicDataTypeProperty_t());
        if (type_support == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_type_support error");
        }

        // --- Create topic --- //

        /* Register type before creating topic */
        System.String type_name = EXAMPLE_TYPE_NAME;
        try {
            type_support.register_type(participant, EXAMPLE_TYPE_NAME);
        } catch (DDS.Exception e) {
            Console.WriteLine("register_type error {0}", e);
            shutdown(participant);
            throw e;
        }

        /* Make sure both publisher and subscriber share the same topic name.
         * In the Shapes example: we are subscribing to a Square, wich is the
         * topic name. If you want to publish other shapes (Triangle or Circle),
         * you just need to update the topic name.
         */
        DDS.Topic topic = participant.create_topic(
            "Square",
            type_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (topic == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_topic error");
        }

        // --- Create reader --- //

        /* Create a data reader listener */
        ShapeTypeListener reader_listener =
            new ShapeTypeListener();

        /* First, we create a generic DataReader for our topic */
        DDS.DataReader reader = subscriber.create_datareader(
            topic,
            DDS.Subscriber.DATAREADER_QOS_DEFAULT,
            reader_listener,
            DDS.StatusMask.STATUS_MASK_ALL);
        if (reader == null)
        {
            shutdown(participant);
            reader_listener = null;
            throw new ApplicationException("create_datareader error");
        }

        // --- Wait for data --- //

        /* Main loop */
        const System.Int32 receive_period = 4000; // milliseconds

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            Console.WriteLine(
                "ShapeType subscriber sleeping for {0} sec...",
                receive_period / 1000);

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
    static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //

        /* 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;
    }
Esempio n. 28
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 = sequencesTypeSupport.get_type_name();
        try {
            sequencesTypeSupport.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 sequences",
            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 */
        sequencesListener reader_listener =
            new sequencesListener();

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

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Esempio n. 29
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 = deadline_contentfilterTypeSupport.get_type_name();
        try {
            deadline_contentfilterTypeSupport.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 deadline_contentfilter",
            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 --- //

        //// Start changes for Deadline
        // Set up content filtered topic to show interaction with deadline
        DDS.StringSeq parameters = new DDS.StringSeq(1);
        // need to specify length otherwise create_contentfilteredtopic will
        // throw an unhandled exception error!
        if (parameters.ensure_length(1, 1) == false)
        {
            Console.WriteLine("ensure_length error\n");
        }
        parameters.set_at(0, "2");

        DDS.ContentFilteredTopic cft = participant.create_contentfilteredtopic(
            "ContentFilteredTopic", topic, "code < %0", parameters);

        /* Create a data reader listener */
        deadline_contentfilterListener reader_listener =
            new deadline_contentfilterListener();

        /* To customize the data reader QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataReader reader = subscriber.create_datareader(
            cft,
            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 change the DataReader'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_datareader call above.
         *
         * In this case, we set the deadline period to 2 seconds to trigger
         * a deadline if the DataWriter does not update often enough, or if
         * the content-filter filters out data so there is no data available
         * with 2 seconds.
         */

        /*
         * DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos();
         * subscriber.get_default_datareader_qos(datareader_qos);
         * if (datareader_qos == null)
         * {
         *  shutdown(participant);
         *  reader_listener = null;
         *  throw new ApplicationException("get_default_datareader_qos error");
         * }
         *
         * // Set deadline QoS to be 2 sec
         * datareader_qos.deadline.period.sec = 2;
         * datareader_qos.deadline.period.nanosec = 0;
         *
         * DDS.DataReader reader = subscriber.create_datareader(
         *  topic,
         *  datareader_qos, //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)
        {
            // After 10 seconds, change content filter to accept only
            // instance 0

            if (count == 10)
            {
                Console.WriteLine("Starting to filter out instance1\n");
                parameters.set_at(0, "1");
                cft.set_expression_parameters(parameters);
            }
            System.Threading.Thread.Sleep(receive_period);
        }

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Esempio n. 30
0
        private static void Subscribe(int domainId, int sampleCount)
        {
            // --- Create participant --- //
            DDS.DomainParticipant participant =
                DDS.DomainParticipantFactory.get_instance().create_participant(
                    domainId,
                    DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
                    null /* listener */,
                    DDS.StatusMask.STATUS_MASK_NONE);
            if (participant == null)
            {
                Shutdown(participant);
                throw new Exception("create_participant error");
            }

            // --- Create subscriber --- //
            DDS.Subscriber subscriber = participant.create_subscriber(
                DDS.DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
            if (subscriber == null)
            {
                Shutdown(participant);
                throw new ApplicationException("create_subscriber error");
            }

            // --- Create topic --- //
            // Register the type before creating the topic.
            string typeName = FlightTypeSupport.get_type_name();

            try {
                FlightTypeSupport.register_type(participant, typeName);
            }
            catch (DDS.Exception e) {
                Console.WriteLine("register_type error {0}", e);
                Shutdown(participant);
                throw e;
            }

            DDS.Topic topic = participant.create_topic(
                "Example Flight",
                typeName,
                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 --- //
            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");
            }

            FlightDataReader flightReader = (FlightDataReader)reader;

            // Query for company named 'CompanyA' and for flights in cruise
            // (about 30,000ft). The company parameter will be changed in
            // run-time. NOTE: There must be single-quotes in the query
            // parameters around-any strings! The single-quote do NOT go in the
            // query condition itself.
            DDS.StringSeq queryParameters = new DDS.StringSeq();
            queryParameters.ensure_length(2, 2);
            queryParameters.set_at(0, "'CompanyA'");
            queryParameters.set_at(1, "30000");
            Console.WriteLine(
                "Setting parameters to company: {0} and altitude >= {1}\n",
                queryParameters.get_at(0), queryParameters.get_at(1));

            // Create the query condition with an expession to MATCH the id
            // field in the structure and a numeric comparison.
            DDS.QueryCondition queryCondition =
                reader.create_querycondition(
                    DDS.SampleStateKind.ANY_SAMPLE_STATE,
                    DDS.ViewStateKind.ANY_VIEW_STATE,
                    DDS.InstanceStateKind.ALIVE_INSTANCE_STATE,
                    "company MATCH %0 AND altitude >= %1",
                    queryParameters);

            // --- Wait for data --- //
            const int receivePeriod = 1000; // Milliseconds
            bool      update        = false;

            for (int count = 0;
                 (sampleCount == 0) || (count < sampleCount);
                 count++)
            {
                // Poll for new samples every second.
                System.Threading.Thread.Sleep(receivePeriod);

                // Change the filter parameter after 5 seconds.
                if ((count + 1) % 10 == 5)
                {
                    queryParameters.set_at(0, "'CompanyB'");
                    update = true;
                }
                else if ((count + 1) % 10 == 0)
                {
                    queryParameters.set_at(0, "'CompanyA'");
                    update = true;
                }

                // Set new parameters.
                if (update)
                {
                    Console.WriteLine("Changing parameter to {0}",
                                      queryParameters.get_at(0));
                    queryCondition.set_query_parameters(queryParameters);
                    update = false;
                }

                // Iterate through the samples using read_w_condition.
                FlightSeq         dataSeq = new FlightSeq();
                DDS.SampleInfoSeq infoSeq = new DDS.SampleInfoSeq();
                try {
                    flightReader.read_w_condition(
                        dataSeq,
                        infoSeq,
                        DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
                        queryCondition);
                } catch (DDS.Retcode_NoData) {
                    continue;
                } catch (DDS.Exception e) {
                    Shutdown(participant);
                    throw e;
                }

                for (int i = 0; i < dataSeq.length; i++)
                {
                    DDS.SampleInfo info = (DDS.SampleInfo)infoSeq.get_at(i);
                    if (info.valid_data)
                    {
                        Flight flight_info = (Flight)dataSeq.get_at(i);
                        Console.WriteLine(
                            "\t[trackId: {0}, company: {1}, altitude: {2}]\n",
                            flight_info.trackId,
                            flight_info.company,
                            flight_info.altitude);
                    }
                }

                try {
                    flightReader.return_loan(dataSeq, infoSeq);
                } catch (DDS.Exception e) {
                    Console.WriteLine("return loan error {0}", e);
                }
            }

            // Delete all entities
            Shutdown(participant);
        }