public async Task <Guid> SendBgApiAsync(BgApiCommand bgApiCommand)
        {
            if (!CanSend())
            {
                return(Guid.Empty);
            }
            var handler = (InboundSessionHandler)Channel.Pipeline.Last();

            return(await handler.SendBgApiAsync(bgApiCommand, Channel));
        }
Beispiel #2
0
        public async void SendCommandTest()
        {
            const string address  = "192.168.74.128";
            const string password = "******";
            const int    port     = 8021;

            var client = new OutboundSession(address, port, password);
            await client.ConnectAsync();

            Thread.Sleep(100); // this is due to the asynchronous pattern of the framework

            var cmd   = new BgApiCommand("fsctl", "debug_level 7");
            var reply = await client.SendCommandAsync(cmd);

            Assert.True(reply.IsOk);
        }
Beispiel #3
0
        /// <summary>
        /// Sends a command and receives a response from a ble device.
        /// </summary>
        /// <param name="command">The BgApiCommand to send.</param>
        /// <param name="no_return">True, iff the command has no response (ex. the reset command does not return a response).</param>
        /// <returns>The response from the bgapi device.</returns>
        private BgApiResponse Send(BgApiCommand command, bool no_return)
        {
            try
            {
                // ensure an open connection before attempting to send
                Open();
                // wait for response
                m_waitHandleResponse.Reset();
                m_response = null;
#if NET35
                log("--> " + string.Join(" ", command.Data.Select(x => x.ToString("X2")).ToArray()));
#else
                log("--> " + string.Join(" ", command.Data.Select(x => x.ToString("X2"))));
#endif
                // write command
                m_stream.Write(command.Data, 0, command.Data.Length);
                m_stream.Flush();

                if (no_return)
                {
                    // do not expect a response for this command
                    return(null);
                }

                // what is the maximum wait time for a response
                if (!m_waitHandleResponse.WaitOne(5000))
                {
                    throw new BgApiException("Response timeout");
                }
#if NET35
                log("<-- " + string.Join(" ", m_response.Data.Select(x => x.ToString("X2")).ToArray()));
#else
                log("<-- " + string.Join(" ", m_response.Data.Select(x => x.ToString("X2"))));
#endif
                return(m_response);
            }
            catch (Exception e)
            {
                log(e.Message);
                // do not assume anything about the state of the ble device
                // after an exception
                Close();
                throw;
            }
        }
        /// <summary>
        ///     SendBgApi().
        ///     It is used to send commands to FreeSwitch that will be executed in the background. It will return a UUID.
        /// </summary>
        /// <param name="command">The command to send</param>
        /// <returns>Job ID</returns>
        public async Task <Guid> SendBgApi(BgApiCommand command)
        {
            if (!Connected)
            {
                return(Guid.Empty);
            }
            // Send command
            var reply = await Send(command);

            if (reply == null)
            {
                return(Guid.Empty);
            }
            if (!reply.IsSuccessful)
            {
                return(Guid.Empty);
            }
            var  jobId = reply["Job-UUID"];
            Guid jobUuid;

            return(Guid.TryParse(jobId, out jobUuid) ? jobUuid : Guid.Empty);
        }
        internal async Task <Guid> SendBgApiAsync(BgApiCommand command,
                                                  IChannel context)
        {
            var asyncEvent = new CommandAsyncEvent(command);
            var jobUuid    = Guid.Empty;

            CommandAsyncEvents.Enqueue(asyncEvent);
            await context.WriteAndFlushAsync(command);

            var reply = await asyncEvent.Task as CommandReply;

            if (reply == null)
            {
                return(jobUuid);
            }
            if (reply.IsOk)
            {
                return(Guid.TryParse(reply[EslHeaders.JobUuid],
                                     out jobUuid) ? jobUuid : Guid.Empty);
            }
            return(jobUuid);
        }
