Пример #1
0
 public static APIResponse Cast(Event evnt)
 {
     APIResponse cls = new APIResponse("");
         cls._headers = evnt._headers;
         cls._raw_body = evnt._raw_body;
     return cls;
 }
Пример #2
0
 private Event _auth_request(Event ev)
 {
     /*
     Receives auth/request callback.
      */
     _response_queue.Add(ev);
     return ev;
 }
Пример #3
0
 private Event _api_response(Event ev)
 {
     /*
     Receives api/response callback.
     Gets raw data for this event.*/
     string raw = read_raw(ev);
        // If raw was found, this is our Event body.
     if (!string.IsNullOrEmpty(raw))
     ev.SetBody(raw);
     // Pushes Event to response events queue and returns Event.
     _response_queue.Add(ev);
     return ev;
 }
Пример #4
0
        public void ON_CALL_UPDATE(Event ev)
        {
            //A Leg from API outbound call answered
            var agbaraFlag = ev.GetHeader("variable_agbara_app");
            //TODO
            //agbaraflag

                var disposition = ev.GetHeader("variable_endpoint_disposition");
                if (disposition == "ANSWER")
                {
                    var CallSid = ev.GetHeader("variable_agbara_callsid");
                    CallElapsedTime[CallSid] = 0;
                }
        }
Пример #5
0
        public static CommandResponse Cast(Event evnt)
        {
            CommandResponse cls = new CommandResponse("");

                cls._headers = evnt._headers;
                cls._raw_body = evnt._raw_body;
            return cls;
        }
Пример #6
0
 public void OnChannelUnabridgeEvent(Event evt)
 {
     // special case to get bleg uuid for Dial
     if (CurrentElement == "Dial")
     {
     this._actionEventQueue.Add(evt);
      }
 }
Пример #7
0
 //In order to "block" the execution of our service until the
 //command is finished, we use a synchronized queue from gevent
 //and wait for such event to come. The on_channel_execute_complete
 //method will put that event in the queue, then we may continue working.
 //However, other events will still come, like for instance, DTMF.
 public void OnChannelExecuteComplete(Event evt)
 {
     string app = evt.GetHeader("Application");
     if  (WAIT_FOR_ACTIONS.Contains(app))
     // If transfer has begun, put empty event to break current action
     if (evt.GetHeader("variable_agbara_transfer_progress") == "true")
     {
         this._actionEventQueue.Add(new Event());
     }
     else
     {
        this._actionEventQueue.Add(evt);
     }
 }
Пример #8
0
 private Event _event_json(Event ev)
 {
     /*
     Receives text/event-json callback.
     Gets json data for this event */
     JsonEvent jsonev;
     Event retev = new Event();
     string json_data = read_raw(ev);
     //If raw was found drops current event and replaces with JsonEvent created from json_data
     if (!string.IsNullOrEmpty(json_data))
     {
     jsonev = new JsonEvent(json_data);
     retev = (Event)jsonev;
     }
     return retev;
 }
Пример #9
0
        private void SetHangUpComplete(string CallSid, string call_uuid, string reason, Event ev, string hangup_url)
        {
            long answer_seconds_since_epoch = long.Parse(ev.GetHeader("Caller-Channel-Answered-Time"));
            long hangup_seconds_since_epoch = long.Parse(ev.GetHeader("Caller-Channel-Hangup-Time"));
            string called_num = ev.GetHeader("Caller-Destination-Number");
                string caller_num = ev.GetHeader("Caller-Caller-ID-Number");
            string direction = ev.GetHeader("variable_agbara_call_direction");
            //get call details
            Call call = callSrvc.GetCallDetail(CallSid);
            call.Status = CallStatus.completed;
            call.CallerId = caller_num;
            call.CallTo = called_num;
            call.DateUpdated = DateTime.Now;
            call.StartTime = EpochTimeConverter.ConvertFromEpochTime(answer_seconds_since_epoch);
            call.EndTime = EpochTimeConverter.ConvertFromEpochTime(hangup_seconds_since_epoch);
            call.Duration = EpochTimeConverter.GetEpochTimeDifferent(hangup_seconds_since_epoch, answer_seconds_since_epoch);
            call.Direction = direction;

             try
            {
                CallRequest.Remove(CallSid);
                CallElapsedTime.Remove(CallSid);
            }
            catch (Exception ex)
            {
            }

             try
             {
                 callSrvc.UpdateCallLog(call);
             }
             catch (Exception ex)
             {
             }
        }
