Ejemplo n.º 1
0
        private static void Client_OnReady(Client client)
        {
            // This is called when the client has established a new session, this is NOT called when a session is restored
            Logger.LogInformation("OnReady");

            // Hook all the callbacks for testing
            client.Calling.OnCallReceived += CallingAPI_OnCallReceived;

            Task.Run(() =>
            {
                // Request that the inbound calls for the given context reach this client
                try { client.Calling.Receive(sCallReceiveContext); }
                catch (Exception exc)
                {
                    Logger.LogError(exc, "CallReceive failed");
                    sCompleted.Set();
                    return;
                }

                // Create the first outbound call leg to the inbound DID associated to the context this client is receiving
                PhoneCall callA = null;
                try
                {
                    callA = client.Calling.NewPhoneCall(sCallToNumber, sCallFromNumber);

                    callA.Begin();
                }
                catch (Exception exc)
                {
                    Logger.LogError(exc, "CallBeginPhone failed");
                    sCompleted.Set();
                    return;
                }

                // Block waiting for a reasonable amount of time for the state to change to answered or ended, this will succeed
                // after answering the call in CallingAPI_OnCallReceived
                if (!callA.WaitForState(TimeSpan.FromSeconds(20), CallState.answered, CallState.ended))
                {
                    Logger.LogError("CallA was not answered or ended in a timely fashion: {0}", callA.State);
                    sCompleted.Set();
                    return;
                }

                // If it's not answered, it ended for some reason
                if (callA.State != CallState.answered)
                {
                    Logger.LogError("CallA was not answered");
                    sCompleted.Set();
                    return;
                }

                // The call was answered, try to connect another outbound call to it
                Call callB = null;
                try
                {
                    // The top level list of the devices represents entries that will be called in serial,
                    // one at a time.  The inner list of devices represents a set of devices to call in
                    // parallel with each other.  Ultimately only one device wins by answering first.
                    callB = callA.Connect(new List <List <CallDevice> >
                    {
                        new List <CallDevice>
                        {
                            new CallDevice
                            {
                                Type       = CallDevice.DeviceType.phone,
                                Parameters = new CallDevice.PhoneParams
                                {
                                    ToNumber   = sCallToNumber,
                                    FromNumber = sCallFromNumber,
                                }
                            }
                        }
                    });
                }
                catch (Exception exc)
                {
                    Logger.LogError(exc, "Connect failed");
                    sCompleted.Set();
                    return;
                }

                // If it was connected then we just hangup both calls
                try
                {
                    callB.Hangup();
                    callA.Hangup();
                }
                catch (Exception exc)
                {
                    Logger.LogError(exc, "Hangup failed");
                    sCompleted.Set();
                    return;
                }

                // Mark the test successful and terminate
                sSuccessful = true;
                sCompleted.Set();
            });
        }