Exemplo n.º 1
0
        public void TestGetKeyValue()
        {
            // Initialize entities
            DataWriter writer = _publisher.CreateDataWriter(_topic);

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

            // Call GetKeyValue with HandleNil
            TestStruct data   = new TestStruct();
            ReturnCode result = dataWriter.GetKeyValue(data, InstanceHandle.HandleNil);

            Assert.AreEqual(ReturnCode.BadParameter, result);

            // Register an instance
            TestStruct instance = new TestStruct {
                Id = 1
            };
            InstanceHandle handle = dataWriter.RegisterInstance(instance);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle);

            // Call GetKeyValue
            data   = new TestStruct();
            result = dataWriter.GetKeyValue(data, handle);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(1, data.Id);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
0
        public void TestLookupInstance()
        {
            // Initialize entities
            DataWriter writer = _publisher.CreateDataWriter(_topic);

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

            // Lookup for a non-existing instance
            InstanceHandle handle = dataWriter.LookupInstance(new TestStruct {
                Id = 1
            });

            Assert.AreEqual(InstanceHandle.HandleNil, handle);

            // Register an instance
            InstanceHandle handle1 = dataWriter.RegisterInstance(new TestStruct {
                Id = 1
            });

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle1);

            // Lookup for an existing instance
            handle = dataWriter.LookupInstance(new TestStruct {
                Id = 1
            });
            Assert.AreNotEqual(InstanceHandle.HandleNil, handle);
            Assert.AreEqual(handle1, handle);
        }
Exemplo n.º 4
0
        public void TestGetLivelinessLostStatus()
        {
            // Initialize entities
            DataWriterQos dwQos = new DataWriterQos();

            dwQos.Liveliness.Kind          = LivelinessQosPolicyKind.ManualByTopicLivelinessQos;
            dwQos.Liveliness.LeaseDuration = new Duration
            {
                Seconds = 1,
            };
            DataWriter writer = _publisher.CreateDataWriter(_topic, dwQos);

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

            Assert.IsNotNull(dataWriter);

            // After half second liveliness should not be lost yet
            System.Threading.Thread.Sleep(500);

            LivelinessLostStatus status = default;
            ReturnCode           result = writer.GetLivelinessLostStatus(ref status);

            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(0, status.TotalCount);
            Assert.AreEqual(0, status.TotalCountChange);

            // After one second and a half one liveliness should be lost
            System.Threading.Thread.Sleep(1000);

            result = writer.GetLivelinessLostStatus(ref status);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(1, status.TotalCount);
            Assert.AreEqual(1, status.TotalCountChange);
        }
Exemplo n.º 5
0
        public void TestInitialize()
        {
            _participant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN);
            Assert.IsNotNull(_participant);
            _participant.BindRtpsUdpTransportConfig();

            TestStructTypeSupport support = new TestStructTypeSupport();
            string     typeName           = support.GetTypeName();
            ReturnCode result             = support.RegisterType(_participant, typeName);

            Assert.AreEqual(ReturnCode.Ok, result);

            _topic = _participant.CreateTopic(TestContext.TestName, typeName);
            Assert.IsNotNull(_topic);
            Assert.IsNull(_topic.Listener);
            Assert.AreEqual(TestContext.TestName, _topic.Name);
            Assert.AreEqual(typeName, _topic.TypeName);

            _subscriber = _participant.CreateSubscriber();
            Assert.IsNotNull(_subscriber);

            _publisher = _participant.CreatePublisher();
            Assert.IsNotNull(_publisher);

            _writer = _publisher.CreateDataWriter(_topic);
            Assert.IsNotNull(_writer);
            _dataWriter = new TestStructDataWriter(_writer);

            DataReaderQos qos = new DataReaderQos();

            qos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            _reader = _subscriber.CreateDataReader(_topic, qos);
            Assert.IsNotNull(_reader);
        }
