예제 #1
0
        public void TestOnDataOnReaders()
        {
            Subscriber subscriber = null;

            // Attach to the event
            int count = 0;

            _listener.DataOnReaders += (s) =>
            {
                subscriber = s;
                count++;
            };

            // Enable entities
            ReturnCode result = _writer.Enable();

            Assert.AreEqual(ReturnCode.Ok, result);

            result = _reader.Enable();
            Assert.AreEqual(ReturnCode.Ok, result);

            // Wait for discovery
            bool found = _writer.WaitForSubscriptions(1, 1000);

            Assert.IsTrue(found);

            // Write some instances
            int total = 5;

            for (int i = 1; i <= total; i++)
            {
                result = _dataWriter.Write(new TestStruct
                {
                    Id = i,
                });
                Assert.AreEqual(ReturnCode.Ok, result);

                result = _dataWriter.WaitForAcknowledgments(new Duration {
                    Seconds = 5
                });
                Assert.AreEqual(ReturnCode.Ok, result);
            }

            System.Threading.Thread.Sleep(100);

            Assert.AreEqual(total, count);
            Assert.IsNotNull(subscriber);
            Assert.AreEqual(_subscriber, subscriber);
        }
예제 #2
0
        public void TestWaitForAcknowledgments()
        {
            // Initialize entities
            DataWriter writer = _publisher.CreateDataWriter(_topic);

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

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataReaderQos drQos = new DataReaderQos();

            drQos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            DataReader reader = subscriber.CreateDataReader(_topic);

            Assert.IsNotNull(reader);

            // Write some instances and wait for acknowledgments
            for (int i = 0; i < 10; i++)
            {
                ReturnCode result = dataWriter.Write(new TestStruct
                {
                    Id = i,
                });
                Assert.AreEqual(ReturnCode.Ok, result);

                result = dataWriter.WaitForAcknowledgments(new Duration {
                    Seconds = 5
                });
                Assert.AreEqual(ReturnCode.Ok, result);
            }
        }
예제 #3
0
        public void TestOnDataAvailable()
        {
            // Attach to the event
            int count = 0;

            _listener.DataAvailable += (r) =>
            {
                Assert.AreEqual(_reader, r);
                count++;
            };

            // Enable entities
            ReturnCode result = _writer.Enable();

            Assert.AreEqual(ReturnCode.Ok, result);

            result = _reader.Enable();
            Assert.AreEqual(ReturnCode.Ok, result);

            // Wait for discovery
            bool found = _writer.WaitForSubscriptions(1, 1000);

            Assert.IsTrue(found);

            // Write some instances
            for (int i = 1; i <= 5; i++)
            {
                result = _dataWriter.Write(new TestStruct
                {
                    Id = i
                });
                Assert.AreEqual(ReturnCode.Ok, result);

                result = _dataWriter.WaitForAcknowledgments(new Duration {
                    Seconds = 5
                });
                Assert.AreEqual(ReturnCode.Ok, result);
            }

            Thread.Sleep(100);

            Assert.AreEqual(5, count);
        }
예제 #4
0
        public void TestNotifyDataReaders()
        {
            // 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.Listener);
            Assert.AreEqual(nameof(TestNotifyDataReaders), topic.Name);
            Assert.AreEqual(typeName, topic.TypeName);

            Publisher publisher = _participant.CreatePublisher();

            Assert.IsNotNull(publisher);

            DataWriter writer = publisher.CreateDataWriter(topic);

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

            int subscriberReceived = 0;
            int readerReceived     = 0;

            // Create the Subscriber and the DataReader with the corresponding listeners
            MySubscriberListener subListener = new MySubscriberListener();

            subListener.DataOnReaders += (sub) =>
            {
                subscriberReceived++;
                if (subscriberReceived % 2 == 0)
                {
                    sub.NotifyDataReaders();
                }
            };
            Subscriber subscriber = _participant.CreateSubscriber(null, subListener);

            Assert.IsNotNull(subscriber);

            MyDataReaderListener readListener = new MyDataReaderListener();

            readListener.DataAvailable += (read) =>
            {
                readerReceived++;
            };
            DataReader reader = subscriber.CreateDataReader(topic, null, readListener);

            Assert.IsNotNull(reader);

            Assert.IsTrue(writer.WaitForSubscriptions(1, 5000));
            Assert.IsTrue(reader.WaitForPublications(1, 5000));

            // Publish instances
            for (int i = 0; i < 10; i++)
            {
                dataWriter.Write(new TestStruct
                {
                    Id         = i,
                    ShortField = (short)i,
                });

                var ret = dataWriter.WaitForAcknowledgments(new Duration
                {
                    Seconds = 5,
                });
                Assert.AreEqual(ReturnCode.Ok, ret);
            }

            System.Threading.Thread.Sleep(1000);

            // Check the received instances
            Assert.AreEqual(10, subscriberReceived);
            Assert.AreEqual(5, readerReceived);
        }
