static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //
        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 subscriber --- //
        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 DynamicData using TypeCode from Shapes.cxx
         * If you are NOT using a type generated with rtiddsgen, you
         * need to create this TypeCode from scratch.
         */
        DDS.TypeCode type_code = ShapeType.get_typecode();
        if (type_code == null) {
            shutdown(participant);
            throw new ApplicationException("create_typecode error");
        }

        /* Create the Dynamic data type support object */
        DDS.DynamicDataTypeSupport type_support =
            new DDS.DynamicDataTypeSupport(type_code,
                new DDS.DynamicDataTypeProperty_t());
        if (type_support == null) {
            shutdown(participant);
            throw new ApplicationException("create_type_support error");
        }

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

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

        /* Make sure both publisher and subscriber share the same topic name.
         * In the Shapes example: we are subscribing to a Square, wich is the
         * topic name. If you want to publish other shapes (Triangle or Circle),
         * you just need to update the topic name.
         */
        DDS.Topic topic = participant.create_topic(
            "Square",
            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 */
        ShapeTypeListener reader_listener =
            new ShapeTypeListener();

        /* First, we create a generic DataReader for our topic */
        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 ApplicationException("create_datareader error");
        }

        // --- Wait for data --- //

        /* Main loop */
        const System.Int32 receive_period = 4000; // milliseconds
        for (int count=0;
             (sample_count == 0) || (count < sample_count);
             ++count) {
            Console.WriteLine(
                "ShapeType subscriber sleeping for {0} sec...",
                receive_period / 1000);

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
    static void publish(int domain_id, int sample_count)
    {
        // --- Create participant --- //

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

        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 DynamicData using TypeCode from Shapes.cpp
         * If you are NOT using a type generated with rtiddsgen, you
         * need to create this TypeCode from scratch.
         */
        DDS.TypeCode type_code = ShapeType.get_typecode();
        if (type_code == null) {
            shutdown(participant);
            throw new ApplicationException("create_typecode error");
        }

        /* Create the Dynamic data type support object */
        DDS.DynamicDataTypeSupport type_support =
            new DDS.DynamicDataTypeSupport(type_code,
                new DDS.DynamicDataTypeProperty_t());
        if (type_support == null) {
            shutdown(participant);
            throw new ApplicationException("create_type_support error");
        }

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

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

        /* Make sure both publisher and subscriber share the same topic
         * name.
         * In the Shapes example: we are publishing a Square, which is the
         * topic name. If you want to publish other shapes (Triangle or
         * Circle), you just need to update the topic name. */
        DDS.Topic topic = participant.create_topic(
            "Square",
            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 --- //

        /* First, we create a generic DataWriter for our topic */
        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");
        }

        /* Then, to use DynamicData, we need to assign the generic
         * DataWriter to a DynamicDataWriter, using a casting. The follow
         * casting should never fail.
         */
        DDS.DynamicDataWriter DynamicData_writer =
            (DDS.DynamicDataWriter)writer;

        // --- Write --- //

        /* Create data sample for writing */
        DDS.DynamicData data = type_support.create_data();
        if (data == null) {
            shutdown(participant);
            throw new ApplicationException(
                "ShapeTypeTypeSupport.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 = ShapeType_writer.register_instance(instance);
        */

        /*** Shape direction variables ***/
        int direction = 1; /* 1 means left to right and -1, right to left */
        int x_position = 50; /* 50 is the initial position */

        /* Initialize the DynamicData object */
        data.set_string("color", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, "BLUE");
        data.set_int("x", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, x_position);
        data.set_int("y", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 100);
        data.set_int("shapesize", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 30);

        /* Main loop */
        const System.Int32 send_period = 100; // milliseconds
        for (int count=0;
             (sample_count == 0) || (count < sample_count);
             ++count) {

            Console.WriteLine("Sending shapesize {0}", 30 + (count % 20));
            Console.WriteLine("Sending x position {0}", x_position);

            /* Modify the shapesize from 30 to 50 */
            try {
                data.set_int("shapesize", DDS.DynamicData.MEMBER_ID_UNSPECIFIED,
                    30 + (count % 20));
            } catch (DDS.Exception e) {
                Console.WriteLine("{0}\nError writing shapesize = {1}",
                    e, 30 + (count %20));
            }

            /* Modify the position */
            try {
                data.set_int("x", DDS.DynamicData.MEMBER_ID_UNSPECIFIED,
                    x_position);
            } catch (DDS.Exception e) {
                Console.WriteLine("{0}\nError writing x_position = {1}",
                    e, x_position);
            }

            /* The x_position will be modified adding or substracting
             * 2 to the previous x_position depending on the direction.
             */
             x_position += (direction * 2);
            /* The x_position will stay between 50 and 150 pixels.
             * When the position is greater than 150 'direction' will be
             * negative (moving to the left) and when it is lower than 50
             * 'direction' will be possitive (moving to the right).
             */
            if (x_position >= 150) {
                direction = -1;
            }
            if (x_position <= 50) {
                direction = 1;
            }

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

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

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

        // --- Shutdown --- //

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

        /* Delete all entities */
        shutdown(participant);
    }
Exemplo n.º 3
0
    static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //
        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 subscriber --- //
        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 DynamicData using TypeCode from Shapes.cxx
         * If you are NOT using a type generated with rtiddsgen, you
         * need to create this TypeCode from scratch.
         */
        DDS.TypeCode type_code = ShapeType.get_typecode();
        if (type_code == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_typecode error");
        }

        /* Create the Dynamic data type support object */
        DDS.DynamicDataTypeSupport type_support =
            new DDS.DynamicDataTypeSupport(type_code,
                                           new DDS.DynamicDataTypeProperty_t());
        if (type_support == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_type_support error");
        }

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

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

        /* Make sure both publisher and subscriber share the same topic name.
         * In the Shapes example: we are subscribing to a Square, wich is the
         * topic name. If you want to publish other shapes (Triangle or Circle),
         * you just need to update the topic name.
         */
        DDS.Topic topic = participant.create_topic(
            "Square",
            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 */
        ShapeTypeListener reader_listener =
            new ShapeTypeListener();

        /* First, we create a generic DataReader for our topic */
        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 ApplicationException("create_datareader error");
        }

        // --- Wait for data --- //

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

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            Console.WriteLine(
                "ShapeType subscriber sleeping for {0} sec...",
                receive_period / 1000);

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }
Exemplo n.º 4
0
    static void publish(int domain_id, int sample_count)
    {
        // --- Create participant --- //

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

        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 DynamicData using TypeCode from Shapes.cpp
         * If you are NOT using a type generated with rtiddsgen, you
         * need to create this TypeCode from scratch.
         */
        DDS.TypeCode type_code = ShapeType.get_typecode();
        if (type_code == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_typecode error");
        }

        /* Create the Dynamic data type support object */
        DDS.DynamicDataTypeSupport type_support =
            new DDS.DynamicDataTypeSupport(type_code,
                                           new DDS.DynamicDataTypeProperty_t());
        if (type_support == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_type_support error");
        }

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

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

        /* Make sure both publisher and subscriber share the same topic
         * name.
         * In the Shapes example: we are publishing a Square, which is the
         * topic name. If you want to publish other shapes (Triangle or
         * Circle), you just need to update the topic name. */
        DDS.Topic topic = participant.create_topic(
            "Square",
            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 --- //

        /* First, we create a generic DataWriter for our topic */
        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");
        }

        /* Then, to use DynamicData, we need to assign the generic
         * DataWriter to a DynamicDataWriter, using a casting. The follow
         * casting should never fail.
         */
        DDS.DynamicDataWriter DynamicData_writer =
            (DDS.DynamicDataWriter)writer;

        // --- Write --- //

        /* Create data sample for writing */
        DDS.DynamicData data = type_support.create_data();
        if (data == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "ShapeTypeTypeSupport.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 = ShapeType_writer.register_instance(instance);
         */


        /*** Shape direction variables ***/
        int direction  = 1;  /* 1 means left to right and -1, right to left */
        int x_position = 50; /* 50 is the initial position */

        /* Initialize the DynamicData object */
        data.set_string("color", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, "BLUE");
        data.set_int("x", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, x_position);
        data.set_int("y", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 100);
        data.set_int("shapesize", DDS.DynamicData.MEMBER_ID_UNSPECIFIED, 30);


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

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            Console.WriteLine("Sending shapesize {0}", 30 + (count % 20));
            Console.WriteLine("Sending x position {0}", x_position);

            /* Modify the shapesize from 30 to 50 */
            try {
                data.set_int("shapesize", DDS.DynamicData.MEMBER_ID_UNSPECIFIED,
                             30 + (count % 20));
            } catch (DDS.Exception e) {
                Console.WriteLine("{0}\nError writing shapesize = {1}",
                                  e, 30 + (count % 20));
            }

            /* Modify the position */
            try {
                data.set_int("x", DDS.DynamicData.MEMBER_ID_UNSPECIFIED,
                             x_position);
            } catch (DDS.Exception e) {
                Console.WriteLine("{0}\nError writing x_position = {1}",
                                  e, x_position);
            }

            /* The x_position will be modified adding or substracting
             * 2 to the previous x_position depending on the direction.
             */
            x_position += (direction * 2);

            /* The x_position will stay between 50 and 150 pixels.
             * When the position is greater than 150 'direction' will be
             * negative (moving to the left) and when it is lower than 50
             * 'direction' will be possitive (moving to the right).
             */
            if (x_position >= 150)
            {
                direction = -1;
            }
            if (x_position <= 50)
            {
                direction = 1;
            }

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

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

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

        // --- Shutdown --- //

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

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