Exemplo n.º 6
0
        public void TestUnregisterInstance()
        {
            // Initialize entities
            DataWriter writer = _publisher.CreateDataWriter(_topic);

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

            // Unregister not registered instance
            ReturnCode result = dataWriter.UnregisterInstance(new TestStruct {
                Id = 1
            });

            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);

            // Register an instance
            TestStruct instance1 = new TestStruct {
                Id = 1
            };
            InstanceHandle handle1 = dataWriter.RegisterInstance(instance1);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle1);

            // Unregister the previous registered instance with the simplest overload
            result = dataWriter.UnregisterInstance(new TestStruct {
                Id = 1
            });
            Assert.AreEqual(ReturnCode.Ok, result);

            // Register a new instance
            TestStruct instance2 = new TestStruct {
                Id = 2
            };
            InstanceHandle handle2 = dataWriter.RegisterInstance(instance2);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle2);

            // Unregister the previous registered instance with the handle
            result = dataWriter.UnregisterInstance(new TestStruct {
                Id = 2
            }, handle2);
            Assert.AreEqual(ReturnCode.Ok, result);

            // Register a new instance
            TestStruct instance3 = new TestStruct {
                Id = 3
            };
            InstanceHandle handle3 = dataWriter.RegisterInstance(instance3);

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle3);

            // Unregister the previous registered instance with the handle and the timestamp
            result = dataWriter.UnregisterInstance(new TestStruct {
                Id = 3
            }, handle3, DateTime.Now.ToTimestamp());
            Assert.AreEqual(ReturnCode.Ok, result);
        }
Exemplo n.º 7
0
        public void TestGetOfferedDeadlineMissedStatus()
        {
            // Initialize entities
            DataWriterQos qos = new DataWriterQos();

            qos.Deadline.Period = new Duration
            {
                Seconds = 1,
            };
            DataWriter writer = _publisher.CreateDataWriter(_topic, qos);

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

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataReader reader = subscriber.CreateDataReader(_topic);

            Assert.IsNotNull(reader);

            // Wait for discovery and write an instance
            bool found = writer.WaitForSubscriptions(1, 1000);

            Assert.IsTrue(found);

            dataWriter.Write(new TestStruct
            {
                Id = 1,
            });

            // After half second deadline should not be lost yet
            System.Threading.Thread.Sleep(500);

            OfferedDeadlineMissedStatus status = default;
            ReturnCode result = writer.GetOfferedDeadlineMissedStatus(ref status);

            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(0, status.TotalCount);
            Assert.AreEqual(0, status.TotalCountChange);
            Assert.AreEqual(InstanceHandle.HandleNil, status.LastInstanceHandle);

            // After one second and a half one deadline should be lost
            System.Threading.Thread.Sleep(1000);

            result = writer.GetOfferedDeadlineMissedStatus(ref status);
            Assert.AreEqual(ReturnCode.Ok, result);
            Assert.AreEqual(1, status.TotalCount);
            Assert.AreEqual(1, status.TotalCountChange);
            Assert.AreNotEqual(InstanceHandle.HandleNil, status.LastInstanceHandle);
        }
