it works
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import struct
|
||||
from enum import IntEnum, Enum, auto
|
||||
from dataclasses import dataclass
|
||||
from typing import Tuple, Optional
|
||||
from typing import Callable
|
||||
from pipe_operator import PipeOperator
|
||||
|
||||
PIPE_NAME: str = "ed0e3f1f-d214-4880-9562-640bce15e72e"
|
||||
|
||||
|
||||
class ProtocolCode(IntEnum):
|
||||
HANDSHAKE_REQUEST = 0x61 # Trainer -> Presenter
|
||||
HANDSHAKE_RESPONSE = 0x62 # Presenter -> Trainer
|
||||
@@ -14,18 +15,21 @@ class ProtocolCode(IntEnum):
|
||||
REQUEST_STOP = 0x71 # Presenter -> Trainer (request stop)
|
||||
STOP = 0x71 # Trainer -> Presenter (confirm stop)
|
||||
|
||||
|
||||
class PixelKind(IntEnum):
|
||||
GRAY_FLOAT32 = 0x01 # Grayscale represented by one float32
|
||||
GRAY_U8 = 0x02 # Grayscale represented by one u8
|
||||
RGB_FLOAT32 = 0x03 # RGB represented by three float32
|
||||
RGB_U8 = 0x04 # RGB represented by three u8
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImageProperties:
|
||||
class HandshakePayload:
|
||||
pixel_kind: PixelKind
|
||||
width: int
|
||||
height: int
|
||||
|
||||
|
||||
class ServerStatus(Enum):
|
||||
Ready = auto()
|
||||
Running = auto()
|
||||
@@ -33,7 +37,8 @@ class ServerStatus(Enum):
|
||||
|
||||
|
||||
CODE_PACKER: struct.Struct = struct.Struct("=B")
|
||||
HANDSHAKE_RESPONSE_PACKER: struct.Struct = struct.Struct("=BII")
|
||||
HANDSHAKE_REQUEST_PACKER: struct.Struct = struct.Struct("=BII")
|
||||
|
||||
|
||||
class CommandServer:
|
||||
"""
|
||||
@@ -41,71 +46,75 @@ class CommandServer:
|
||||
"""
|
||||
|
||||
pipe_operator: PipeOperator
|
||||
statue: ServerStatus
|
||||
status: ServerStatus
|
||||
|
||||
def __init__(self):
|
||||
self.pipe_operator = PipeOperator(PIPE_NAME)
|
||||
self.handshaked = ServerStatus.Ready
|
||||
self.status = ServerStatus.Ready
|
||||
|
||||
def __del__(self):
|
||||
"""Cleanup resources when object is destroyed."""
|
||||
self.pipe_operator.close()
|
||||
|
||||
def wait_handshake(self) -> ImageProperties:
|
||||
def wait_handshake(self, payload: HandshakePayload) -> None:
|
||||
"""
|
||||
Wait for handshake from Presenter, send request first and wait for response with data properties.
|
||||
Returns a tuple of (pixel_kind, width, height) from the Presenter.
|
||||
"""
|
||||
if self.statue != ServerStatus.Ready:
|
||||
if self.status != ServerStatus.Ready:
|
||||
raise RuntimeError("unexpected server status")
|
||||
|
||||
# Send handshake request to Presenter (code 0x61)
|
||||
self.pipe_operator.write(CODE_PACKER.pack(ProtocolCode.HANDSHAKE_REQUEST))
|
||||
self.pipe_operator.write(
|
||||
CODE_PACKER.pack(ProtocolCode.HANDSHAKE_REQUEST))
|
||||
# And the payload data
|
||||
self.pipe_operator.write(
|
||||
HANDSHAKE_REQUEST_PACKER.pack(payload.pixel_kind, payload.width,
|
||||
payload.height))
|
||||
|
||||
# Wait for handshake response from Presenter (code 0x62)
|
||||
code_bytes = self.pipe_operator.read(CODE_PACKER.size)
|
||||
(code, ) = CODE_PACKER.unpack(code_bytes)
|
||||
if ProtocolCode(code) != ProtocolCode.HANDSHAKE_RESPONSE:
|
||||
raise RuntimeError("expect handshake response code, but got another")
|
||||
|
||||
# Read data properties from Presenter (pixel_kind, width, height)
|
||||
handshake_response_payload = self.pipe_operator.read(HANDSHAKE_RESPONSE_PACKER.size)
|
||||
(raw_pixel_kind, width, height) = HANDSHAKE_RESPONSE_PACKER.unpack(handshake_response_payload)
|
||||
raise RuntimeError(
|
||||
"expect handshake response code, but got another")
|
||||
|
||||
# Set status and return
|
||||
self.statue = ServerStatus.Running
|
||||
return ImageProperties(PixelKind(raw_pixel_kind), width, height)
|
||||
self.status = ServerStatus.Running
|
||||
return
|
||||
|
||||
def tick(self, request_stop: bool) -> bool:
|
||||
def tick(self, data_receiver: Callable[[], None],
|
||||
request_stop: bool) -> bool:
|
||||
"""
|
||||
Tick function called every frame to wait for data ready from Presenter and send response.
|
||||
Returns True if a stop code was received (meaning the process should stop), False otherwise.
|
||||
"""
|
||||
if self.statue != ServerStatus.Running:
|
||||
if self.status != ServerStatus.Running:
|
||||
raise RuntimeError("unexpected server status")
|
||||
|
||||
# If there is stop requested, we post it first and return
|
||||
if request_stop:
|
||||
self.pipe_operator.write(CODE_PACKER.pack(ProtocolCode.STOP))
|
||||
self.statue = ServerStatus.Stop
|
||||
self.status = ServerStatus.Stop
|
||||
return True
|
||||
|
||||
# Wait for code from Presenter
|
||||
code_bytes = self.pipe_operator.read(CODE_PACKER.size)
|
||||
(code, ) = CODE_PACKER.unpack(code_bytes)
|
||||
|
||||
|
||||
# Analyse code
|
||||
match ProtocolCode(code):
|
||||
case ProtocolCode.DATA_READY:
|
||||
# Receive data
|
||||
print('Data received')
|
||||
data_receiver()
|
||||
# Send data received symbol
|
||||
self.pipe_operator.write(CODE_PACKER.pack(ProtocolCode.DATA_RECEIVED))
|
||||
self.pipe_operator.write(
|
||||
CODE_PACKER.pack(ProtocolCode.DATA_RECEIVED))
|
||||
case ProtocolCode.REQUEST_STOP:
|
||||
# Presenter requested stop.
|
||||
# Agree with it, send code and return.
|
||||
self.pipe_operator.write(CODE_PACKER.pack(ProtocolCode.STOP))
|
||||
self.statue = ServerStatus.Stop
|
||||
self.status = ServerStatus.Stop
|
||||
return True
|
||||
case _:
|
||||
raise RuntimeError("unexpected protocol code when running")
|
||||
|
||||
@@ -1,6 +1,31 @@
|
||||
from command_server import CommandServer, HandshakePayload, PixelKind
|
||||
import logging
|
||||
|
||||
|
||||
def receive_data() -> None:
|
||||
logging.info('Data received')
|
||||
|
||||
|
||||
def main():
|
||||
print("Hello from basalt-trainer!")
|
||||
server = CommandServer()
|
||||
|
||||
print(
|
||||
'Please launch BasaltPresenter now and then press Enter to continue.')
|
||||
input()
|
||||
|
||||
logging.info('Waiting BasaltPresenter...')
|
||||
server.wait_handshake(HandshakePayload(PixelKind.GRAY_U8, 600, 600))
|
||||
|
||||
logging.info('Start to running.')
|
||||
while True:
|
||||
if server.tick(receive_data, False):
|
||||
break
|
||||
|
||||
logging.info('Program stop.')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='[%(asctime)s] [%(levelname)s] %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
main()
|
||||
|
||||
@@ -45,9 +45,6 @@ class PipeOperator:
|
||||
)
|
||||
if self.pipe_handle == win32file.INVALID_HANDLE_VALUE:
|
||||
raise RuntimeError("Failed to create named pipe.")
|
||||
|
||||
# Wait for client to connect
|
||||
win32pipe.ConnectNamedPipe(self.pipe_handle, None)
|
||||
else:
|
||||
# POSIX implementation
|
||||
self.pipe_name = f"/tmp/{name}"
|
||||
@@ -158,4 +155,3 @@ class PipeOperator:
|
||||
total_written += bytes_written
|
||||
except OSError as e:
|
||||
raise RuntimeError(f"Failed to write to named pipe: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user