예제 #1
0
        public void WorkerLoopWhenSingleMessage()
        {
            // arrange
            var message           = new Message("IncomingRoute", "test");
            var serializedMessage = new SerializedMessage("OutcomingRoute", new byte[4]);
            SerializedMessage?messageFromEvent = null;

            var dataContract = new Mock <IDataContractOperations>();

            dataContract.Setup(x => x.Serialize(message)).Returns(serializedMessage);

            var worker = new MessageSerializer(dataContract.Object);

            worker.OnNewMessage += m => messageFromEvent = m;

            // act
            worker.SerializeMessage(message);
            var messageProcessed = worker.DoWork();

            // assert
            Assert.IsTrue(messageProcessed);
            Assert.AreEqual(serializedMessage, messageFromEvent);

            dataContract.Verify(x => x.Serialize(message), Times.Once);
        }
예제 #2
0
        // FIXME?(AFL): kinda ugly signature
        public void SendRequest(TRequest request, TimeSpan?requestTimeout, string destinationClientID = null, bool retained = false)
        {
            requestTimeout = requestTimeout ?? TimeSpan.FromSeconds(20);

            var wrappedRequest = _requestSerializer.SerializeMessage(request);

            wrappedRequest.Source      = _client.Options.ClientOptions.ClientId;;
            wrappedRequest.Destination = destinationClientID;

            var payload       = _requestSerializer.SerializeWrapper(wrappedRequest);
            var packedMessage = new MqttApplicationMessageBuilder()
                                .WithTopic(RequestTopic)
                                .WithPayload(payload)
                                .WithQualityOfServiceLevel(_qosLevel)
                                .WithRetainFlag(retained)
                                .Build();

            try
            {
                _client.PublishAsync(packedMessage, _cancellationToken.Token).Wait();
            }
            catch (Exception e)
            {
                Log.Error($"Failed to publish response message on topic \"{ResponseTopic}\". Exception: {e}");
                throw;
            }
        }
예제 #3
0
        public void SerializeMessage_ShouldNotSerializeDefaultValuesForUndefinedError()
        {
            var message    = MessageFactory.GetMessage(new UndefinedError(null, null));
            var serialized = MessageSerializer.SerializeMessage(message);

            Assert.IsFalse(serialized.Contains("position"));
            Assert.IsFalse(serialized.Contains("holdingPiece"));
        }
예제 #4
0
        public void MessageSerializer_ShouldSerializeAllTypesCorrectly()
        {
            foreach (var message in messages)
            {
                var serialized = MessageSerializer.SerializeMessage(message);
            }

            Assert.Pass();
        }
예제 #5
0
        public void DeserializeMessage_ShouldNotThrowIfSetToThrowButAgentIdSet()
        {
            foreach (var message in messages)
            {
                var serialized   = MessageSerializer.SerializeMessage(message);
                var deserialized = MessageSerializer.DeserializeMessage(serialized, true);
            }

            Assert.Pass();
        }
예제 #6
0
        public void DeserializeMessage_ShouldReturnDerivedTypeMessage()
        {
            foreach (var message in messages)
            {
                var     serialized   = MessageSerializer.SerializeMessage(message);
                dynamic deserialized = MessageSerializer.DeserializeMessage(serialized);

                Assert.IsTrue(MessagingTestHelper.IsMessagePayloadDerived(deserialized));
            }
        }
예제 #7
0
        public T MessageRoundTrip <T>(T inMsg, Func <MessageDeserializer, T> deserialize) where T : Message
        {
            MessageSerializer ser = new MessageSerializer();

            ser.SerializeMessage(inMsg);
            MessageDeserializer deser = new MessageDeserializer();

            deser.InitWithBuffer(ser.GetBytes());
            return(deserialize(deser));
        }
예제 #8
0
        public void DeserializeMessage_ShouldNotSetUnrequiredFieldsForUndefinedError()
        {
            var     message      = MessageFactory.GetMessage(new UndefinedError(null, null));
            var     serialized   = MessageSerializer.SerializeMessage(message);
            dynamic deserialized = MessageSerializer.DeserializeMessage(serialized);

            var payload = deserialized.Payload as UndefinedError;

            Assert.IsNull(payload.Position);
            Assert.IsNull(payload.HoldingPiece);
        }
예제 #9
0
        public void DeserializeMessage_ShouldNotChangeValue()
        {
            foreach (var message in messages)
            {
                var serialized    = MessageSerializer.SerializeMessage(message);
                var deserialized  = MessageSerializer.DeserializeMessage(serialized);
                var newSerialized = MessageSerializer.SerializeMessage(deserialized);

                Assert.AreEqual(serialized, newSerialized);
            }
        }
    public void SendString(string message)
    {
        var serializer = new MessageSerializer();
        //var debug = new DebugLogMessage
        //{
        //    Message = message
        //};

        var welcome = new WelcomeMessage();
        var bytes   = serializer.SerializeMessage(welcome);

        _connection.Send(new IPEndPoint(IPAddress.Broadcast, serverPort), bytes);
    }