예제 #5
0
        private static void TestPublisher(DomainParticipant participant)
        {
            Console.WriteLine("Starting applicattion as Publisher...");
            Publisher publisher = participant.CreatePublisher();

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

            Topic topic = CreateTestTopic(participant);

            DataWriterQos qos = new DataWriterQos();

            qos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            DataWriter dw = publisher.CreateDataWriter(topic, qos);

            if (dw == null)
            {
                throw new ApplicationException("DataWriter could not be created.");
            }
            TestStructDataWriter dataWriter = new TestStructDataWriter(dw);

            Console.WriteLine("Waiting for the subscriber...");
            bool wait = WaitForSubscriptions(dw, 1, 60000);

            if (wait)
            {
                Console.WriteLine("Subscription found. Sending test data with default values...");
                TestStruct data = new TestStruct();
                dataWriter.Write(data);
                ReturnCode ret = dataWriter.WaitForAcknowledgments(new Duration
                {
                    Seconds     = 60,
                    NanoSeconds = 0,
                });

                if (ret == ReturnCode.Ok)
                {
                    Console.WriteLine("Data sent and acknowledged.");
                }
                else
                {
                    Console.WriteLine("No acknowledge received: " + ret.ToString());
                    return;
                }

                Console.WriteLine("Subscription found. Sending test data with custom values...");

                data = new TestStruct
                {
                    ShortField            = -1,
                    LongField             = -2,
                    LongLongField         = -3,
                    UnsignedShortField    = 1,
                    UnsignedLongField     = 2,
                    UnsignedLongLongField = 3,
                    BooleanField          = true,
                    CharField             = 'C',
                    WCharField            = 'W',
                    FloatField            = 42.42f,
                    DoubleField           = 0.42,
                    //LongDoubleField = 0.4242m,
                    OctetField                      = 0x42,
                    UnboundedStringField            = "Unbounded string field.",
                    UnboundedWStringField           = "Unbounded WString field.",
                    BoundedStringField              = "Bounded string field.",
                    BoundedWStringField             = "Bounded WString field.",
                    BoundedBooleanSequenceField     = { true, true, false },
                    UnboundedBooleanSequenceField   = { true, true, false, true, true, false },
                    BoundedCharSequenceField        = { '1', '2', '3', '4', '5' },
                    UnboundedCharSequenceField      = { '1', '2', '3', '4', '5', '6' },
                    BoundedWCharSequenceField       = { '1', '2', '3', '4', '5' },
                    UnboundedWCharSequenceField     = { '1', '2', '3', '4', '5', '6' },
                    BoundedOctetSequenceField       = { 0x42, 0x69 },
                    UnboundedOctetSequenceField     = { 0x42, 0x69, 0x42, 0x69, 0x42, 0x69 },
                    BoundedShortSequenceField       = { 1, 2, 3, 4, 5 },
                    UnboundedShortSequenceField     = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 },
                    BoundedUShortSequenceField      = { 1, 2, 3, 4, 5 },
                    UnboundedUShortSequenceField    = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 },
                    BoundedLongSequenceField        = { 1, 2, 3, 4, 5 },
                    UnboundedLongSequenceField      = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 },
                    BoundedULongSequenceField       = { 1, 2, 3, 4, 5 },
                    UnboundedULongSequenceField     = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 },
                    BoundedLongLongSequenceField    = { 1, 2, 3, 4, 5 },
                    UnboundedLongLongSequenceField  = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 },
                    BoundedULongLongSequenceField   = { 1, 2, 3, 4, 5 },
                    UnboundedULongLongSequenceField = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 },
                    BoundedFloatSequenceField       = { 0.42f, 42.42f, 1f, 2f, 3f },
                    UnboundedFloatSequenceField     = { 0.42f, 42.42f, 1f, 2f, 3f, 0.42f, 42.42f, 1f, 2f, 3f },
                    BoundedDoubleSequenceField      = { 0.42, 42.42, 1, 2, 3 },
                    UnboundedDoubleSequenceField    = { 0.42, 42.42, 1, 2, 3, 0.42, 42.42, 1, 2, 3 },
                    BoundedStringSequenceField      = { "This", "is", "the", "end." },
                    BoundedWStringSequenceField     = { "This", "is", "the", "end." },
                    UnboundedStringSequenceField    = { "This", "is", "the", "end.", "This", "is", "the", "end." },
                    UnboundedWStringSequenceField   = { "This", "is", "the", "end.", "This", "is", "the", "end." },
                    NestedStructField               = { Id = 1, Message = "This is the end." },
                    BoundedStructSequenceField      = { new NestedStruct {
                                                            Id = 1, Message = "This is the end."
                                                        }, new NestedStruct   {
                                                            Id = 2, Message = "my only friend, the end."
                                                        } },
                    UnboundedStructSequenceField = { new NestedStruct {
                                                         Id = 1, Message = "This is the end."
                                                     }, new NestedStruct{
                                                         Id = 2, Message = "my only friend, the end."
                                                     } },
                    TestEnumField              = TestEnum.ENUM12,
                    BoundedEnumSequenceField   = { TestEnum.ENUM1, TestEnum.ENUM2, TestEnum.ENUM3, TestEnum.ENUM4, TestEnum.ENUM5 },
                    UnboundedEnumSequenceField = { TestEnum.ENUM1, TestEnum.ENUM2, TestEnum.ENUM3, TestEnum.ENUM4, TestEnum.ENUM5, TestEnum.ENUM6, TestEnum.ENUM7, TestEnum.ENUM8, TestEnum.ENUM9, TestEnum.ENUM10, TestEnum.ENUM11, TestEnum.ENUM12 },
                    ShortArrayField            = new short[] { 1, -2, 3, -4, 5 },
                    UnsignedShortArrayField    = new ushort[] { 1, 2, 3, 4, 5 },
                    LongArrayField             = new int[] { 1, -2, 3, -4, 5 },
                    UnsignedLongArrayField     = new uint[] { 1, 2, 3, 4, 5 },
                    LongLongArrayField         = new long[] { 1, -2, 3, -4, 5 },
                    UnsignedLongLongArrayField = new ulong[] { 1, 2, 3, 4, 5 },
                    CharArrayField             = new char[] { 'A', 'B', 'C', 'D', 'E' },
                    WCharArrayField            = new char[] { 'A', 'B', 'C', 'D', 'E' },
                    BooleanArrayField          = new bool[] { true, true, false, true, true },
                    OctetArrayField            = new byte[] { 0x42, 0x42, 0x69, 0x42, 0x42 },
                    FloatArrayField            = new float[] { 0.42f, 0.4242f, 1f, 2f, 3f },
                    DoubleArrayField           = new double[] { 0.42, 0.4242, 1, 2, 3 },
                    StringArrayField           = new string[] { "This", "is", "the", "end", "my only friend, the end." },
                    WStringArrayField          = new string[] { "This", "is", "the", "end", "my only friend, the end." },
                    EnumArrayField             = new TestEnum[] { TestEnum.ENUM1, TestEnum.ENUM2, TestEnum.ENUM3, TestEnum.ENUM4, TestEnum.ENUM5 },
                    StructArrayField           = new NestedStruct[]
                    {
                        new NestedStruct {
                            Id = 1, Message = "This is the end."
                        },
                        new NestedStruct {
                            Id = 2, Message = "This is the end."
                        },
                        new NestedStruct {
                            Id = 3, Message = "This is the end."
                        },
                        new NestedStruct {
                            Id = 4, Message = "This is the end."
                        },
                        new NestedStruct {
                            Id = 5, Message = "This is the end."
                        },
                    },
                    ShortMultiArrayField = new short[, , ]
                    {
                        {
                            { -01, -02 },
                            { -03, -04 },
                            { -05, -06 },
                            { -07, -08 },
                        },
                        {
                            { -09, -10 },
                            { -11, -12 },
                            { -13, -14 },
                            { -15, -16 },
                        },
                        {
                            { -17, -18 },
                            { -19, -20 },
                            { -21, -22 },
                            { -23, -24 },
                        }
                    },
                    UnsignedShortMultiArrayField = new ushort[, , ]
                    {
                        {
                            { 01, 02 },
                            { 03, 04 },
                            { 05, 06 },
                            { 07, 08 },
                        },
                        {
                            { 09, 10 },
                            { 11, 12 },
                            { 13, 14 },
                            { 15, 16 },
                        },
                        {
                            { 17, 18 },
                            { 19, 20 },
                            { 21, 22 },
                            { 23, 24 },
                        }
                    },
                    LongMultiArrayField = new[, , ]
                    {
                        {
                            { -01, 02 },
                            { -03, 04 },
                            { -05, 06 },
                            { -07, 08 },
                        },
                        {
                            { -09, 10 },
                            { -11, 12 },
                            { -13, 14 },
                            { -15, 16 },
                        },
                        {
                            { -17, 18 },
                            { -19, 20 },
                            { -21, 22 },
                            { -23, 24 },
                        }
                    },
                    UnsignedLongMultiArrayField = new[, , ]
                    {
                        {
                            { 25U, 26U },
                            { 27U, 28U },
                            { 29U, 30U },
                            { 31U, 32U },
                        },
                        {
                            { 33U, 34U },
                            { 35U, 36U },
                            { 37U, 38U },
                            { 39U, 40U },
                        },
                        {
                            { 41U, 42U },
                            { 43U, 44U },
                            { 45U, 46U },
                            { 47U, 48U },
                        }
                    },
                    LongLongMultiArrayField = new[, , ]
                    {
                        {
                            { -25L, -26L },
                            { -27L, -28L },
                            { -29L, -30L },
                            { -31L, -32L },
                        },
                        {
                            { -33L, -34L },
                            { -35L, -36L },
                            { -37L, -38L },
                            { -39L, -40L },
                        },
                        {
                            { -41L, -42L },
                            { -43L, -44L },
                            { -45L, -46L },
                            { -47L, -48L },
                        }
                    },
                    UnsignedLongLongMultiArrayField = new[, , ]
                    {
                        {
                            { 49UL, 50UL },
                            { 51UL, 52UL },
                            { 53UL, 54UL },
                            { 55UL, 56UL },
                        },
                        {
                            { 57UL, 58UL },
                            { 59UL, 60UL },
                            { 61UL, 62UL },
                            { 63UL, 64UL },
                        },
                        {
                            { 65UL, 66UL },
                            { 67UL, 68UL },
                            { 69UL, 70UL },
                            { 71UL, 72UL },
                        }
                    },
                    FloatMultiArrayField = new[, , ]
                    {
                        {
                            { 01.01f, 02.02f },
                            { 03.03f, 04.04f },
                            { 05.05f, 06.06f },
                            { 07.07f, 08.08f }
                        },
                        {
                            { 09.09f, 10.10f },
                            { 11.11f, 12.12f },
                            { 13.13f, 14.14f },
                            { 15.15f, 16.16f },
                        },
                        {
                            { 17.17f, 18.18f },
                            { 19.19f, 20.20f },
                            { 21.21f, 22.22f },
                            { 23.23f, 24.24f },
                        }
                    },
                    DoubleMultiArrayField = new[, , ]
                    {
                        {
                            { 01.01, 02.02 },
                            { 03.03, 04.04 },
                            { 05.05, 06.06 },
                            { 07.07, 08.08 },
                        },
                        {
                            { 09.09, 10.10 },
                            { 11.11, 12.12 },
                            { 13.13, 14.14 },
                            { 15.15, 16.16 },
                        },
                        {
                            { 17.17, 18.18 },
                            { 19.19, 20.20 },
                            { 21.21, 22.22 },
                            { 23.23, 24.24 },
                        }
                    },
                    BooleanMultiArrayField = new[, , ]
                    {
                        {
                            { true, false },
                            { true, false },
                            { true, false },
                            { true, false },
                        },
                        {
                            { true, false },
                            { true, false },
                            { true, false },
                            { true, false },
                        },
                        {
                            { true, false },
                            { true, false },
                            { true, false },
                            { true, false },
                        }
                    },
                    OctetMultiArrayField = new byte[, , ]
                    {
                        {
                            { 01, 02 },
                            { 03, 04 },
                            { 05, 06 },
                            { 07, 08 },
                        },
                        {
                            { 09, 10 },
                            { 11, 12 },
                            { 13, 14 },
                            { 15, 16 },
                        },
                        {
                            { 17, 18 },
                            { 19, 20 },
                            { 21, 22 },
                            { 23, 24 },
                        }
                    },
                    EnumMultiArrayField = new TestEnum[, , ]
                    {
                        {
                            { TestEnum.ENUM1, TestEnum.ENUM2 },
                            { TestEnum.ENUM3, TestEnum.ENUM4 },
                            { TestEnum.ENUM5, TestEnum.ENUM6 },
                            { TestEnum.ENUM7, TestEnum.ENUM8 },
                        },
                        {
                            { TestEnum.ENUM9, TestEnum.ENUM10 },
                            { TestEnum.ENUM11, TestEnum.ENUM12 },
                            { TestEnum.ENUM1, TestEnum.ENUM2 },
                            { TestEnum.ENUM3, TestEnum.ENUM4 },
                        },
                        {
                            { TestEnum.ENUM5, TestEnum.ENUM6 },
                            { TestEnum.ENUM7, TestEnum.ENUM8 },
                            { TestEnum.ENUM9, TestEnum.ENUM10 },
                            { TestEnum.ENUM11, TestEnum.ENUM12 },
                        },
                    },
                    StructMultiArrayField = new NestedStruct[, , ]
                    {
                        {
                            { new NestedStruct {
                                  Id = 1, Message = "01"
                              }, new NestedStruct {
                                  Id = 2, Message = "02"
                              } },
                            { new NestedStruct {
                                  Id = 3, Message = "03"
                              }, new NestedStruct {
                                  Id = 4, Message = "04"
                              } },
                            { new NestedStruct {
                                  Id = 5, Message = "05"
                              }, new NestedStruct {
                                  Id = 6, Message = "06"
                              } },
                            { new NestedStruct {
                                  Id = 7, Message = "07"
                              }, new NestedStruct {
                                  Id = 8, Message = "08"
                              } },
                        },
                        {
                            { new NestedStruct {
                                  Id = 9, Message = "09"
                              }, new NestedStruct {
                                  Id = 10, Message = "10"
                              } },
                            { new NestedStruct {
                                  Id = 11, Message = "11"
                              }, new NestedStruct {
                                  Id = 12, Message = "12"
                              } },
                            { new NestedStruct {
                                  Id = 13, Message = "13"
                              }, new NestedStruct {
                                  Id = 14, Message = "14"
                              } },
                            { new NestedStruct {
                                  Id = 15, Message = "15"
                              }, new NestedStruct {
                                  Id = 16, Message = "16"
                              } },
                        },
                        {
                            { new NestedStruct {
                                  Id = 17, Message = "17"
                              }, new NestedStruct {
                                  Id = 18, Message = "18"
                              } },
                            { new NestedStruct {
                                  Id = 19, Message = "19"
                              }, new NestedStruct {
                                  Id = 20, Message = "20"
                              } },
                            { new NestedStruct {
                                  Id = 21, Message = "21"
                              }, new NestedStruct {
                                  Id = 22, Message = "22"
                              } },
                            { new NestedStruct {
                                  Id = 23, Message = "23"
                              }, new NestedStruct {
                                  Id = 24, Message = "24"
                              } },
                        },
                    },
                    StringMultiArrayField = new[, , ]
                    {
                        {
                            { "01", "02" },
                            { "03", "04" },
                            { "05", "06" },
                            { "07", "08" },
                        },
                        {
                            { "09", "10" },
                            { "11", "12" },
                            { "13", "14" },
                            { "15", "16" },
                        },
                        {
                            { "17", "18" },
                            { "19", "20" },
                            { "21", "22" },
                            { "23", "24" },
                        },
                    },
                    WStringMultiArrayField = new[, , ]
                    {
                        {
                            { "01", "02" },
                            { "03", "04" },
                            { "05", "06" },
                            { "07", "08" },
                        },
                        {
                            { "09", "10" },
                            { "11", "12" },
                            { "13", "14" },
                            { "15", "16" },
                        },
                        {
                            { "17", "18" },
                            { "19", "20" },
                            { "21", "22" },
                            { "23", "24" },
                        },
                    },
                    CharMultiArrayField = new[, , ]
                    {
                        {
                            { '1', '2' },
                            { '3', '4' },
                            { '5', '6' },
                            { '7', '8' },
                        },
                        {
                            { '9', '0' },
                            { '1', '2' },
                            { '3', '4' },
                            { '5', '6' },
                        },
                        {
                            { '7', '8' },
                            { '9', '0' },
                            { '1', '2' },
                            { '3', '4' },
                        }
                    },
                    WCharMultiArrayField = new[, , ]
                    {
                        {
                            { '1', '2' },
                            { '3', '4' },
                            { '5', '6' },
                            { '7', '8' }
                        },
                        {
                            { '9', '0' },
                            { '1', '2' },
                            { '3', '4' },
                            { '5', '6' },
                        },
                        {
                            { '7', '8' },
                            { '9', '0' },
                            { '1', '2' },
                            { '3', '4' },
                        }
                    },
                };

                dataWriter.Write(data);
                ret = dataWriter.WaitForAcknowledgments(new Duration
                {
                    Seconds     = 60,
                    NanoSeconds = 0,
                });

                if (ret == ReturnCode.Ok)
                {
                    Console.WriteLine("Data sent and acknowledged.");
                }
                else
                {
                    Console.WriteLine("No acknowledge received: " + ret.ToString());
                }
            }
            else
            {
                Console.WriteLine("Subscription not found.");
            }
        }
