Minor code tidying

This commit is contained in:
Torbjörn Axelsson 2017-12-18 12:58:51 -08:00
parent a012cd2ee4
commit c764c20ae7
2 changed files with 12 additions and 17 deletions

View file

@ -37,7 +37,7 @@ class RequestHandler(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.wfile.write(body) self.wfile.write(body)
except Exception as e: except Exception as e:
logging.error(e) logging.error('ConfServer: {}'.format(e))
class HTTPServerThread(HTTPServer, Thread): class HTTPServerThread(HTTPServer, Thread):
@ -55,7 +55,7 @@ class HTTPServerThread(HTTPServer, Thread):
while not self.exit_flag: while not self.exit_flag:
self.handle_request() self.handle_request()
except Exception as e: except Exception as e:
logging.error(e) logging.error('ConfServer: {}'.format(e))
def disconnect(self): def disconnect(self):
self.exit_flag = True self.exit_flag = True
# make a connection to # make a connection to
@ -79,7 +79,7 @@ class ConfServer():
except KeyboardInterrupt: except KeyboardInterrupt:
self.disconnect() self.disconnect()
except Exception as e: except Exception as e:
logging.error(e) logging.error('ConfServer: {}'.format(e))
def disconnect(self): def disconnect(self):
logging.info('ConfServer: shutting down...') logging.info('ConfServer: shutting down...')
self.server.disconnect() self.server.disconnect()

View file

@ -31,7 +31,7 @@ class XMPPServer():
self.clients.append(client) self.clients.append(client)
self.socket.close() self.socket.close()
except Exception as e: except Exception as e:
logging.error('e: ' + e) logging.error('XMPPServer: {}'.format(e))
except KeyboardInterrupt: except KeyboardInterrupt:
logging.debug('XMPPServer: Keyboard interrupt') logging.debug('XMPPServer: Keyboard interrupt')
finally: finally:
@ -76,14 +76,14 @@ class Client(threading.Thread):
self._set_state('DISCONNECT') self._set_state('DISCONNECT')
def _tag_strip_uri(self, tag): def _tag_strip_uri(self, tag):
if tag[0] == "{": if tag[0] == '{':
uri, ignore, tag = tag[1:].partition("}") uri, ignore, tag = tag[1:].partition('}')
return tag return tag
def _set_state(self, state): def _set_state(self, state):
new_state = getattr(Client, state) new_state = getattr(Client, state)
if self.state > new_state: if self.state > new_state:
raise Exception("{} illegal state change {}->{}".format(self.address, self.state, new_state)) raise Exception('{} illegal state change {}->{}'.format(self.address, self.state, new_state))
logging.info('{} state: {}'.format(self.address, state)) logging.info('{} state: {}'.format(self.address, state))
self.state = new_state self.state = new_state
@ -108,7 +108,6 @@ class Client(threading.Thread):
# forward # forward
for client in XMPPServer.clients: for client in XMPPServer.clients:
if client.address != self.address and client.state == client.READY: if client.address != self.address and client.state == client.READY:
logging.debug('sending result: ' + data.decode('utf-8'))
client.send(data.decode('utf-8')) client.send(data.decode('utf-8'))
def run(self): def run(self):
@ -134,17 +133,13 @@ class Client(threading.Thread):
child = self._tag_strip_uri(xml[0].tag) child = self._tag_strip_uri(xml[0].tag)
else: else:
child = None child = None
if xml.get('id'):
last_id = xml.get('id')
else:
last_id = '0'
if xml.tag == 'iq': if xml.tag == 'iq':
res = None res = None
if child == 'bind': if child == 'bind':
res = '<iq type="result" id="{}"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>{}</jid></bind></iq>'.format(last_id, XMPPServer.bot_id) res = '<iq type="result" id="{}"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>{}</jid></bind></iq>'.format(xml.get('id'), XMPPServer.bot_id)
self._set_state('BIND') self._set_state('BIND')
elif child == 'session': elif child == 'session':
res = '<iq type="result" id="{}" />'.format(last_id) res = '<iq type="result" id="{}" />'.format(xml.get('id'))
self._set_state('READY') self._set_state('READY')
elif child == 'query': elif child == 'query':
self._handle_ctl(xml, data) self._handle_ctl(xml, data)
@ -166,12 +161,12 @@ class Client(threading.Thread):
self.type = self.CONTROLLER self.type = self.CONTROLLER
logging.info('{} type set to CONTROLLER (based on presence tag)'.format(self.address)) logging.info('{} type set to CONTROLLER (based on presence tag)'.format(self.address))
except ET.ParseError as e: except ET.ParseError as e:
logging.debug("error: {}".format(e)) logging.debug('error: {}'.format(e))
except Exception as e: except Exception as e:
logging.error(e) logging.error('XMPPServer: {}'.format(e))
self._set_state('DISCONNECT') self._set_state('DISCONNECT')
except Exception as e: except Exception as e:
logging.error(e) logging.error('XMPPServer: {}'.format(e))
self._set_state('DISCONNECT') self._set_state('DISCONNECT')
finally: finally:
self.disconnect() self.disconnect()