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); } }
internal ParticipantMessaging(IRestfulClient restfulClient, ParticipantMessagingResource resource, Uri baseUri, Uri resourceUri, Participant parent) : base(restfulClient, resource, baseUri, resourceUri, parent) { if (parent == null) { throw new ArgumentNullException(nameof(parent), "Conversation is required"); } }
internal override bool ProcessAndDispatchEventsToChild(EventContext eventcontext) { ParticipantChangeEventArgs participantChangeEventArgs = null; //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(ParticipantResource)))) { if (eventcontext.EventEntity.In != null) { //TODO: currently we do not handle in link return(true); } string normalizedUri = UriHelper.NormalizeUri(eventcontext.EventEntity.Link.Href, this.BaseUri); ParticipantResource participantResource = this.ConvertToPlatformServiceResource <ParticipantResource>(eventcontext); switch (eventcontext.EventEntity.Relationship) { case Rtc.Internal.RestAPI.ResourceModel.EventOperation.Added: { Participant tempParticipant = null; if (!m_participantsCache.TryGetValue(normalizedUri, out tempParticipant)) { tempParticipant = new Participant(this.RestfulClient, participantResource, this.BaseUri, UriHelper.CreateAbsoluteUri(this.BaseUri, eventcontext.EventEntity.Link.Href), this.Parent as Conversation); m_participantsCache.TryAdd(normalizedUri, tempParticipant); participantChangeEventArgs = new ParticipantChangeEventArgs(); participantChangeEventArgs.AddedParticipants = new List <IParticipant> { tempParticipant }; } else { //Should get some participant added In event , ignore currently } break; } case Rtc.Internal.RestAPI.ResourceModel.EventOperation.Updated: { Participant tempParticipant = null; if (!m_participantsCache.TryGetValue(normalizedUri, out tempParticipant)) { Logger.Instance.Warning("Get a participant updated event for a participant not in cache, any error happened ?"); tempParticipant = new Participant(this.RestfulClient, participantResource, this.BaseUri, UriHelper.CreateAbsoluteUri(this.BaseUri, eventcontext.EventEntity.Link.Href), this.Parent as Conversation); m_participantsCache.TryAdd(normalizedUri, tempParticipant); } tempParticipant.HandleResourceEvent(eventcontext); participantChangeEventArgs = new ParticipantChangeEventArgs(); participantChangeEventArgs.UpdatedParticipants = new List <IParticipant> { tempParticipant }; break; } case Rtc.Internal.RestAPI.ResourceModel.EventOperation.Deleted: { Participant tempParticipant = null; if (m_participantsCache.TryRemove(normalizedUri, out tempParticipant)) { tempParticipant.HandleResourceEvent(eventcontext); participantChangeEventArgs = new ParticipantChangeEventArgs(); participantChangeEventArgs.RemovedParticipants = new List <IParticipant> { tempParticipant }; } break; } } if (participantChangeEventArgs != null) { var conv = this.Parent as Conversation; conv.OnParticipantChange(participantChangeEventArgs); } return(true); } else if (string.Equals(eventcontext.EventEntity.Link.Token, TokenMapper.GetTokenName(typeof(ParticipantMessagingResource))) || string.Equals(eventcontext.EventEntity.Link.Token, TokenMapper.GetTokenName(typeof(ParticipantAudioResource))) || string.Equals(eventcontext.EventEntity.Link.Token, TokenMapper.GetTokenName(typeof(ParticipantApplicationSharingResource)))) { if (eventcontext.EventEntity.In != null) { string normalizedParticipantUri = UriHelper.NormalizeUri(eventcontext.EventEntity.In.Href, this.BaseUri); Participant tempParticipant = null; if (m_participantsCache.TryGetValue(normalizedParticipantUri, out tempParticipant)) { tempParticipant.ProcessAndDispatchEventsToChild(eventcontext); } } return(true); //We do not rely on the response of tempParticipant.ProcessAndDispatchEventsToChild since we already know this is participant modality event } else { return(false); } }