Ejemplo n.º 1
0
        public void BinaryUDPPackageReaderTestMethod()
        {
            int           _port   = 35678;
            UInt32        _dataId = CommonDefinitions.DataSetId;
            List <string> _Events = new List <string>();

            using (BinaryUDPPackageReader _reader = new BinaryUDPPackageReader(_port, z => _Events.Add(z)))
            {
                Assert.IsNotNull(_reader);
                Assert.AreEqual <int>(0, _reader.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _reader.m_NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _reader.m_NumberOfSentMessages);
                Assert.AreEqual <HandlerState>(HandlerState.Disabled, _reader.State.State);
                _reader.AttachToNetwork();
                Assert.AreEqual <HandlerState>(HandlerState.Operational, _reader.State.State);
                Assert.AreEqual <int>(1, _reader.m_NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _reader.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _reader.m_NumberOfSentMessages);
                object[]             _buffer   = new object[CommonDefinitions.TestValues.Length];
                IConsumerBinding[]   _bindings = new IConsumerBinding[_buffer.Length];
                Action <object, int> _assign   = (x, y) => _buffer[y] = x;
                for (int i = 0; i < _buffer.Length; i++)
                {
                    _bindings[i] = new ConsumerBinding(i, _assign, Type.GetTypeCode(CommonDefinitions.TestValues[i].GetType()));
                }
                int _redItems = 0;
                _reader.ReadMessageCompleted += (x, y) => _reader_ReadMessageCompleted(x, y, _dataId, (z) => { _redItems++; return(_bindings[z]); }, _buffer.Length);
                _reader.SendUDPMessage(CommonDefinitions.GetTestBinaryArrayVariant(), _dataId, _port);
                Assert.AreEqual <int>(1, _reader.m_NumberOfAttachToNetwork);
                Assert.AreEqual <int>(116, _reader.m_NumberOfSentBytes);
                Assert.AreEqual <int>(1, _reader.m_NumberOfSentMessages);
                Thread.Sleep(1500);
                foreach (string _item in _Events)
                {
                    Console.WriteLine(_item);
                }
                //test packet content
                Assert.AreEqual <Guid>(CommonDefinitions.TestGuid, _reader.Header.PublisherId);
                Assert.AreEqual <byte>(MessageHandling.CommonDefinitions.ProtocolVersion, _reader.Header.ProtocolVersion);
                Assert.AreEqual <byte>(0, _reader.Header.NetworkMessageFlags);
                Assert.AreEqual <UInt32>(0, _reader.Header.SecurityTokenId);
                Assert.AreEqual <ushort>(1, _reader.Header.NonceLength);
                Assert.AreEqual <int>(1, _reader.Header.Nonce.Length);
                Assert.AreEqual <byte>(0xcc, _reader.Header.Nonce[0]);
                Assert.AreEqual <ushort>(1, _reader.Header.MessageCount);
                Assert.AreEqual <int>(1, _reader.Header.DataSetWriterIds.Count);
                Assert.AreEqual <UInt32>(CommonDefinitions.DataSetId, _reader.Header.DataSetWriterIds[0]);

                Assert.AreEqual <int>(_buffer.Length, _redItems);
                object[] _shouldBeInBuffer = CommonDefinitions.TestValues;
                Assert.AreEqual <int>(_shouldBeInBuffer.Length, _buffer.Length);
                Assert.AreEqual <string>(String.Join(",", _shouldBeInBuffer), String.Join(",", _buffer));
                Assert.AreEqual <byte>(1, _reader.Header.MessageCount);
                Assert.AreEqual <int>(3, _Events.Count);
            }
            Thread.Sleep(150);
            Assert.AreEqual <int>(3, _Events.Count);
        }
        public void DataTransferTest()
        {
            uint _dataId = CommonDefinitions.DataSetId;
            BinaryDataTransferGraphReceiverFixture _BinaryDataTransferGraphReceiverFixture = new DTGFixture();

            using (BinaryDecoder _reader = new BinaryDecoder(_BinaryDataTransferGraphReceiverFixture, new Helpers.UABinaryDecoderImplementation()))
            {
                Assert.IsNotNull(_reader);
                Assert.AreEqual <int>(0, _BinaryDataTransferGraphReceiverFixture.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _BinaryDataTransferGraphReceiverFixture.NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _BinaryDataTransferGraphReceiverFixture.m_NumberOfSentMessages);
                Assert.AreEqual <HandlerState>(HandlerState.Disabled, _BinaryDataTransferGraphReceiverFixture.State.State);
                _reader.AttachToNetwork();
                _reader.State.Enable();
                Assert.AreEqual <HandlerState>(HandlerState.Operational, _BinaryDataTransferGraphReceiverFixture.State.State);
                Assert.AreEqual <int>(1, _BinaryDataTransferGraphReceiverFixture.NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _BinaryDataTransferGraphReceiverFixture.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _BinaryDataTransferGraphReceiverFixture.m_NumberOfSentMessages);
                object[]             _buffer   = new object[CommonDefinitions.TestValues.Length];
                IConsumerBinding[]   _bindings = new IConsumerBinding[_buffer.Length];
                Action <object, int> _assign   = (x, y) => _buffer[y] = x;
                for (int i = 0; i < _buffer.Length; i++)
                {
                    _bindings[i] = new ConsumerBinding(i, _assign, Type.GetTypeCode(CommonDefinitions.TestValues[i].GetType()));
                }
                int _redItems = 0;
                _reader.ReadMessageCompleted += (x, y) => _reader_ReadMessageCompleted(x, y, _dataId, (z) => { _redItems++; return(_bindings[z]); }, _buffer.Length);
                _BinaryDataTransferGraphReceiverFixture.SendUDPMessage(CommonDefinitions.GetTestBinaryArrayVariant(), _dataId);
                Assert.AreEqual <int>(1, _BinaryDataTransferGraphReceiverFixture.NumberOfAttachToNetwork);
                Assert.AreEqual <int>(116, _BinaryDataTransferGraphReceiverFixture.m_NumberOfSentBytes);
                Assert.AreEqual <int>(1, _BinaryDataTransferGraphReceiverFixture.m_NumberOfSentMessages);
                //test packet content
                PacketHeader _readerHeader = _reader.Header;
                Assert.AreEqual <Guid>(CommonDefinitions.TestGuid, _readerHeader.PublisherId);
                Assert.AreEqual <byte>(MessageHandling.CommonDefinitions.ProtocolVersion, _readerHeader.ProtocolVersion);
                Assert.AreEqual <byte>(0, _readerHeader.NetworkMessageFlags);
                Assert.AreEqual <uint>(0, _readerHeader.SecurityTokenId);
                Assert.AreEqual <ushort>(1, _readerHeader.NonceLength);
                Assert.AreEqual <int>(1, _readerHeader.Nonce.Length);
                Assert.AreEqual <byte>(0xcc, _readerHeader.Nonce[0]);
                Assert.AreEqual <ushort>(1, _readerHeader.MessageCount);
                Assert.AreEqual <int>(1, _readerHeader.DataSetWriterIds.Count);
                Assert.AreEqual <uint>(CommonDefinitions.DataSetId, _readerHeader.DataSetWriterIds[0]);
                Assert.AreEqual <int>(_buffer.Length, _redItems);
                object[] _shouldBeInBuffer = CommonDefinitions.TestValues;
                Assert.AreEqual <int>(_shouldBeInBuffer.Length, _buffer.Length);
                Assert.AreEqual <string>(string.Join(",", _shouldBeInBuffer), string.Join(",", _buffer));
                Assert.AreEqual <byte>(1, _readerHeader.MessageCount);
            }
        }
