Commit 8d28ba6e by Owo Sugiana

Penggunaan class Handler

1 parent 4047472c
...@@ -6,7 +6,6 @@ keys = dict() ...@@ -6,7 +6,6 @@ keys = dict()
ERR_KEY = 91 ERR_KEY = 91
ERR_ACTION = 76 ERR_ACTION = 76
ERR_OTHER = 76
class ErrKey(BaseError): class ErrKey(BaseError):
...@@ -19,51 +18,37 @@ class ErrAction(BaseError): ...@@ -19,51 +18,37 @@ class ErrAction(BaseError):
super().__init__(ERR_ACTION, message) super().__init__(ERR_ACTION, message)
def ack(code=0, msg='OK', result=dict()): class Handler:
r = dict(result) def __init__(self, ip):
r['code'] = code self.ip = ip
r['message'] = msg self.client_id = None
return r
def login(self, d: dict):
def ack_from_exception(e: BaseError):
return ack(e.code, e.message)
def ack_other(msg: str):
return ack(ERR_OTHER, message=msg)
def login(d: dict):
if 'key' not in d: if 'key' not in d:
raise ErrKey('Field key tidak ditemukan') raise ErrKey('Field key tidak ditemukan')
if d['key'] not in keys: if d['key'] not in keys:
raise ErrKey('key tidak terdaftar') raise ErrKey('key tidak terdaftar')
client_id = keys[d['key']] self.client_id = keys[d['key']]
return dict(action='login', client_id=client_id, code=0, message='OK') return dict(
action='login', client_id=self.client_id, code=0, message='OK')
def close(self):
def logout(client_id):
pass pass
def save_message(self, method, data):
def log_message(
ip: str, protocol: str, method: str, data: dict, client_id=None):
pass pass
async def parse(self, d: dict):
def parse(d: dict):
action = d.get('action') action = d.get('action')
if not action: if not action:
raise ErrAction('Field action tidak ditemukan') raise ErrAction('Field action tidak ditemukan')
if action == 'echo': if action == 'echo':
return ack() return dict(action='echo', code=0, message='OK')
msg = f'ERROR action {action} tidak dipahami' msg = f'ERROR action {action} tidak dipahami'
raise ErrAction(msg) raise ErrAction(msg)
def get_data(self):
def get_data(): return dict()
pass
def init(conf: dict): def init(conf: dict):
......
...@@ -107,9 +107,9 @@ def main(argv=sys.argv[1:]): ...@@ -107,9 +107,9 @@ def main(argv=sys.argv[1:]):
def log_unknown_error(): def log_unknown_error():
log_info(exception_message(), log.error) log_info(exception_message(), log.error)
def log_message(method: str, d: dict): def save_message(method: str, d: dict):
try: try:
module.log_message(ip, 'websocket', method, d, client_id) handler.save_message(method, d)
except Exception: except Exception:
log_unknown_error() log_unknown_error()
...@@ -119,7 +119,7 @@ def main(argv=sys.argv[1:]): ...@@ -119,7 +119,7 @@ def main(argv=sys.argv[1:]):
async def kirim_pesan(d: dict): async def kirim_pesan(d: dict):
text = json.dumps(d) text = json.dumps(d)
log_message('send', d) save_message('send', d)
d = {'type': 'websocket.send', 'text': text} d = {'type': 'websocket.send', 'text': text}
await kirim(d) await kirim(d)
...@@ -131,7 +131,7 @@ def main(argv=sys.argv[1:]): ...@@ -131,7 +131,7 @@ def main(argv=sys.argv[1:]):
if q['status'] == 'send': if q['status'] == 'send':
await kirim_pesan(q['data']) await kirim_pesan(q['data'])
try: try:
d = module.get_data() d = handler.get_data()
if d: if d:
await kirim_pesan(d) await kirim_pesan(d)
except Exception: except Exception:
...@@ -140,7 +140,7 @@ def main(argv=sys.argv[1:]): ...@@ -140,7 +140,7 @@ def main(argv=sys.argv[1:]):
async def login(dc: dict): async def login(dc: dict):
cid = None cid = None
try: try:
dc = module.login(dc) dc = handler.login(dc)
cid = dc['client_id'] cid = dc['client_id']
if cid in ws_data: if cid in ws_data:
old_mem_id = mem_clients[cid] old_mem_id = mem_clients[cid]
...@@ -164,6 +164,7 @@ def main(argv=sys.argv[1:]): ...@@ -164,6 +164,7 @@ def main(argv=sys.argv[1:]):
ip = scope['client'][0] ip = scope['client'][0]
mem_id = id(scope) mem_id = id(scope)
client_id = None client_id = None
handler = module.Handler(ip)
start_time = time() start_time = time()
while True: while True:
if not client_id and time() - start_time > login_timeout: if not client_id and time() - start_time > login_timeout:
...@@ -187,7 +188,7 @@ def main(argv=sys.argv[1:]): ...@@ -187,7 +188,7 @@ def main(argv=sys.argv[1:]):
text = message.get('text') text = message.get('text')
d = json.loads(text) d = json.loads(text)
log_info(f'Decode JSON {d}') log_info(f'Decode JSON {d}')
log_message('receive', d) save_message('receive', d)
if first: if first:
first = False first = False
client_id = await login(d) client_id = await login(d)
...@@ -195,7 +196,7 @@ def main(argv=sys.argv[1:]): ...@@ -195,7 +196,7 @@ def main(argv=sys.argv[1:]):
break break
else: else:
try: try:
await module.parse(d) await handler.parse(d)
except BaseError as e: except BaseError as e:
log_info(e.message, log.error) log_info(e.message, log.error)
except Exception as e: except Exception as e:
...@@ -204,9 +205,8 @@ def main(argv=sys.argv[1:]): ...@@ -204,9 +205,8 @@ def main(argv=sys.argv[1:]):
if client_id in ws_data: if client_id in ws_data:
del ws_data[client_id] del ws_data[client_id]
break break
if client_id:
try: try:
module.logout(client_id) handler.close()
except Exception: except Exception:
log_unknown_error() log_unknown_error()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!