def handle_line(self): if self.socket: bytes = '' try: bytes = self.socket.recv(4096) except: pass if len(bytes) == 0: self.disconnect() self.buffer += bytes in_lines = self.buffer.split('\n') self.lines.extend([l + '\n' for l in in_lines[:-1]]) self.buffer = in_lines[-1] for line in self.lines: tmp_debug("IO", "%s => O: %s" % (self.uid, deprotect(line))) line = line.strip() # if locked, we are waiting for a result if not self.locked: if line.lower().startswith('action:'): self.action = \ Action(line[line.find(':') + 1:].strip()) elif line == '': if self.action: self.locked = bigtime() self.push(self.action) self.action = None else: if ':' in line: k = line.split(':')[0] v = line[line.find(':') + 1:].strip() if self.action: self.action.add_parameters({k: v}) self.lines = []
class ServerThread(Thread): def __init__(self, octopasty, channel, details): Thread.__init__(self) self.octopasty = octopasty self.socket = channel self.details = details self.action = None self.logged = False self.authtype = None self.multiple_servers = list() self.id = 'unknown_%d' % bigtime() self.locked = 0 self.wants_events = False self.binded_server = None self.keep_flow = False self.buffer = '' self.lines = list() def disconnect(self): if self.socket: try: self.socket.shutdown(socket.SHUT_RDWR) self.socket.close() except: pass if self.id in self.octopasty.clients: self.octopasty.clients.pop(self.id) self.connected = False self.socket = None def run(self): self.connected = True self.socket.sendall("Asterisk Call Manager/1.1\r\n") self.octopasty.clients.update({self.id: self}) def send(self, packet): released_lock = None if self.locked: if packet.locked == self.locked: tmp_debug("IO", "O => %s: %s" % (self.uid, deprotect(packet.packet))) try: self.socket.sendall(str(packet.packet)) except: self.disconnect() return None released_lock = self.locked if not self.keep_flow: self.locked = 0 else: # just discard, may be too old pass else: # on let events go if not packet.locked: tmp_debug("IO", "O => %s %s" % (self.uid, deprotect(packet.packet))) try: self.socket.sendall(str(packet.packet)) except: self.disconnect() else: # humm, why we get that ?? pass return released_lock def handle_line(self): if self.socket: bytes = '' try: bytes = self.socket.recv(4096) except: pass if len(bytes) == 0: self.disconnect() self.buffer += bytes in_lines = self.buffer.split('\n') self.lines.extend([l + '\n' for l in in_lines[:-1]]) self.buffer = in_lines[-1] for line in self.lines: tmp_debug("IO", "%s => O: %s" % (self.uid, deprotect(line))) line = line.strip() # if locked, we are waiting for a result if not self.locked: if line.lower().startswith('action:'): self.action = \ Action(line[line.find(':') + 1:].strip()) elif line == '': if self.action: self.locked = bigtime() self.push(self.action) self.action = None else: if ':' in line: k = line.split(':')[0] v = line[line.find(':') + 1:].strip() if self.action: self.action.add_parameters({k: v}) self.lines = [] def push(self, packet): if packet.name.lower() in STARTING_EVENTS_KEYWORDS: self.keep_flow = True p = dict(emiter=self.id, locked=self.locked, timestamp=time(), packet=packet) tmp_debug("PACKET", "%s (L:%s, KF:%s) >> %s" % (self.uid, self.locked, self.keep_flow, deprotect(str(p)))) self.octopasty.in_queue.put(Packet(p)) def _get_available(self): return self.connected and not self.locked available = property(_get_available) def _get_uid(self): return self.id uid = property(_get_uid)