mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 08:26:30 +00:00
62 lines
2.2 KiB
Cython
62 lines
2.2 KiB
Cython
import threading
|
|
from threading import Thread
|
|
import traceback
|
|
from credentials cimport Credentials
|
|
from remote_command cimport RemoteCommand, CommandType
|
|
from remote_command_handler cimport RemoteCommandHandler
|
|
from file_data cimport FileData
|
|
from api_client cimport ApiClient
|
|
|
|
cdef class CommandProcessor:
|
|
cdef RemoteCommandHandler remote_handler
|
|
cdef ApiClient api_client
|
|
cdef bint running
|
|
cdef object shutdown_event
|
|
|
|
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
|
|
|
|
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()
|
|
print('EXIT!')
|
|
|
|
cdef on_command(self, RemoteCommand command):
|
|
try:
|
|
if command.command_type == CommandType.LOGIN:
|
|
self.api_client.set_credentials(Credentials.from_msgpack(command.data))
|
|
elif command.command_type == CommandType.LOAD:
|
|
self.load_file(command)
|
|
elif command.command_type == CommandType.EXIT:
|
|
t = Thread(target=self.stop) # non-block worker:
|
|
t.start()
|
|
else:
|
|
pass
|
|
except Exception as e:
|
|
print(f"Error handling client: {e}")
|
|
|
|
cdef load_file(self, RemoteCommand command):
|
|
cdef RemoteCommand response
|
|
cdef FileData file_data
|
|
cdef bytes file_bytes
|
|
try:
|
|
file_data = FileData.from_msgpack(command.data)
|
|
file_bytes = self.api_client.load_bytes(file_data.filename, file_data.folder)
|
|
response = RemoteCommand(CommandType.DATA_BYTES, file_bytes)
|
|
except Exception as e:
|
|
response = RemoteCommand(CommandType.DATA_BYTES, None, str(e))
|
|
self.remote_handler.send(command.client_id, response.serialize())
|
|
|
|
def stop(self):
|
|
self.shutdown_event.set()
|
|
self.remote_handler.stop()
|
|
self.running = False
|