Exemplo n.º 1
0
        public void TestAttachCondition()
        {
            // Initialize
            WaitSet        waitSet        = new WaitSet();
            GuardCondition guardCondition = new GuardCondition();

            // Test with null parameter
            ReturnCode result = waitSet.AttachCondition(null);

            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Test with correct parameter
            result = waitSet.AttachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Attach again should return OK but not actually add another condition to the WaitSet
            result = waitSet.AttachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            List <Condition> conditions = new List <Condition>();

            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(1, conditions.Count);
            Assert.AreEqual(guardCondition, conditions[0]);
        }
Exemplo n.º 2
0
        public void TestDetachCondition()
        {
            // Initialize
            WaitSet        waitSet        = new WaitSet();
            GuardCondition guardCondition = new GuardCondition();
            ReturnCode     result         = waitSet.AttachCondition(guardCondition);

            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with null parameter
            result = waitSet.DetachCondition(null);
            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Test with correct parameter
            result = waitSet.DetachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Test again the same condition
            result = waitSet.DetachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);

            // Detach a not attached condition
            GuardCondition notAttachedCondition = new GuardCondition();

            result = waitSet.DetachCondition(notAttachedCondition);
            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);
        }
Exemplo n.º 3
0
        public void TestGetConditions()
        {
            // Initialize
            WaitSet        waitSet             = new WaitSet();
            GuardCondition guardCondition      = new GuardCondition();
            GuardCondition otherGuardCondition = new GuardCondition();
            ReturnCode     result = waitSet.AttachCondition(guardCondition);

            Assert.AreEqual(ReturnCode.Ok, result);
            result = waitSet.AttachCondition(otherGuardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with null parameter
            result = waitSet.GetConditions(null);
            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Test with correct parameter
            List <Condition> conditions = new List <Condition>()
            {
                null
            };

            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(2, conditions.Count);
            Assert.IsNotNull(conditions[0]);
            Assert.IsNotNull(conditions[1]);
            Assert.IsTrue(conditions[0] != conditions[1]);
            Assert.IsTrue(guardCondition == conditions[0] || guardCondition == conditions[1]);
            Assert.IsTrue(otherGuardCondition == conditions[0] || otherGuardCondition == conditions[1]);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            DDSEntityManager     mgr    = new DDSEntityManager("Listener");
            ReturnCode           status = ReturnCode.Error;
            ListenerDataListener myListener;
            String partitionName = "Listener Example";
            int    count         = 0;

            // create Domain Participant
            mgr.createParticipant(partitionName);

            // create Type
            MsgTypeSupport msgTS = new MsgTypeSupport();

            mgr.registerType(msgTS);

            // create Topic
            mgr.createTopic("ListenerData_Msg");

            // create Subscriber
            mgr.createSubscriber();

            // create DataReader
            mgr.createReader(false);

            IDataReader dreader = mgr.getReader();

            myListener       = new ListenerDataListener();
            myListener.MsgDR = dreader as MsgDataReader;

            Console.WriteLine("=== [ListenerDataSubscriber] SetListener");
            StatusKind kind = StatusKind.DataAvailable | StatusKind.RequestedDeadlineMissed;

            status = myListener.MsgDR.SetListener(myListener, kind);
            ErrorHandler.checkStatus(status, "DataReader.SetListener");

            Console.WriteLine("=== [ListenerDataSubscriber] Ready...");
            myListener.terminated = false;

            WaitSet ws = new WaitSet();

            ws.AttachCondition(myListener.guardCond);
            ICondition[] cond = null;

            while (!myListener.terminated && count < 1500)
            {
                Console.WriteLine("=== [SubscriberUsingListener] waiting waitset ...");
                ws.Wait(ref cond, Duration.Infinite);
                myListener.guardCond.SetTriggerValue(false);
                ++count;
            }

            Console.WriteLine("===[ListenerDataSubscriber] Market Closed.");

            mgr.getSubscriber().DeleteDataReader(myListener.MsgDR);
            mgr.deleteSubscriber();
            mgr.deleteTopic();
            mgr.deleteParticipant();
        }
        private void RunExample(int domainId, int sampleCount)
        {
            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId);

            // A Topic has a name and a datatype. Create a Topic named
            // "ChocolateTemperature" with type Temperature
            // In this example we use a DynamicType defined in XML, which creates
            // a DynamicData topic.
            var provider = new QosProvider("../chocolate_factory.xml");
            Topic <DynamicData> topic = participant.CreateTopic(
                "ChocolateTemperature",
                provider.GetType("Temperature"));

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // This DataReader reads data of type Temperature on Topic
            // "ChocolateTemperature". DataReader QoS is configured in
            // USER_QOS_PROFILES.xml
            DataReader <DynamicData> reader = subscriber.CreateDataReader(topic);

            // Obtain the DataReader's Status Condition
            StatusCondition statusCondition = reader.StatusCondition;

            // Enable the 'data available' status.
            statusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate an event handler with the status condition.
            // This will run when the condition is triggered, in the context of
            // the dispatch call (see below)
            int samplesRead = 0;

            statusCondition.Triggered += _ => samplesRead += ProcessData(reader);

            // Create a WaitSet and attach the StatusCondition
            var waitset = new WaitSet();

            waitset.AttachCondition(statusCondition);
            while (samplesRead < sampleCount && !shutdownRequested)
            {
                // Dispatch will call the handlers associated to the WaitSet
                // conditions when they activate
                Console.WriteLine("ChocolateTemperature subscriber sleeping for 4 sec...");
                waitset.Dispatch(Duration.FromSeconds(4));
            }
        }
