ros2-http-gateway 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ros2_http_gateway/__init__.py +9 -0
- ros2_http_gateway/__main__.py +5 -0
- ros2_http_gateway/_version.py +8 -0
- ros2_http_gateway/actions.py +256 -0
- ros2_http_gateway/app.py +865 -0
- ros2_http_gateway/audit.py +154 -0
- ros2_http_gateway/auth.py +96 -0
- ros2_http_gateway/cache.py +172 -0
- ros2_http_gateway/cli.py +182 -0
- ros2_http_gateway/codegen.py +604 -0
- ros2_http_gateway/config.py +712 -0
- ros2_http_gateway/config.schema.json +231 -0
- ros2_http_gateway/console.py +153 -0
- ros2_http_gateway/demo_node.py +163 -0
- ros2_http_gateway/openapi.py +672 -0
- ros2_http_gateway/params.py +137 -0
- ros2_http_gateway/policy.py +153 -0
- ros2_http_gateway/py.typed +0 -0
- ros2_http_gateway/resolve.py +417 -0
- ros2_http_gateway/ros_env.py +33 -0
- ros2_http_gateway/runtime.py +428 -0
- ros2_http_gateway/schemagen.py +192 -0
- ros2_http_gateway/server.py +125 -0
- ros2_http_gateway/sse.py +234 -0
- ros2_http_gateway/static/console/console.css +265 -0
- ros2_http_gateway/static/console/console.js +1344 -0
- ros2_http_gateway/static/console/index.html +47 -0
- ros2_http_gateway/static/swagger/LICENSE +202 -0
- ros2_http_gateway/static/swagger/README.md +7 -0
- ros2_http_gateway/static/swagger/index.html +95 -0
- ros2_http_gateway/static/swagger/swagger-ui-bundle.js +2 -0
- ros2_http_gateway/static/swagger/swagger-ui-bundle.js.LICENSE.txt +106 -0
- ros2_http_gateway/static/swagger/swagger-ui.css +3 -0
- ros2_http_gateway-0.2.0.data/data/share/ament_index/resource_index/packages/ros2_http_gateway +0 -0
- ros2_http_gateway-0.2.0.data/data/share/ros2_http_gateway/examples/demo.yaml +84 -0
- ros2_http_gateway-0.2.0.data/data/share/ros2_http_gateway/examples/dev.yaml +93 -0
- ros2_http_gateway-0.2.0.data/data/share/ros2_http_gateway/examples/locked_down.yaml +72 -0
- ros2_http_gateway-0.2.0.data/data/share/ros2_http_gateway/package.xml +37 -0
- ros2_http_gateway-0.2.0.dist-info/METADATA +343 -0
- ros2_http_gateway-0.2.0.dist-info/RECORD +44 -0
- ros2_http_gateway-0.2.0.dist-info/WHEEL +5 -0
- ros2_http_gateway-0.2.0.dist-info/entry_points.txt +3 -0
- ros2_http_gateway-0.2.0.dist-info/licenses/LICENSE +202 -0
- ros2_http_gateway-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""Config-driven, authenticated HTTP + SSE gateway for ROS 2.
|
|
2
|
+
|
|
3
|
+
Importing this package never imports ``rclpy``, so the config engine, code generator
|
|
4
|
+
and HTTP layer stay unit-testable on a plain Python installation.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from ._version import __version__
|
|
8
|
+
|
|
9
|
+
__all__ = ["__version__"]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Single source of truth for the package version.
|
|
2
|
+
|
|
3
|
+
``pyproject.toml`` reads this attribute (``[tool.setuptools.dynamic]``), ``setup.py``
|
|
4
|
+
executes this file for the ament build, and ``package.xml`` mirrors the value under a
|
|
5
|
+
test that fails on drift. Do not hard-code the version anywhere else.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.2.0"
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"""Goal registry for action endpoints.
|
|
2
|
+
|
|
3
|
+
An action outlives its request, so a submitted goal gets a record; later requests find it by id,
|
|
4
|
+
and it is evicted once terminal for ``actions.result_ttl_s``.
|
|
5
|
+
|
|
6
|
+
Loop-confined: feedback and results arrive on ROS threads and are handed over with
|
|
7
|
+
``loop.call_soon_threadsafe``, so the registry needs no locks.
|
|
8
|
+
|
|
9
|
+
Eviction is lazy -- a sweep on creation, the only way the registry grows, plus a drop on lookup
|
|
10
|
+
-- so there is no timer and an idle gateway does nothing.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import asyncio
|
|
16
|
+
import logging
|
|
17
|
+
import time
|
|
18
|
+
import uuid
|
|
19
|
+
from collections.abc import Callable
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from typing import Any, Final
|
|
22
|
+
|
|
23
|
+
import orjson
|
|
24
|
+
|
|
25
|
+
_LOG = logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"TERMINAL",
|
|
29
|
+
"GoalRecord",
|
|
30
|
+
"GoalRegistry",
|
|
31
|
+
"feedback_frame",
|
|
32
|
+
"status_name",
|
|
33
|
+
"terminal_frame",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
# action_msgs/msg/GoalStatus, transcribed rather than imported so this file needs no ROS.
|
|
37
|
+
_STATUS_NAMES: Final[dict[int, str]] = {
|
|
38
|
+
0: "unknown",
|
|
39
|
+
1: "accepted",
|
|
40
|
+
2: "executing",
|
|
41
|
+
3: "canceling",
|
|
42
|
+
4: "succeeded",
|
|
43
|
+
5: "canceled",
|
|
44
|
+
6: "aborted",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
TERMINAL: Final = frozenset({"succeeded", "canceled", "aborted"})
|
|
48
|
+
|
|
49
|
+
# Per-goal stream backlog: a slow client loses its oldest feedback rather than stalling the goal.
|
|
50
|
+
_STREAM_BACKLOG: Final = 64
|
|
51
|
+
|
|
52
|
+
_NULL: Final = b"null"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def status_name(code: int) -> str:
|
|
56
|
+
"""Map an ``action_msgs`` status code onto its protocol name."""
|
|
57
|
+
return _STATUS_NAMES.get(code, "unknown")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def feedback_frame(encoded: bytes) -> bytes:
|
|
61
|
+
"""Frame one feedback message as an SSE event."""
|
|
62
|
+
return b"event: feedback\ndata: " + encoded + b"\n\n"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def terminal_frame(status: str, result: bytes | None) -> bytes:
|
|
66
|
+
"""Frame the single terminal event that ends a per-goal stream."""
|
|
67
|
+
return (
|
|
68
|
+
b'event: terminal\ndata: {"status":"'
|
|
69
|
+
+ status.encode("ascii")
|
|
70
|
+
+ b'","result":'
|
|
71
|
+
+ (result or _NULL)
|
|
72
|
+
+ b"}\n\n"
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(slots=True)
|
|
77
|
+
class GoalRecord:
|
|
78
|
+
"""One submitted goal and everything a later request might ask about it."""
|
|
79
|
+
|
|
80
|
+
goal_id: str
|
|
81
|
+
endpoint: str
|
|
82
|
+
status: str = "accepted"
|
|
83
|
+
result: bytes | None = None
|
|
84
|
+
last_feedback: bytes | None = None
|
|
85
|
+
terminal_at: float | None = None
|
|
86
|
+
handle: Any = None
|
|
87
|
+
result_task: asyncio.Task[None] | None = None
|
|
88
|
+
subscribers: list[asyncio.Queue[bytes | None]] = field(default_factory=list)
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def is_terminal(self) -> bool:
|
|
92
|
+
"""Whether this goal has finished, one way or another."""
|
|
93
|
+
return self.status in TERMINAL
|
|
94
|
+
|
|
95
|
+
def current_status(self) -> str:
|
|
96
|
+
"""Live status from the goal handle while running, recorded status once finished.
|
|
97
|
+
|
|
98
|
+
The handle supplies only the non-terminal progression. It goes terminal before the result
|
|
99
|
+
task has collected the outcome, and a terminal status must imply the result is present,
|
|
100
|
+
so terminal comes from the record -- which is set together with the result. ``canceling``
|
|
101
|
+
is likewise kept, since it is what this gateway already told the client.
|
|
102
|
+
"""
|
|
103
|
+
if self.is_terminal or self.status == "canceling" or self.handle is None:
|
|
104
|
+
return self.status
|
|
105
|
+
live = status_name(self.handle.status)
|
|
106
|
+
if live == "unknown" or live in TERMINAL:
|
|
107
|
+
return self.status
|
|
108
|
+
return live
|
|
109
|
+
|
|
110
|
+
def status_body(self) -> bytes:
|
|
111
|
+
"""The JSON body for a status read, built without re-encoding the payloads."""
|
|
112
|
+
return b"".join(
|
|
113
|
+
(
|
|
114
|
+
b'{"status":"',
|
|
115
|
+
self.current_status().encode("ascii"),
|
|
116
|
+
b'","result":',
|
|
117
|
+
self.result or _NULL,
|
|
118
|
+
b',"last_feedback":',
|
|
119
|
+
self.last_feedback or _NULL,
|
|
120
|
+
b"}",
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class GoalRegistry:
|
|
126
|
+
"""Tracks live and recently finished goals.
|
|
127
|
+
|
|
128
|
+
``clock`` is injectable so eviction can be tested without sleeping through the TTL.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
__slots__ = ("_clock", "_records", "_ttl_s")
|
|
132
|
+
|
|
133
|
+
def __init__(self, ttl_s: float, clock: Callable[[], float] = time.monotonic) -> None:
|
|
134
|
+
self._ttl_s = ttl_s
|
|
135
|
+
self._clock = clock
|
|
136
|
+
self._records: dict[str, GoalRecord] = {}
|
|
137
|
+
|
|
138
|
+
def __len__(self) -> int:
|
|
139
|
+
return len(self._records)
|
|
140
|
+
|
|
141
|
+
def create(self, endpoint: str) -> GoalRecord:
|
|
142
|
+
"""Register a new goal, sweeping expired ones first."""
|
|
143
|
+
self.sweep()
|
|
144
|
+
record = GoalRecord(goal_id=uuid.uuid4().hex, endpoint=endpoint)
|
|
145
|
+
self._records[record.goal_id] = record
|
|
146
|
+
return record
|
|
147
|
+
|
|
148
|
+
def get(self, endpoint: str, goal_id: str) -> GoalRecord | None:
|
|
149
|
+
"""Find a goal belonging to an endpoint, or None if unknown or expired.
|
|
150
|
+
|
|
151
|
+
Keyed by endpoint too, so an id issued by one cannot be read through another's routes.
|
|
152
|
+
"""
|
|
153
|
+
record = self._records.get(goal_id)
|
|
154
|
+
if record is None or record.endpoint != endpoint:
|
|
155
|
+
return None
|
|
156
|
+
if self._expired(record):
|
|
157
|
+
self.discard(record)
|
|
158
|
+
return None
|
|
159
|
+
return record
|
|
160
|
+
|
|
161
|
+
def discard(self, record: GoalRecord) -> None:
|
|
162
|
+
"""Drop a record and close any streams still attached to it."""
|
|
163
|
+
self._records.pop(record.goal_id, None)
|
|
164
|
+
self._close_subscribers(record)
|
|
165
|
+
|
|
166
|
+
def sweep(self) -> int:
|
|
167
|
+
"""Evict every goal whose result has outlived the TTL. Returns how many went."""
|
|
168
|
+
expired = [record for record in self._records.values() if self._expired(record)]
|
|
169
|
+
for record in expired:
|
|
170
|
+
self.discard(record)
|
|
171
|
+
return len(expired)
|
|
172
|
+
|
|
173
|
+
def _expired(self, record: GoalRecord) -> bool:
|
|
174
|
+
return record.terminal_at is not None and (self._clock() - record.terminal_at) > self._ttl_s
|
|
175
|
+
|
|
176
|
+
# ------------------------------------------------------------------ loop-side updates
|
|
177
|
+
|
|
178
|
+
def on_feedback(self, record: GoalRecord, encoded: bytes) -> None:
|
|
179
|
+
"""Record feedback and fan the framed bytes out to every stream.
|
|
180
|
+
|
|
181
|
+
Encoded once by the caller and shared by reference: N listeners cost N queue appends.
|
|
182
|
+
"""
|
|
183
|
+
if record.is_terminal:
|
|
184
|
+
# call_soon_threadsafe ordering allows a callback scheduled from the ROS thread to
|
|
185
|
+
# land after on_terminal has run. Readers have already stopped at the sentinel, but
|
|
186
|
+
# letting last_feedback change afterwards would make two reads of a finished goal
|
|
187
|
+
# disagree.
|
|
188
|
+
return
|
|
189
|
+
record.last_feedback = encoded
|
|
190
|
+
if not record.subscribers:
|
|
191
|
+
return
|
|
192
|
+
frame = feedback_frame(encoded)
|
|
193
|
+
for queue in record.subscribers:
|
|
194
|
+
_offer(queue, frame)
|
|
195
|
+
|
|
196
|
+
def on_terminal(self, record: GoalRecord, status: str, result: bytes | None) -> None:
|
|
197
|
+
"""Mark a goal finished, then send the closing event to every stream."""
|
|
198
|
+
# Status last: it is the flag readers key on, and "terminal" has to imply the result is
|
|
199
|
+
# already there. Nothing can interleave between these lines today, since this runs on the
|
|
200
|
+
# loop with no await, but ordering them this way means that stays true even if some
|
|
201
|
+
# future caller yields.
|
|
202
|
+
record.result = result
|
|
203
|
+
record.terminal_at = self._clock()
|
|
204
|
+
record.status = status
|
|
205
|
+
frame = terminal_frame(status, result)
|
|
206
|
+
for queue in record.subscribers:
|
|
207
|
+
# Both through _offer: a queue sitting at maxsize accepts the frame by dropping its
|
|
208
|
+
# oldest and is then full again, so a bare put_nowait for the sentinel raises
|
|
209
|
+
# QueueFull -- which would escape into the result task, abandon every subscriber
|
|
210
|
+
# after this one without a frame or a sentinel, and leave their streams open until
|
|
211
|
+
# the connection died. The sentinel is the one frame that must never be the dropped
|
|
212
|
+
# one, so it gets the same treatment with more reason, not less.
|
|
213
|
+
try:
|
|
214
|
+
_offer(queue, frame)
|
|
215
|
+
_offer(queue, None)
|
|
216
|
+
except Exception:
|
|
217
|
+
_LOG.exception("failed to close a stream for goal %s", record.goal_id)
|
|
218
|
+
|
|
219
|
+
def subscribe(self, record: GoalRecord) -> asyncio.Queue[bytes | None]:
|
|
220
|
+
"""Attach a stream to a goal."""
|
|
221
|
+
queue: asyncio.Queue[bytes | None] = asyncio.Queue(maxsize=_STREAM_BACKLOG)
|
|
222
|
+
record.subscribers.append(queue)
|
|
223
|
+
return queue
|
|
224
|
+
|
|
225
|
+
def unsubscribe(self, record: GoalRecord, queue: asyncio.Queue[bytes | None]) -> None:
|
|
226
|
+
"""Detach a stream, whether it ended or the client went away."""
|
|
227
|
+
try:
|
|
228
|
+
record.subscribers.remove(queue)
|
|
229
|
+
except ValueError:
|
|
230
|
+
pass
|
|
231
|
+
|
|
232
|
+
def _close_subscribers(self, record: GoalRecord) -> None:
|
|
233
|
+
for queue in record.subscribers:
|
|
234
|
+
_offer(queue, None)
|
|
235
|
+
record.subscribers.clear()
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _offer(queue: asyncio.Queue[bytes | None], item: bytes | None) -> None:
|
|
239
|
+
"""Append to a stream queue, discarding the oldest frame if it is full."""
|
|
240
|
+
try:
|
|
241
|
+
queue.put_nowait(item)
|
|
242
|
+
except asyncio.QueueFull:
|
|
243
|
+
try:
|
|
244
|
+
queue.get_nowait()
|
|
245
|
+
except asyncio.QueueEmpty: # pragma: no cover - it was full a moment ago
|
|
246
|
+
pass
|
|
247
|
+
queue.put_nowait(item)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def encode_feedback(converter: Callable[[Any], dict[str, Any]], message: Any) -> bytes:
|
|
251
|
+
"""Encode an action feedback message.
|
|
252
|
+
|
|
253
|
+
The callback receives the generated ``*_FeedbackMessage`` wrapper, not the Feedback type, so
|
|
254
|
+
the converter applies to ``.feedback``.
|
|
255
|
+
"""
|
|
256
|
+
return orjson.dumps(converter(message.feedback), option=orjson.OPT_SERIALIZE_NUMPY)
|