Code Listing: Handling Endpoint Events in C#

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following example handles events raised by an endpoint.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.UccApi;
using System.Windows.Forms;

namespace UccApiComponents
{
    /// <summary>
    /// Handling Unified Communications Client API events raised by this endpoint instance.
    /// </summary>
    public partial class UccApiEndpoint : _IUccEndpointEvents
                                        , _IUccServerSignalingSettingsEvents
    {

        private IUccEndpoint m_Endpoint;
        private IUccEndpoint m_TelEndpoint;
        private UccPlatform m_Platform;
        private IEnumerator servers = null;

        /// <summary>
        /// To propagate _IUccEndpointEvents.OnEnable event.
        /// </summary>
        public event EventHandler<EndpointEnabledEventArgs> OnEnabled;

        /// <summary>
        /// To propagate _IUccEndpointOnDisable event.
        /// </summary>
        public event EventHandler<EndpointDisabledEventArgs> OnDisabled;

        /// <summary>
        /// To propagate _IUccServerSignalingSettingsEvents.OnFindServer event.
        /// </summary>
        public event EventHandler<FindServerEventArgs> OnFindServer;


        /// <summary>
        /// Endpoint disable event handler unadvises for endpoint advisement,
        /// de-references IUccEndpoint instance, and shuts down platform.
        /// </summary>
        /// <param name="pEventSource">disabled endpoint instance</param>
        /// <param name="pEventData">operation progress event</param>
        void _IUccEndpointEvents.OnDisable(
            IUccEndpoint pEventSource,
            IUccOperationProgressEvent pEventData
            )
        {
            try
            {
                string eURI = pEventSource.Uri.Value;

                if (pEventData.IsComplete && pEventData.StatusCode >=0)
                {
                    UCC_Unadvise<_IUccEndpointEvents>(pEventSource, this);

                    if (this.m_TelEndpoint != null)
                    {
                        UCC_Unadvise<_IUccEndpointEvents>(this.m_TelEndpoint, this);
                    }

                    // Sign out completed.
                    this.m_Endpoint = null;

                    // Shut down the platform.
                    if (this.m_Platform != null)
                    {
                        this.m_Platform.Shutdown(null);
                        this._platformInitialized = false;
                    }
                }
            }
            catch (COMException e)
            {
                throw e;
            }
        }


        void _IUccEndpointEvents.OnEnable(
            IUccEndpoint pEventSource,
            IUccOperationProgressEvent pEventData)
        {
            try
            {
                if (pEventData.IsComplete )
                {
                    if (pEventData.StatusCode >= 0)
                    {
                        // Sign in succeeded. Proceed with publication and subscription.
                    }
                    else
                    {

                       //
                       // The first server in the server collection returned with OnFindServers 
                       // did not accept the endpoint registration (Enable). The next server in the collection
                       // is tried. 
                       //

                        // Initial Boolean flag for server found?
                        bool nextAvailable = false;

                        // Declare new instance of server signaling settings interface.
                        IUccServerSignalingSettings serverSignalingSettings = null;

                        // If OnFindServer returned collection of servers and IEnumerator for collection
                        // is not null.
                        if (this.servers != null)
                        {
                            // Initialize server signaling settings by casting from endpoint instance.
                            serverSignalingSettings = pEventSource as IUccServerSignalingSettings;

                                    // Get the next server in the servers IEnumerator instance.
                            nextAvailable = this.servers.MoveNext();
                        }

                        // If next server was found.
                        if (nextAvailable)
                        {
                            // Set found server as property value of server signaling settings and enable.
                            serverSignalingSettings.Server = this.servers.Current as IUccSignalingServer;

                            // Enable the endpoint sign in.
                            pEventSource.Enable(null);
                        }
                        else
                        {
                            pEventSource = null;
                        }

                    }

                }
                else
                {
                    // Sign in failed. Make the endpoint null.
                    pEventSource = null;
                }
            }
            catch (COMException ex)
            {
                throw ex;
            }
        }


        void _IUccServerSignalingSettingsEvents.OnFindServer(IUccEndpoint pEventSource, UccFindServerEvent pFindServerEventData)
        {
            IUccServerSignalingSettings serverSignalingSettings = pEventSource as IUccServerSignalingSettings;

            if (pFindServerEventData.IsComplete)
            {
                // Instantiate declared IEnumerator with collection of servers. 
                this.servers = pFindServerEventData.SignalingServers.GetEnumerator();

                // Iterate on collection and set Server property to first server in collection.
                foreach (IUccSignalingServer ss in pFindServerEventData.SignalingServers)
                {
                    serverSignalingSettings.Server = ss;
                    break;
                }
            }

            // Enable the endpoint sign in.
            pEventSource.Enable(null);
        }

}

See Also

Concepts

Code Listing: Programming Endpoint Object in C#