Exemplo n.º 6
0
        public ShapeWaitSet(ShapeTypeDataReader dataReader, Action <ShapeTypeDataReader> dataAvailableFunc)
        {
            _dataAvailableFunc = dataAvailableFunc;
            _dataReader        = dataReader;

            _waitSet = new WaitSet();
            _thread  = new Thread(DoThreadActivity)
            {
                IsBackground = true
            };

            _cancelCondition = new GuardCondition();
            _statusCondition = dataReader.StatusCondition;
            _waitSet.AttachCondition(_statusCondition);
            _waitSet.AttachCondition(_cancelCondition);
            _statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus;
            _thread.Start();
        }
Exemplo n.º 7
0
        public RTIReader(
            DataReader <T> reader,
            ITypeHelper <T> dataType,
            Parameters arguments)
        {
            this.reader    = reader;
            dataTypeHelper = dataType;
            if (arguments.UseReadThread)
            {
                WaitSetProperty property = new WaitSetProperty(
                    (int)arguments.WaitsetEventCount,
                    Duration.FromMilliseconds(arguments.WaitsetDelayUsec / 1000));

                waitset = new WaitSet(property);
                StatusCondition readerStatus = reader.StatusCondition;
                readerStatus.EnabledStatuses = StatusMask.DataAvailable;
                waitset.AttachCondition(readerStatus);
            }
        }
Exemplo n.º 8
0
        public void TestWait()
        {
            // Initialize
            WaitSet        waitSet        = new WaitSet();
            GuardCondition guardCondition = new GuardCondition();
            ReturnCode     result         = waitSet.AttachCondition(guardCondition);

            Assert.AreEqual(ReturnCode.Ok, result);

            // Test with null conditions
            result = waitSet.Wait(null);
            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Attach again should return OK but not actually add another condition to the WaitSet
            result = waitSet.AttachCondition(guardCondition);
            Assert.AreEqual(ReturnCode.Ok, result);

            List <Condition> conditions = new List <Condition>();

            result = waitSet.GetConditions(conditions);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.IsNotNull(conditions);
            Assert.AreEqual(1, conditions.Count);
            Assert.AreEqual(guardCondition, conditions[0]);

            // Test thread wait infinite
            int    count  = 0;
            Thread thread = new Thread(() =>
            {
                result = waitSet.Wait(conditions);
                Assert.AreEqual(ReturnCode.Ok, result);
                Assert.IsNotNull(conditions);
                Assert.AreEqual(1, conditions.Count);
                Assert.AreEqual(guardCondition, conditions[0]);
                guardCondition.TriggerValue = false;
                count++;
            });

            thread.Start();

            guardCondition.TriggerValue = true;
            thread.Join();
            Assert.AreEqual(1, count);

            // Test timeout
            count  = 0;
            thread = new Thread(() =>
            {
                result = waitSet.Wait(conditions, new Duration {
                    Seconds = 0, NanoSeconds = 100000000
                });
                Assert.AreEqual(ReturnCode.Timeout, result);
                Assert.IsNotNull(conditions);
                Assert.AreEqual(0, conditions.Count);
                count++;
            });
            thread.Start();
            thread.Join();
            Assert.AreEqual(1, count);

            // Test exit before timeout
            count  = 0;
            thread = new Thread(() =>
            {
                result = waitSet.Wait(conditions, new Duration {
                    Seconds = 5
                });
                Assert.AreEqual(ReturnCode.Ok, result);
                Assert.IsNotNull(conditions);
                Assert.AreEqual(1, conditions.Count);
                Assert.AreEqual(guardCondition, conditions[0]);
                Assert.IsTrue(guardCondition.TriggerValue);
                guardCondition.TriggerValue = false;
                count++;
            });
            thread.Start();

            guardCondition.TriggerValue = true;
            thread.Join();
            Assert.AreEqual(1, count);
        }