Beispiel #6
0
        /// <summary>
        /// Sends a command and receives a response from a ble device.
        /// </summary>
        /// <param name="command">The BgApiCommand to send.</param>
        /// <param name="no_return">True, iff the command has no response (ex. the reset command does not return a response).</param>
        /// <returns>The response from the bgapi device.</returns>
        private BgApiResponse Send(BgApiCommand command, bool no_return)
        {
            try
            {
                // ensure an open connection before attempting to send
                Open();
                // wait for response
                m_waitHandleResponse.Reset();
                m_response = null;

                log("--> " + string.Join(" ", command.Data.Select(x => x.ToString("X2"))));

                // write command
                m_stream.Write(command.Data, 0, command.Data.Length);
                m_stream.Flush();

                if (no_return)
                {
                    // do not expect a response for this command
                    return null;
                }

                // what is the maximum wait time for a response
                if (!m_waitHandleResponse.WaitOne(5000))
                {
                    throw new BgApiException("Response timeout");
                }
                log("<-- " + string.Join(" ", m_response.Data.Select(x => x.ToString("X2"))));
                return m_response;
            }
            catch (Exception e)
            {
                log(e.Message);
                // do not assume anything about the state of the ble device
                // after an exception
                Close();
                throw e;
            }
        }
        private static void Main(string[] args)
        {
            // Let us start the outboud server
            server              = new FreeSwitchServer();
            server.ClientReady += OnClientReady;

            server.Start(IPAddress.Parse("192.168.74.1"), ServerPort);
            Thread.Sleep(500);

            string evenlist = "BACKGROUND_JOB  CHANNEL_PROGRESS  CHANNEL_PROGRESS_MEDIA  CHANNEL_HANGUP_COMPLETE  CHANNEL_STATE SESSION_HEARTBEAT  CALL_UPDATE RECORD_STOP  CHANNEL_BRIDGE  CHANNEL_UNBRIDGE  CHANNEL_ANSWER  CHANNEL_ORIGINATE CHANNEL_EXECUTE  CHANNEL_EXECUTE_COMPLETE";

            client = new OutboundChannelSession(Address, Port, Password, evenlist);
            client.OnChannelProgressMedia += OnChannelProgressMedia;
            client.OnChannelState         += OnChannelState;
            bool connected = Connect().Result;

            if (connected)
            {
                Thread.Sleep(500);

                Log.Info("Connection Status {0}", client.Connected);
                Log.Info("Authentication Status {0}", client.Authenticated);

                var         command  = new ApiCommand("sofia profile external gwlist up");
                ApiResponse response = client.SendApi(command).Result;
                if (response == null)
                {
                    Log.Info("No gateways available");
                }
                else if (string.IsNullOrEmpty(response.Body))
                {
                    Log.Info("No gateways available");
                }
                else
                {
                    List <string> gws = response.Body.Split().ToList();
                    if (gws.Any())
                    {
                        var counter = 1;
                        foreach (var gw in gws)
                        {
                            Log.Info("Gateway no {0} : {1}", counter++, gw);
                        }
                    }
                }

                command  = new ApiCommand("sofia status");
                response = client.SendApi(command).Result;
                Log.Info("sofia status :\n");
                Log.Info("\n\n" + response.Body);

                // Let us make a call and handle it
                string       callCommand  = "{ignore_early_media=false,originate_timeout=120}sofia/gateway/smsghlocalsip/233247063817 &socket(192.168.74.1:10000 async full)";
                BgApiCommand bgapicommand = new BgApiCommand("originate", callCommand);
                Guid         jobId        = client.SendBgApi(bgapicommand).Result;
                Log.Info("Job Id {0}", jobId);

                Thread.Sleep(500);

                // EslLogLevels levels = EslLogLevels.INFO;
                // client.SetLogLevel(levels);

                // client.CloseAsync();

                //Thread.Sleep(500);
                //Log.Info("Connection Status {0}", client.Connected);
                //Log.Info("Authentication Status {0}", client.Authenticated);
            }

            Console.ReadKey();
        }