Example #1
0
        /// <summary>
        /// process conversation bridge participant events
        /// </summary>
        /// <param name="eventcontext"></param>
        /// <returns></returns>
        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(BridgedParticipantResource))))
            {
                if (eventcontext.EventEntity.Relationship == EventOperation.Added)
                {
                    BridgedParticipantResource resource = this.ConvertToPlatformServiceResource <BridgedParticipantResource>(eventcontext);
                    if (resource != null)
                    {
                        BridgedParticipant newBridgedParticipant = new BridgedParticipant(this.RestfulClient, resource, this.BaseUri,
                                                                                          UriHelper.CreateAbsoluteUri(eventcontext.BaseUri, resource.SelfUri), this);
                        TaskCompletionSource <BridgedParticipant> tcs = null;
                        newBridgedParticipant.HandleResourceEvent(eventcontext);
                        m_bridgedParticipants.TryAdd(UriHelper.NormalizeUri(resource.SelfUri, this.BaseUri), newBridgedParticipant);
                        if (m_bridgedParticipantTcses.TryRemove(resource.Uri.ToLower(), out tcs))
                        {
                            tcs.SetResult(newBridgedParticipant);
                        }
                    }
                }

                if (eventcontext.EventEntity.Relationship == EventOperation.Updated)
                {
                    BridgedParticipantResource resource = this.ConvertToPlatformServiceResource <BridgedParticipantResource>(eventcontext);
                    if (resource != null)
                    {
                        BridgedParticipant newBridgedParticipant = null;

                        if (m_bridgedParticipants.TryGetValue(UriHelper.NormalizeUri(resource.SelfUri, this.BaseUri), out newBridgedParticipant))
                        {
                            newBridgedParticipant.HandleResourceEvent(eventcontext);
                        }
                    }
                }

                if (eventcontext.EventEntity.Relationship == EventOperation.Deleted)
                {
                    BridgedParticipant newBridgedParticipant = null;
                    if (m_bridgedParticipants.TryRemove(UriHelper.NormalizeUri(eventcontext.EventEntity.Link.Href, this.BaseUri), out newBridgedParticipant))
                    {
                        newBridgedParticipant.HandleResourceEvent(eventcontext);
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Add bridged participant
        /// </summary>
        /// <param name="logginContext"></param>
        /// <param name="displayName"></param>
        /// <param name="sipUri"></param>
        /// <param name="enableMessageFilter"></param>
        /// <returns>the bridgeParticipant added</returns>
        public async Task <IBridgedParticipant> AddBridgedParticipantAsync(LoggingContext logginContext, string displayName, string sipUri, bool enableMessageFilter)
        {
            if (string.IsNullOrWhiteSpace(sipUri) || sipUri.IndexOf('@') < 0)
            {
                throw new ArgumentException(nameof(sipUri));
            }

            string href = PlatformResource?.BridgedParticipantsResourceLink?.Href;

            if (string.IsNullOrWhiteSpace(href))
            {
                throw new CapabilityNotAvailableException("Link to get BridgedsParticipants is not available.");
            }

            Uri bridgeUri = UriHelper.CreateAbsoluteUri(this.BaseUri, href);

            var input = new BridgedParticipantInput()
            {
                DisplayName        = displayName,
                MessageFilterState = enableMessageFilter ? FilterState.Enabled : FilterState.Disabled,
                Uri = sipUri
            };

            TaskCompletionSource <BridgedParticipant> tcs = new TaskCompletionSource <BridgedParticipant>();

            m_bridgedParticipantTcses.TryAdd(sipUri.ToLower(), tcs);
            //Waiting for bridgedParticipant operation added
            await PostRelatedPlatformResourceAsync(bridgeUri, input, new ResourceJsonMediaTypeFormatter(), logginContext).ConfigureAwait(false);

            BridgedParticipant result = null;

            try
            {
                result = await tcs.Task.TimeoutAfterAsync(WaitForEvents).ConfigureAwait(false);
            }
            catch (TimeoutException)
            {
                throw new RemotePlatformServiceException("Timeout to get bridged participant added from platformservice!");
            }

            if (result == null)
            {
                throw new RemotePlatformServiceException("Platformservice do not deliver a bridged participant added resource with uri " + sipUri);
            }

            return(result);
        }