예제 #11
0
        public void WrapMessage_ShouldNotChangeValue()
        {
            foreach (var message in messages)
            {
                var serialized        = MessageSerializer.SerializeMessage(message);
                var wrapped           = MessageSerializer.WrapSerializedMessage(serialized);
                var unwrappedMessages = MessageSerializer.UnwrapMessages(wrapped, wrapped.Length);
                var unwrapped         = unwrappedMessages.Single();

                Assert.AreEqual(1, unwrappedMessages.Count());
                Assert.AreEqual(serialized, unwrapped);
            }
        }
예제 #12
0
        public bool HandleMessageReceived(string serializedMessage)
        {
            EndpointMessage requestMessage;

            if (!_requestDeserializer.Deserialize(serializedMessage, out requestMessage))
            {
                return(false);
            }

            var requestSource      = requestMessage.Source;
            var requestDestination = requestMessage.Destination;

            // Ignore request if its meant for another bot
            if (!string.IsNullOrEmpty(requestDestination) && !string.Equals(requestDestination, _client.Options.ClientOptions.ClientId))
            {
                return(true);
            }

            TRequest request;

            if (!_requestDeserializer.Deserialize(requestMessage, out request))
            {
                return(false);
            }

            var response        = _requestReceivedHandler(request);
            var responseMessage = _responseSerializer.SerializeMessage(response);

            responseMessage.Source      = requestDestination;
            responseMessage.Destination = requestSource;

            var payload       = _responseSerializer.SerializeWrapper(responseMessage);
            var packedMessage = new MqttApplicationMessageBuilder()
                                .WithTopic(ResponseTopic)
                                .WithPayload(payload)
                                .WithQualityOfServiceLevel(_qosLevel)
                                .Build();

            try
            {
                _client.PublishAsync(packedMessage, _cancellationToken.Token).Wait();
            }
            catch (Exception e)
            {
                Log.Error($"Failed to publish response message on topic \"{ResponseTopic}\". Exception: {e}");
                throw;
            }

            return(true);
        }
예제 #13
0
        public void DeserializeMessage_ShouldReturnDerivedTypeMessageAfterAddingToList()
        {
            var deserializedMessages = new List <BaseMessage>();

            foreach (var message in messages)
            {
                var serialized = MessageSerializer.SerializeMessage(message);
                deserializedMessages.Add(MessageSerializer.DeserializeMessage(serialized));
            }

            foreach (var deserializedMessage in deserializedMessages)
            {
                dynamic dynamicMessage = deserializedMessage;
                Assert.IsTrue(MessagingTestHelper.IsMessagePayloadDerived(dynamicMessage));
            }
        }
예제 #14
0
        public static async Task <bool> HandleMessage(WebSocket socket, MessageBase message)
        {
            try {
                if (socket.State == WebSocketState.Open)
                {
                    var encoded = Encoding.UTF8.GetBytes(MessageSerializer.SerializeMessage(message));
                    await socket.SendAsync(new ArraySegment <byte>(encoded, 0, encoded.Length), WebSocketMessageType.Text, true, CancellationToken.None);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //TODO: LOG
            }
            return(false);
        }
예제 #15
0
        public void OnException()
        {
            // arrange
            var message = new Message("IncomingRoute", "test");

            var dataContract = new Mock <IDataContractOperations>();

            dataContract.Setup(x => x.Serialize(message)).Throws <Exception>();

            var worker = new MessageSerializer(dataContract.Object);

            // act
            worker.SerializeMessage(message);

            // assert
            Assert.Throws <SerializationException>(() =>
            {
                worker.DoWork();
            });
        }
예제 #16
0
        public void UnwrapMessages_ShouldSeparateMessages()
        {
            var messagesCount   = messages.Count;
            var bytesCount      = 0;
            var wrappedMessages = new byte[(1 << 13) - 2];

            foreach (var message in messages)
            {
                var wrapped = MessageSerializer.SerializeAndWrapMessage(message);

                Array.Copy(wrapped, 0, wrappedMessages, bytesCount, wrapped.Length);
                bytesCount += wrapped.Length;
            }

            var unwrappedMessages = MessageSerializer.UnwrapMessages(wrappedMessages, bytesCount).ToList();

            Assert.AreEqual(messagesCount, unwrappedMessages.Count);
            for (int i = 0; i < messagesCount; i++)
            {
                var serialized = MessageSerializer.SerializeMessage(messages[i]);
                Assert.AreEqual(serialized, unwrappedMessages[i]);
            }
        }
예제 #17
0
    public void SendMessage(IUdpMessage message)
    {
        var bytes = _serializer.SerializeMessage(message);

        _connection.Send(_serverEndpoint, bytes);
    }
예제 #18
0
 internal void SendMessage(Message message)
 {
     _messageSerializer.SerializeMessage(message);
 }
예제 #19
0
 public void Write(Message.Message message)
 {
     _websocket.Send(MessageSerializer.SerializeMessage(message));
 }