Fix #12
This is a fix for #17. Eventually I need to rewrite it again because this isn't very well-written.
This commit is contained in:
parent
549cfb10f9
commit
94fa2bfa35
114
server/wss.py
114
server/wss.py
|
@ -6,6 +6,8 @@ import importlib
|
||||||
import os
|
import os
|
||||||
import parse_config
|
import parse_config
|
||||||
import random
|
import random
|
||||||
|
import requests
|
||||||
|
import secrets
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -30,17 +32,54 @@ ph = argon2.PasswordHasher()
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
|
|
||||||
class HCRAServer(tornado.websocket.WebSocketHandler):
|
token = secrets.token_urlsafe(64)
|
||||||
def open(self):
|
|
||||||
|
|
||||||
|
class DisconnectError(BaseException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Client:
|
||||||
|
def __init__(self, conn):
|
||||||
global client
|
global client
|
||||||
if client is not None:
|
if client is not None:
|
||||||
self.write_message('err%*inuse%Server already in use')
|
raise DisconnectError('err%*inuse%Server already in use')
|
||||||
self.close()
|
|
||||||
else:
|
|
||||||
client = self
|
client = self
|
||||||
|
self.conn = conn
|
||||||
self.items = {}
|
self.items = {}
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.good = True
|
self.good = True
|
||||||
|
|
||||||
|
def send(self, item, name):
|
||||||
|
with self.lock:
|
||||||
|
self.items[name] = item
|
||||||
|
|
||||||
|
def ack(self):
|
||||||
|
with self.lock:
|
||||||
|
self.good = False
|
||||||
|
|
||||||
|
def get_item_to_send(self):
|
||||||
|
with self.lock:
|
||||||
|
if self.items:
|
||||||
|
name, content = list(self.items.items())[0]
|
||||||
|
del self.items[name]
|
||||||
|
return name, content
|
||||||
|
elif not self.good:
|
||||||
|
return 'ack', 'ack'
|
||||||
|
else:
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class HCRAServer(tornado.websocket.WebSocketHandler):
|
||||||
|
def open(self):
|
||||||
|
try:
|
||||||
|
self.client = Client(self)
|
||||||
|
except DisconnectError as e:
|
||||||
|
self.write_message(str(e))
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
imgname = imgproc.get_full_img()
|
imgname = imgproc.get_full_img()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -51,47 +90,20 @@ class HCRAServer(tornado.websocket.WebSocketHandler):
|
||||||
img = f.read()
|
img = f.read()
|
||||||
os.unlink(imgname)
|
os.unlink(imgname)
|
||||||
self.write_message(f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}')
|
self.write_message(f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}')
|
||||||
self.ack()
|
self.is_open = True
|
||||||
loop = asyncio.new_event_loop()
|
self.client.ack()
|
||||||
threading.Thread(target=self.run, args=(loop,)).start()
|
|
||||||
|
|
||||||
def send(self, item, name):
|
|
||||||
with self.lock:
|
|
||||||
self.items[name] = item
|
|
||||||
|
|
||||||
def ack(self):
|
|
||||||
with self.lock:
|
|
||||||
self.items['ack'] = 'ack'
|
|
||||||
|
|
||||||
async def cycle(self):
|
|
||||||
with self.lock:
|
|
||||||
while not self.good:
|
|
||||||
pass
|
|
||||||
for name, item in list(self.items.items()):
|
|
||||||
if not self.good:
|
|
||||||
break
|
|
||||||
self.write_message(item)
|
|
||||||
del self.items[name]
|
|
||||||
if item == 'ack':
|
|
||||||
self.good = False
|
|
||||||
|
|
||||||
def run(self, loop):
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
while True:
|
|
||||||
if client is not self:
|
|
||||||
break
|
|
||||||
loop.run_until_complete(self.cycle())
|
|
||||||
|
|
||||||
def on_close(self):
|
def on_close(self):
|
||||||
global client
|
global client
|
||||||
if client is self:
|
if client is self.client:
|
||||||
self.good = None
|
self.client.good = None
|
||||||
client = None
|
client = None
|
||||||
|
self.is_open = False
|
||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
action = message.split(' ', 1)[0]
|
action = message.split(' ', 1)[0]
|
||||||
if action == 'ack':
|
if action == 'ack':
|
||||||
self.good = True
|
self.client.good = True
|
||||||
else:
|
else:
|
||||||
_, password, x, y, w, is_long = message.split(' ')
|
_, password, x, y, w, is_long = message.split(' ')
|
||||||
try:
|
try:
|
||||||
|
@ -99,13 +111,12 @@ class HCRAServer(tornado.websocket.WebSocketHandler):
|
||||||
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
|
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
|
||||||
imgproc.touch(x, y, w, is_long)
|
imgproc.touch(x, y, w, is_long)
|
||||||
except argon2.exceptions.VerifyMismatchError:
|
except argon2.exceptions.VerifyMismatchError:
|
||||||
self.send(f'err%*badpass%Incorrect password', 'BADPASS')
|
self.client.send(f'err%*badpass%Incorrect password', 'BADPASS')
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
def check_origin(self, origin):
|
def check_origin(self, origin):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def cycle():
|
def cycle():
|
||||||
try:
|
try:
|
||||||
changed = imgproc.get_split_imgs()
|
changed = imgproc.get_split_imgs()
|
||||||
|
@ -115,8 +126,11 @@ def cycle():
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
threads = []
|
||||||
for i in changed:
|
for i in changed:
|
||||||
threading.Thread(target=do_img, args=(i,)).start()
|
thread = threading.Thread(target=do_img, args=(i,))
|
||||||
|
threads.append(thread)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
if client is not None:
|
if client is not None:
|
||||||
client.ack()
|
client.ack()
|
||||||
|
@ -145,6 +159,22 @@ def do_cycles():
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Cycler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
if client is not None:
|
||||||
|
name, item = client.get_item_to_send()
|
||||||
|
if item is not None:
|
||||||
|
client.conn.write_message(item)
|
||||||
|
self.write('OK')
|
||||||
|
|
||||||
|
|
||||||
|
def get_token():
|
||||||
|
while True:
|
||||||
|
requests.get('http://localhost:1234/' + token)
|
||||||
|
|
||||||
|
|
||||||
tmp = tempfile.mkdtemp(prefix="HCRA-")
|
tmp = tempfile.mkdtemp(prefix="HCRA-")
|
||||||
try:
|
try:
|
||||||
shutil.copy('crops.json', tmp)
|
shutil.copy('crops.json', tmp)
|
||||||
|
@ -155,8 +185,10 @@ try:
|
||||||
|
|
||||||
application = tornado.web.Application([
|
application = tornado.web.Application([
|
||||||
(r"/", HCRAServer),
|
(r"/", HCRAServer),
|
||||||
|
('/' + token, Cycler),
|
||||||
])
|
])
|
||||||
application.listen(1234)
|
application.listen(1234)
|
||||||
|
threading.Thread(target=get_token).start()
|
||||||
tornado.ioloop.IOLoop.current().start()
|
tornado.ioloop.IOLoop.current().start()
|
||||||
finally:
|
finally:
|
||||||
os.chdir('/')
|
os.chdir('/')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user