static void publish(int domain_id, int sample_count)
    {
        // --- Create participant --- //

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

        // --- Create publisher --- //

        /* To customize publisher QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Publisher publisher = participant.create_publisher_with_profile(
            "ordered_Library", "ordered_Profile_subscriber_instance",
            null /* listener */, DDS.StatusMask.STATUS_MASK_NONE);
        if (publisher == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_publisher error");
        }


        /* If you want to change the Publisher's QoS programmatically rather
         * than using the XML file, you will need to add the following lines to
         * your code and comment out the create_publisher call above.
         *
         * In this case, we set the presentation publish mode ordered in the
         * topic.
         */

        /* Get default publisher QoS to customize */

/*        DDS.PublisherQos publisher_qos = new DDS.PublisherQos();
 *      participant.get_default_publisher_qos(publisher_qos);
 *
 *      publisher_qos.presentation.access_scope =
 *          DDS.PresentationQosPolicyAccessScopeKind.TOPIC_PRESENTATION_QOS;
 *      publisher_qos.presentation.ordered_access = true;
 *
 *      // --- Create publisher --- //
 *
 *      DDS.Publisher publisher = participant.create_publisher(
 *      publisher_qos, null, DDS.StatusMask.STATUS_MASK_NONE);
 *      if (publisher == null) {
 *          shutdown(participant);
 *          throw new ApplicationException("create_publisher error");
 *      }
 */
        // --- Create topic --- //

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

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

        // --- Create writer --- //

        /* To customize data writer QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataWriter writer = publisher.create_datawriter(
            topic,
            DDS.Publisher.DATAWRITER_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (writer == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_datawriter error");
        }
        orderedDataWriter ordered_writer =
            (orderedDataWriter)writer;

        /* Start changes for Ordered_Presentation */

        /* Create two instances */
        ordered instance0 = null;
        ordered instance1 = null;

        DDS.InstanceHandle_t handle0 = DDS.InstanceHandle_t.HANDLE_NIL;
        DDS.InstanceHandle_t handle1 = DDS.InstanceHandle_t.HANDLE_NIL;



        // --- Write --- //

        /* Create data sample for writing */
        instance0 = orderedTypeSupport.create_data();
        if (instance0 == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "orderedTypeSupport.create_data error");
        }

        instance1 = orderedTypeSupport.create_data();
        if (instance1 == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "orderedTypeSupport.create_data error");
        }

        /* For a data type that has a key, if the same instance is going to be
         * written multiple times, initialize the key here
         * and register the keyed instance prior to writing */
        instance0.id = 0;
        instance1.id = 1;

        handle0 = ordered_writer.register_instance(instance0);
        handle1 = ordered_writer.register_instance(instance1);

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

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            instance0.value = count;
            instance1.value = count;

            Console.WriteLine("writing instance0, value->{0}", instance0.value);
            try {
                ordered_writer.write(instance0, ref handle0);
            } catch (DDS.Exception e) {
                Console.WriteLine("write instance0 error {0}", e);
            }

            Console.WriteLine("writing instance1, value->{0}", instance1.value);
            try {
                ordered_writer.write(instance1, ref handle1);
            } catch (DDS.Exception e) {
                Console.WriteLine("write instance1 error {0}", e);
            }

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


        try {
            ordered_writer.unregister_instance(
                instance0, ref handle0);
        } catch (DDS.Exception e) {
            Console.WriteLine("unregister instance0 error: {0}", e);
        }
        try {
            ordered_writer.unregister_instance(
                instance1, ref handle1);
        } catch (DDS.Exception e) {
            Console.WriteLine("unregister instance1 error: {0}", e);
        }

        // --- Shutdown --- //

        /* Delete data sample */
        try {
            orderedTypeSupport.delete_data(instance0);
        } catch (DDS.Exception e) {
            Console.WriteLine(
                "orderedTypeSupport.delete_data0 error: {0}", e);
        }
        try {
            orderedTypeSupport.delete_data(instance1);
        } catch (DDS.Exception e) {
            Console.WriteLine(
                "orderedTypeSupport.delete_data1 error: {0}", e);
        }

        /* Delete all entities */
        shutdown(participant);
    }
Ejemplo n.º 2
0
    static void publish(int domain_id, int sample_count, int turbo_mode_on)
    {
        System.String profile_name = null;
        System.String library_name = "batching_Library";

        /* We pick the profile name if the turbo_mode is selected or not.
         * If Turbo_mode is not selected, the batching profile will be used.
         */

        if (turbo_mode_on == 1)
        {
            profile_name = "turbo_mode_profile";
            Console.WriteLine("Turbo Mode enable");
        }
        else
        {
            profile_name = "batch_profile";
            Console.WriteLine("Manual batching enable");
        }

        // --- Create participant --- //

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

        // --- Create publisher --- //

        /* To customize publisher QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Publisher publisher = participant.create_publisher_with_profile(
            library_name, profile_name,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (publisher == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_publisher error");
        }

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

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

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

        // --- Create writer --- //

        /* To customize data writer QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataWriter writer = publisher.create_datawriter_with_profile(
            topic,
            library_name, profile_name,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (writer == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_datawriter error");
        }
        batch_dataDataWriter batch_data_writer =
            (batch_dataDataWriter)writer;

        // --- Write --- //

        /* Create data sample for writing */
        batch_data instance = batch_dataTypeSupport.create_data();

        if (instance == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "batch_dataTypeSupport.create_data error");
        }

        /* For a data type that has a key, if the same instance is going to be
         * written multiple times, initialize the key here
         * and register the keyed instance prior to writing */
        DDS.InstanceHandle_t instance_handle = DDS.InstanceHandle_t.HANDLE_NIL;

        /*
         * instance_handle = batch_data_writer.register_instance(instance);
         */

        /* Main loop */
        System.Int32 send_period = 1000; // milliseconds
        if (turbo_mode_on == 1)
        {
            send_period = 0;
        }
        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            Console.WriteLine("Writing batch_data, count {0}", count);

            /* Modify the data to be sent here */
            instance.x = count;

            try {
                batch_data_writer.write(instance, ref instance_handle);
            }
            catch (DDS.Exception e) {
                Console.WriteLine("write error {0}", e);
            }

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

        /*
         * try {
         *  batch_data_writer.unregister_instance(
         *      instance, ref instance_handle);
         * } catch(DDS.Exception e) {
         *  Console.WriteLine("unregister instance error: {0}", e);
         * }
         */

        // --- Shutdown --- //

        /* Delete data sample */
        try {
            batch_dataTypeSupport.delete_data(instance);
        } catch (DDS.Exception e) {
            Console.WriteLine(
                "batch_dataTypeSupport.delete_data error: {0}", e);
        }

        /* Delete all entities */
        shutdown(participant);
    }