Exemplo n.º 9
0
        public void WaitForReadySubscriptionCallback()
        {
            WaitSet waitSet = new WaitSet(node.Subscriptions);

            waitSet.Wait(0.1);
        }
Exemplo n.º 10
0
 public void Create()
 {
     WaitSet waitSet = new WaitSet();
 }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            bool useListener = true;

            OpenDDSharp.Ace.Init();

            ParticipantService       participantService = ParticipantService.Instance;
            DomainParticipantFactory domainFactory      = participantService.GetDomainParticipantFactory(args);
            DomainParticipantQos     qos = new DomainParticipantQos();

            qos.EntityFactory.AutoenableCreatedEntities = false;
            qos.UserData.Value = Encoding.UTF8.GetBytes("sometext");
            DomainParticipant participant = domainFactory.CreateParticipant(42, qos);

            if (participant == null)
            {
                throw new Exception("Could not create the participant");
            }

            DomainParticipantQos aux = new DomainParticipantQos();
            ReturnCode           ret = participant.GetQos(aux);

            aux.EntityFactory.AutoenableCreatedEntities = true;
            ret = participant.SetQos(aux);

            if (participant != null)
            {
                TestStructTypeSupport support = new TestStructTypeSupport();
                string     typeName           = support.GetTypeName();
                ReturnCode result             = support.RegisterType(participant, typeName);
                if (result != ReturnCode.Ok)
                {
                    throw new Exception("Could not register the type");
                }

                Topic     topic     = participant.CreateTopic("TopicName", typeName);
                Publisher publisher = participant.CreatePublisher();
                if (publisher == null)
                {
                    throw new Exception("Could not create the publisher");
                }

                DataWriter dw = publisher.CreateDataWriter(topic);
                if (dw == null)
                {
                    throw new Exception("Could not create the datawriter");
                }
                TestStructDataWriter dataWriter = new TestStructDataWriter(dw);
                Subscriber           subscriber = participant.CreateSubscriber();
                if (subscriber == null)
                {
                    throw new Exception("Could not create the subscribre");
                }

                MyDataListener listener = null;
                if (useListener)
                {
                    listener = new MyDataListener();
                }
                DataReader dataReader = subscriber.CreateDataReader(topic, listener, StatusKind.DataAvailableStatus);
                if (dataReader == null)
                {
                    throw new Exception("Could not create the datareader");
                }

                WaitSet         waitSet         = null;
                StatusCondition statusCondition = null;
                if (!useListener)
                {
                    waitSet         = new WaitSet();
                    statusCondition = dataReader.StatusCondition;
                    waitSet.AttachCondition(statusCondition);
                    statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus;

                    new System.Threading.Thread(delegate()
                    {
                        ICollection <Condition> conditions = new List <Condition>();
                        Duration duration = new Duration
                        {
                            Seconds = Duration.InfiniteSeconds
                        };
                        waitSet.Wait(conditions, duration);

                        foreach (Condition cond in conditions)
                        {
                            if (cond == statusCondition && cond.TriggerValue)
                            {
                                StatusCondition sCond = (StatusCondition)cond;
                                StatusMask mask       = sCond.EnabledStatuses;
                                if ((mask & StatusKind.DataAvailableStatus) != 0)
                                {
                                    DataAvailable(dataReader);
                                }
                            }
                        }
                    }).Start();
                }

                TestStruct test = new TestStruct
                {
                    RawData = "Hello, I love you, won't you tell me your name?"
                };

                test.LongSequence.Add(20);
                test.LongSequence.Add(10);
                test.LongSequence.Add(0);

                test.StringSequence.Add("Hello,");
                test.StringSequence.Add("I love you");
                test.StringSequence.Add("won't you tell me your name?");

                test.LongDoubleType = 1.1;

                test.LongDoubleSequence.Add(1.1);
                test.LongDoubleSequence.Add(2.2);
                test.LongDoubleSequence.Add(3.3);

                test.LongArray[0, 0] = 1;
                test.LongArray[0, 1] = 2;
                test.LongArray[0, 2] = 3;
                test.LongArray[0, 3] = 4;
                test.LongArray[1, 0] = 1;
                test.LongArray[1, 1] = 2;
                test.LongArray[1, 2] = 3;
                test.LongArray[1, 3] = 4;
                test.LongArray[2, 0] = 1;
                test.LongArray[2, 1] = 2;
                test.LongArray[2, 2] = 3;
                test.LongArray[2, 3] = 4;

                test.StringArray[0, 0] = "Hello,";
                test.StringArray[0, 1] = "I love you,";
                test.StringArray[1, 0] = "won't you tell me";
                test.StringArray[1, 1] = "your name?";

                test.StructArray[0, 0] = new BasicTestStruct()
                {
                    Id = 0
                };
                test.StructArray[0, 1] = new BasicTestStruct()
                {
                    Id = 1
                };
                test.StructArray[1, 0] = new BasicTestStruct()
                {
                    Id = 2
                };
                test.StructArray[1, 1] = new BasicTestStruct()
                {
                    Id = 3
                };

                test.LongDoubleArray[0, 0] = 1.1;
                test.LongDoubleArray[0, 1] = 2.2;
                test.LongDoubleArray[1, 0] = 3.3;
                test.LongDoubleArray[1, 1] = 4.4;

                test.StructSequence.Add(new BasicTestStruct()
                {
                    Id = 1
                });

                test.StructSequence.Add(new BasicTestStruct()
                {
                    Id = 2
                });

                test.StructSequence.Add(new BasicTestStruct()
                {
                    Id = 3
                });

                result = dataWriter.Write(test);

                System.Threading.Thread.Sleep(1000);

                if (!useListener)
                {
                    waitSet.DetachCondition(statusCondition);
                }

                participant.DeleteContainedEntities();
                domainFactory.DeleteParticipant(participant);
            }

            participantService.Shutdown();
            OpenDDSharp.Ace.Fini();

            Console.WriteLine("Press ENTER to finish the test.");
            Console.ReadLine();
        }
