Exemplo n.º 1
0
        public void SwitchConnection_Execute()
        {
            // Verify the Execute command.

            var handlerDone = false;
            var gotCommand  = false;

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;
                    SwitchPacket packet;

                    AuthHandshake(serverConnection, false);

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "api status" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);
                    serverConnection.SendResponse("foobar");

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.StartListener(binding, 10);

                SwitchConnection   connection = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                CommandDisposition disposition;

                connection.Connect();
                disposition = connection.Execute("status");

                Assert.IsTrue(disposition.Success);
                Assert.AreEqual("foobar", disposition.ResponseText);

                Helper.WaitFor(() => handlerDone, TimeSpan.FromMilliseconds(5000));

                Assert.IsTrue(gotCommand);
            }
            finally
            {
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Simulates the server side of the authentication handshake.
        /// </summary>
        /// <param name="serverConnection">The server side connection.</param>
        /// <param name="failAuth">Pass <c>true</c> to simulate an authentication failure.</param>
        private void AuthHandshake(SwitchConnection serverConnection, bool failAuth)
        {
            ArgCollection properties = new ArgCollection(ArgCollectionType.Unconstrained);
            SwitchPacket  packet;

            // Receive and respond to the [auth] command.

            packet = serverConnection.ReceivePacket();
            if (packet.CommandText != string.Format("auth {0}", SwitchConnection.DefaultPassword))
            {
                serverConnection.Close();
                return;
            }

            properties["Content-Type"] = "auth/request";
            serverConnection.SendPacket(new SwitchPacket(properties, null));

            // Receive and respond to the [api status] command.

            packet = serverConnection.ReceivePacket();
            if (packet.CommandText != string.Format("api status", SwitchConnection.DefaultPassword))
            {
                serverConnection.Close();
                return;
            }

            serverConnection.SendReply(failAuth ? "-ERR access denied" : "+OK success");

            properties["Content-Type"] = "api/response";
            serverConnection.SendPacket(new SwitchPacket(properties, Helper.ASCIIEncoding.GetBytes("Hello World!")));

            if (failAuth)
            {
                Thread.Sleep(1000);     // Get the client a chance to see the reply.
            }
        }
Exemplo n.º 3
0
        public void SwitchConnection_Subscribe()
        {
            // Verify the Subscribe/Unsubscribe commands.

            var handlerDone = false;
            var gotCommand  = false;

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;
                    SwitchPacket packet;

                    AuthHandshake(serverConnection, false);

                    // First command: subscribe heartbeat

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "event plain SWITCH_EVENT_HEARTBEAT" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);

                    // Second command: subscribe dtmf

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "event plain SWITCH_EVENT_DTMF" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);

                    // Third command: unsubscribe dtmf

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "nixevent SWITCH_EVENT_DTMF" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);

                    // Fourth command: unsubscribe null

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "noevents" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);

                    // Fifth command: unsubscribe <empty set>

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "noevents" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.StartListener(binding, 10);

                SwitchConnection   connection = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                CommandDisposition disposition;

                connection.Connect();
                Assert.IsTrue(connection.EventSubscriptions.IsEmpty);

                disposition = connection.Subscribe(new SwitchEventCodeSet(SwitchEventCode.Heartbeat));
                Assert.IsTrue(disposition.Success);
                Assert.IsTrue(new SwitchEventCodeSet(SwitchEventCode.Heartbeat) == connection.EventSubscriptions);

                disposition = connection.Subscribe(new SwitchEventCodeSet(SwitchEventCode.Dtmf));
                Assert.IsTrue(disposition.Success);
                Assert.IsTrue(new SwitchEventCodeSet(SwitchEventCode.Heartbeat, SwitchEventCode.Dtmf) == connection.EventSubscriptions);

                disposition = connection.Unsubscribe(new SwitchEventCodeSet(SwitchEventCode.Dtmf));
                Assert.IsTrue(disposition.Success);
                Assert.IsTrue(new SwitchEventCodeSet(SwitchEventCode.Heartbeat) == connection.EventSubscriptions);

                disposition = connection.Unsubscribe(null);
                Assert.IsTrue(disposition.Success);
                Assert.IsTrue(connection.EventSubscriptions.IsEmpty);

                disposition = connection.Unsubscribe(SwitchEventCodeSet.Empty);
                Assert.IsTrue(disposition.Success);
                Assert.IsTrue(connection.EventSubscriptions.IsEmpty);

                Helper.WaitFor(() => handlerDone, TimeSpan.FromMilliseconds(5000));

                Assert.IsTrue(gotCommand);
            }
            finally
            {
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }
Exemplo n.º 4
0
        public void SwitchConnection_ExecuteBackground()
        {
            // Verify the ExecuteBackground command.

            var handlerDone = false;
            var gotCommand  = false;
            var jobID       = Guid.NewGuid();

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;
                    SwitchPacket packet;

                    AuthHandshake(serverConnection, false);

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "bgapi status" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null, new NameValue("Job-UUID", jobID.ToString("D")));
                    SendEvent(serverConnection, SwitchEventCode.BackgroundJob, string.Empty, new NameValue("Job-UUID", jobID.ToString("D")));

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.StartListener(binding, 10);

                SwitchConnection   connection      = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                bool               gotJobCompleted = false;
                CommandDisposition disposition;

                connection.JobCompleted +=
                    (s, a) =>
                {
                    gotJobCompleted = a.JobID == jobID;
                };

                connection.Connect();
                disposition = connection.ExecuteBackground("status");

                Assert.IsTrue(disposition.Success);
                Assert.AreEqual(jobID, disposition.JobID);

                Helper.WaitFor(() => handlerDone, TimeSpan.FromMilliseconds(5000));
                Helper.WaitFor(() => gotJobCompleted, TimeSpan.FromMilliseconds(5000));

                Assert.IsTrue(gotCommand);
                Assert.IsTrue(gotJobCompleted);
            }
            finally
            {
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }
Exemplo n.º 5
0
        public void SwitchConnection_SetLogLevel()
        {
            // Verify the SetLogLevel command.

            var handlerDone = false;
            var gotCommand  = false;

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;
                    SwitchPacket packet;

                    AuthHandshake(serverConnection, false);

                    // First command should be: log 5 (notify)

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "log 5" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);
                    serverConnection.SendResponse("log level set");

                    // Second command should be: log 2 (critical)

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "log 2" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);
                    serverConnection.SendResponse("log level set");

                    // Third command should be: nolog

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "nolog" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null);
                    serverConnection.SendResponse("logging disabled");

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.StartListener(binding, 10);

                SwitchConnection   connection = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                CommandDisposition disposition;

                connection.Connect();
                Assert.AreEqual(SwitchLogLevel.None, connection.LogLevel);

                disposition = connection.SetLogLevel(SwitchLogLevel.Notice);
                Assert.IsTrue(disposition.Success);
                Assert.AreEqual("log level set", disposition.ResponseText);
                Assert.AreEqual(SwitchLogLevel.Notice, connection.LogLevel);

                disposition = connection.SetLogLevel(SwitchLogLevel.Critical);
                Assert.IsTrue(disposition.Success);
                Assert.AreEqual("log level set", disposition.ResponseText);
                Assert.AreEqual(SwitchLogLevel.Critical, connection.LogLevel);

                disposition = connection.SetLogLevel(SwitchLogLevel.None);
                Assert.IsTrue(disposition.Success);
                Assert.AreEqual("logging disabled", disposition.ResponseText);
                Assert.AreEqual(SwitchLogLevel.None, connection.LogLevel);

                Helper.WaitFor(() => handlerDone, TimeSpan.FromMilliseconds(5000));

                Assert.IsTrue(gotCommand);
            }
            finally
            {
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }