Beispiel #1
0
        internal override bool ProcessAndDispatchEventsToChild(EventContext eventcontext)
        {
            //No child to dispatch any more, need to dispatch to child , process locally for message type
            if (string.Equals(eventcontext.EventEntity.Link.Token, TokenMapper.GetTokenName(typeof(MessageResource))))
            {
                MessageResource message = this.ConvertToPlatformServiceResource <MessageResource>(eventcontext);
                if (message != null)
                {
                    if (message.Direction == Direction.Incoming)
                    {
                        // File incoming message received event
                        IncomingMessageEventArgs args = new IncomingMessageEventArgs
                        {
                            HtmlMessage  = message.HtmlMessage == null? null : message.HtmlMessage.Value as TextHtmlMessage,
                            PlainMessage = message.TextMessage == null ? null : message.TextMessage.Value as TextPlainMessage
                        };
                        Conversation conv = this.Parent as Conversation;

                        Participant fromParticipant = conv.TryGetParticipant(message.ParticipantResourceLink.Href) as Participant;
                        if (fromParticipant == null)
                        {
                            Logger.Instance.Warning("Get message from an unknown participant, probably client code bug or server bug");
                        }
                        else
                        {
                            args.FromParticipantName = fromParticipant.Name;
                        }

                        IncomingMessageReceived?.Invoke(this, args);
                    }
                    else //For out going, just detect the completed event
                    {
                        if (eventcontext.EventEntity.Relationship == EventOperation.Completed)
                        {
                            TaskCompletionSource <string> tcs = null;
                            m_outGoingmessageTcses.TryGetValue(UriHelper.CreateAbsoluteUri(this.BaseUri, eventcontext.EventEntity.Link.Href).ToString().ToLower(), out tcs);
                            if (tcs != null)
                            {
                                if (eventcontext.EventEntity.Status == EventStatus.Success)
                                {
                                    tcs.TrySetResult(string.Empty);
                                }
                                else if (eventcontext.EventEntity.Status == EventStatus.Failure)
                                {
                                    string error = eventcontext.EventEntity.Error == null ? null : eventcontext.EventEntity.Error.GetErrorInformationString();
                                    tcs.TrySetException(new RemotePlatformServiceException("Send Message failed with error" + error + eventcontext.LoggingContext.ToString()));
                                }
                                else
                                {
                                    Logger.Instance.Error("Do not get a valid status code for message complete event!");
                                    tcs.TrySetException(new RemotePlatformServiceException("Send Message failed !"));
                                }
                                m_outGoingmessageTcses.TryRemove(eventcontext.EventEntity.Link.Href.ToLower(), out tcs);
                            }
                        }
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #2
0
        internal override bool ProcessAndDispatchEventsToChild(EventContext eventContext)
        {
            if (eventContext.EventEntity.Link.Token == ResourceModel.TokenMapper.GetTokenName(typeof(ToneResource)))
            {
                var toneResource       = ConvertToPlatformServiceResource <ToneResource>(eventContext);
                Uri audioVideoFlowLink = UriHelper.CreateAbsoluteUri(this.BaseUri, toneResource.AudioVideoFlowLink.Href);

                if (string.Equals(audioVideoFlowLink.ToString(), this.ResourceUri.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    var eventArgs = new ToneReceivedEventArgs(toneResource.ToneValue);
                    m_toneReceivedEvent?.Invoke(this, eventArgs);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            //No child to dispatch any more, need to dispatch to child , process locally for message type
            else if (string.Equals(eventContext.EventEntity.Link.Token, ResourceModel.TokenMapper.GetTokenName(typeof(PromptResource))))
            {
                PromptResource prompt = this.ConvertToPlatformServiceResource <PromptResource>(eventContext);
                if (prompt != null)
                {
                    if (eventContext.EventEntity.Relationship == ResourceModel.EventOperation.Completed)
                    {
                        TaskCompletionSource <Prompt> tcs = null;
                        Uri resourceAbsoluteUri           = UriHelper.CreateAbsoluteUri(this.BaseUri, eventContext.EventEntity.Link.Href);
                        m_onGoingPromptTcses.TryGetValue(resourceAbsoluteUri.ToString().ToLower(), out tcs);
                        if (tcs != null)
                        {
                            Prompt p = new Prompt(this.RestfulClient, prompt, this.BaseUri, resourceAbsoluteUri, this);

                            if (eventContext.EventEntity.Status == ResourceModel.EventStatus.Success)
                            {
                                tcs.TrySetResult(p);
                            }
                            else if (eventContext.EventEntity.Status == ResourceModel.EventStatus.Failure)
                            {
                                ResourceModel.ErrorInformation error = eventContext.EventEntity.Error;
                                ErrorInformation errorInfo           = error == null ? null : new ErrorInformation(error);
                                string           errorMessage        = errorInfo?.ToString();
                                tcs.TrySetException(new RemotePlatformServiceException("PlayPrompt failed with error " + errorMessage + eventContext.LoggingContext?.ToString(), errorInfo));
                            }
                            else
                            {
                                Logger.Instance.Error("Received invalid status code for prompt completed event");
                                tcs.TrySetException(new RemotePlatformServiceException("PlayPrompt failed"));
                            }
                            m_onGoingPromptTcses.TryRemove(eventContext.EventEntity.Link.Href.ToLower(), out tcs);
                        }
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }