Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            byte[]          b   = new byte[8192];
            WriteByteBuffer buf = new WriteByteBuffer(b, 8192, true);

            int i = 127;

            buf.Write(i);

            buf.Write(i);
        }
Exemplo n.º 2
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;
            }
        }
Exemplo n.º 3
0
        protected void Write(OPSObject opsObject)
        {
            if (opsObject == null)
            {
                throw new CommException("Trying to send OPSObject that is null!");
            }

            if (sendDataHandler == null)
            {
                Init();
            }

            if (sendDataHandler == null)
            {
                return;
            }

            this.sampleTime2 = this.sampleTime1;
            this.sampleTime1 = System.DateTime.Now.Ticks;

            OPSMessage message = new OPSMessage();
            opsObject.SetKey(this.key);
            message.SetData(opsObject);
            message.SetPublicationID(this.currentPublicationID);
            message.SetTopicName(this.topic.GetName());
            message.SetPublisherName(this.name);

            //inProcessTransport.copyAndPutMessage(message);
            WriteByteBuffer buf = new WriteByteBuffer(this.bytes, Globals.MAX_SEGMENT_SIZE);
            try
            {
                OPSArchiverOut archiverOut = new OPSArchiverOut(buf);

                archiverOut.Inout("message", message);

                // If o has spare bytes, write them to the end of the buf
                if (opsObject.spareBytes.Length > 0)
                {
                    buf.Write(opsObject.spareBytes, 0, opsObject.spareBytes.Length);
                }
                // Finish will fill in nrOf segments in all segments.
                buf.Finish();
                int sizeToSend = buf.Position();

                sendDataHandler.SendData(this.bytes, sizeToSend, this.topic);
            }
            catch (System.IO.IOException ex)
            {
                Logger.ExceptionLogger.LogException(ex);
            }
            IncCurrentPublicationID();
        }