Commit 4047472c by Owo Sugiana

Koneksi client sebelumnya akan diputus bila ada yang sama

1 parent ba3cd5e4
test-*
*egg-info
__pycache__
*~
*.bak
......@@ -43,6 +43,15 @@ def login(d: dict):
return dict(action='login', client_id=client_id, code=0, message='OK')
def logout(client_id):
pass
def log_message(
ip: str, protocol: str, method: str, data: dict, client_id=None):
pass
def parse(d: dict):
action = d.get('action')
if not action:
......@@ -53,6 +62,10 @@ def parse(d: dict):
raise ErrAction(msg)
def get_data():
pass
def init(conf: dict):
d = str2dict(conf['keys'])
keys.update(d)
import sys
import asyncio
import json
from time import time
from logging import getLogger
from configparser import ConfigParser
import uvicorn
......@@ -81,8 +82,13 @@ def main(argv=sys.argv[1:]):
conf = ConfigParser()
conf.read(conf_file)
cf = dict(conf.items('main'))
login_timeout = int(cf['login_timeout'])
module = get_module_object(cf['module'])
module.init(cf)
# Memory ID didaftar ini akan berakhir
old_clients = []
# Key client_id, Value mem_id
mem_clients = dict()
# Configure a normal WSGI app then wrap it with WSGI -> ASGI class
with Configurator(settings=cf) as config:
wsgi_app = config.make_wsgi_app()
......@@ -91,12 +97,21 @@ def main(argv=sys.argv[1:]):
@app.route('/ws', protocol='websocket')
async def main_websocket(scope, receive, send):
def log_info(msg):
def log_info(msg, func=log.info):
if client_id:
msg = f'{ip} {mem_id} Client {client_id} {msg}'
else:
msg = f'{ip} {mem_id} {msg}'
log.info(msg)
func(msg)
def log_unknown_error():
log_info(exception_message(), log.error)
def log_message(method: str, d: dict):
try:
module.log_message(ip, 'websocket', method, d, client_id)
except Exception:
log_unknown_error()
async def kirim(d: dict):
log_info(f'Send {d}')
......@@ -104,7 +119,9 @@ def main(argv=sys.argv[1:]):
async def kirim_pesan(d: dict):
text = json.dumps(d)
await kirim({'type': 'websocket.send', 'text': text})
log_message('send', d)
d = {'type': 'websocket.send', 'text': text}
await kirim(d)
async def run_queue():
if client_id and client_id in ws_data:
......@@ -113,33 +130,50 @@ def main(argv=sys.argv[1:]):
del ws_data[client_id][0]
if q['status'] == 'send':
await kirim_pesan(q['data'])
async def login(d: dict):
try:
d = module.get_data()
if d:
await kirim_pesan(d)
except Exception:
log_unknown_error()
async def login(dc: dict):
cid = None
try:
d = module.login(d)
client_id = d['client_id']
if client_id in ws_data:
d = dict(
code=91,
message=f'Client {client_id} masih terhubung')
client_id = None
dc = module.login(dc)
cid = dc['client_id']
if cid in ws_data:
old_mem_id = mem_clients[cid]
old_clients.append(old_mem_id)
log_info(
f'Koneksi Client {cid} sebelumnya akan diputus',
log.warning)
else:
ws_data[client_id] = []
ws_data[cid] = []
mem_clients[cid] = mem_id
except BaseError as e:
d = dict(code=e.code, message=e.message)
dc = dict(code=e.code, message=e.message)
except Exception:
msg = exception_message()
log.error(msg)
d = dict(code=91, message='Login gagal')
log_info(f'Encode JSON {d}')
await kirim_pesan(d)
return client_id
log_unknown_error()
dc = dict(code=91, message='Login gagal')
log_info(f'Encode JSON {dc}')
await kirim_pesan(dc)
return cid
first = True
ip = scope['client'][0]
mem_id = id(scope)
client_id = None
start_time = time()
while True:
if not client_id and time() - start_time > login_timeout:
log_info('Login timeout', log.error)
break
if mem_id in old_clients:
log_info('Koneksi berakhir karena sudah ada yang baru')
i = old_clients.index(mem_id)
del old_clients[i]
break
try:
async with asyncio.timeout(2):
message = await receive()
......@@ -153,15 +187,28 @@ def main(argv=sys.argv[1:]):
text = message.get('text')
d = json.loads(text)
log_info(f'Decode JSON {d}')
log_message('receive', d)
if first:
first = False
client_id = await login(d)
if not client_id:
break
else:
try:
await module.parse(d)
except BaseError as e:
log_info(e.message, log.error)
except Exception as e:
log_unknown_error()
elif message['type'] == 'websocket.disconnect':
if client_id in ws_data:
del ws_data[client_id]
break
if client_id:
try:
module.logout(client_id)
except Exception:
log_unknown_error()
log.info(f"Listen {cf['ip']} port {cf['port']}")
uvicorn.run(app, host=cf['ip'], port=int(cf['port']), log_level='info')
......@@ -2,6 +2,7 @@
module = async_web.handlers.default_server
ip = 0.0.0.0
port = 8000
login_timeout = 30
# Data untuk handlers/default_server.py
keys =
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!