Ejemplo n.º 1
0
  def run(self):
    ldebug('starting AMI server thread', self)
    
    data = ''
    while self.runthread and self.socket:
      try:
        recv_buffer = self.socket.recv(128)
      except socket.timeout as exception:
        # Socket timeout occured
        continue
      except:
        lerror(format_exc(), self)
        self.runthread = False
        break

      if not recv_buffer:
        ldebug('client connection lost', self)
        break

      data += recv_buffer
      messages = data.split(self.LINE_SEPARATOR * 2)
      data = messages.pop()

      for message_str in messages:
        if not message_str:
          continue
        
        message = to_hash(message_str.split(self.LINE_SEPARATOR))
        self.message_queue.appendleft({'type': 'ami_client_message', 'body': message})

    ldebug('exiting AMI server thread', self)
Ejemplo n.º 2
0
    def process_message(self, content_type, message_head, content_length,
                        message_body):

        if content_type == 'auth/request':
            self.authenticate()
        if content_type == 'command/reply':
            if 'Reply-Text' in message_head:
                ldebug(
                    'FreeSWITCH command reply: %s' %
                    message_head['Reply-Text'], self)
        elif content_type == 'text/event-plain':
            event = to_hash(message_body.split(self.LINE_SEPARATOR))

            if 'Event-Name' in event and event[
                    'Event-Name'] in self.client_queues:
                event_type = event['Event-Name']
                for entry_id, message_pipe in self.client_queues[
                        event_type].items():
                    if type(message_pipe) == deque:
                        if len(message_pipe) < self.MESSAGE_PIPE_MAX_LENGTH:
                            message_pipe.appendleft({
                                'type': 'freeswitch_event',
                                'body': event
                            })
                        else:
                            lwarn("event queue %d full" % entry_id)
                    else:
                        ldebug(
                            "force-deregister event queue %d for event type %s"
                            % (entry_id, event_type), self)
                        del self.client_queues[event_type][entry_id]
Ejemplo n.º 3
0
  def process_message(self, content_type, message_head, content_length, message_body):

    if content_type == 'auth/request':
      self.authenticate()
    if content_type == 'command/reply':
      if 'Reply-Text' in message_head:
        ldebug('FreeSWITCH command reply: %s' % message_head['Reply-Text'], self)
    elif content_type == 'text/event-plain':
      event = to_hash(message_body.split(self.LINE_SEPARATOR))
      
      if 'Event-Name' in event and event['Event-Name'] in self.client_queues:
        event_type = event['Event-Name']
        for entry_id, message_pipe in self.client_queues[event_type].items():
          if type(message_pipe) == deque:
            if len(message_pipe) < self.MESSAGE_PIPE_MAX_LENGTH:
              message_pipe.appendleft({'type': 'freeswitch_event', 'body': event})
            else:
              lwarn("event queue %d full" % entry_id)
          else:
            ldebug("force-deregister event queue %d for event type %s" % (entry_id, event_type), self)
            del self.client_queues[event_type][entry_id]
Ejemplo n.º 4
0
  def run(self):
    ldebug('starting FreeSWITCH event_socket thread', self)

    while self.runthread:
      if not self.connect():
        ldebug('could not connect to FreeSWITCH - retry', self)
        sleep(self.SOCKET_TIMEOUT)
        continue
      ldebug('opening event_socket connection', self)

      data = ''
      while self.runthread and self.fs:
        
        try:
          recv_buffer = self.fs.recv(128)
        except socket.timeout as exception:
          # Socket timeout occured
          continue
        except:
          lerror(format_exc(), self)
          self.runthread = False
          break
        
        if not recv_buffer:
          ldebug('event_socket connection lost', self)
          break

        data += recv_buffer
        messages = data.split(self.LINE_SEPARATOR * 2)
        data = messages.pop()

        for message_str in messages:
          if not message_str:
            continue
          message_body = None
          
          message = to_hash(message_str.split(self.LINE_SEPARATOR))

          if not 'Content-Type' in message:
            ldebug('message without Content-Type', self)
            continue

          if 'Content-Length' in message and int(message['Content-Length']) > 0:
            content_length = int(message['Content-Length'])
            while len(data) < int(message['Content-Length']):
              try:
                data += self.fs.recv(content_length - len(data))
              except socket.timeout as exception:
                ldebug('Socket timeout in message body', self)
                continue
              except:
                lerror(format_exc(), self)
                break
            message_body = data.strip()
            data = ''
          else:
            content_length = 0

          self.process_message(message['Content-Type'], message, content_length, message_body)


      ldebug('closing event_socket connection', self)
      if self.fs:
        self.fs.close()
Ejemplo n.º 5
0
    def run(self):
        ldebug('starting FreeSWITCH event_socket thread', self)

        while self.runthread:
            if not self.connect():
                ldebug('could not connect to FreeSWITCH - retry', self)
                sleep(self.SOCKET_TIMEOUT)
                continue
            ldebug('opening event_socket connection', self)

            data = ''
            while self.runthread and self.fs:

                try:
                    recv_buffer = self.fs.recv(128)
                except socket.timeout as exception:
                    # Socket timeout occured
                    continue
                except:
                    lerror(format_exc(), self)
                    self.runthread = False
                    break

                if not recv_buffer:
                    ldebug('event_socket connection lost', self)
                    break

                data += recv_buffer
                messages = data.split(self.LINE_SEPARATOR * 2)
                data = messages.pop()

                for message_str in messages:
                    if not message_str:
                        continue
                    message_body = None

                    message = to_hash(message_str.split(self.LINE_SEPARATOR))

                    if not 'Content-Type' in message:
                        ldebug('message without Content-Type', self)
                        continue

                    if 'Content-Length' in message and int(
                            message['Content-Length']) > 0:
                        content_length = int(message['Content-Length'])
                        while len(data) < int(message['Content-Length']):
                            try:
                                data += self.fs.recv(content_length -
                                                     len(data))
                            except socket.timeout as exception:
                                ldebug('Socket timeout in message body', self)
                                continue
                            except:
                                lerror(format_exc(), self)
                                break
                        message_body = data.strip()
                        data = ''
                    else:
                        content_length = 0

                    self.process_message(message['Content-Type'], message,
                                         content_length, message_body)

            ldebug('closing event_socket connection', self)
            if self.fs:
                self.fs.close()