Пример #1
0
        private void DoThreadActivity()
        {
            while (true)
            {
                ICollection <Condition> conditions = new List <Condition>();
                Duration duration = new Duration
                {
                    Seconds = Duration.InfiniteSeconds
                };
                _waitSet.Wait(conditions, duration);

                foreach (Condition cond in conditions)
                {
                    if (cond == _cancelCondition && cond.TriggerValue)
                    {
                        // We reset the cancellation condition because it is a good practice, but in this implementation it probably doesn't change anything.
                        _cancelCondition.TriggerValue = false;

                        // The thread activity has been canceled. We exit even if another condition (data available)
                        // is active: no point extracting data if we're going down anyway.
                        return;
                    }

                    if (cond == _statusCondition && cond.TriggerValue)
                    {
                        StatusCondition sCond = (StatusCondition)cond;
                        StatusMask      mask  = sCond.EnabledStatuses;
                        if ((mask & StatusKind.DataAvailableStatus) != 0)
                        {
                            _dataAvailableFunc(_dataReader);
                        }
                    }
                }
            }
        }
Пример #2
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();
        }
Пример #3
0
        static void Main(string[] args)
        {
            Ace.Init();

            DomainParticipantFactory dpf         = ParticipantService.Instance.GetDomainParticipantFactory("-DCPSConfigFile", "rtps.ini");
            DomainParticipant        participant = dpf.CreateParticipant(42);

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

            MessageTypeSupport support = new MessageTypeSupport();
            ReturnCode         result  = support.RegisterType(participant, support.GetTypeName());

            if (result != ReturnCode.Ok)
            {
                throw new Exception("Could not register type: " + result.ToString());
            }

            Topic topic = participant.CreateTopic("MessageTopic", support.GetTypeName());

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

            Subscriber subscriber = participant.CreateSubscriber();

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

            DataReader reader = subscriber.CreateDataReader(topic);

            if (reader == null)
            {
                throw new Exception("Could not create the message data reader");
            }
            MessageDataReader messageReader = new MessageDataReader(reader);

            while (true)
            {
                StatusMask mask = messageReader.StatusChanges;
                if ((mask & StatusKind.DataAvailableStatus) != 0)
                {
                    List <Message>    receivedData = new List <Message>();
                    List <SampleInfo> receivedInfo = new List <SampleInfo>();
                    result = messageReader.Take(receivedData, receivedInfo);

                    if (result == ReturnCode.Ok)
                    {
                        bool messageReceived = false;
                        for (int i = 0; i < receivedData.Count; i++)
                        {
                            if (receivedInfo[i].ValidData)
                            {
                                Console.WriteLine(receivedData[i].Content);
                                messageReceived = true;
                            }
                        }

                        if (messageReceived)
                        {
                            break;
                        }
                    }
                }

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

            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();

            participant.DeleteContainedEntities();
            dpf.DeleteParticipant(participant);
            ParticipantService.Instance.Shutdown();

            Ace.Fini();
        }