Exemplo n.º 1
0
        private static void TestSubscriber(DomainParticipant participant)
        {
            Console.WriteLine("Starting applicattion as Subscriber...");
            Subscriber subscriber = participant.CreateSubscriber();

            if (subscriber == null)
            {
                throw new ApplicationException("Subscriber could not be created.");
            }

            Console.WriteLine("Creating Topic...");
            Topic topic = CreateTestTopic(participant);

            Console.WriteLine("Creating DataReader...");
            DataReaderQos qos = new DataReaderQos();

            qos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            DataReader dr = subscriber.CreateDataReader(topic, qos);

            if (dr == null)
            {
                Console.WriteLine("DataReader could not be created.");
                throw new ApplicationException("DataReader could not be created.");
            }
            Console.WriteLine("DataReader created...");
            TestStructDataReader dataReader = new TestStructDataReader(dr);

            Console.WriteLine("Waiting for sample with default values...");
            List <TestStruct> received   = new List <TestStruct>();
            List <SampleInfo> sampleInfo = new List <SampleInfo>();
            ReturnCode        ret        = dataReader.Take(received, sampleInfo);

            while (ret != ReturnCode.Ok)
            {
                Thread.Sleep(100);
                ret = dataReader.Take(received, sampleInfo);
            }

            if (received.Count > 0)
            {
                PrintReceivedSample(received[0]);
            }

            Console.WriteLine("Waiting for sample with custom values...");
            if (received.Count < 2)
            {
                received   = new List <TestStruct>();
                sampleInfo = new List <SampleInfo>();
                ret        = dataReader.Take(received, sampleInfo);
                while (ret != ReturnCode.Ok)
                {
                    Thread.Sleep(100);
                    ret = dataReader.Take(received, sampleInfo);
                }

                if (received.Count > 0)
                {
                    PrintReceivedSample(received[0]);
                }
            }
            else
            {
                PrintReceivedSample(received[1]);
            }
        }
Exemplo n.º 2
0
        public void TestGetDataReaders()
        {
            // Initialize entities
            TestStructTypeSupport support = new TestStructTypeSupport();
            string     typeName           = support.GetTypeName();
            ReturnCode result             = support.RegisterType(_participant, typeName);

            Assert.AreEqual(ReturnCode.Ok, result);

            Topic topic = _participant.CreateTopic(nameof(TestNotifyDataReaders), typeName);

            Assert.IsNotNull(topic);
            Assert.IsNull(topic.GetListener());
            Assert.AreEqual(nameof(TestNotifyDataReaders), topic.Name);
            Assert.AreEqual(typeName, topic.TypeName);

            Topic otherTopic = _participant.CreateTopic("Other" + nameof(TestNotifyDataReaders), typeName);

            Assert.IsNotNull(otherTopic);
            Assert.IsNull(otherTopic.GetListener());
            Assert.AreEqual("Other" + nameof(TestNotifyDataReaders), otherTopic.Name);
            Assert.AreEqual(typeName, otherTopic.TypeName);

            Publisher publisher = _participant.CreatePublisher();

            Assert.IsNotNull(publisher);

            DataWriter writer = publisher.CreateDataWriter(topic);

            Assert.IsNotNull(writer);
            TestStructDataWriter dataWriter = new TestStructDataWriter(writer);

            DataWriter otherWriter = publisher.CreateDataWriter(otherTopic);

            Assert.IsNotNull(otherWriter);
            TestStructDataWriter otherDataWriter = new TestStructDataWriter(otherWriter);

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataReader reader = subscriber.CreateDataReader(topic);

            Assert.IsNotNull(reader);
            TestStructDataReader dataReader = new TestStructDataReader(reader);

            DataReader otherReader = subscriber.CreateDataReader(otherTopic);

            Assert.IsNotNull(otherReader);
            TestStructDataReader otherDataReader = new TestStructDataReader(otherReader);

            // Check that the GetDataReaders without sending any sample
            List <DataReader> list = new List <DataReader>();

            result = subscriber.GetDataReaders(list);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(0, list.Count);

            // Publish in the topic and check again
            result = dataWriter.Write(new TestStruct
            {
                Id = 1
            });
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);

            result = subscriber.GetDataReaders(list);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(1, list.Count);

            // Publish in the otherTopic and check again
            result = otherDataWriter.Write(new TestStruct
            {
                Id = 1
            });
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);

            result = subscriber.GetDataReaders(list, SampleStateKind.NotReadSampleState | SampleStateKind.ReadSampleState, ViewStateMask.AnyViewState, InstanceStateMask.AnyInstanceState);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(2, list.Count);

            // Take from both DataReaders and check again
            List <TestStruct> received    = new List <TestStruct>();
            List <SampleInfo> sampleInfos = new List <SampleInfo>();

            result = dataReader.Take(received, sampleInfos);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = otherDataReader.Take(received, sampleInfos);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = subscriber.GetDataReaders(list);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(0, list.Count);

            // Test GetDataReaders with null parameter
            result = subscriber.GetDataReaders(null);
            Assert.AreEqual(ReturnCode.BadParameter, result);
        }