Exemplo n.º 12
0
        // Exercise #4.4: Add monitor_temperature function

        private void RunExample(
            int domainId       = 0,
            uint lotsToProcess = 10)
        {
            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId);

            // A Topic has a name and a datatype. Create a Topic named
            // "ChocolateLotState" with type ChocolateLotState.
            Topic <ChocolateLotState> lotStateTopic = participant.CreateTopic <ChocolateLotState>(
                "ChocolateLotState");
            // Exercise #4.1: Add a Topic for Temperature to this application

            // A Publisher allows an application to create one or more DataWriters
            // Publisher QoS is configured in USER_QOS_PROFILES.xml
            Publisher publisher = participant.CreatePublisher();

            // This DataWriter writes data on Topic "ChocolateLotState"
            // DataWriter QoS is configured in USER_QOS_PROFILES.xml
            DataWriter <ChocolateLotState> lotStateWriter =
                publisher.CreateDataWriter(lotStateTopic);

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState".
            // DataReader QoS is configured in USER_QOS_PROFILES.xml
            DataReader <ChocolateLotState> lotStateReader =
                subscriber.CreateDataReader(lotStateTopic);

            // Exercise #4.2: Add a DataReader for Temperature to this application

            // Obtain the DataReader's Status Condition
            StatusCondition lotStateStatusCondition = lotStateReader.StatusCondition;

            // Enable the 'data available' status.
            lotStateStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            int lotsProcessed = 0;

            lotStateStatusCondition.Triggered +=
                _ => lotsProcessed            += MonitorLotState(lotStateReader);

            // Create a WaitSet and attach the StatusCondition
            WaitSet waitset = new WaitSet();

            waitset.AttachCondition(lotStateStatusCondition);

            // Exercise #4.3: Add the new DataReader's StatusCondition to the Waitset

            // Start publishing in a separate thread
            var startLotTask = Task.Run(() => PublishStartLot(lotStateWriter, lotsToProcess));

            while (!shutdownRequested && lotsProcessed < lotsToProcess)
            {
                waitset.Dispatch(Duration.FromSeconds(4));
            }

            startLotTask.Wait();
        }
        private void RunExample(
            int domainId       = 0,
            uint lotsToProcess = 10)
        {
            // Loads the QoS from the qos_profiles.xml file.
            var qosProvider = new QosProvider("./qos_profiles.xml");

            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // Load DomainParticipant QoS profile
            var participantQos = qosProvider.GetDomainParticipantQos(
                "ChocolateFactoryLibrary::MonitoringControlApplication");
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId, participantQos);

            // A Topic has a name and a datatype. Create a Topic with type
            // ChocolateLotState.  Topic name is a constant defined in the IDL file.
            Topic <ChocolateLotState> lotStateTopic =
                participant.CreateTopic <ChocolateLotState>("ChocolateLotState");
            // Add a Topic for Temperature to this application
            Topic <Temperature> temperatureTopic =
                participant.CreateTopic <Temperature>("ChocolateTemperature");
            ContentFilteredTopic <Temperature> filteredTemperatureTopic =
                participant.CreateContentFilteredTopic(
                    name: "FilteredTemperature",
                    relatedTopic: temperatureTopic,
                    filter: new Filter(
                        expression: "degrees > %0 or degrees < %1",
                        parameters: new string[] { "32", "30" }));

            // A Publisher allows an application to create one or more DataWriters
            // Publisher QoS is configured in USER_QOS_PROFILES.xml
            Publisher publisher = participant.CreatePublisher();

            // This DataWriter writes data on Topic "ChocolateLotState"
            var writerQos = qosProvider.GetDataWriterQos(
                "ChocolateFactoryLibrary::ChocolateLotStateProfile");
            DataWriter <ChocolateLotState> lotStateWriter =
                publisher.CreateDataWriter(lotStateTopic, writerQos);

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState".
            // DataReader QoS is configured in USER_QOS_PROFILES.xml
            var readerQos = qosProvider.GetDataReaderQos(
                "ChocolateFactoryLibrary::ChocolateLotStateProfile");
            DataReader <ChocolateLotState> lotStateReader =
                subscriber.CreateDataReader(lotStateTopic, readerQos);

            // Add a DataReader for Temperature to this application
            readerQos = qosProvider.GetDataReaderQos(
                "ChocolateFactoryLibrary::ChocolateTemperatureProfile");
            DataReader <Temperature> temperatureReader =
                subscriber.CreateDataReader(filteredTemperatureTopic, readerQos);

            // Obtain the DataReader's Status Condition
            StatusCondition temperatureStatusCondition = temperatureReader.StatusCondition;

            temperatureStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate a handler with the status condition. This will run when the
            // condition is triggered, in the context of the dispatch call (see below)
            temperatureStatusCondition.Triggered +=
                _ => MonitorTemperature(temperatureReader);

            // Do the same with the lotStateReader's StatusCondition
            StatusCondition lotStateStatusCondition = lotStateReader.StatusCondition;

            lotStateStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            int lotsProcessed = 0;

            lotStateStatusCondition.Triggered +=
                _ => lotsProcessed            += MonitorLotState(lotStateReader);

            // Create a WaitSet and attach the StatusCondition
            var waitset = new WaitSet();

            waitset.AttachCondition(lotStateStatusCondition);

            // Add the new DataReader's StatusCondition to the Waitset
            waitset.AttachCondition(temperatureStatusCondition);

            // Start publishing in a separate thread
            var startLotTask = Task.Run(() => PublishStartLot(lotStateWriter, lotsToProcess));

            while (!shutdownRequested && lotsProcessed < lotsToProcess)
            {
                waitset.Dispatch(Duration.FromSeconds(4));
            }

            startLotTask.Wait();
        }
        private void RunExample(
            int domainId       = 0,
            uint lotsToProcess = 10)
        {
            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId);

            // A Topic has a name and a datatype. Create a Topic named
            // "ChocolateLotState" with type ChocolateLotState
            // In this example we use a DynamicType defined in XML, which creates
            // a DynamicData topic.
            Topic <ChocolateLotState> lotStateTopic = participant.CreateTopic(
                "ChocolateLotState",
                types.ChocolateLotState);
            // Exercise #4.1: Add a Topic for Temperature to this application
            Topic <Temperature> temperatureTopic = participant.CreateTopic(
                "ChocolateTemperature",
                types.Temperature);

            // A Publisher allows an application to create one or more DataWriters
            // Publisher QoS is configured in USER_QOS_PROFILES.xml
            Publisher publisher = participant.CreatePublisher();

            // This DataWriter writes data on Topic "ChocolateLotState"
            // DataWriter QoS is configured in USER_QOS_PROFILES.xml
            DataWriter <ChocolateLotState> lotStateWriter =
                publisher.CreateDataWriter(lotStateTopic);

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState".
            // DataReader QoS is configured in USER_QOS_PROFILES.xml
            DataReader <ChocolateLotState> lotStateReader =
                subscriber.CreateDataReader(lotStateTopic);

            // Exercise #4.2: Add a DataReader for Temperature to this application
            DataReader <Temperature> temperatureReader =
                subscriber.CreateDataReader(temperatureTopic);

            // Obtain the DataReader's Status Condition
            StatusCondition temperatureStatusCondition = temperatureReader.StatusCondition;

            temperatureStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate a handler with the status condition. This will run when the
            // condition is triggered, in the context of the dispatch call (see below)
            temperatureStatusCondition.Triggered += _ => MonitorTemperature(temperatureReader);

            // Do the same with the lotStateReader's StatusCondition
            StatusCondition lotStateStatusCondition = lotStateReader.StatusCondition;

            lotStateStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            int lotsProcessed = 0;

            lotStateStatusCondition.Triggered +=
                _ => lotsProcessed            += MonitorLotState(lotStateReader);

            // Create a WaitSet and attach the StatusCondition
            WaitSet waitset = new WaitSet();

            waitset.AttachCondition(lotStateStatusCondition);

            // Exercise #4.3: Add the new DataReader's StatusCondition to the Waitset
            waitset.AttachCondition(temperatureStatusCondition);

            var startLotTask = Task.Run(() => PublishStartLot(lotStateWriter, lotsToProcess));

            while (!shutdownRequested && lotsProcessed < lotsToProcess)
            {
                waitset.Dispatch(Duration.FromSeconds(4));
            }

            startLotTask.Wait();
        }
        private void RunExample(int domainId, string sensorId)
        {
            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // Uses TemperingApplication QoS profile to set participant name.
            var qosProvider    = new QosProvider("./qos_profiles.xml");
            var participantQos = qosProvider.GetDomainParticipantQos(
                "ChocolateFactoryLibrary::TemperingApplication");
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId, participantQos);

            // Create the topics
            Topic <Temperature> temperatureTopic =
                participant.CreateTopic <Temperature>("ChocolateTemperature");
            Topic <ChocolateLotState> lotStateTopic =
                participant.CreateTopic <ChocolateLotState>("ChocolateLotState");

            // Exercise #1.1: Create a Content-Filtered Topic that filters out
            // chocolate lot state unless the next_station = TEMPERING_CONTROLLER

            // A Publisher allows an application to create one or more DataWriters
            // Create Publisher with default QoS.
            Publisher publisher = participant.CreatePublisher();

            // Create DataWriter of Topic "ChocolateTemperature"
            // using ChocolateTemperatureProfile QoS profile for Streaming Data
            DataWriter <Temperature> temperatureWriter = publisher.CreateDataWriter(
                temperatureTopic,
                qos: qosProvider.GetDataWriterQos("ChocolateFactoryLibrary::ChocolateTemperatureProfile"));

            // Create DataWriter of Topic "ChocolateLotState"
            // using ChocolateLotStateProfile QoS profile for State Data
            DataWriter <ChocolateLotState> lotStateWriter = publisher.CreateDataWriter(
                lotStateTopic,
                qos: qosProvider.GetDataWriterQos("ChocolateFactoryLibrary::ChocolateLotStateProfile"));

            // A Subscriber allows an application to create one or more DataReaders
            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState".
            // using ChocolateLotStateProfile QoS profile for State Data
            // Exercise #1.2: Change the DataReader's Topic to use a
            // Content-Filtered Topic
            DataReader <ChocolateLotState> lotStateReader = subscriber.CreateDataReader(
                lotStateTopic,
                qos: qosProvider.GetDataReaderQos("ChocolateFactoryLibrary::ChocolateLotStateProfile"),
                preEnableAction: reader => reader.RequestedIncompatibleQos += OnRequestedIncompatibleQos);

            // Obtain the DataReader's Status Condition
            StatusCondition statusCondition = lotStateReader.StatusCondition;

            // Enable the 'data available' status.
            statusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate an event handler with the status condition.
            // This will run when the condition is triggered, in the context of
            // the dispatch call (see below)
            statusCondition.Triggered +=
                _ => ProcessLot(lotStateReader, lotStateWriter);

            // Create a WaitSet and attach the StatusCondition
            var waitset = new WaitSet();

            waitset.AttachCondition(statusCondition);

            // Create a thread to periodically publish the temperature
            Console.WriteLine($"ChocolateTemperature Sensor with ID: {sensorId} starting");
            var temperatureTask = Task.Run(
                () => PublishTemperature(temperatureWriter, sensorId));

            while (!shutdownRequested)
            {
                // Wait for ChocolateLotState
                Console.WriteLine("Waiting for lot");
                waitset.Dispatch(Duration.FromSeconds(10));
            }

            temperatureTask.Wait();
        }
        private void RunExample(
            int domainId       = 0,
            uint lotsToProcess = 10)
        {
            // Exercise #1.1: Add QoS provider

            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // Exercise #1.2: Load DomainParticipant QoS profile
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId);

            // A Topic has a name and a datatype. Create a Topic named
            // "ChocolateLotState" with type ChocolateLotState.
            Topic <ChocolateLotState> lotStateTopic = participant.CreateTopic <ChocolateLotState>(
                CHOCOLATE_LOT_STATE_TOPIC.Value);
            // Add a Topic for Temperature to this application
            Topic <Temperature> temperatureTopic = participant.CreateTopic <Temperature>(
                CHOCOLATE_TEMPERATURE_TOPIC.Value);

            // A Publisher allows an application to create one or more DataWriters
            // Publisher QoS is configured in USER_QOS_PROFILES.xml
            Publisher publisher = participant.CreatePublisher();

            // This DataWriter writes data on Topic "ChocolateLotState"
            // Exercise #4.1: Load ChocolateLotState DataWriter QoS profile after
            // debugging incompatible QoS
            DataWriter <ChocolateLotState> lotStateWriter =
                publisher.CreateDataWriter(lotStateTopic);

            // A Subscriber allows an application to create one or more DataReaders
            // Subscriber QoS is configured in USER_QOS_PROFILES.xml
            Subscriber subscriber = participant.CreateSubscriber();

            // Create DataReader of Topic "ChocolateLotState".
            // Exercise #1.3: Update the lotStateReader and temperatureReader
            // to use correct QoS
            DataReader <ChocolateLotState> lotStateReader =
                subscriber.CreateDataReader(lotStateTopic);

            // Add a DataReader for Temperature to this application
            DataReader <Temperature> temperatureReader =
                subscriber.CreateDataReader(temperatureTopic);

            // Obtain the DataReader's Status Condition
            StatusCondition temperatureStatusCondition = temperatureReader.StatusCondition;

            temperatureStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate a handler with the status condition. This will run when the
            // condition is triggered, in the context of the dispatch call (see below)
            temperatureStatusCondition.Triggered += _ => MonitorTemperature(temperatureReader);

            // Do the same with the lotStateReader's StatusCondition
            StatusCondition lotStateStatusCondition = lotStateReader.StatusCondition;

            lotStateStatusCondition.EnabledStatuses = StatusMask.DataAvailable;

            int lotsProcessed = 0;

            lotStateStatusCondition.Triggered +=
                _ => lotsProcessed            += MonitorLotState(lotStateReader);

            // Create a WaitSet and attach the StatusCondition
            var waitset = new WaitSet();

            waitset.AttachCondition(lotStateStatusCondition);

            // Add the new DataReader's StatusCondition to the Waitset
            waitset.AttachCondition(temperatureStatusCondition);

            // Start publishing in a separate thread
            var startLotTask = Task.Run(() => PublishStartLot(lotStateWriter, lotsToProcess));

            while (!shutdownRequested && lotsProcessed < lotsToProcess)
            {
                waitset.Dispatch(Duration.FromSeconds(4));
            }

            startLotTask.Wait();
        }
