def test_event_plain(self): ev1 = Event(self.EVENT_CONTENT_LENGTH) self.assertEquals(ev1.get_content_length(), 491) self.assertEquals(ev1.get_content_type(), "text/event-plain") ev2 = Event(self.EVENT_PLAIN) self.assertEquals(ev2.get_header("Event-Name"), "RE_SCHEDULE") self.assertEquals(len(self.EVENT_PLAIN), ev1.get_content_length())
def on_channel_execute_complete(self, event): if event['Application'] in self.WAIT_FOR_ACTIONS: # If transfer has begun, put empty event to break current action if event['variable_plivo_transfer_progress'] == 'true': self._action_queue.put(Event()) else: self._action_queue.put(event)
def _protocol_send(self, command, args=""): if self._closing_state: return Event() self.trace("_protocol_send %s %s" % (command, args)) # Append command to pool # and send it to eventsocket _cmd_uuid = str(uuid1()) _async_res = gevent.event.AsyncResult() with self._lock: self._commands_pool.append((_cmd_uuid, _async_res)) self._send("%s %s" % (command, args)) self.trace("_protocol_send %s wait ..." % command) _uuid, event = _async_res.get() if _cmd_uuid != _uuid: raise InternalSyncError("in _protocol_send") # Casts Event to appropriate event type : # Casts to ApiResponse, if event is api if command == 'api': event = ApiResponse.cast(event) # Casts to BgapiResponse, if event is bgapi elif command == "bgapi": event = BgapiResponse.cast(event) # Casts to CommandResponse by default else: event = CommandResponse.cast(event) self.trace("_protocol_send %s done" % command) return event
def _event_plain(self, event): ''' Receives text/event-plain callback. ''' # Gets raw data for this event raw = self.read_raw(event) # If raw was found drops current event # and replaces with Event created from raw if raw: event = Event(raw) # Gets raw response from Event Content-Length header # and raw buffer raw_response = self.read_raw_response(event, raw) # If rawresponse was found, this is our Event body if raw_response: event.set_body(raw_response) # Returns Event return event
def disconnect(self): # Prevent command to be stuck while waiting response try: self._action_queue.put_nowait(Event()) except gevent.queue.Full: pass self.log.debug('Releasing Connection ...') super(PlivoOutboundEventSocket, self).disconnect() self.log.debug('Releasing Connection Done')
def on_channel_hangup_complete(self, event): """ Capture Channel Hangup Complete """ self._hangup_cause = event['Hangup-Cause'] self.log.info('Event: channel %s has hung up (%s)' % (self.get_channel_unique_id(), self._hangup_cause)) self.session_params['HangupCause'] = self._hangup_cause self.session_params['CallStatus'] = 'completed' # Prevent command to be stuck while waiting response self._action_queue.put_nowait(Event())
def wait_for_action(self): """ Wait until an action is over and return action event. """ self.log.debug("wait for action start") try: event = self._action_queue.get(timeout=3600) self.log.debug("wait for action end %s" % str(event)) return event except gevent.queue.Empty: self.log.warn("wait for action end timed out!") return Event()
def _disconnect_notice(self, event): ''' Receives text/disconnect-notice callback. ''' self._closing_state = True # Gets raw data for this event raw = self.read_raw(event) if raw: event = Event(raw) # Gets raw response from Event Content-Length header # and raw buffer raw_response = self.read_raw_response(event, raw) # If rawresponse was found, this is our Event body if raw_response: event.set_body(raw_response) return None
def wait_for_action(self, timeout=3600, raise_on_hangup=False): """ Wait until an action is over and return action event. """ self.log.debug("wait for action start") try: event = self._action_queue.get(timeout=timeout) self.log.debug("wait for action end %s" % str(event)) if raise_on_hangup is True and self.has_hangup(): self.log.warn("wait for action call hung up !") raise RESTHangup() return event except gevent.queue.Empty: if raise_on_hangup is True and self.has_hangup(): self.log.warn("wait for action call hung up !") raise RESTHangup() self.log.warn("wait for action end timed out!") return Event()
def read_event(self): ''' Reads one Event from socket until EOL. Returns Event instance. Raises LimitExceededError if MAXLINES_PER_EVENT is reached. ''' buff = '' for x in range(MAXLINES_PER_EVENT): line = self.transport.read_line() if line == '': self.trace("no more data in read_event !") raise ConnectError("connection closed") elif line == EOL: # When matches EOL, creates Event and returns it. return Event(buff) else: # Else appends line to current buffer. buff = "%s%s" % (buff, line) raise LimitExceededError("max lines per event (%d) reached" % MAXLINES_PER_EVENT)
def test_command_reply(self): ev = Event(self.EVENT_COMMAND_REPLY) self.assertEquals(ev.get_content_type(), "command/reply") self.assertEquals(ev.get_reply_text(), "+OK accepted") self.assertTrue(ev.is_reply_text_success())
def _flush_commands(self): # Flush all commands pending for _cmd_uuid, _async_res in self._commands_pool: _async_res.set((_cmd_uuid, Event()))
event = BgapiResponse.cast(event) # Casts to CommandResponse by default else: event = CommandResponse.cast(event) self.trace("_protocol_send %s done" % command) return event def _protocol_sendmsg(self, name, args=None, uuid="", lock=False, loops=1, async=False): if self._closing_state: return Event() self.trace("_protocol_sendmsg %s" % name) # Append command to pool # and send it to eventsocket _cmd_uuid = str(uuid1()) _async_res = gevent.event.AsyncResult() with self._lock: self._commands_pool.append((_cmd_uuid, _async_res)) self._sendmsg(name, args, uuid, lock, loops, async) self.trace("_protocol_sendmsg %s wait ..." % name) _uuid, event = _async_res.get() if _cmd_uuid != _uuid: raise InternalSyncError("in _protocol_sendmsg") self.trace("_protocol_sendmsg %s done" % name) # Always casts Event to CommandResponse return CommandResponse.cast(event)
def test_auth_request(self): ev = Event(self.EVENT_AUTH_REQUEST) self.assertEquals(ev.get_content_type(), "auth/request")