Skip to content

hertzforge.service.server

server

The analysis service: sds/1 over WebSocket.

On connect the server sends hello. Each request is answered by a response with its id; requests run concurrently, so a long project.open does not hold up a catalog.list sent after it, and responses may come back in a different order from the requests. Heavy work (opening a project, running an operation, framing a large result) runs in a worker thread so the event loop stays responsive.

Each connection has at most one project of its own (project.*), which every operation runs on and which ends with it. Table files (tables.*) are read and written by path beside it, without touching it.

A plain HTTP request, one that does not ask to upgrade to WebSocket, is answered with the shell's built pages when the package carries them (shell_folder), so hertzforge with no command can open the interface in a browser from a pip install.

Classes:

Name Description
Service

Everything but the transport: the operations and the worker

Connection

One client: its project, its requests in flight, and the lock that

Functions:

Name Description
preload

Import everything in PRELOAD.

shell_folder

The shell's built pages: hertzforge/shell in an installed package

shell_response

The file a plain GET asks for from the shell's folder, or None for

start

A listening server (port 0 picks a free one; server.sockets says

Classes

Service

Service(operations: Sequence[Operation] = OPERATIONS, workers: int = 2, dialogs: bool = False)

Everything but the transport: the operations and the worker threads. One per process.

Methods:

Name Description
close

Stop the worker threads without waiting; work not yet started is

in_worker

call(*args) run on a worker thread, awaited without blocking the

catalog_list

The catalog.list result: every operation's descriptor.

operation

The operation an op.run names.

Methods:
close
close() -> None

Stop the worker threads without waiting; work not yet started is cancelled.

in_worker async
in_worker(call: Callable[..., Any], *args: Any) -> Any

call(*args) run on a worker thread, awaited without blocking the event loop.

catalog_list
catalog_list() -> dict[str, Any]

The catalog.list result: every operation's descriptor.

operation
operation(params: Any) -> Operation

The operation an op.run names.

Connection

Connection(service: Service, socket: ServerConnection)

One client: its project, its requests in flight, and the lock that keeps its frames whole.

Methods:

Name Description
send

Send one frame, under the lock, so concurrent responses never

event

Send one event on topic, numbered in this connection's order.

progress

A (done, total) callable for a reader on a worker thread that

op_progress

A (done, total) callable for an operation on a worker thread: it sends

cancel_op

op.cancel: stop the operation running on the project at its next report of

project_request

Answer one project.* request on this connection's project, after

project_op

An operation on this connection's project: its views and the

report_request

Answer one report.* request on this connection's project.

fit_state

The fit's state with its CMIF drawn in this connection's units.

units_request

units.system: the unit system this connection's views are drawn

fit_request

Answer one fit.* request: a step of the mode fit on this

serve

Send hello, then answer each request in its own task until the

choose_files

files.choose: the system's open or save dialog on this machine, and the

answer

Run one request and send its response. A ProtocolError becomes an

Attributes:

Name Type Description
system Any

The UnitSystem this connection draws in.

Attributes
system property
system: Any

The UnitSystem this connection draws in.

Methods:
send async
send(frame: str | bytes) -> None

Send one frame, under the lock, so concurrent responses never interleave on the socket.

event async
event(topic: str, data: dict[str, Any]) -> None

Send one event on topic, numbered in this connection's order.

progress
progress(path: str) -> Callable[[int, int], None]

A (done, total) callable for a reader on a worker thread that sends project.progress events, at most one per PROGRESS_INTERVAL_S and always the last.

op_progress
op_progress(op_id: str) -> Callable[[int, int], None]

A (done, total) callable for an operation on a worker thread: it sends op.progress events, at most one per PROGRESS_INTERVAL_S but always when the stage or its total changes and at the end, and raises Cancelled once op.cancel is asked.

cancel_op
cancel_op(params: Any) -> dict[str, Any]

op.cancel: stop the operation running on the project at its next report of progress. Answers which operation that is, or null when none is running.

project_request async
project_request(method: str, params: Any) -> dict[str, Any]

Answer one project.* request on this connection's project, after any still running.

project_op async
project_op(params: Mapping[str, Any]) -> dict[str, Any]

An operation on this connection's project: its views and the project's summary, which counts as modified when the operation added or removed an object.

report_request async
report_request(method: str, params: Any) -> dict[str, Any]

Answer one report.* request on this connection's project.

fit_state
fit_state(fit: Fit) -> dict[str, Any]

The fit's state with its CMIF drawn in this connection's units.

units_request
units_request(params: Any) -> dict[str, Any]

units.system: the unit system this connection's views are drawn in, set when name is given; the systems there are.

fit_request async
fit_request(method: str, params: Any) -> dict[str, Any]

Answer one fit.* request: a step of the mode fit on this connection's project, and the fit's state after it.

serve async
serve() -> None

Send hello, then answer each request in its own task until the client goes. A frame that will not decode closes the connection with 1007, a message that is not a request with an integer id with 1008; requests still running when it ends are cancelled.

choose_files async
choose_files(params: Any) -> dict[str, Any]

files.choose: the system's open or save dialog on this machine, and the paths chosen in it (none when cancelled). Refused where the service shows no dialogs or none can be shown here, so the shell asks for a typed path.

answer async
answer(request: dict[str, Any]) -> None

Run one request and send its response. A ProtocolError becomes an error response with its code; any other exception is logged and sent as internal, so one failing request never ends the connection.

Functions:

preload

preload() -> None

Import everything in PRELOAD.

shell_folder

shell_folder() -> Path | None

The shell's built pages: hertzforge/shell in an installed package (the release workflow builds it into the wheel), else ui/build in a source checkout after npm --prefix ui run build. None when neither holds an index.html.

shell_response

shell_response(folder: Path, request: Request) -> Response | None

The file a plain GET asks for from the shell's folder, or None for a WebSocket upgrade, which the protocol handles. The shell is a single-page app, so a path with no suffix gets index.html; a missing file with a suffix is a 404, and nothing outside folder is served.

start async

start(service: Service, host: str = '127.0.0.1', port: int = 8791, shell: Path | None = None) -> Server

A listening server (port 0 picks a free one; server.sockets says which). With shell, a plain HTTP request is answered from that folder of built pages (shell_response). It changes nothing beyond the server: the process that serves calls preload and sets SWITCH_INTERVAL first (__main__._serve).