Ejemplo n.º 3
0
 public void GetConsumerPacketHeaderTestMethod()
 {
     using (MemoryStream _stream = new MemoryStream(new ArraySegment <byte>(CommonDefinitions.GetTestBinaryArrayVariant(), 0, 27).ToArray <byte>()))
         using (PacketReader _reader = new PacketReader(_stream))
         {
             PacketHeader _header = PacketHeader.GetConsumerPacketHeader(_reader);
             Assert.IsNotNull(_header);
             Assert.AreEqual <Guid>(CommonDefinitions.TestGuid, _header.PublisherId);
             Assert.AreEqual <Byte>(110, _header.ProtocolVersion);
             Assert.AreEqual <Byte>(0, _header.NetworkMessageFlags);
             Assert.AreEqual <UInt32>(0, _header.SecurityTokenId);
             Assert.AreEqual <Byte>(1, _header.NonceLength);
             CollectionAssert.AreEqual(new byte[] { 0xcc }, _header.Nonce);
             Assert.AreEqual <Byte>(1, _header.MessageCount);
             CollectionAssert.AreEqual(new UInt16[] { CommonDefinitions.DataSetId }, _header.DataSetWriterIds);
         }
 }
Ejemplo n.º 4
0
        public void BinaryUDPPackageWriterTestMethod()
        {
            int _port = 35678;

            using (BinaryUDPPackageWriter _writer = new BinaryUDPPackageWriter("localhost", _port, new Helpers.UABinaryEncoderImplementation()))
            {
                Assert.AreEqual <int>(0, _writer.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _writer.m_NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _writer.m_NumberOfSentMessages);
                Assert.AreEqual <HandlerState>(HandlerState.Disabled, _writer.State.State);
                _writer.AttachToNetwork();
                Assert.AreEqual <HandlerState>(HandlerState.Operational, _writer.State.State);
                Assert.AreEqual <int>(1, _writer.m_NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _writer.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _writer.m_NumberOfSentMessages);
                ProducerBinding _binding = new ProducerBinding()
                {
                    Value = String.Empty
                };
                int          _sentItems        = 0;
                Guid         m_Guid            = CommonDefinitions.TestGuid;
                DataSelector _testDataSelector = new DataSelector()
                {
                    DataSetWriterId = CommonDefinitions.DataSetId, PublisherId = CommonDefinitions.TestGuid
                };
                ((IMessageWriter)_writer).Send((x) => { _binding.Value = CommonDefinitions.TestValues[x]; _sentItems++; return(_binding); },
                                               Convert.ToUInt16(CommonDefinitions.TestValues.Length),
                                               UInt64.MaxValue,
                                               FieldEncodingEnum.VariantFieldEncoding,
                                               _testDataSelector,
                                               0,
                                               CommonDefinitions.TestMinimalDateTime, new ConfigurationVersionDataType()
                {
                    MajorVersion = 0, MinorVersion = 0
                }
                                               );
                Assert.AreEqual(CommonDefinitions.TestValues.Length, _sentItems);
                Assert.AreEqual <int>(1, _writer.m_NumberOfAttachToNetwork);
                Assert.AreEqual <int>(115, _writer.m_NumberOfSentBytes);
                Assert.AreEqual <int>(1, _writer.m_NumberOfSentMessages);
                byte[] _shouldBeInBuffer = CommonDefinitions.GetTestBinaryArrayVariant4Consumer();
                byte[] _outputBuffer     = _writer.DoUDPRead();
                CollectionAssert.AreEqual(_outputBuffer, _shouldBeInBuffer);
            }
        }
        public void BinaryUDPPackageWriterTestMethod()
        {
            BinaryDataTransferGraphSenderFixture _binaryStreamObservable = new BinaryDataTransferGraphSenderTest();

            using (BinaryEncoder _writer = new BinaryEncoder(_binaryStreamObservable, new Helpers.UABinaryEncoderImplementation(), MessageLengthFieldTypeEnum.TwoBytes))
            {
                Assert.AreEqual <int>(0, _binaryStreamObservable.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _binaryStreamObservable.NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _binaryStreamObservable.m_NumberOfSentMessages);
                Assert.AreEqual <HandlerState>(HandlerState.Disabled, _binaryStreamObservable.State.State);
                _writer.AttachToNetwork();
                _writer.State.Enable();
                Assert.AreEqual <HandlerState>(HandlerState.Operational, _binaryStreamObservable.State.State);
                Assert.AreEqual <int>(1, _binaryStreamObservable.NumberOfAttachToNetwork);
                Assert.AreEqual <int>(0, _binaryStreamObservable.m_NumberOfSentBytes);
                Assert.AreEqual <int>(0, _binaryStreamObservable.m_NumberOfSentMessages);
                ProducerBindingFixture _binding = new ProducerBindingFixture()
                {
                    Value = string.Empty
                };
                int          _sentItems        = 0;
                Guid         m_Guid            = CommonDefinitions.TestGuid;
                DataSelector _testDataSelector = new DataSelector()
                {
                    DataSetWriterId = CommonDefinitions.DataSetId, PublisherId = CommonDefinitions.TestGuid
                };
                ((IMessageWriter)_writer).Send((x) => { _binding.Value = CommonDefinitions.TestValues[x]; _sentItems++; return(_binding); },
                                               Convert.ToUInt16(CommonDefinitions.TestValues.Length),
                                               ulong.MaxValue,
                                               FieldEncodingEnum.VariantFieldEncoding,
                                               _testDataSelector,
                                               0,
                                               CommonDefinitions.TestMinimalDateTime, new ConfigurationVersionDataType()
                {
                    MajorVersion = 0, MinorVersion = 0
                }
                                               );
                Assert.AreEqual(CommonDefinitions.TestValues.Length, _sentItems);
                Assert.AreEqual <int>(1, _binaryStreamObservable.NumberOfAttachToNetwork);
                Assert.AreEqual <int>(115, _binaryStreamObservable.m_NumberOfSentBytes);
                Assert.AreEqual <int>(1, _binaryStreamObservable.m_NumberOfSentMessages);
                byte[] _shouldBeInBuffer = CommonDefinitions.GetTestBinaryArrayVariant4Consumer();
                CollectionAssert.AreEqual(_binaryStreamObservable.Buffer, _shouldBeInBuffer);
            }
        }
Ejemplo n.º 6
0
 public void ProtocolVersionTestMethod()
 {
     byte[] _result = null;
     using (MemoryStream _stream = new MemoryStream())
         using (PacketWriter _writer = new PacketWriter(_stream))
         {
             PacketHeader _header = PacketHeader.GetProducerPacketHeader(_writer, CommonDefinitions.TestGuid, new UInt16[] { CommonDefinitions.DataSetId });
             Assert.IsNotNull(_header);
             Assert.AreEqual <Byte>(110, _header.ProtocolVersion);
             Assert.AreEqual <Guid>(CommonDefinitions.TestGuid, _header.PublisherId);
             _header.NetworkMessageFlags = 0;
             _header.SecurityTokenId     = 0;
             _header.NonceLength         = 1;
             _header.Nonce = new byte[] { 0xcc };
             _header.WritePacketHeader();
             _writer.Flush();
             _result = _stream.ToArray();
         }
     byte[] _expected = new ArraySegment <byte>(CommonDefinitions.GetTestBinaryArrayVariant(), 0, 27).ToArray <byte>();
     CollectionAssert.AreEqual(_expected, _result);
 }