mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 21:36:30 +00:00
def7aad833
incorrect pass / hw handling in a loader
78 lines
3.7 KiB
Cython
78 lines
3.7 KiB
Cython
import threading
|
|
from threading import Thread
|
|
import traceback
|
|
cimport constants
|
|
from credentials cimport Credentials
|
|
from remote_command cimport RemoteCommand, CommandType
|
|
from remote_command_handler cimport RemoteCommandHandler
|
|
from file_data cimport FileData, UploadFileData, FileList
|
|
from api_client cimport ApiClient
|
|
|
|
cdef class CommandProcessor:
|
|
cdef RemoteCommandHandler remote_handler
|
|
cdef ApiClient api_client
|
|
cdef bint running
|
|
cdef object shutdown_event
|
|
cdef RemoteCommand ok_response
|
|
|
|
def __init__(self, int zmq_port, str api_url):
|
|
self.api_client = ApiClient(api_url)
|
|
self.shutdown_event = threading.Event()
|
|
self.remote_handler = RemoteCommandHandler(zmq_port, self.on_command)
|
|
self.remote_handler.start()
|
|
self.running = True
|
|
self.ok_response = RemoteCommand(CommandType.OK)
|
|
|
|
def start(self):
|
|
while self.running:
|
|
try:
|
|
while not self.shutdown_event.is_set():
|
|
self.shutdown_event.wait(timeout=1.0)
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
constants.log('EXIT!')
|
|
|
|
|
|
cdef on_command(self, RemoteCommand command):
|
|
try:
|
|
if command.command_type == CommandType.EXIT:
|
|
self.remote_handler.send(command.client_id, self.ok_response.serialize())
|
|
t = Thread(target=self.stop) # non-block worker:
|
|
t.start()
|
|
return
|
|
|
|
if command.command_type == CommandType.LOGIN:
|
|
self.api_client.set_credentials(Credentials.from_msgpack(command.data))
|
|
self.remote_handler.send(command.client_id, self.ok_response.serialize())
|
|
elif command.command_type == CommandType.CHECK_RESOURCE:
|
|
self.api_client.check_resource()
|
|
self.remote_handler.send(command.client_id, RemoteCommand(CommandType.OK).serialize())
|
|
elif command.command_type == CommandType.LOAD:
|
|
file_data = FileData.from_msgpack(command.data)
|
|
file_bytes = self.api_client.load_bytes(file_data.filename, file_data.folder)
|
|
self.remote_handler.send(command.client_id, RemoteCommand(CommandType.DATA_BYTES, file_bytes).serialize())
|
|
elif command.command_type == CommandType.LIST_REQUEST:
|
|
search_data = FileData.from_msgpack(command.data)
|
|
list_files = self.api_client.load_bytes(search_data.folder, search_data.filename)
|
|
file_list_bytes = FileList(list_files).serialize()
|
|
self.remote_handler.send(command.client_id, RemoteCommand(CommandType.LIST_FILES, file_list_bytes).serialize())
|
|
elif command.command_type == CommandType.LOAD_BIG_SMALL:
|
|
data = FileData.from_msgpack(command.data)
|
|
file_bytes = self.api_client.load_big_small_resource(data.filename, data.folder)
|
|
self.remote_handler.send(command.client_id, RemoteCommand(CommandType.DATA_BYTES, file_bytes).serialize())
|
|
elif command.command_type == CommandType.UPLOAD_BIG_SMALL:
|
|
data = UploadFileData.from_msgpack(command.data)
|
|
file_bytes = self.api_client.upload_big_small_resource(data.resource, data.filename, data.folder)
|
|
self.remote_handler.send(command.client_id, RemoteCommand(CommandType.OK).serialize())
|
|
else:
|
|
pass
|
|
except Exception as e:
|
|
constants.logerror(f"Error handling client: {e}")
|
|
err_command = RemoteCommand(CommandType.ERROR, None, str(e))
|
|
self.remote_handler.send(command.client_id, err_command.serialize())
|
|
|
|
def stop(self):
|
|
self.shutdown_event.set()
|
|
self.remote_handler.stop()
|
|
self.running = False
|