コード例 #1
0
        static void publish(int domain_id, int sample_count)
        {
            // --- Register userGenerated datatype ---
            DDS.DomainParticipantFactory.get_instance().
            register_type_support(
                ChatObjectTypeSupport.get_instance(),
                "My::Type::Chat::Obj");

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

            /* To customize the participant QoS, use
             * the configuration file USER_QOS_PROFILES.xml */
            DDS.DomainParticipant participant =
                DDS.DomainParticipantFactory.get_instance().
                create_participant_from_config(
                    "MyParticipant_Library::MyPublicationParticipant");
            if (participant == null)
            {
                shutdown(participant);
                throw new ApplicationException("create_participant error");
            }

            // --- Lookup writer --- //

            /* To customize data writer QoS, use
             * the configuration file USER_QOS_PROFILES.xml */
            DDS.DataWriter writer = participant.lookup_datawriter_by_name(
                "MyPublisher::ChatObjectWriter");
            if (writer == null)
            {
                shutdown(participant);
                throw new ApplicationException("lookup_datawriter error");
            }
            ChatObjectDataWriter ChatObject_writer =
                (ChatObjectDataWriter)writer;

            // --- Write --- //

            /* Create data sample for writing */
            ChatObject instance = ChatObjectTypeSupport.create_data();

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

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

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

                /* Modify the data to be sent here */
                /* >>> */
                instance.msg = "Hello " + count;
                /* <<< */

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

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

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

            // --- Shutdown --- //

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

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