Unified Communications Development

Michael Greenlee (mgreenlee at claritycon dot com)
in

TransferInitiator class

public class TransferInitiator
{
    private const string APP_USER_AGENT = ...
    private const string LOCALHOST = ...
    private const int PORT = ...
    private const string GRUU = ...
 
    private const string OCS_SERVER = ...
    private const int TLS_PORT = 5061;
 
    private string _mySipUri;
    private string _yourSipUri;
    private string _yourSipUri2;
 
    private CollaborationPlatform _platform;
    private UserEndpoint _endpoint;
 
    private Conversation _conversation;
    private AudioVideoCall _call;
 
    private Conversation _conversation2;
    private AudioVideoCall _call2;
 
    public void Run()
    {
        Console.Write("Enter your SIP URI: ");
        _mySipUri = Console.ReadLine();
 
        Console.Write("Enter another SIP URI: ");
        _yourSipUri = Console.ReadLine();
 
        Console.Write("Enter a third SIP URI: ");
        _yourSipUri2 = Console.ReadLine();
 
        IAsyncResult startupAsyncResult = StartUp();
 
        Console.WriteLine("-----Press enter to shut down.-----");
        Console.ReadLine();
 
        startupAsyncResult.AsyncWaitHandle.WaitOne();
 
        Console.WriteLine("Shutting down...");
 
        IAsyncResult shutdownAsyncResult = Stop();
 
        Console.WriteLine("-----Press enter to exit.-----");
        Console.ReadLine();
 
        shutdownAsyncResult.AsyncWaitHandle.WaitOne();
    }
 
    #region Startup
 
    private IAsyncResult StartUp()
    {
        ServerPlatformSettings settings =
            new ServerPlatformSettings(
                APP_USER_AGENT,
                LOCALHOST,
                PORT,
                GRUU,
                GetLocalCertificate()
            );
 
        _platform = new CollaborationPlatform(settings);
 
        return _platform.BeginStartup(StartupCompleted, null);
    }
 
    private void StartupCompleted(IAsyncResult result)
    {
        _platform.EndStartup(result);
 
        Console.WriteLine("Started up platform.");
 
        UserEndpointSettings endpointSettings =
            new UserEndpointSettings(
                _mySipUri,
                OCS_SERVER,
                TLS_PORT
            );
 
        _endpoint = new UserEndpoint(_platform, endpointSettings);
 
        _endpoint.BeginEstablish(EstablishCompleted, null);
    }
 
    private void EstablishCompleted(IAsyncResult result)
    {
        _endpoint.EndEstablish(result);
 
        Console.WriteLine("Established endpoint.");
 
        ExecuteTransfer();
    }
 
    #endregion
 
    private void ExecuteTransfer()
    {
        // Create a conversation and a call.
        _conversation = new Conversation(_endpoint);
        _call = new AudioVideoCall(_conversation);
 
        _conversation2 = new Conversation(_endpoint);
        _call2 = new AudioVideoCall(_conversation2);
 
        // Establish a call to the first SIP URI.
        _call.BeginEstablish(_yourSipUri, null, CallEstablishCompleted, _call);
        _call2.BeginEstablish(_yourSipUri2, null, CallEstablishCompleted, _call2);
    }
 
    private void CallEstablishCompleted(IAsyncResult result)
    {
        AudioVideoCall call = result.AsyncState as AudioVideoCall;
 
        call.EndEstablish(result);
 
        if (_call.State == CallState.Established && _call2.State == CallState.Established)
        {
            // Execute a supervised transfer
            _call.BeginTransfer(_call2, TransferCompleted, null);
        }
    }
 
    private void TransferCompleted(IAsyncResult result)
    {
        try
        {
            _call.EndTransfer(result);
        }
        catch (OperationFailureException ex)
        {
            Console.WriteLine("Couldn't complete the transfer.");
        }
 
        Console.WriteLine("Transfer initiated.");
    }
 
    #region Shutdown
 
    private IAsyncResult Stop()
    {
        return _endpoint.BeginTerminate(TerminateCompleted, null);
    }
 
    private void TerminateCompleted(IAsyncResult result)
    {
        _endpoint.EndTerminate(result);
 
        Console.WriteLine("Terminated endpoint.");
 
        _platform.BeginShutdown(ShutdownCompleted, null);
    }
 
    private void ShutdownCompleted(IAsyncResult result)
    {
        _platform.EndShutdown(result);
 
        Console.WriteLine("Shut down platform.");
    }
 
    private static X509Certificate2 GetLocalCertificate()
    {
        X509Store store =
            new X509Store(StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);
 
        X509Certificate2Collection certificates =
            store.Certificates;
 
        foreach (X509Certificate2 certificate in certificates)
        {
            if (certificate.SubjectName.Name.Contains(
                Dns.GetHostEntry("localhost").HostName)
                && certificate.HasPrivateKey)
            {
                return certificate;
            }
        }
 
        return null;
    }
 
    #endregion
}

Comments

Unified Communications Development said:

College students, like very small children, are easily entertained. Back in my college days, I always

# April 14, 2009 1:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)