private void StartupUserEndpoint(AsyncTask task, object state) { AsyncTask proxyTask = new AsyncTask(m_customerSession.AppFrontEnd.CreateOrGetUserEndpoint, m_customerSession.Customer.UserUri); proxyTask.TaskCompleted += delegate(object sender, AsyncTaskCompletedEventArgs e) { UserEndpointCreationActionResult result = proxyTask.TaskResult as UserEndpointCreationActionResult; if (result != null) { m_myUserEndpoint = result.MyUserEndpoint; m_contactGroupServices = m_myUserEndpoint.UserEndpoint.ContactGroupServices; } task.Complete(e.ActionResult.Exception); }; proxyTask.StartTask(); }
/// <summary> /// Releases the user endpoint from usage from a component. /// </summary> /// <param name="task">The task to be done.</param> /// <param name="state">The user endpoint to release.</param> /// <remarks>If ref count is reduced to 0, the userendpoint will be terminated.</remarks> public void RelaseUserEndpoint(AsyncTask task, object state) { MyUserEndpoint myUserEndpoint = state as MyUserEndpoint; RealTimeAddress ownerAddress = new RealTimeAddress(myUserEndpoint.UserEndpoint.OwnerUri); bool completeNeeded = true; lock (this.SyncRoot) { if (m_userEndpoints.ContainsKey(ownerAddress)) { MyUserEndpoint storedEndpoint = m_userEndpoints[ownerAddress]; if (storedEndpoint == myUserEndpoint) // What we have matches the released endpoint. Reduce ref count. { int count = m_userEndpointReferenceCounts[ownerAddress]; count--; m_userEndpointReferenceCounts[ownerAddress] = count; if (count == 0) { // Terminate the endpoint. AsyncTask terminateEndpointTask = new AsyncTask(this.ShutdownUserEndpoint, myUserEndpoint.UserEndpoint); terminateEndpointTask.TaskCompleted += delegate(object sender, AsyncTaskCompletedEventArgs e) { task.Complete(e.ActionResult.Exception); }; terminateEndpointTask.StartTask(); completeNeeded = false; } } } } if (completeNeeded) { task.Complete(null); } }
public UserEndpointCreationActionResult(MyUserEndpoint myUserEndpoint, Exception exception) : base(exception) { m_myUserEndpoint = myUserEndpoint; }
public void CreateOrGetUserEndpoint(AsyncTask task, object state) { string ownerUri = state as string; if (String.IsNullOrEmpty(ownerUri)) { task.Complete(new InvalidOperationException("OwnerUri is needed to request a user endpoint.")); return; } RealTimeAddress ownerAddress = null; try { ownerAddress = new RealTimeAddress(ownerUri); } catch (ArgumentException exception) { task.Complete(exception); return; } MyUserEndpoint myUserEndpoint = null; lock (this.SyncRoot) { if (m_userEndpoints.ContainsKey(ownerAddress)) { myUserEndpoint = m_userEndpoints[ownerAddress]; if (myUserEndpoint.UserEndpoint.State == LocalEndpointState.Terminating || myUserEndpoint.UserEndpoint.State == LocalEndpointState.Terminated) { myUserEndpoint = null; // Loose it since it is going away. m_userEndpoints.Remove(ownerAddress); m_userEndpointReferenceCounts.Remove(ownerAddress); } else { int count = m_userEndpointReferenceCounts[ownerAddress]; count++; m_userEndpointReferenceCounts[ownerAddress] = count; } } if (myUserEndpoint == null) { // Create and add user endpoint into dictionary. // One could use the platform discover server from uri if the topology has DNS srv records for server auto discovery. // Normally, this would point to a director. Here, we will use the proxy of the application endpoint. UserEndpointSettings userEndpointSettings = new UserEndpointSettings(ownerUri, m_settings.ProxyHost, m_settings.ProxyPort); UserEndpoint userEndpoint = new UserEndpoint(m_parent.Platform, userEndpointSettings); myUserEndpoint = new MyUserEndpoint(userEndpoint); m_userEndpoints.Add(ownerAddress, myUserEndpoint); m_userEndpointReferenceCounts.Add(ownerAddress, 1); myUserEndpoint.UserEndpoint.StateChanged += UserEndpointStateChanged; // Ensures that only one registration per endpoint. } UserEndpointCreationActionResult result = new UserEndpointCreationActionResult(myUserEndpoint, null); task.TaskResult = result; // Store it for now. if (myUserEndpoint.UserEndpoint.State == LocalEndpointState.Established) { task.Complete(null, result); } else if (myUserEndpoint.UserEndpoint.State == LocalEndpointState.Establishing) { // Wait till the endpoint establish completes. lock (this.SyncRoot) { m_pendingUserEndpointCreationTasks.Add(task); } } else if (myUserEndpoint.UserEndpoint.State == LocalEndpointState.Idle) { AsyncTask establishTask = new AsyncTask(this.StartupUserEndpoint, myUserEndpoint.UserEndpoint); establishTask.TaskCompleted += delegate(object sender, AsyncTaskCompletedEventArgs e) { task.TaskResult.Exception = e.ActionResult.Exception; // Transfer task.Complete(e.ActionResult.Exception, task.TaskResult); lock (this.SyncRoot) { // Complete pending tasks foreach (AsyncTask pendingTask in m_pendingUserEndpointCreationTasks) { pendingTask.TaskResult.Exception = e.ActionResult.Exception; pendingTask.Complete(e.ActionResult.Exception, pendingTask.TaskResult); } m_pendingUserEndpointCreationTasks.Clear(); } }; establishTask.StartTask(); } } }