コード例 #1
0
        private async void DispatchSingleObjectMsg(XElement xmlObj)
        {
            ModelXmlMapper.MappedType typeInElement = GetMappedType(xmlObj);

            switch (typeInElement)
            {
            case ModelXmlMapper.MappedType.MESSAGE:
                MessageModel msg = new MessageModel();
                if (msg.FromXml(xmlObj))
                {
                    string message = msg.From + ": " + msg.Content;
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                    {
                        context.ChatWinodw.MessageReceived(message);
                    }
                        );
                }
                break;

            case ModelXmlMapper.MappedType.CHAT_REQUEST:
                ChatRequest req = new ChatRequest();
                if (req.FromXml(xmlObj))
                {
                    // TODO: check if ongoing chat and return failure from here:
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                    {
                        context.ContactsWindow.ChatRequestReceived(req);
                    }
                        );
                }
                break;

            case ModelXmlMapper.MappedType.CHAT_REQUEST_RESPONSE:
                ChatRequestResponse resp = new ChatRequestResponse();
                if (resp.FromXml(xmlObj))
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                    {
                        context.ContactsWindow.ChatResponseReceived(resp);
                        // context.ChatWinodw.Disconnected(resp);
                    }
                        );
                }
                break;

            case ModelXmlMapper.MappedType.LOGIN_RESPONSE:
                LoginResponse loginResp = new LoginResponse();
                if (loginResp.FromXml(xmlObj))
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                    {
                        context.LoginWindow.OnLoginResponseAsync(loginResp);
                    }
                        );
                }
                break;

            case ModelXmlMapper.MappedType.CONTACT:
                Contact contact = new Contact();
                if (contact.FromXml(xmlObj))
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () =>
                    {
                        if (context.ContactsWindow != null)
                        {
                            context.ContactsWindow.OnContactUpdate(contact);
                        }
                    }
                        );
                }
                break;

            default:
                // do nothing
                break;
            }
        }
コード例 #2
0
        public void HandleMessage(string incommingMessage, TalkBackDBContext dbContext)
        {
            MappedType t           = MappedType.UNDEFINED;
            XDocument  serialized  = XDocument.Parse(incommingMessage);
            XElement   typeElement = serialized.Root.Element("Type");

            if (typeElement == null)
            {
                // ignore
                return;
            }
            string type = typeElement.Value;

            if (ModelXmlMapper.map.TryGetValue(type, out t))
            {
                switch (t)
                {
                case MappedType.MESSAGE:
                    MessageModel msg = new MessageModel();
                    if (msg.FromXml(serialized.Root))
                    {
                        HandleChatMessageMsg(msg, dbContext);
                    }
                    break;

                case MappedType.USER:
                    UserModel user = new UserModel();
                    user.FromXml(serialized.Root);
                    break;

                case MappedType.CONTACT:
                    Contact contact = new Contact();
                    contact.FromXml(serialized.Root);
                    break;

                case MappedType.CHAT_REQUEST:
                    ChatRequest req = new ChatRequest();
                    if (req.FromXml(serialized.Root))
                    {
                        HandleChatRequestMsg(req, dbContext);
                    }
                    break;

                case MappedType.CHAT_REQUEST_RESPONSE:
                    ChatRequestResponse resp = new ChatRequestResponse();
                    if (resp.FromXml(serialized.Root))
                    {
                        HandleChatResponseMsg(resp, dbContext);
                    }
                    break;

                case MappedType.LOGIN:
                    Login login = new Login();
                    if (login.FromXml(serialized.Root))
                    {
                        HandleLoginMsg(login, dbContext);
                    }
                    break;

                case MappedType.LOGIN_RESPONSE:
                    LoginResponse loginResp = new LoginResponse();
                    loginResp.FromXml(serialized.Root);
                    break;

                case MappedType.UNDEFINED:
                    throw new Exception("Don't know how to parse this type");
                }
            }
        }