Пример #10
0
        public void ON_SESSION_HEARTBEAT(Event ev)
        {
            //TODO
            //store in db

            long answer_seconds_since_epoch = long.Parse(ev.GetHeader("Caller-Channel-Answered-Time"));
            long heartbeat_seconds_since_epoch = long.Parse(ev.GetHeader("Event-Date-Timestamp"));
            int ElapsedTime = EpochTimeConverter.GetEpochTimeDifferent(heartbeat_seconds_since_epoch, answer_seconds_since_epoch);
            var BCallSid = ev.GetHeader("variable_agbara_bleg_callsid");
            var ACallSid = ev.GetHeader("variable_agbara_callsid");
            if (!string.IsNullOrEmpty(BCallSid))
            {
                int current = CallElapsedTime[BCallSid];
                CallElapsedTime[BCallSid] = current + ElapsedTime;
            }
            else if (!string.IsNullOrEmpty(ACallSid))
            {
                int current = CallElapsedTime[ACallSid];
                CallElapsedTime[ACallSid] = current + ElapsedTime;
            }

            //TODO
            //bill user real time here
        }
Пример #11
0
 public void ON_CUSTOM(Event ev)
 {
 }
Пример #12
0
 public void ON_CHANNEL_PROGRESS_MEDIA(Event ev)
 {
     Request call_req;
     string CallSid = ev.GetHeader("variable_agbara_CallSid");
     string direction = ev.GetHeader("Call-Direction");
     //Detect early media state
     if (!string.IsNullOrEmpty(CallSid) && direction == "outbound")
     {
         try
         {
             call_req = CallRequest[CallSid];
         }
         catch (Exception ex)
         {
             return;
         }
         // only send if not already ringing/early state
         if (string.IsNullOrEmpty(call_req.state_flag))
             //# set state flag to true
             call_req.state_flag = "EarlyMedia";
         // clear gateways to avoid retry
         call_req.gateways.Clear();
         string called_num = ev.GetHeader("Caller-Destination-Number");
         string caller_num = ev.GetHeader("Caller-Caller-ID-Number");
         // send ring if ring_url found
         string ring_url = call_req.ring_url;
         if (!string.IsNullOrEmpty(ring_url))
         {
             SortedList param = new SortedList();
             param.Add("RequestUUID", CallSid);
             param.Add("Direction", direction);
             param.Add("To", called_num);
             param.Add("CallStatus", "ringing");
             param.Add("From", caller_num);
             string response = Task<string>.Factory.StartNew(() => SendToUrl(ring_url, param, string.Empty)).Result;
         }
     }
 }
Пример #13
0
 public void ON_CHANNEL_HANGUP_COMPLETE(Event ev)
 {
     Request call_req;
     string CallSid = ev.GetHeader("variable_agbara_callsid");
     string direction = ev.GetHeader("Call-Direction");
     if (!string.IsNullOrEmpty(CallSid) && direction != "outbound")
         return;
     string call_uuid = ev.GetHeader("Unique-ID");
     string reason = ev.GetHeader("Hangup-Cause");
     try
     {
         call_req = CallRequest[CallSid];
     }
     catch (Exception ex)
     {
         return;
     }
     // If there are gateways to try again, spawn originate
     if (call_req.gateways.Count > 0)
     {
         Originate(CallSid);
         return;
     }
     else // Else clean call request
     {
         string hangup_url = call_req.hangup_url;
         SetHangUpComplete(CallSid, call_uuid, reason, ev, hangup_url);
     }
 }
