public XElement ConvertToXml(Message message)
        {
            Contract.Requires(message != null);
            Contract.Ensures(Contract.Result<XElement>() != null);

            return default(XElement);
        }
Exemplo n.º 2
0
        private Message CreateMessage(Command command)
        {
            Contract.Requires(command != null);
            Contract.Ensures(Contract.Result<Message>() != null);

            var message = new Message(1, messageId, command);
            Interlocked.Increment(ref messageId);
            return message;
        }
        public XElement ConvertToXml(Message message)
        {
            XElement element = new XElement("message");

            // We should use AsDynamicWriter instead AsDynamic
            // because we're changing exiting element not reading one
            dynamic dynamicElement = element.AsDynamicWriter();
            dynamicElement.version = message.Version;
            dynamicElement.messageId = message.Id;

            dynamicElement.command["type"] = message.Command.CommandType.ToString();

            // Writing command to dynamicElement
            WriteCommand(dynamicElement, message.Command);

            return element;
        }
        public void ConvertAckCommandTest()
        {
            long version = 1;
            long messageId = 123123;
            long ackMessageId = 3242;

            AckCommand command = new AckCommand(ackMessageId);
            Message message = new Message(version, messageId, command);

            //Converting to xml
            DynamicXmlMessageConverter converter = new DynamicXmlMessageConverter();
            XElement xml = converter.ConvertToXml(message);

            // And back again
            Message message2 = converter.ConvertFromXml(xml);
            AckCommand command2 = (AckCommand) message2.Command;
            Assert.That(command2.MessageId, Is.EqualTo(command.MessageId));
        }
        public void ConvertLoginCommandTest()
        {
            long version = 1;
            long messageId = 12312;
            string userName = "******";
            LoginCommand command = new LoginCommand(new UserInfo(userName));
            Message message = new Message(version, messageId, command);

            // Converting to xml
            DynamicXmlMessageConverter converter = new DynamicXmlMessageConverter();
            XElement xml = converter.ConvertToXml(message);

            // And back again
            Message message2 = converter.ConvertFromXml(xml);

            // Check message equivalence
            Assert.That(message2.Version, Is.EqualTo(message.Version));
            Assert.That(message2.Id, Is.EqualTo(message.Id));
            LoginCommand command2 = (LoginCommand) message2.Command;
            Assert.That(command2.UserInfo.Name, Is.EqualTo(userName));
        }
Exemplo n.º 6
0
        private void SendMessage(Message message)
        {
            Contract.Requires(message != null);

            string xml = messageConverter.ConvertToXml(message).ToString();
            connection.SendString(xml);
        }
Exemplo n.º 7
0
        private void SendUpdatedUserState(FullUserInfo userInfo)
        {
            Contract.Requires(userInfo != null);
            // Creating message to send
            var userStateCommand = new UserStateCommand(userInfo.UserInfo, userInfo.UserState);
            var message = new Message(1, messageId, userStateCommand);
            string xml = messageConverter.ConvertToXml(message).ToString();

            // Finding all connected users (without this particular user)
            var collectionToSend = users.Where(ui => ui.EndPoint != null && ui != userInfo);

            // And sending user information to them
            foreach (var ui in collectionToSend)
            {
                tcpServer.SendString(ui.EndPoint, xml);
            }
        }
Exemplo n.º 8
0
 private void SendMessage(IPEndPoint endPoint, Message message)
 {
     string xml = messageConverter.ConvertToXml(message).ToString();
     tcpServer.SendString(endPoint, xml);
 }
Exemplo n.º 9
0
 protected abstract void MessageReceived(Message message);
        public void ConvertTextMessageCommandTest()
        {
            long version = 1;
            long messageId = 123123;

            TextMessageCommand command = new TextMessageCommand(
                new UserInfo("from user"), new UserInfo("to user"), "text message");
            Message message = new Message(version, messageId, command);

            //Converting to xml
            DynamicXmlMessageConverter converter = new DynamicXmlMessageConverter();
            XElement xml = converter.ConvertToXml(message);

            // And back again
            Message message2 = converter.ConvertFromXml(xml);
            TextMessageCommand command2 = (TextMessageCommand)message2.Command;
            Assert.That(command2.From.Name, Is.EqualTo(command.From.Name));
            Assert.That(command2.To.Name, Is.EqualTo(command.To.Name));
            Assert.That(command2.Message, Is.EqualTo(command.Message));
        }
        public void ConvertUserInfoCommandTest()
        {
            long version = 1;
            long messageId = 123123;

            UserStateCommand command = new UserStateCommand(new UserInfo("userName"), "userState");
            Message message = new Message(version, messageId, command);

            //Converting to xml
            DynamicXmlMessageConverter converter = new DynamicXmlMessageConverter();
            XElement xml = converter.ConvertToXml(message);

            // And back again
            Message message2 = converter.ConvertFromXml(xml);
            UserStateCommand command2 = (UserStateCommand)message2.Command;
            Assert.That(command2.UserInfo.Name, Is.EqualTo(command.UserInfo.Name));
            Assert.That(command2.UserState, Is.EqualTo(command.UserState));
        }