Exemplo n.º 17
0
 public void Create()
 {
     WaitSet waitSet = new WaitSet(context, node.Subscriptions);
 }
        private void RunExample(int domainId, string sensorId)
        {
            // A DomainParticipant allows an application to begin communicating in
            // a DDS domain. Typically there is one DomainParticipant per application.
            // Uses TemperingApplication QoS profile to set participant name.
            var qosProvider    = new QosProvider("./qos_profiles.xml");
            var participantQos = qosProvider.GetDomainParticipantQos(
                "ChocolateFactoryLibrary::TemperingApplication");
            DomainParticipant participant = DomainParticipantFactory.Instance
                                            .CreateParticipant(domainId, participantQos);

            // A Topic has a name and a datatype.
            Topic <Temperature> temperatureTopic = participant.CreateTopic <Temperature>(
                CHOCOLATE_TEMPERATURE_TOPIC.Value);
            Topic <ChocolateLotState> lotStateTopic = participant.CreateTopic <ChocolateLotState>(
                CHOCOLATE_LOT_STATE_TOPIC.Value);

            // A Publisher allows an application to create one or more DataWriters
            // Create Publisher with default QoS.
            Publisher publisher = participant.CreatePublisher();

            // Create DataWriter of Topic "ChocolateTemperature"
            // using ChocolateTemperatureProfile QoS profile for Streaming Data
            DataWriter <Temperature> temperatureWriter = publisher.CreateDataWriter(
                temperatureTopic,
                qos: qosProvider.GetDataWriterQos("ChocolateFactoryLibrary::ChocolateTemperatureProfile"));

            // Create DataWriter of Topic "ChocolateLotState"
            // using ChocolateLotStateProfile QoS profile for State Data
            DataWriter <ChocolateLotState> lotStateWriter = publisher.CreateDataWriter(
                lotStateTopic,
                qos: qosProvider.GetDataWriterQos("ChocolateFactoryLibrary::ChocolateLotStateProfile"));

            // A Subscriber allows an application to create one or more DataReaders
            Subscriber subscriber = participant.CreateSubscriber();

            // This DataReader reads data of type Temperature on Topic
            // "ChocolateTemperature" using ChocolateLotStateProfile QoS
            // profile for State Data.
            //
            // We will handle the "requested incompatible qos" event. By doing
            // it as a preEnableAction, we avoid a race condition in which
            // the event could trigger right after the reader creation but
            // right before adding the event handler.
            DataReader <ChocolateLotState> lotStateReader = subscriber.CreateDataReader(
                lotStateTopic,
                qos: qosProvider.GetDataReaderQos("ChocolateFactoryLibrary::ChocolateLotStateProfile"),
                preEnableAction: reader => reader.RequestedIncompatibleQos += OnRequestedIncompatibleQos);

            // Obtain the DataReader's Status Condition
            StatusCondition statusCondition = lotStateReader.StatusCondition;

            // Enable the 'data available' status.
            statusCondition.EnabledStatuses = StatusMask.DataAvailable;

            // Associate an event handler with the status condition.
            // This will run when the condition is triggered, in the context of
            // the dispatch call (see below)
            statusCondition.Triggered +=
                _ => ProcessLot(lotStateReader, lotStateWriter);

            // Create a WaitSet and attach the StatusCondition
            var waitset = new WaitSet();

            waitset.AttachCondition(statusCondition);

            // Create a thread to periodically publish the temperature
            Console.WriteLine($"ChocolateTemperature Sensor with ID: {sensorId} starting");
            var temperatureTask = Task.Run(
                () => PublishTemperature(temperatureWriter, sensorId));

            while (!shutdownRequested)
            {
                // Wait for ChocolateLotState
                Console.WriteLine("Waiting for lot");
                waitset.Dispatch(Duration.FromSeconds(4));
            }

            temperatureTask.Wait();
        }