Пример #14
0
 public void ON_CHANNEL_BRIDGE(Event ev)
 {
     var disposition = ev.GetHeader("variable_endpoint_disposition");
     if (disposition == "ANSWER")
     {
         var CallSid = ev.GetHeader("variable_agbara_callsid");
         CallElapsedTime[CallSid] = 0;
         Call call = new Call();
         //get call B Sid
         call.Sid = ev.GetHeader("variable_agbara_bleg_callsid");
         CallElapsedTime[call.Sid] = 0;
         call.Direction = CallDirection.outbounddial;
         call.Status = CallStatus.inprogress;
         try
         {
             callSrvc.AddCallLog(call);
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
Пример #15
0
 public void ON_CHANNEL_ANSWER(Event ev)
 {
     //Set up call elapsed
     var CallSid = ev.GetHeader("variable_agbara_callsid");
     CallElapsedTime[CallSid] = 0;
 }
Пример #16
0
 private Event _command_reply(Event ev)
 {
     /*
     Receives command/reply callback.
     Pushes Event to response events queue and returns Event.*/
     _response_queue.Add(ev);
     return ev;
 }
Пример #17
0
 private Event _disconnect_notice(Event ev)
 {
     /*
        Receives text/disconnect-notice callback.
        */
     _closing_state = true;
     return ev;
 }
Пример #18
0
        public override Event ProtocolSend(string commands, string args = "")
        {
            Event ev = new Event();
            if (_closing_state)
            return ev;

            lock (objlock)
            {
            if (_response_queue.Count != 0)
            {
                _response_queue.Take();
            }
            _send(string.Format("{0} {1}", commands, args));
            ev = _response_queue.Take();

            }

            // Casts Event to appropriate event type :
            // Casts to ApiResponse, if event is api
            if (commands == "api")
            {
            ev = APIResponse.Cast(ev);
            }
            //Casts to BgapiResponse, if event is bgapi
            else if (commands == "bgapi")
            {
             ev = BgApiResponse.Cast(ev);
            }
            //Casts to CommandResponse by default
            else
            {
            ev = CommandResponse.Cast(ev);
            }
            return ev;
        }
Пример #19
0
 private Event _event_plain(Event ev)
 {
     /*
     Receives text/event-plain callback.
     Gets raw data for this event*/
     string raw = read_raw(ev);
     // If raw was found drops current event and replaces with Event created from raw
     if (!string.IsNullOrEmpty(raw))
     ev = new Event(raw);
     // Gets raw response from Event Content-Length header and raw buffer
     string raw_response = read_raw_response(ev, raw);
     //If rawresponse was found, this is our Event body
     if(!string.IsNullOrEmpty(raw_response))
         ev.SetBody(raw_response);
        // Returns Event
     return ev;
 }
Пример #20
0
 public override Event ProtocolSendMsg(string name, string args, string uuid = "", bool Lock = false, int loop = 1, bool async=false)
 {
     Event ev = new Event();
     if (_closing_state)
     return ev;
     lock (objlock)
     {
     if (_response_queue.Count != 0)
     {
         _response_queue.Take();
     }
     _sendmsg(name, args, uuid, Lock, loop, async);
      ev = _response_queue.Take();
     }
        // _resetEvent.Reset();
       ev = CommandResponse.Cast(ev);
     // Always casts Event to CommandResponse
     return ev;
 }
Пример #21
0
 public void OnChannelHangupCompleteEvent(Event evt)
 {
     //   Capture Channel Hangup Complete
     this._hangup_cause = evt.GetHeader("Hangup-Cause");
     this.session_params["HangupCause"] = this._hangup_cause;
     this.session_params["CallStatus"] = CallStatus.completed;
     // Prevent command to be stuck while waiting response
     this._actionEventQueue.Add(new Event());
 }
Пример #22
0
 public string read_raw(Event ev)
 {
     /*
     Reads raw data based on Event Content-Length.
     Returns raw string or None if not found.
     */
     int length = ev.GetContentLength();
     //# Reads length bytes if length > 0
     if (length >0)
     return transport.read(length);
     return string.Empty;
 }
Пример #23
0
        public void OnCustomEvent(Event evt)
        {
            Console.WriteLine("Subclass {0}",evt.GetHeader("Event-Subclass"));
            Console.WriteLine("Action Performed {0}", evt.GetHeader("Action"));
            Console.WriteLine("Member Id {0}", evt.GetHeader("Member-ID"));
            // case conference event
            if(CurrentElement == "Conference")
            {
            //special case to get Member-ID for Conference
            if (evt.GetHeader("Event-Subclass") == "conference::maintenance" && evt.GetHeader("Action") == "add-member" && evt.GetHeader("Unique-ID") == channel_uuid)
            {
                Console.WriteLine("Subclass {0}", evt.GetHeader("Event-Subclass"));
                Console.WriteLine("Action Performed {0}", evt.GetHeader("Action"));
                Console.WriteLine("Member Id {0}", evt.GetHeader("Member-ID"));
               // this._actionEventQueue.Add(evt);
            }
            // special case for hangupOnStar in Conference
            else if(evt.GetHeader("Event-Subclass") == "conference::maintenance" && evt.GetHeader("Action") == "kick" && evt.GetHeader("Unique-ID") == channel_uuid)
            {
              string  room = evt.GetHeader("Conference-Name");
               string member_id = evt.GetHeader("Member-ID");
                if (!string.IsNullOrEmpty(room)&& !string.IsNullOrEmpty(member_id))
                {
                    BgAPICommand(string.Format("conference {0} kick {1}",room, member_id));
                }
            }
            // special case to send callback for Conference
            else if(evt.GetHeader("Event-Subclass") == "conference::maintenance" && evt.GetHeader("Action") == "digits-match" && evt.GetHeader("Unique-ID") == channel_uuid)
            {

                string digits_action = evt.GetHeader("Callback-Url");
                string digits_method = evt.GetHeader("Callback-Method");
                if (!string.IsNullOrEmpty(digits_action)&& !string.IsNullOrEmpty(digits_method))
                {
                    SortedList param = new SortedList();
                    param["ConferenceMemberID"] = evt.GetHeader("Member-ID");
                    param["ConferenceUUID"] = evt.GetHeader("Conference-Unique-ID");
                    param["ConferenceName"] = evt.GetHeader("Conference-Name");
                    param["ConferenceDigitsMatch"] = evt.GetHeader("Digits-Match");
                    param["ConferenceAction"] = "digits";
                    Task.Factory.StartNew(() => this.SendToUrl(digits_action, param, digits_method));
                }
            }
            }
            // case dial event
            else if(CurrentElement == "Dial")
            {
            if (evt.GetHeader("Event-Subclass") == "agbara::dial" && evt.GetHeader("Action") == "digits-match" && evt.GetHeader("Unique-ID") == channel_uuid)
            {
                string digits_action = evt.GetHeader("Callback-Url");
                string digits_method = evt.GetHeader("Callback-Method");
                if (!string.IsNullOrEmpty(digits_action)&& !string.IsNullOrEmpty(digits_method))
                {
                    SortedList param = new SortedList();
                    param["DialDigitsMatch"] = evt.GetHeader("Digits-Match");
                    param["DialAction"] = "digits";
                    param["DialALegUUID"] = evt.GetHeader("Unique-ID");
                    param["DialBLegUUID"] = evt.GetHeader("variable_bridge_uuid");
                    Task.Factory.StartNew(() => this.SendToUrl(digits_action, param, digits_method));
                }
            }
            }
        }
Пример #24
0
 public string read_raw_response(Event ev,string raw)
 {
     /*
     Extracts raw response from raw buffer and length based on Event Content-Length.
     Returns raw string or None if not found.
     */
     int length = ev.GetContentLength();
     if (length > 0 && !string.IsNullOrEmpty(raw))
     return raw.Substring(raw.Length - length, length);
     return raw;
 }
Пример #25
0
 private void dispatch_event(Event ev)
 {
     EventHandlers handler;
     if (null != (handler = (EventHandlers)event_handlers[ev.EventName]))
     {
     handler(ev);
     }
 }
Пример #26
0
        public void ON_BACKGROUND_JOB(Event ev)
        {
            Request call_req = new Request(null, null, null, null, null);

            string status = string.Empty;
            string reason = string.Empty;
            //Capture Job Event
            // Capture background job only for originate and ignore all other jobs
            string job_cmd = ev.GetHeader("Job-Command");
            string job_uuid = ev.GetHeader("Job-UUID");
            if (job_cmd == "originate" && string.IsNullOrEmpty(job_uuid))
            {
                try
                {
                    string data = ev.GetBody();
                    int pos = data.IndexOf(' ');
                    if (pos != -1)
                    {
                        status = data.Substring(0, pos).Trim();
                        reason = data.Substring(pos + 1).Trim();
                    }
                }
                catch (Exception ex)
                {
                    return;
                }
                string CallSid = BackgroundJobs[job_uuid];
                if (string.IsNullOrEmpty(CallSid))
                {
                    return;
                }
                try
                {
                    call_req = CallRequest[CallSid];
                }
                catch (Exception ex)
                {
                    return;
                }
                //Handle failure case of originate
                //This case does not raise a on_channel_hangup event.
                //All other failures will be captured by on_channel_hangup
                if (!status.Contains("OK"))
                    //In case ring/early state done, just warn releasing call request will be done in hangup event
                    if (call_req.state_flag == "Ringing" || call_req.state_flag == "EarlyMedia")
                    {
                        return;
                    }
                    //If no more gateways, release call request
                    else if (call_req.gateways.Count == 0)
                    {
                        //set an empty call_uuid
                        string call_uuid = "";
                        string hangup_url = call_req.hangup_url;
                        SetHangUpComplete(CallSid, call_uuid, reason, ev, hangup_url);
                        return;
                    }
                    //If there are gateways and call request state_flag is not set
                    //try again a call
                    else if (call_req.gateways.Count > 0)
                        Originate(CallSid);

            }
        }