2019-10-12 19:48:19 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Star Inc. multiprocessing data switching
|
|
|
|
===
|
2019-10-14 20:43:06 +08:00
|
|
|
To switch data in multiprocessing.
|
2019-10-12 19:48:19 +08:00
|
|
|
|
|
|
|
LICENSE: MPL 2.0
|
2020-02-07 20:35:26 +08:00
|
|
|
(c)2020 Star Inc.
|
2019-10-12 19:48:19 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Initializing
|
|
|
|
import json
|
2020-02-28 18:37:54 +08:00
|
|
|
import types
|
2020-02-28 18:13:08 +08:00
|
|
|
from abc import ABC
|
2019-10-12 19:48:19 +08:00
|
|
|
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado.web import Application, RequestHandler
|
|
|
|
|
|
|
|
# Works
|
2020-02-28 18:13:08 +08:00
|
|
|
_work = {}
|
|
|
|
auth_code = 0
|
2019-10-12 19:48:19 +08:00
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2020-02-28 18:13:08 +08:00
|
|
|
class IndexHandler(RequestHandler, ABC):
|
2019-10-12 19:48:19 +08:00
|
|
|
def get(self):
|
|
|
|
self.write('''
|
|
|
|
<b>Python MDS Server</b><br>
|
2019-10-14 20:43:06 +08:00
|
|
|
To switch data in multiprocessing.<hr>
|
2020-02-28 18:13:08 +08:00
|
|
|
(c)2020 <a href="https://starinc.xyz">Star Inc.</a>
|
2019-10-12 19:48:19 +08:00
|
|
|
''')
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
global auth_code
|
|
|
|
req_body = self.request.body
|
|
|
|
req_str = req_body.decode('utf8')
|
|
|
|
req_res = json.loads(req_str)
|
|
|
|
if req_res.get("code") == auth_code:
|
2020-02-07 20:35:26 +08:00
|
|
|
result = _work[req_res.get("do")](
|
|
|
|
{
|
2020-02-25 21:02:14 +08:00
|
|
|
"path": req_res.get("path"),
|
|
|
|
"data": req_res.get("data")
|
2020-02-07 20:35:26 +08:00
|
|
|
}
|
|
|
|
)
|
2019-10-12 19:48:19 +08:00
|
|
|
else:
|
2019-12-25 21:35:43 +08:00
|
|
|
result = {"status": 401}
|
2020-02-28 18:37:54 +08:00
|
|
|
if isinstance(result, types.GeneratorType):
|
|
|
|
result = {"status": 200}
|
2019-10-12 19:48:19 +08:00
|
|
|
self.write(json.dumps(result))
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2020-02-28 18:13:08 +08:00
|
|
|
class PythonMDS:
|
|
|
|
switch_data = {}
|
|
|
|
|
|
|
|
# Main
|
|
|
|
app = Application([
|
|
|
|
('/', IndexHandler)
|
|
|
|
])
|
|
|
|
server = HTTPServer(app)
|
|
|
|
async_lock = IOLoop.current()
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
_work["UPT"] = self._update
|
|
|
|
_work["DEL"] = self._delete
|
|
|
|
_work["GET"] = self._query
|
|
|
|
_work["SYC"] = self._sync
|
|
|
|
_work["YLD"] = self._yuuki_limit_decrease
|
2020-02-28 18:37:54 +08:00
|
|
|
_work["EXT"] = self._shutdown
|
2020-02-28 18:13:08 +08:00
|
|
|
|
|
|
|
def _query(self, data):
|
|
|
|
query_data = data["path"]
|
|
|
|
if type(self.switch_data) is dict and type(query_data) is list:
|
|
|
|
result = self.switch_data
|
|
|
|
query_len = len(query_data) - 1
|
|
|
|
for count, key in enumerate(query_data):
|
|
|
|
if key in result:
|
|
|
|
if count < query_len:
|
|
|
|
if type(result.get(key)) is not dict:
|
|
|
|
result = 1 # "unknown_type" + type(source_data.get(key))
|
|
|
|
break
|
|
|
|
result = result.get(key)
|
|
|
|
else:
|
|
|
|
result = 2 # "unknown_key"
|
|
|
|
break
|
|
|
|
|
|
|
|
return {"status": 200, "data": result}
|
|
|
|
return {"status": 400}
|
|
|
|
|
|
|
|
def _update(self, data):
|
|
|
|
if type(data["path"]) is list:
|
|
|
|
over = self._query({"path": data["path"]})
|
|
|
|
over.get("data").update(data["data"])
|
|
|
|
return {"status": 200}
|
|
|
|
return {"status": 400}
|
2020-02-26 21:50:49 +08:00
|
|
|
|
2020-02-28 18:13:08 +08:00
|
|
|
def _delete(self, data):
|
|
|
|
if type(data["path"]) is list:
|
|
|
|
over = self._query({"path": data["path"]})
|
|
|
|
over.get("data").pop(data["data"])
|
|
|
|
return {"status": 200}
|
|
|
|
return {"status": 400}
|
2020-02-26 21:50:49 +08:00
|
|
|
|
2020-02-28 18:13:08 +08:00
|
|
|
def _sync(self, data):
|
|
|
|
self.switch_data = data["path"]
|
|
|
|
return {"status": 200}
|
2020-02-26 21:50:49 +08:00
|
|
|
|
2020-02-28 18:13:08 +08:00
|
|
|
def _yuuki_limit_decrease(self, data):
|
|
|
|
self.switch_data["LimitInfo"][data["path"]][data["userId"]] -= 1
|
|
|
|
return {"status": 200}
|
2020-02-26 21:50:49 +08:00
|
|
|
|
2020-02-28 18:37:54 +08:00
|
|
|
def _shutdown(self, data):
|
2020-02-28 18:13:08 +08:00
|
|
|
if data:
|
|
|
|
pass
|
2020-02-28 18:37:54 +08:00
|
|
|
yield self.__mds_exit()
|
|
|
|
|
|
|
|
async def __mds_exit(self):
|
2020-02-28 18:13:08 +08:00
|
|
|
self.server.stop()
|
|
|
|
self.async_lock.stop()
|
|
|
|
self.async_lock.close()
|
2020-02-28 18:37:54 +08:00
|
|
|
|
|
|
|
def listen(self, code):
|
|
|
|
global auth_code
|
|
|
|
auth_code = code
|
|
|
|
self.server.listen(2019)
|
|
|
|
self.async_lock.start()
|