Пример #1
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;
    }
    static void subscribe(int domain_id, int sample_count, 
        String participant_auth, String reader_auth)
    {
        // --- Create participant --- //

        /* Start changes for programmatically qos */

        /* 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 participant call above.
         */

        /* Set user_data qos field for participant */

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

        DDS.StringSeq temp = new DDS.StringSeq(0);
        participant_qos.discovery.multicast_receive_addresses = temp;

        // user_data is opaque to DDS, so we include trailing \0 for string
        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.Default.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 ApplicationException("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 ApplicationException("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 ApplicationException("create_topic error");
        }

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

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

        /* Start changes for Builtin_Topics */
        /* Set user_data qos field for datareader */
        DDS.DataReaderQos datareader_qos = new DDS.DataReaderQos();
        subscriber.get_default_datareader_qos(datareader_qos);

        /* user_data is opaque to DDS, so we include trailing \0 for string */
        len = reader_auth.Length;
        max = participant_qos.resource_limits.reader_user_data_max_length;

        if (len > max) {
            Console.WriteLine(
                "error, datareader 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.
             */
            datareader_qos.user_data.value.from_array(
                System.Text.Encoding.Default.GetBytes(reader_auth));
        }

        /* To customize the data reader QoS, use
           the configuration file USER_QOS_PROFILES.xml */
        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");
        }

        // --- 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;
    }