示例#1
0
    static void publish(int domain_id, int sample_count, int initial_value,
                        int dwh, int sleep)
    {
        // --- 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(
            DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT,
            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 = hello_worldTypeSupport.get_type_name();
        try {
            hello_worldTypeSupport.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 hello_world",
            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 --- //

        /* If you use Durable Writer History, you need to set several
         * properties. These properties are set in the USER_QOS_PROFILE.xml
         * file, "durable_writer_history_Profile" profile. See that file for
         * further details.
         */
        DDS.DataWriter writer = null;
        if (dwh == 1)
        {
            writer = publisher.create_datawriter_with_profile(topic,
                                                              "persistence_example_Library",
                                                              "durable_writer_history_Profile",
                                                              null, DDS.StatusMask.STATUS_MASK_ALL);
        }
        else
        {
            writer = publisher.create_datawriter_with_profile(topic,
                                                              "persistence_example_Library",
                                                              "persistence_service_Profile",
                                                              null, DDS.StatusMask.STATUS_MASK_ALL);
        }
        if (writer == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_datawriter error");
        }
        hello_worldDataWriter hello_world_writer =
            (hello_worldDataWriter)writer;

        // --- Write --- //

        /* Create data sample for writing */
        hello_world instance = hello_worldTypeSupport.create_data();

        if (instance == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "hello_worldTypeSupport.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 = hello_world_writer.register_instance(instance);
         */

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

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            Console.WriteLine("Writing hello_world, count {0}", initial_value);

            /* Modify the data to be sent here */
            instance.data = initial_value;
            initial_value++;

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

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

        while (sleep != 0)
        {
            System.Threading.Thread.Sleep(one_sec);
            sleep--;
        }

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

        // --- Shutdown --- //

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

        /* Delete all entities */
        shutdown(participant);
    }
示例#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);
    }