Exemplo n.º 8
0
        public void TestInitialize()
        {
            _participant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN);
            Assert.IsNotNull(_participant);
            _participant.BindRtpsUdpTransportConfig();

            TestStructTypeSupport support = new TestStructTypeSupport();
            string     typeName           = support.GetTypeName();
            ReturnCode result             = support.RegisterType(_participant, typeName);

            Assert.AreEqual(ReturnCode.Ok, result);

            _topic = _participant.CreateTopic(TestContext.TestName, typeName);
            Assert.IsNotNull(_topic);
            Assert.IsNull(_topic.GetListener());
            Assert.AreEqual(TestContext.TestName, _topic.Name);
            Assert.AreEqual(typeName, _topic.TypeName);

            SubscriberQos sQos = new SubscriberQos();

            sQos.EntityFactory.AutoenableCreatedEntities = false;
            sQos.Presentation.OrderedAccess  = true;
            sQos.Presentation.CoherentAccess = true;
            sQos.Presentation.AccessScope    = PresentationQosPolicyAccessScopeKind.InstancePresentationQos;
            _subscriber = _participant.CreateSubscriber(sQos);
            Assert.IsNotNull(_subscriber);

            PublisherQos pQos = new PublisherQos();

            pQos.EntityFactory.AutoenableCreatedEntities = false;
            pQos.Presentation.OrderedAccess  = true;
            pQos.Presentation.CoherentAccess = true;
            pQos.Presentation.AccessScope    = PresentationQosPolicyAccessScopeKind.InstancePresentationQos;
            _publisher = _participant.CreatePublisher(pQos);
            Assert.IsNotNull(_publisher);

            _listener = new MyDataWriterListener();
            _writer   = _publisher.CreateDataWriter(_topic, _listener);
            Assert.IsNotNull(_writer);
            _dataWriter = new TestStructDataWriter(_writer);

            DataReaderQos qos = new DataReaderQos();

            qos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            _reader = _subscriber.CreateDataReader(_topic, qos);
            Assert.IsNotNull(_reader);
        }
Exemplo n.º 9
0
        public void TestCleanup()
        {
            if (_participant != null)
            {
                ReturnCode result = _participant.DeleteContainedEntities();
                Assert.AreEqual(ReturnCode.Ok, result);
            }

            if (AssemblyInitializer.Factory != null)
            {
                ReturnCode result = AssemblyInitializer.Factory.DeleteParticipant(_participant);
                Assert.AreEqual(ReturnCode.Ok, result);
            }

            _participant = null;
            _publisher   = null;
            _subscriber  = null;
            _topic       = null;
            _writer      = null;
            _dataWriter  = null;
            _reader      = null;
        }
Exemplo n.º 10
0
        public void TestRegisterInstance()
        {
            // Initialize entities
            DataWriter writer = _publisher.CreateDataWriter(_topic);

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

            // Register an instance
            InstanceHandle handle = dataWriter.RegisterInstance(new TestStruct {
                Id = 1
            });

            Assert.AreNotEqual(InstanceHandle.HandleNil, handle);

            // Register an instance with timestamp
            InstanceHandle otherHandle = dataWriter.RegisterInstance(new TestStruct {
                Id = 2
            }, DateTime.Now.ToTimestamp());

            Assert.AreNotEqual(InstanceHandle.HandleNil, otherHandle);
            Assert.AreNotEqual(handle, otherHandle);
        }
Exemplo n.º 11
0
        public void TestBeginEndAccess()
        {
            // OpenDDS Issue: Coherent sets for PRESENTATION QoS not Currently implemented on RTPS.
            // Just prepare the unit test for the moment.

            // 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);

            PublisherQos pubQos = new PublisherQos();

            pubQos.Presentation.AccessScope    = PresentationQosPolicyAccessScopeKind.GroupPresentationQos;
            pubQos.Presentation.CoherentAccess = true;
            pubQos.Presentation.OrderedAccess  = true;
            Publisher publisher = _participant.CreatePublisher(pubQos);

            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);

            SubscriberQos subQos = new SubscriberQos();

            subQos.Presentation.AccessScope    = PresentationQosPolicyAccessScopeKind.GroupPresentationQos;
            subQos.Presentation.CoherentAccess = true;
            subQos.Presentation.OrderedAccess  = true;
            MySubscriberListener listener = new MySubscriberListener();

            listener.DataOnReaders += (sub) =>
            {
                result = sub.BeginAccess();
                Assert.AreEqual(ReturnCode.Ok, result);

                List <DataReader> list = new List <DataReader>();
                result = sub.GetDataReaders(list);

                // Here we should check that we received two DataReader
                // read the data of each one and confirm tha the group coherent access
                // is working as expected.

                result = sub.EndAccess();
                Assert.AreEqual(ReturnCode.Ok, result);
            };

            Subscriber subscriber = _participant.CreateSubscriber(subQos, listener);

            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);

            // Call EndAccess without calling first BeginAccess
            result = subscriber.EndAccess();
            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);

            // Publish a samples in both topics
            result = publisher.BeginCoherentChanges();
            Assert.AreEqual(ReturnCode.Ok, result);

            result = dataWriter.Write(new TestStruct
            {
                Id = 1
            });
            Assert.AreEqual(ReturnCode.Ok, result);

            result = otherDataWriter.Write(new TestStruct
            {
                Id = 1
            });
            Assert.AreEqual(ReturnCode.Ok, result);

            result = publisher.EndCoherentChanges();
            Assert.AreEqual(ReturnCode.Ok, result);

            // Give some time to the subscriber to process the messages
            System.Threading.Thread.Sleep(500);
        }