Exemplo n.º 3
0
        public void TestDispose()
        {
            // Initialize entities
            Duration duration = new Duration {
                Seconds = 5
            };

            DataWriterQos qos = new DataWriterQos();

            qos.WriterDataLifecycle.AutodisposeUnregisteredInstances = false;
            DataWriter writer = _publisher.CreateDataWriter(_topic, qos);

            Assert.IsNotNull(writer);
            TestStructDataWriter dataWriter = new TestStructDataWriter(writer);

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataReaderQos drQos = new DataReaderQos();

            qos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            MyDataReaderListener listener   = new MyDataReaderListener();
            DataReader           dataReader = subscriber.CreateDataReader(_topic, drQos, listener);

            Assert.IsNotNull(dataReader);
            int       count     = 0;
            Timestamp timestamp = default;

            listener.DataAvailable += (reader) =>
            {
                List <TestStruct>    samples = new List <TestStruct>();
                List <SampleInfo>    infos   = new List <SampleInfo>();
                TestStructDataReader dr      = new TestStructDataReader(reader);
                ReturnCode           ret     = dr.Take(samples, infos);
                if (ret == ReturnCode.Ok)
                {
                    foreach (var info in infos)
                    {
                        if (info.InstanceState == InstanceStateKind.NotAliveDisposedInstanceState)
                        {
                            count++;
                            if (count == 3)
                            {
                                timestamp = infos.First().SourceTimestamp;
                            }
                        }
                    }
                }
            };

            // Wait for discovery
            writer.WaitForSubscriptions(1, 1000);
            dataReader.WaitForPublications(1, 1000);

            // Dispose an instance that does not exist
            ReturnCode result = dataWriter.Dispose(new TestStruct {
                Id = 1
            }, InstanceHandle.HandleNil);

            Assert.AreEqual(ReturnCode.Error, result);

            // Call dispose with the simplest overload
            TestStruct instance1 = new TestStruct {
                Id = 1
            };

            result = dataWriter.Write(instance1);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.WaitForAcknowledgments(duration);
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);
            Assert.AreEqual(0, count);

            result = dataWriter.Dispose(instance1);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.WaitForAcknowledgments(duration);
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);
            Assert.AreEqual(1, count);

            // Call dispose with the handle parameter
            TestStruct instance2 = new TestStruct {
                Id = 2
            };
            InstanceHandle handle2 = dataWriter.RegisterInstance(instance2);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle2);

            result = dataWriter.Write(instance2, handle2);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.WaitForAcknowledgments(duration);
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);
            Assert.AreEqual(1, count);

            result = dataWriter.Dispose(instance2, handle2);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.WaitForAcknowledgments(duration);
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);
            Assert.AreEqual(2, count);

            // Call dispose with the handle parameter and specific timestamp
            Timestamp  now       = DateTime.Now.ToTimestamp();
            TestStruct instance3 = new TestStruct {
                Id = 3
            };
            InstanceHandle handle3 = dataWriter.RegisterInstance(instance3);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle3);

            result = dataWriter.Write(instance3, handle3);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.WaitForAcknowledgments(duration);
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);
            Assert.AreEqual(2, count);

            result = dataWriter.Dispose(instance3, handle3, now);
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.WaitForAcknowledgments(duration);
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(100);
            Assert.AreEqual(3, count);
            Assert.AreEqual(now.Seconds, timestamp.Seconds);
            Assert.AreEqual(now.NanoSeconds, timestamp.NanoSeconds);
        }