Пример #1
0
        //
        public bool CreatePublisher(Participant part, string topicName)
        {
            if (myPub == null)
            {
                Topic topic = part.CreateTopic(topicName);
                if (topic == null)
                {
                    Log("No such Topic '" + topicName + "'. Failed to create Publisher!");
                    return(false);
                }
                else
                {
                    Log("[Topic: " + topic.GetName() + "] Publisher on Transport: " + topic.GetTransport() + ", " + topic.GetDomainAddress() + "::" + topic.GetPort());
                }
                myPub = new Publisher(topic);
                myPub.SetName("This is a publisher");

                string IP   = "";
                int    port = 0;
                myPub.GetLocalEndpoint(ref IP, ref port);
                Log("[Topic: " + topic.GetName() + "] Publisher using endpoint: " + IP + "::" + port);
            }
            else
            {
                Log("Publisher for Topic '" + topicName + "' is already created!");
            }
            return(true);    // myPub exist
        }
Пример #2
0
 //
 public bool CreateSubscriber(Participant part, string topicName, int deadLineEventIntervall)
 {
     if (mySub == null)
     {
         Topic topic = part.CreateTopic(topicName);
         if (topic == null)
         {
             Log("No such Topic '" + topicName + "'. Failed to create Subscriber!");
             return(false);
         }
         else
         {
             Log("[Topic: " + topic.GetName() + "] Subscriber on Transport: " + topic.GetTransport() + ", " + topic.GetDomainAddress() + "::" + topic.GetPort());
         }
         mySub = new Subscriber(topic);
         mySub.newDataDefault += new NewDataDefaultEventHandler(SubscriberNewData);
         mySub.deadlineEvent.AddObserver(this);
         StartSubscriber(deadLineEventIntervall);
     }
     else
     {
         Log("Subscriber for Topic '" + topicName + "' is already created!");
     }
     return(true);    // mySub exists
 }
        public Topic(string topicName, AdapterTopicQos requiredTopicQoS, TypeSupport clientTypeSupport)
        {
            if (clientTypeSupport == null)
            {
                throw new ArgumentNullException("clientTypeSupport");
            }

            string domain = null;

            // just using the default partition, named after the topic name
            TopicName = topicName;
            TopicType = typeof(T).Name;

            // Create a DomainParticipantFactory and a DomainParticipant
            RegisterDomain(this, domain);

            ErrorHandler.CheckHandle(Participant, "DDS.DomainParticipantFactory.CreateParticipant");

            // Register the required datatype.
            typeSupport = new TopicTypeSupport(clientTypeSupport);
            ReturnCode status = typeSupport.RegisterType(Participant, topicName);

            ErrorHandler.CheckStatus(status, "Topic.TopicMessageTypeSupport.RegisterType");

            TopicQos topicQos = null;

            Participant.GetDefaultTopicQos(ref topicQos);
            ApplyQos(topicQos, requiredTopicQoS);

            TopicQos = topicQos;

            TopicMessageTopic = Participant.CreateTopic(topicName, topicName, topicQos);

            Participant.SetDefaultTopicQos(topicQos);
            if (TopicMessageTopic == null)
            {
                throw new Exception("QoS for topic " + TopicName + " FAILED!, please check QoS!");
            }
        }
Пример #4
0
        public void DoTest()
        {
            // ===========================================
            cd1 = new ChildData();
            ChildData cd2 = new ChildData();
            ChildData cd3 = new ChildData();

            // ==============================================================
            LogNL("Test initial state...");
            CheckEmpty(cd1);
            CheckEmpty(cd2);
            CheckEmpty(cd3);

            CheckObjects(cd1, cd2);
            LogNL("Finished ");

            LogNL("Test cloning...");
            FillChildData(cd1);
            cd1.FillClone(cd2);
            CheckObjects(cd1, cd2);

            LogNL("Finished ");

            // ==============================================================
            LogNL("Serialize filled object");
            byte[]          bytes = new byte[Globals.MAX_SEGMENT_SIZE];
            WriteByteBuffer buf   = new WriteByteBuffer(bytes, Globals.MAX_SEGMENT_SIZE, true);
            OPSArchiverOut  ao    = new OPSArchiverOut(buf, false);

            LogNL("  Position()= " + buf.Position());
            ao.Inout <ChildData>("data", cd1);
            LogNL("  optNonVirt = false, Position()= " + buf.Position());
            AssertEQ(buf.Position(), 3150, "Serialized size error");

            buf = new WriteByteBuffer(bytes, Globals.MAX_SEGMENT_SIZE, true);
            ao  = new OPSArchiverOut(buf, true);
            LogNL("  Position()= " + buf.Position());
            ao.Inout <ChildData>("data", cd1);
            LogNL("  optNonVirt = true,  Position()= " + buf.Position());
            AssertEQ(buf.Position(), 2591, "Serialized size error");
            LogNL("Serialize finished");

            // Create a new stream to write to the file
            //BinaryWriter Writer = new BinaryWriter(File.OpenWrite(@"csharp-dump-opt.bin"));
            //Writer.Write(bytes, 0, buf.Position());
            //Writer.Flush();
            //Writer.Close();

            // ==============================================================
            LogNL("Test publish/subscribe");

            Logger.ExceptionLogger.AddLogger(logger);

            if (File.Exists("ops_config.xml"))
            {
                LogNL("Using config file in CWD");
            }
            else
            {
                string cwd = Environment.CurrentDirectory;
                int    pos = cwd.IndexOf("Example");
                if (pos > 0)
                {
                    cwd = cwd.Substring(0, pos) + "Examples/OPSIdls/TestAll/ops_config.xml";
                    LogNL("Using config file: " + cwd);
                    OPSConfigRepository.Add(cwd);
                }
            }

            participant = Participant.GetInstance("TestAllDomain");
            if (participant == null)
            {
                LogNL("Create participant failed. do you have ops_config.xml on your rundirectory?");
                return;
            }
            participant.AddTypeSupport(new TestAllTypeFactory());

            {
                // Setup & start subscriber w polling
                Topic topic = participant.CreateTopic("ChildTopic");
                sub = new ChildDataSubscriber(topic);
                sub.Start();

                // Setup & start publisher
                pub = new ChildDataPublisher(topic);
                pub.SetName("C#");
                pub.Start();

                Thread.Sleep(100);

                // Publish data
                pub.Write(cd1);

                // Check that sent data isn't affected by publish
                CheckObjects(cd1, cd2);

                // Check received values against sent values
                sub.WaitForNextData(500);
                OPSMessage msg = sub.GetMessage();
                if (msg != null)
                {
                    cd3 = (ChildData)msg.GetData();
                }
                AssertTRUE((msg != null) && (cd3 != null), "No received data");

                if ((msg != null) && (cd3 != null))
                {
                    CheckObjects(cd1, cd3);
                }

                LogNL("Finished ");

                sub.newData += new ChildDataEventHandler(SubscriberNewData);

                // Create a timer with a 5 second interval.
                aTimer = new System.Timers.Timer(5000);
                // Hook up the Elapsed event for the timer.
                aTimer.Elapsed  += OnTimedEvent;
                aTimer.AutoReset = true;
                aTimer.Enabled   = true;
            }
        }