Exemplo n.º 12
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.º 13
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.GetListener());
            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(subListener);

            Assert.IsNotNull(subscriber);

            MyDataReaderListener readListener = new MyDataReaderListener();

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

            Assert.IsNotNull(reader);

            System.Threading.Thread.Sleep(100);

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

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

            System.Threading.Thread.Sleep(100);

            // Check the received instances
            Assert.AreEqual(10, subscriberReceived);
            Assert.AreEqual(5, readerReceived);
        }
Exemplo n.º 14
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.º 15
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.");
            }
        }
Exemplo n.º 16
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);
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        public void TestWaitForAcknowledgments()
        {
            // 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(TestWaitForAcknowledgments), typeName);

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

            Publisher publisher = _participant.CreatePublisher();

            Assert.IsNotNull(publisher);

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataWriter writer = publisher.CreateDataWriter(topic);

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

            // Call WaitForAcknowledgments without DataReaders
            result = dataWriter.Write(new TestStruct
            {
                Id = 1
            });
            Assert.AreEqual(ReturnCode.Ok, result);

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

            // Create a Reliable DataReader, write a new sample and Call WaitForAcknowledgments
            DataReaderQos drQos = new DataReaderQos();

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

            Assert.IsNotNull(reader);

            System.Threading.Thread.Sleep(500);

            result = dataWriter.Write(new TestStruct
            {
                Id = 2
            });
            Assert.AreEqual(ReturnCode.Ok, result);

            result = publisher.WaitForAcknowledgments(new Duration
            {
                Seconds = 5
            });
            Assert.AreEqual(ReturnCode.Ok, result);
        }
Exemplo n.º 19
0
        public void TestSuspendResumePublications()
        {
            // 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(TestSuspendResumePublications), typeName);

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

            Publisher publisher = _participant.CreatePublisher();

            Assert.IsNotNull(publisher);

            Subscriber subscriber = _participant.CreateSubscriber();

            Assert.IsNotNull(subscriber);

            DataWriter writer = publisher.CreateDataWriter(topic);

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

            DataReaderQos drQos = new DataReaderQos();

            drQos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            drQos.History.Kind     = HistoryQosPolicyKind.KeepAllHistoryQos;
            DataReader reader = subscriber.CreateDataReader(topic, drQos);

            Assert.IsNotNull(reader);

            TestStructDataReader dataReader = new TestStructDataReader(reader);

            // Call ResumePublications without calling first SuspendPublications
            result = publisher.ResumePublications();
            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);

            // Suspend publications and write samples
            result = publisher.SuspendPublications();
            Assert.AreEqual(ReturnCode.Ok, result);

            for (int i = 1; i <= 5; i++)
            {
                // OpenDDS issue: cannot register more than one instance during SuspendPublications.
                // Looks like that the control messages are never delivered and the controlTracker never get free during delete_datawriter
                TestStruct sample = new TestStruct
                {
                    Id        = 1,
                    ShortType = (short)i
                };

                InstanceHandle handle = dataWriter.RegisterInstance(sample);
                Assert.AreNotEqual(InstanceHandle.HandleNil, handle);

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

            System.Threading.Thread.Sleep(500);

            // Check that not samples arrived
            List <TestStruct> data        = new List <TestStruct>();
            List <SampleInfo> sampleInfos = new List <SampleInfo>();

            result = dataReader.Read(data, sampleInfos);
            Assert.AreEqual(ReturnCode.NoData, result);

            // Resume publication and check the samples
            result = publisher.ResumePublications();
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(500);

            data        = new List <TestStruct>();
            sampleInfos = new List <SampleInfo>();
            result      = dataReader.Read(data, sampleInfos);
            Assert.AreEqual(ReturnCode.Ok, result);

            for (int i = 0; i < data.Count; i++)
            {
                Assert.IsTrue(sampleInfos[i].ValidData);
                Assert.AreEqual(i + 1, data[i].ShortType);
            }
        }