예제 #6
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);
        }
예제 #7
0
        public void TestWrite()
        {
            // Initialize entities
            Duration duration = new Duration {
                Seconds = 5
            };

            DataWriter writer = _publisher.CreateDataWriter(_topic);

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

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataReaderQos qos = new DataReaderQos();

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

            Assert.IsNotNull(dataReader);

            int               count           = 0;
            Timestamp         timestamp       = default;
            ReturnCode        retReadInstance = ReturnCode.Error;
            InstanceHandle    lookupHandle    = InstanceHandle.HandleNil;
            List <TestStruct> samples         = new List <TestStruct>();
            List <SampleInfo> infos           = new List <SampleInfo>();

            listener.DataAvailable += (reader) =>
            {
                count++;
                if (count == 4)
                {
                    TestStructDataReader dr = new TestStructDataReader(reader);

                    lookupHandle = dr.LookupInstance(new TestStruct {
                        Id = count
                    });
                    retReadInstance = dr.ReadInstance(samples, infos, lookupHandle);
                    if (retReadInstance == ReturnCode.Ok && infos != null && infos.Count == 1)
                    {
                        timestamp = infos.First().SourceTimestamp;
                    }
                }
            };

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

            // Write an instance with the simplest overload
            ReturnCode result = dataWriter.Write(new TestStruct {
                Id = 1
            });

            Assert.AreEqual(ReturnCode.Ok, result);

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

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

            // Write an instance with the handle parameter as HandleNil
            result = dataWriter.Write(new TestStruct {
                Id = 2
            }, InstanceHandle.HandleNil);
            Assert.AreEqual(ReturnCode.Ok, result);

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

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

            // Write an instance with the handle parameter with a previously registered instance
            TestStruct instance = new TestStruct {
                Id = 3
            };
            InstanceHandle handle = dataWriter.RegisterInstance(instance);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle);

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

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

            System.Threading.Thread.Sleep(10);
            Assert.AreEqual(3, count);

            // Write an instance with the handle parameter and the timestamp
            Timestamp  now       = DateTime.Now.ToTimestamp();
            TestStruct instance1 = new TestStruct {
                Id = 4
            };
            InstanceHandle handle1 = dataWriter.RegisterInstance(instance1);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle1);

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

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

            System.Threading.Thread.Sleep(10);
            Assert.AreEqual(4, count);
            Assert.AreNotEqual(InstanceHandle.HandleNil, lookupHandle);
            Assert.AreEqual(ReturnCode.Ok, retReadInstance);
            Assert.IsNotNull(infos);
            Assert.AreEqual(1, infos.Count);
            Assert.AreEqual(now.Seconds, timestamp.Seconds);
            Assert.AreEqual(now.NanoSeconds, timestamp.NanoSeconds);
        }