예제 #1
0
    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 --- //

        /* Start - modifying the generated example to showcase the usage of
         * the get_publishers API
         */

        /* We create 2 publishers */
        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");
        }

        /* We use the publisher.GetHashCode() to identify the publisher ID */
        Console.WriteLine("The first publishers is " + publisher.GetHashCode());

        DDS.Publisher publisher2 = participant.create_publisher(
            DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (publisher2 == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_publisher2 error");
        }

        Console.WriteLine("The second publishers is " + publisher2.GetHashCode());
        Console.WriteLine("Let's call get_publisher() now and check if " +
                          "I get all my publishers...");

        DDS.PublisherSeq publisherSeq = new DDS.PublisherSeq();

        participant.get_publishers(publisherSeq);

        for (int count = 0; count < publisherSeq.length; ++count)
        {
            DDS.Publisher tmp = publisherSeq.get_at(count);
            Console.WriteLine("The {0} publisher I found is: {1}",
                              count, tmp.GetHashCode());
        }

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

        // --- Shutdown --- //

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