Пример #1
0
    static void publish(int domain_id, int sample_count, Route routeIn, Route.Bus busIn)
    {
        Random random       = new Random();
        int    randomNumber = random.Next(0, 100);

        // --- 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 atype_name = AccidentTypeSupport.get_type_name();
        System.String ptype_name = PositionTypeSupport.get_type_name();
        try
        {
            AccidentTypeSupport.register_type(
                participant, atype_name);
            PositionTypeSupport.register_type(
                participant, ptype_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 atopic = participant.create_topic(
            "P3464_BHANDERSON: PT/ALR/ACC",
            atype_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        DDS.Topic ptopic = participant.create_topic(
            "P3464_BHANDERSON: PT/POS",
            ptype_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);

        if (atopic == null || ptopic == 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 awriter = publisher.create_datawriter(
            atopic,
            DDS.Publisher.DATAWRITER_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        DDS.DataWriter pwriter = publisher.create_datawriter(
            ptopic,
            DDS.Publisher.DATAWRITER_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);

        if (awriter == null || pwriter == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_datawriter error");
        }
        AccidentDataWriter Accident_writer =
            (AccidentDataWriter)awriter;
        PositionDataWriter Position_writer =
            (PositionDataWriter)pwriter;
        // --- Write --- //

        /* Create data sample for writing */
        Accident ainstance = AccidentTypeSupport.create_data();
        Position pinstance = PositionTypeSupport.create_data();

        if (ainstance == null || pinstance == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "(Accident|Position)TypeSupport.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 = Accident_writer.register_instance(instance);
         */



        int numPasses = 0;
        /* Main loop */
        int send_period = 4000; // milliseconds

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            if (count % routeIn.numStops == 0)
            {
                numPasses++;
            }
            if (numPasses > 3)
            {
                break;
            }

            Console.WriteLine("Writing Accident|Position, count {0}", count);

            /* Modify the data to be sent here */

            int traffic = random.Next(0, 100);
            if (traffic <= 25) // light 25%
            {
                pinstance.trafficConditions = "light";
                pinstance.timeBetweenStops  = (int)(Math.Ceiling(routeIn.timeBetweenStops * .75));
            }
            else if (traffic <= 50) // heavy 25%
            {
                pinstance.trafficConditions = "heavy";
                pinstance.timeBetweenStops  = (int)(routeIn.timeBetweenStops + Math.Ceiling(routeIn.timeBetweenStops * .25));
            }
            else // normal 50%
            {
                pinstance.trafficConditions = "normal";
                pinstance.timeBetweenStops  = routeIn.timeBetweenStops;
            }

            ainstance.timestamp  = DateTime.Now.ToString();
            ainstance.route      = routeIn.name;
            ainstance.vehicle    = busIn.id;
            ainstance.stopNumber = busIn.stop;

            pinstance.timestamp   = DateTime.Now.ToString();
            pinstance.route       = routeIn.name;
            pinstance.vehicle     = busIn.id;
            pinstance.stopNumber  = busIn.stop;
            pinstance.numStops    = routeIn.numStops;
            pinstance.fillInRatio = random.Next(0, 100);

            int aOccurs = random.Next(0, 100);

            if (aOccurs <= 10)
            {
                try
                {
                    Accident_writer.write(ainstance, ref instance_handle);
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("write error {0}", e);
                }
                pinstance.timeBetweenStops += 10;
            }

            send_period = pinstance.timeBetweenStops * 1000; // threads use milliseconds

            busIn.stop = (busIn.stop % routeIn.numStops) + 1;

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

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

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

        // --- Shutdown --- //

        /* Delete data sample */
        try
        {
            AccidentTypeSupport.delete_data(ainstance);
            PositionTypeSupport.delete_data(pinstance);
        }
        catch (DDS.Exception e)
        {
            Console.WriteLine(
                "(Accident|Position)TypeSupport.delete_data error: {0}", e);
        }

        /* Delete all entities */
        shutdown(participant);
    }
Пример #2
0
    static void subscribe(int domain_id, int sample_count)
    {
        // --- Create participant --- //

        /* To customize the 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 subscriber --- //

        /* To customize the subscriber QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        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 topic --- //

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

        /* To customize the topic QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Topic atopic = participant.create_topic(
            "P3464_BHANDERSON: PT/ALR/ACC",
            atype_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        DDS.Topic ptopic = participant.create_topic(
            "P3464_BHANDERSON: PT/POS",
            ptype_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);

        if (atopic == null || ptopic == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_topic error");
        }

        // --- Create reader --- //

        /* Create a data reader listener */
        AccidentListener reader_listener =
            new AccidentListener();

        /* To customize the data reader QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DataReader areader = subscriber.create_datareader(
            atopic,
            DDS.Subscriber.DATAREADER_QOS_DEFAULT,
            reader_listener,
            DDS.StatusMask.STATUS_MASK_ALL);
        DDS.DataReader preader = subscriber.create_datareader(
            ptopic,
            DDS.Subscriber.DATAREADER_QOS_DEFAULT,
            reader_listener,
            DDS.StatusMask.STATUS_MASK_ALL);
        if (areader == null || preader == 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(
                "Subscriber sleeping for {0} sec...",
                receive_period / 1000);

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

        // --- Shutdown --- //

        /* Delete all entities */
        shutdown(participant);
        reader_listener = null;
    }