Exemplo n.º 20
0
        public void TestBeginEndCoherentChanges()
        {
            // 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(TestBeginEndCoherentChanges), typeName);

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

            PublisherQos pQos = new PublisherQos();

            pQos.Presentation.CoherentAccess = true;
            pQos.Presentation.OrderedAccess  = true;
            pQos.Presentation.AccessScope    = PresentationQosPolicyAccessScopeKind.TopicPresentationQos;
            Publisher publisher = _participant.CreatePublisher(pQos);

            Assert.IsNotNull(publisher);

            SubscriberQos sQos = new SubscriberQos();

            sQos.Presentation.CoherentAccess = true;
            sQos.Presentation.OrderedAccess  = true;
            sQos.Presentation.AccessScope    = PresentationQosPolicyAccessScopeKind.TopicPresentationQos;
            Subscriber subscriber = _participant.CreateSubscriber(sQos);

            Assert.IsNotNull(subscriber);

            DataWriter writer = publisher.CreateDataWriter(topic);

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

            DataReaderQos drQos = new DataReaderQos();

            drQos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos;
            drQos.History.Kind     = HistoryQosPolicyKind.KeepAllHistoryQos;
            DataReader reader = subscriber.CreateDataReader(topic, drQos);

            Assert.IsNotNull(reader);

            TestStructDataReader dataReader = new TestStructDataReader(reader);

            // Call EndCoherentChanges without calling first SuspendPublications
            result = publisher.EndCoherentChanges();
            Assert.AreEqual(ReturnCode.PreconditionNotMet, result);

            // Begin coherent access and write samples
            result = publisher.BeginCoherentChanges();
            Assert.AreEqual(ReturnCode.Ok, result);

            for (int i = 1; i <= 5; i++)
            {
                TestStruct sample = new TestStruct
                {
                    Id        = i,
                    ShortType = (short)i
                };

                InstanceHandle handle = dataWriter.RegisterInstance(sample);
                Assert.AreNotEqual(InstanceHandle.HandleNil, handle);

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

            System.Threading.Thread.Sleep(500);

            // Check that not samples arrived
            List <TestStruct> data        = new List <TestStruct>();
            List <SampleInfo> sampleInfos = new List <SampleInfo>();

            #region OpenDDS Issue
            // Coherent sets for PRESENTATION QoS not Currently implemented on RTPS
            //result = dataReader.Read(data, sampleInfos);
            //Assert.AreEqual(ReturnCode.NoData, result);
            #endregion

            // End coherent access and check the samples
            result = publisher.EndCoherentChanges();
            Assert.AreEqual(ReturnCode.Ok, result);

            System.Threading.Thread.Sleep(500);

            data        = new List <TestStruct>();
            sampleInfos = new List <SampleInfo>();
            result      = dataReader.Read(data, sampleInfos);
            Assert.AreEqual(ReturnCode.Ok, result);

            for (int i = 0; i < data.Count; i++)
            {
                Assert.IsTrue(sampleInfos[i].ValidData);
                Assert.AreEqual(i + 1, data[i].Id);
                Assert.AreEqual(i + 1, data[i].ShortType);
            }
        }