Exemplo n.º 1
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();
            }
        }