Ejemplo n.º 1
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;
    }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
0
        public override void on_data_available(DDS.DataReader reader)
        {
            if (reader.GetType() == typeof(PositionDataReader))
            {
                PositionDataReader Position_reader =
                    (PositionDataReader)reader;

                try
                {
                    Position_reader.take(
                        pdata_seq,
                        info_seq,
                        DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
                        DDS.SampleStateKind.ANY_SAMPLE_STATE,
                        DDS.ViewStateKind.ANY_VIEW_STATE,
                        DDS.InstanceStateKind.ANY_INSTANCE_STATE);
                }
                catch (DDS.Retcode_NoData)
                {
                    return;
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("take error {0}", e);
                    return;
                }
                //Console.WriteLine("MessageType\t Route\t\t Vehicle\t Traffic\t Stop#\t #Stops\t TimeBetweenStops\t Fill%\t TimeStamp");
                System.Int32 data_length = pdata_seq.length;
                for (int i = 0; i < data_length; ++i)
                {
                    if (info_seq.get_at(i).valid_data)
                    {
                        Position p = pdata_seq.get_at(i);

                        if (string.IsNullOrEmpty(bus))
                        {
                            if (p.route == route)
                            {
                                if (p.stopNumber == start)
                                {
                                    Console.WriteLine("Getting on bus" + p.vehicle);
                                    bus = p.vehicle;
                                }
                            }
                        }
                        else
                        {
                            if (p.route == route && p.vehicle == bus)
                            {
                                Console.WriteLine("At {2} bus{0} arrived at stop {1}", p.vehicle, p.stopNumber, p.timestamp);

                                if (p.stopNumber == end)
                                {
                                    Console.WriteLine("GET OFF THE BUS. At stop " + p.stopNumber);
                                    sample_count = 1;
                                    bus          = "99999999999999999";
                                }
                            }
                        }

                        /*
                         * Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}",
                         *  "Position", p.route, p.vehicle, p.trafficConditions, p.stopNumber, p.numStops, p.timeBetweenStops, p.fillInRatio, p.timestamp);
                         * Accident a = adata_seq.get_at(i);
                         * Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}",
                         *  "Accident", a.route, a.vehicle, string.Empty, a.stopNumber, string.Empty, string.Empty, string.Empty, a.timestamp);
                         */
                        //          PositionTypeSupport.print_data(pdata_seq.get_at(i));
                    }
                }

                try
                {
                    Position_reader.return_loan(pdata_seq, info_seq);
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("return loan error {0}", e);
                }
            }
            else
            {
                AccidentDataReader Accident_reader =
                    (AccidentDataReader)reader;

                try
                {
                    Accident_reader.take(
                        adata_seq,
                        info_seq,
                        DDS.ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
                        DDS.SampleStateKind.ANY_SAMPLE_STATE,
                        DDS.ViewStateKind.ANY_VIEW_STATE,
                        DDS.InstanceStateKind.ANY_INSTANCE_STATE);
                }
                catch (DDS.Retcode_NoData)
                {
                    return;
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("take error {0}", e);
                    return;
                }

                System.Int32 data_length = adata_seq.length;
                for (int i = 0; i < data_length; ++i)
                {
                    if (info_seq.get_at(i).valid_data)
                    {
                        AccidentTypeSupport.print_data(adata_seq.get_at(i));
                    }
                }

                try
                {
                    Accident_reader.return_loan(adata_seq, info_seq);
                }
                catch (DDS.Exception e)
                {
                    Console.WriteLine("return loan error {0}", e);
                }
            }
        }