rocket-welder-sdk 1.1.36.dev14__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.
- rocket_welder_sdk/__init__.py +95 -0
- rocket_welder_sdk/bytes_size.py +234 -0
- rocket_welder_sdk/connection_string.py +291 -0
- rocket_welder_sdk/controllers.py +831 -0
- rocket_welder_sdk/external_controls/__init__.py +30 -0
- rocket_welder_sdk/external_controls/contracts.py +100 -0
- rocket_welder_sdk/external_controls/contracts_old.py +105 -0
- rocket_welder_sdk/frame_metadata.py +138 -0
- rocket_welder_sdk/gst_metadata.py +411 -0
- rocket_welder_sdk/high_level/__init__.py +54 -0
- rocket_welder_sdk/high_level/client.py +235 -0
- rocket_welder_sdk/high_level/connection_strings.py +331 -0
- rocket_welder_sdk/high_level/data_context.py +169 -0
- rocket_welder_sdk/high_level/frame_sink_factory.py +118 -0
- rocket_welder_sdk/high_level/schema.py +195 -0
- rocket_welder_sdk/high_level/transport_protocol.py +238 -0
- rocket_welder_sdk/keypoints_protocol.py +642 -0
- rocket_welder_sdk/opencv_controller.py +278 -0
- rocket_welder_sdk/periodic_timer.py +303 -0
- rocket_welder_sdk/py.typed +2 -0
- rocket_welder_sdk/rocket_welder_client.py +497 -0
- rocket_welder_sdk/segmentation_result.py +420 -0
- rocket_welder_sdk/session_id.py +238 -0
- rocket_welder_sdk/transport/__init__.py +31 -0
- rocket_welder_sdk/transport/frame_sink.py +122 -0
- rocket_welder_sdk/transport/frame_source.py +74 -0
- rocket_welder_sdk/transport/nng_transport.py +197 -0
- rocket_welder_sdk/transport/stream_transport.py +193 -0
- rocket_welder_sdk/transport/tcp_transport.py +154 -0
- rocket_welder_sdk/transport/unix_socket_transport.py +339 -0
- rocket_welder_sdk/ui/__init__.py +48 -0
- rocket_welder_sdk/ui/controls.py +362 -0
- rocket_welder_sdk/ui/icons.py +21628 -0
- rocket_welder_sdk/ui/ui_events_projection.py +226 -0
- rocket_welder_sdk/ui/ui_service.py +358 -0
- rocket_welder_sdk/ui/value_types.py +72 -0
- rocket_welder_sdk-1.1.36.dev14.dist-info/METADATA +845 -0
- rocket_welder_sdk-1.1.36.dev14.dist-info/RECORD +40 -0
- rocket_welder_sdk-1.1.36.dev14.dist-info/WHEEL +5 -0
- rocket_welder_sdk-1.1.36.dev14.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Segmentation result serialization protocol.
|
|
3
|
+
|
|
4
|
+
Binary protocol for efficient streaming of instance segmentation results.
|
|
5
|
+
Compatible with C# implementation for cross-platform interoperability.
|
|
6
|
+
|
|
7
|
+
Protocol (per frame):
|
|
8
|
+
[FrameId: 8B little-endian][Width: varint][Height: varint]
|
|
9
|
+
[classId: 1B][instanceId: 1B][pointCount: varint][points: delta+varint...]
|
|
10
|
+
[classId: 1B][instanceId: 1B][pointCount: varint][points: delta+varint...]
|
|
11
|
+
...
|
|
12
|
+
|
|
13
|
+
Features:
|
|
14
|
+
- Delta encoding for adjacent contour points (efficient compression)
|
|
15
|
+
- Varint encoding for variable-length integers
|
|
16
|
+
- ZigZag encoding for signed deltas
|
|
17
|
+
- Explicit little-endian for cross-platform compatibility
|
|
18
|
+
- Frame boundaries handled by transport layer (IFrameSink)
|
|
19
|
+
- NumPy array support for efficient processing
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import io
|
|
23
|
+
import struct
|
|
24
|
+
from dataclasses import dataclass
|
|
25
|
+
from typing import BinaryIO, Iterator, List, Optional, Tuple, Union
|
|
26
|
+
|
|
27
|
+
import numpy as np
|
|
28
|
+
import numpy.typing as npt
|
|
29
|
+
from typing_extensions import TypeAlias
|
|
30
|
+
|
|
31
|
+
from .transport import IFrameSink, StreamFrameSink
|
|
32
|
+
|
|
33
|
+
# Type aliases
|
|
34
|
+
Point = Tuple[int, int]
|
|
35
|
+
PointArray: TypeAlias = npt.NDArray[np.int32] # Shape: (N, 2)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _write_varint(stream: BinaryIO, value: int) -> None:
|
|
39
|
+
"""Write unsigned integer as varint."""
|
|
40
|
+
if value < 0:
|
|
41
|
+
raise ValueError(f"Varint requires non-negative value, got {value}")
|
|
42
|
+
|
|
43
|
+
while value >= 0x80:
|
|
44
|
+
stream.write(bytes([value & 0x7F | 0x80]))
|
|
45
|
+
value >>= 7
|
|
46
|
+
stream.write(bytes([value & 0x7F]))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _read_varint(stream: BinaryIO) -> int:
|
|
50
|
+
"""Read varint from stream and decode to unsigned integer."""
|
|
51
|
+
result = 0
|
|
52
|
+
shift = 0
|
|
53
|
+
|
|
54
|
+
while True:
|
|
55
|
+
if shift >= 35: # Max 5 bytes for uint32
|
|
56
|
+
raise ValueError("Varint too long (corrupted stream)")
|
|
57
|
+
|
|
58
|
+
byte_data = stream.read(1)
|
|
59
|
+
if not byte_data:
|
|
60
|
+
raise EOFError("Unexpected end of stream reading varint")
|
|
61
|
+
|
|
62
|
+
byte = byte_data[0]
|
|
63
|
+
result |= (byte & 0x7F) << shift
|
|
64
|
+
shift += 7
|
|
65
|
+
|
|
66
|
+
if not (byte & 0x80):
|
|
67
|
+
break
|
|
68
|
+
|
|
69
|
+
return result
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _zigzag_encode(value: int) -> int:
|
|
73
|
+
"""ZigZag encode signed integer to unsigned."""
|
|
74
|
+
return (value << 1) ^ (value >> 31)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _zigzag_decode(value: int) -> int:
|
|
78
|
+
"""ZigZag decode unsigned integer to signed."""
|
|
79
|
+
return (value >> 1) ^ -(value & 1)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True)
|
|
83
|
+
class SegmentationFrameMetadata:
|
|
84
|
+
"""Metadata for a segmentation frame."""
|
|
85
|
+
|
|
86
|
+
frame_id: int
|
|
87
|
+
width: int
|
|
88
|
+
height: int
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@dataclass(frozen=True)
|
|
92
|
+
class SegmentationInstance:
|
|
93
|
+
"""A single instance in a segmentation result."""
|
|
94
|
+
|
|
95
|
+
class_id: int
|
|
96
|
+
instance_id: int
|
|
97
|
+
points: PointArray # NumPy array of shape (N, 2) with dtype int32
|
|
98
|
+
|
|
99
|
+
def to_normalized(self, width: int, height: int) -> npt.NDArray[np.float32]:
|
|
100
|
+
"""
|
|
101
|
+
Convert points to normalized coordinates [0-1] range.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
width: Frame width in pixels
|
|
105
|
+
height: Frame height in pixels
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
NumPy array of shape (N, 2) with dtype float32, normalized to [0-1]
|
|
109
|
+
"""
|
|
110
|
+
if width <= 0 or height <= 0:
|
|
111
|
+
raise ValueError("Width and height must be positive")
|
|
112
|
+
|
|
113
|
+
# Vectorized operation - very efficient
|
|
114
|
+
normalized = self.points.astype(np.float32)
|
|
115
|
+
normalized[:, 0] /= width
|
|
116
|
+
normalized[:, 1] /= height
|
|
117
|
+
return normalized
|
|
118
|
+
|
|
119
|
+
def to_list(self) -> List[Point]:
|
|
120
|
+
"""Convert points to list of tuples."""
|
|
121
|
+
return [(int(x), int(y)) for x, y in self.points]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class SegmentationResultWriter:
|
|
125
|
+
"""
|
|
126
|
+
Writes segmentation results for a single frame via IFrameSink.
|
|
127
|
+
|
|
128
|
+
Frames are buffered in memory and written atomically on close.
|
|
129
|
+
|
|
130
|
+
Thread-safe: No (caller must synchronize)
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
def __init__(
|
|
134
|
+
self,
|
|
135
|
+
frame_id: int,
|
|
136
|
+
width: int,
|
|
137
|
+
height: int,
|
|
138
|
+
stream: Optional[BinaryIO] = None,
|
|
139
|
+
*,
|
|
140
|
+
frame_sink: Optional[IFrameSink] = None,
|
|
141
|
+
) -> None:
|
|
142
|
+
"""
|
|
143
|
+
Initialize writer for a single frame.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
frame_id: Unique frame identifier
|
|
147
|
+
width: Frame width in pixels
|
|
148
|
+
height: Frame height in pixels
|
|
149
|
+
stream: Binary stream (convenience - auto-wraps in StreamFrameSink)
|
|
150
|
+
frame_sink: IFrameSink to write frame to (keyword-only, transport-agnostic)
|
|
151
|
+
|
|
152
|
+
Note:
|
|
153
|
+
Either stream or frame_sink must be provided (not both).
|
|
154
|
+
For convenience, stream is the primary parameter (auto-wraps in StreamFrameSink).
|
|
155
|
+
"""
|
|
156
|
+
if frame_sink is None and stream is None:
|
|
157
|
+
raise TypeError("Either stream or frame_sink must be provided")
|
|
158
|
+
|
|
159
|
+
if frame_sink is not None and stream is not None:
|
|
160
|
+
raise TypeError("Cannot provide both stream and frame_sink")
|
|
161
|
+
|
|
162
|
+
# Convenience: auto-wrap stream in StreamFrameSink
|
|
163
|
+
if stream is not None:
|
|
164
|
+
self._frame_sink: IFrameSink = StreamFrameSink(stream, leave_open=True)
|
|
165
|
+
self._owns_sink = False # Don't close the stream wrapper
|
|
166
|
+
else:
|
|
167
|
+
assert frame_sink is not None
|
|
168
|
+
self._frame_sink = frame_sink
|
|
169
|
+
self._owns_sink = False
|
|
170
|
+
|
|
171
|
+
self._frame_id = frame_id
|
|
172
|
+
self._width = width
|
|
173
|
+
self._height = height
|
|
174
|
+
self._buffer = io.BytesIO() # Buffer frame for atomic write
|
|
175
|
+
self._header_written = False
|
|
176
|
+
self._disposed = False
|
|
177
|
+
|
|
178
|
+
def _ensure_header_written(self) -> None:
|
|
179
|
+
"""Write frame header to buffer if not already written."""
|
|
180
|
+
if self._header_written:
|
|
181
|
+
return
|
|
182
|
+
|
|
183
|
+
# Write FrameId (8 bytes, little-endian)
|
|
184
|
+
self._buffer.write(struct.pack("<Q", self._frame_id))
|
|
185
|
+
|
|
186
|
+
# Write Width and Height as varints
|
|
187
|
+
_write_varint(self._buffer, self._width)
|
|
188
|
+
_write_varint(self._buffer, self._height)
|
|
189
|
+
|
|
190
|
+
self._header_written = True
|
|
191
|
+
|
|
192
|
+
def append(
|
|
193
|
+
self,
|
|
194
|
+
class_id: int,
|
|
195
|
+
instance_id: int,
|
|
196
|
+
points: Union[List[Point], PointArray],
|
|
197
|
+
) -> None:
|
|
198
|
+
"""
|
|
199
|
+
Append an instance with contour points.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
class_id: Object class ID (0-255)
|
|
203
|
+
instance_id: Instance ID within class (0-255)
|
|
204
|
+
points: List of (x, y) tuples or NumPy array of shape (N, 2)
|
|
205
|
+
"""
|
|
206
|
+
if class_id < 0 or class_id > 255:
|
|
207
|
+
raise ValueError(f"class_id must be 0-255, got {class_id}")
|
|
208
|
+
if instance_id < 0 or instance_id > 255:
|
|
209
|
+
raise ValueError(f"instance_id must be 0-255, got {instance_id}")
|
|
210
|
+
|
|
211
|
+
self._ensure_header_written()
|
|
212
|
+
|
|
213
|
+
# Convert to NumPy array if needed
|
|
214
|
+
if not isinstance(points, np.ndarray):
|
|
215
|
+
points_array = np.array(points, dtype=np.int32)
|
|
216
|
+
else:
|
|
217
|
+
points_array = points.astype(np.int32)
|
|
218
|
+
|
|
219
|
+
if points_array.ndim != 2 or points_array.shape[1] != 2:
|
|
220
|
+
raise ValueError(f"Points must be shape (N, 2), got {points_array.shape}")
|
|
221
|
+
|
|
222
|
+
# Write class_id and instance_id
|
|
223
|
+
self._buffer.write(bytes([class_id, instance_id]))
|
|
224
|
+
|
|
225
|
+
# Write point count
|
|
226
|
+
point_count = len(points_array)
|
|
227
|
+
_write_varint(self._buffer, point_count)
|
|
228
|
+
|
|
229
|
+
if point_count == 0:
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
# Write first point (absolute coordinates)
|
|
233
|
+
first_point = points_array[0]
|
|
234
|
+
_write_varint(self._buffer, _zigzag_encode(int(first_point[0])))
|
|
235
|
+
_write_varint(self._buffer, _zigzag_encode(int(first_point[1])))
|
|
236
|
+
|
|
237
|
+
# Write remaining points (delta encoded)
|
|
238
|
+
for i in range(1, point_count):
|
|
239
|
+
delta_x = int(points_array[i, 0] - points_array[i - 1, 0])
|
|
240
|
+
delta_y = int(points_array[i, 1] - points_array[i - 1, 1])
|
|
241
|
+
_write_varint(self._buffer, _zigzag_encode(delta_x))
|
|
242
|
+
_write_varint(self._buffer, _zigzag_encode(delta_y))
|
|
243
|
+
|
|
244
|
+
def flush(self) -> None:
|
|
245
|
+
"""Flush buffered frame via frame sink without closing."""
|
|
246
|
+
if self._disposed:
|
|
247
|
+
return
|
|
248
|
+
|
|
249
|
+
# Ensure header is written (even if no instances appended)
|
|
250
|
+
self._ensure_header_written()
|
|
251
|
+
|
|
252
|
+
# Write buffered frame atomically via sink
|
|
253
|
+
frame_data = self._buffer.getvalue()
|
|
254
|
+
self._frame_sink.write_frame(frame_data)
|
|
255
|
+
self._frame_sink.flush()
|
|
256
|
+
|
|
257
|
+
def close(self) -> None:
|
|
258
|
+
"""Close writer and write buffered frame via frame sink."""
|
|
259
|
+
if self._disposed:
|
|
260
|
+
return
|
|
261
|
+
|
|
262
|
+
self._disposed = True
|
|
263
|
+
|
|
264
|
+
# Ensure header is written (even if no instances appended)
|
|
265
|
+
self._ensure_header_written()
|
|
266
|
+
|
|
267
|
+
# Send complete frame atomically via sink
|
|
268
|
+
frame_data = self._buffer.getvalue()
|
|
269
|
+
self._frame_sink.write_frame(frame_data)
|
|
270
|
+
|
|
271
|
+
# Clean up buffer
|
|
272
|
+
self._buffer.close()
|
|
273
|
+
|
|
274
|
+
def __enter__(self) -> "SegmentationResultWriter":
|
|
275
|
+
"""Context manager entry."""
|
|
276
|
+
return self
|
|
277
|
+
|
|
278
|
+
def __exit__(self, *args: object) -> None:
|
|
279
|
+
"""Context manager exit."""
|
|
280
|
+
self.close()
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
class SegmentationResultReader:
|
|
284
|
+
"""
|
|
285
|
+
Reads segmentation results for a single frame.
|
|
286
|
+
|
|
287
|
+
Thread-safe: No (caller must synchronize)
|
|
288
|
+
Stream ownership: Caller must close stream
|
|
289
|
+
"""
|
|
290
|
+
|
|
291
|
+
def __init__(self, stream: BinaryIO) -> None:
|
|
292
|
+
"""
|
|
293
|
+
Initialize reader for a single frame.
|
|
294
|
+
|
|
295
|
+
Args:
|
|
296
|
+
stream: Binary stream to read from (must support read()).
|
|
297
|
+
Should contain raw frame data without length prefix.
|
|
298
|
+
Use StreamFrameSource to strip length prefixes from transport streams.
|
|
299
|
+
"""
|
|
300
|
+
if not hasattr(stream, "read"):
|
|
301
|
+
raise TypeError("Stream must be a binary readable stream")
|
|
302
|
+
|
|
303
|
+
self._stream = stream
|
|
304
|
+
self._header_read = False
|
|
305
|
+
self._metadata: Optional[SegmentationFrameMetadata] = None
|
|
306
|
+
|
|
307
|
+
# Max points per instance - prevents OOM attacks
|
|
308
|
+
self._max_points_per_instance = 10_000_000 # 10M points
|
|
309
|
+
|
|
310
|
+
def _ensure_header_read(self) -> None:
|
|
311
|
+
"""Read frame header if not already read."""
|
|
312
|
+
if self._header_read:
|
|
313
|
+
return
|
|
314
|
+
|
|
315
|
+
# Read FrameId (8 bytes, little-endian)
|
|
316
|
+
frame_id_bytes = self._stream.read(8)
|
|
317
|
+
if len(frame_id_bytes) != 8:
|
|
318
|
+
raise EOFError("Failed to read FrameId")
|
|
319
|
+
frame_id = struct.unpack("<Q", frame_id_bytes)[0]
|
|
320
|
+
|
|
321
|
+
# Read Width and Height as varints
|
|
322
|
+
width = _read_varint(self._stream)
|
|
323
|
+
height = _read_varint(self._stream)
|
|
324
|
+
|
|
325
|
+
self._metadata = SegmentationFrameMetadata(frame_id, width, height)
|
|
326
|
+
self._header_read = True
|
|
327
|
+
|
|
328
|
+
@property
|
|
329
|
+
def metadata(self) -> SegmentationFrameMetadata:
|
|
330
|
+
"""Get frame metadata (frameId, width, height)."""
|
|
331
|
+
self._ensure_header_read()
|
|
332
|
+
assert self._metadata is not None
|
|
333
|
+
return self._metadata
|
|
334
|
+
|
|
335
|
+
def read_next(self) -> Optional[SegmentationInstance]:
|
|
336
|
+
"""
|
|
337
|
+
Read next instance from stream.
|
|
338
|
+
|
|
339
|
+
Returns:
|
|
340
|
+
SegmentationInstance if available, None if end of stream reached
|
|
341
|
+
|
|
342
|
+
Raises:
|
|
343
|
+
EOFError: If stream ends unexpectedly
|
|
344
|
+
ValueError: If data is corrupted
|
|
345
|
+
"""
|
|
346
|
+
self._ensure_header_read()
|
|
347
|
+
|
|
348
|
+
# Read class_id and instance_id (buffered for performance)
|
|
349
|
+
header = self._stream.read(2)
|
|
350
|
+
|
|
351
|
+
if len(header) == 0:
|
|
352
|
+
# End of stream - no more instances
|
|
353
|
+
return None
|
|
354
|
+
|
|
355
|
+
if len(header) != 2:
|
|
356
|
+
raise EOFError("Unexpected end of stream reading instance header")
|
|
357
|
+
|
|
358
|
+
class_id = header[0]
|
|
359
|
+
instance_id = header[1]
|
|
360
|
+
|
|
361
|
+
# Read point count with validation
|
|
362
|
+
point_count = _read_varint(self._stream)
|
|
363
|
+
if point_count > self._max_points_per_instance:
|
|
364
|
+
raise ValueError(
|
|
365
|
+
f"Point count {point_count} exceeds maximum " f"{self._max_points_per_instance}"
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
if point_count == 0:
|
|
369
|
+
# Empty points array
|
|
370
|
+
points = np.empty((0, 2), dtype=np.int32)
|
|
371
|
+
return SegmentationInstance(class_id, instance_id, points)
|
|
372
|
+
|
|
373
|
+
# Allocate NumPy array for points
|
|
374
|
+
points = np.empty((point_count, 2), dtype=np.int32)
|
|
375
|
+
|
|
376
|
+
# Read first point (absolute coordinates)
|
|
377
|
+
x = _zigzag_decode(_read_varint(self._stream))
|
|
378
|
+
y = _zigzag_decode(_read_varint(self._stream))
|
|
379
|
+
points[0] = [x, y]
|
|
380
|
+
|
|
381
|
+
# Read remaining points (delta encoded)
|
|
382
|
+
for i in range(1, point_count):
|
|
383
|
+
delta_x = _zigzag_decode(_read_varint(self._stream))
|
|
384
|
+
delta_y = _zigzag_decode(_read_varint(self._stream))
|
|
385
|
+
x += delta_x
|
|
386
|
+
y += delta_y
|
|
387
|
+
points[i] = [x, y]
|
|
388
|
+
|
|
389
|
+
return SegmentationInstance(class_id, instance_id, points)
|
|
390
|
+
|
|
391
|
+
def read_all(self) -> List[SegmentationInstance]:
|
|
392
|
+
"""
|
|
393
|
+
Read all instances from frame.
|
|
394
|
+
|
|
395
|
+
Returns:
|
|
396
|
+
List of all instances in frame
|
|
397
|
+
"""
|
|
398
|
+
instances = []
|
|
399
|
+
while True:
|
|
400
|
+
instance = self.read_next()
|
|
401
|
+
if instance is None:
|
|
402
|
+
break
|
|
403
|
+
instances.append(instance)
|
|
404
|
+
return instances
|
|
405
|
+
|
|
406
|
+
def __iter__(self) -> Iterator[SegmentationInstance]:
|
|
407
|
+
"""Iterate over instances in frame."""
|
|
408
|
+
while True:
|
|
409
|
+
instance = self.read_next()
|
|
410
|
+
if instance is None:
|
|
411
|
+
break
|
|
412
|
+
yield instance
|
|
413
|
+
|
|
414
|
+
def __enter__(self) -> "SegmentationResultReader":
|
|
415
|
+
"""Context manager entry."""
|
|
416
|
+
return self
|
|
417
|
+
|
|
418
|
+
def __exit__(self, *args: object) -> None:
|
|
419
|
+
"""Context manager exit."""
|
|
420
|
+
pass
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"""SessionId parsing utilities for NNG URL generation.
|
|
2
|
+
|
|
3
|
+
SessionId format: ps-{guid} (e.g., ps-a1b2c3d4-e5f6-7890-abcd-ef1234567890)
|
|
4
|
+
Prefix "ps" = PipelineSession.
|
|
5
|
+
|
|
6
|
+
This module provides utilities to:
|
|
7
|
+
1. Parse SessionId from environment variable
|
|
8
|
+
2. Extract the Guid portion
|
|
9
|
+
3. Generate NNG IPC URLs for streaming results
|
|
10
|
+
4. Read explicit NNG URLs from environment variables (preferred)
|
|
11
|
+
|
|
12
|
+
## URL Configuration Priority
|
|
13
|
+
|
|
14
|
+
The SDK supports two ways to configure NNG URLs:
|
|
15
|
+
|
|
16
|
+
1. **Explicit URLs (PREFERRED)** - Set by rocket-welder2:
|
|
17
|
+
- SEGMENTATION_SINK_URL
|
|
18
|
+
- KEYPOINTS_SINK_URL
|
|
19
|
+
- ACTIONS_SINK_URL
|
|
20
|
+
|
|
21
|
+
2. **Derived from SessionId (FALLBACK)** - For backwards compatibility:
|
|
22
|
+
- SessionId env var → parse GUID → generate URLs
|
|
23
|
+
|
|
24
|
+
Use `get_nng_urls_from_env()` for explicit URLs (preferred).
|
|
25
|
+
Use `get_nng_urls(session_id)` for SessionId-derived URLs (fallback).
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import logging
|
|
31
|
+
import os
|
|
32
|
+
import uuid
|
|
33
|
+
|
|
34
|
+
logger = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
SESSION_ID_PREFIX = "ps-"
|
|
37
|
+
SESSION_ID_ENV_VAR = "SessionId"
|
|
38
|
+
|
|
39
|
+
# Explicit URL environment variables (set by rocket-welder2)
|
|
40
|
+
SEGMENTATION_SINK_URL_ENV = "SEGMENTATION_SINK_URL"
|
|
41
|
+
KEYPOINTS_SINK_URL_ENV = "KEYPOINTS_SINK_URL"
|
|
42
|
+
ACTIONS_SINK_URL_ENV = "ACTIONS_SINK_URL"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def parse_session_id(session_id: str) -> uuid.UUID:
|
|
46
|
+
"""Parse SessionId (ps-{guid}) to extract Guid.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
session_id: SessionId string (e.g., "ps-a1b2c3d4-...")
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
UUID extracted from SessionId
|
|
53
|
+
|
|
54
|
+
Raises:
|
|
55
|
+
ValueError: If session_id format is invalid
|
|
56
|
+
|
|
57
|
+
Examples:
|
|
58
|
+
>>> parse_session_id("ps-a1b2c3d4-e5f6-7890-abcd-ef1234567890")
|
|
59
|
+
UUID('a1b2c3d4-e5f6-7890-abcd-ef1234567890')
|
|
60
|
+
>>> parse_session_id("a1b2c3d4-e5f6-7890-abcd-ef1234567890") # backwards compat
|
|
61
|
+
UUID('a1b2c3d4-e5f6-7890-abcd-ef1234567890')
|
|
62
|
+
"""
|
|
63
|
+
if session_id.startswith(SESSION_ID_PREFIX):
|
|
64
|
+
return uuid.UUID(session_id[len(SESSION_ID_PREFIX) :])
|
|
65
|
+
# Fallback: try parsing as raw guid for backwards compatibility
|
|
66
|
+
return uuid.UUID(session_id)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def get_session_id_from_env() -> str | None:
|
|
70
|
+
"""Get SessionId from environment variable.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
SessionId string or None if not set
|
|
74
|
+
"""
|
|
75
|
+
return os.environ.get(SESSION_ID_ENV_VAR)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def get_nng_urls(session_id: str) -> dict[str, str]:
|
|
79
|
+
"""Generate NNG IPC URLs from SessionId.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
session_id: SessionId string (e.g., "ps-a1b2c3d4-...")
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
Dictionary with 'segmentation', 'keypoints', 'actions' URLs
|
|
86
|
+
|
|
87
|
+
Examples:
|
|
88
|
+
>>> urls = get_nng_urls("ps-a1b2c3d4-e5f6-7890-abcd-ef1234567890")
|
|
89
|
+
>>> urls["segmentation"]
|
|
90
|
+
'ipc:///tmp/rw-a1b2c3d4-e5f6-7890-abcd-ef1234567890-seg.sock'
|
|
91
|
+
"""
|
|
92
|
+
guid = parse_session_id(session_id)
|
|
93
|
+
return {
|
|
94
|
+
"segmentation": f"ipc:///tmp/rw-{guid}-seg.sock",
|
|
95
|
+
"keypoints": f"ipc:///tmp/rw-{guid}-kp.sock",
|
|
96
|
+
"actions": f"ipc:///tmp/rw-{guid}-actions.sock",
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_segmentation_url(session_id: str) -> str:
|
|
101
|
+
"""Get NNG URL for segmentation stream.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
session_id: SessionId string (e.g., "ps-a1b2c3d4-...")
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
IPC URL for segmentation stream
|
|
108
|
+
"""
|
|
109
|
+
guid = parse_session_id(session_id)
|
|
110
|
+
return f"ipc:///tmp/rw-{guid}-seg.sock"
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def get_keypoints_url(session_id: str) -> str:
|
|
114
|
+
"""Get NNG URL for keypoints stream.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
session_id: SessionId string (e.g., "ps-a1b2c3d4-...")
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
IPC URL for keypoints stream
|
|
121
|
+
"""
|
|
122
|
+
guid = parse_session_id(session_id)
|
|
123
|
+
return f"ipc:///tmp/rw-{guid}-kp.sock"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def get_actions_url(session_id: str) -> str:
|
|
127
|
+
"""Get NNG URL for actions stream.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
session_id: SessionId string (e.g., "ps-a1b2c3d4-...")
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
IPC URL for actions stream
|
|
134
|
+
"""
|
|
135
|
+
guid = parse_session_id(session_id)
|
|
136
|
+
return f"ipc:///tmp/rw-{guid}-actions.sock"
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# ============================================================================
|
|
140
|
+
# Explicit URL functions (PREFERRED - URLs set by rocket-welder2)
|
|
141
|
+
# ============================================================================
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def get_nng_urls_from_env() -> dict[str, str | None]:
|
|
145
|
+
"""Get NNG URLs from explicit environment variables.
|
|
146
|
+
|
|
147
|
+
This is the PREFERRED method for getting NNG URLs. rocket-welder2
|
|
148
|
+
sets these environment variables when starting containers.
|
|
149
|
+
|
|
150
|
+
Returns:
|
|
151
|
+
Dictionary with 'segmentation', 'keypoints', 'actions' URLs.
|
|
152
|
+
Values are None if not configured.
|
|
153
|
+
|
|
154
|
+
Examples:
|
|
155
|
+
>>> os.environ["SEGMENTATION_SINK_URL"] = "ipc:///tmp/rw-abc-seg.sock"
|
|
156
|
+
>>> urls = get_nng_urls_from_env()
|
|
157
|
+
>>> urls["segmentation"]
|
|
158
|
+
'ipc:///tmp/rw-abc-seg.sock'
|
|
159
|
+
"""
|
|
160
|
+
return {
|
|
161
|
+
"segmentation": os.environ.get(SEGMENTATION_SINK_URL_ENV),
|
|
162
|
+
"keypoints": os.environ.get(KEYPOINTS_SINK_URL_ENV),
|
|
163
|
+
"actions": os.environ.get(ACTIONS_SINK_URL_ENV),
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def get_segmentation_url_from_env() -> str | None:
|
|
168
|
+
"""Get segmentation NNG URL from environment variable.
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
IPC URL for segmentation stream, or None if not configured.
|
|
172
|
+
"""
|
|
173
|
+
return os.environ.get(SEGMENTATION_SINK_URL_ENV)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def get_keypoints_url_from_env() -> str | None:
|
|
177
|
+
"""Get keypoints NNG URL from environment variable.
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
IPC URL for keypoints stream, or None if not configured.
|
|
181
|
+
"""
|
|
182
|
+
return os.environ.get(KEYPOINTS_SINK_URL_ENV)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def get_actions_url_from_env() -> str | None:
|
|
186
|
+
"""Get actions NNG URL from environment variable.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
IPC URL for actions stream, or None if not configured.
|
|
190
|
+
"""
|
|
191
|
+
return os.environ.get(ACTIONS_SINK_URL_ENV)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def has_explicit_nng_urls() -> bool:
|
|
195
|
+
"""Check if explicit NNG URLs are configured.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
True if at least segmentation OR keypoints URL is configured.
|
|
199
|
+
"""
|
|
200
|
+
urls = get_nng_urls_from_env()
|
|
201
|
+
return bool(urls["segmentation"] or urls["keypoints"])
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def get_configured_nng_urls() -> dict[str, str]:
|
|
205
|
+
"""Get all configured NNG URLs (explicit or derived from SessionId).
|
|
206
|
+
|
|
207
|
+
Priority:
|
|
208
|
+
1. Explicit URLs from environment (SEGMENTATION_SINK_URL, etc.)
|
|
209
|
+
2. Derived from SessionId environment variable (fallback)
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
Dictionary with 'segmentation', 'keypoints', 'actions' URLs.
|
|
213
|
+
Only includes URLs that are actually configured.
|
|
214
|
+
|
|
215
|
+
Raises:
|
|
216
|
+
ValueError: If no NNG URLs are configured (neither explicit nor SessionId).
|
|
217
|
+
"""
|
|
218
|
+
# Try explicit URLs first (preferred)
|
|
219
|
+
explicit_urls = get_nng_urls_from_env()
|
|
220
|
+
result: dict[str, str] = {}
|
|
221
|
+
|
|
222
|
+
for name, url in explicit_urls.items():
|
|
223
|
+
if url:
|
|
224
|
+
result[name] = url
|
|
225
|
+
|
|
226
|
+
# If we have at least one explicit URL, return what we have
|
|
227
|
+
if result:
|
|
228
|
+
return result
|
|
229
|
+
|
|
230
|
+
# Fallback: derive from SessionId
|
|
231
|
+
session_id = get_session_id_from_env()
|
|
232
|
+
if session_id:
|
|
233
|
+
return get_nng_urls(session_id)
|
|
234
|
+
|
|
235
|
+
raise ValueError(
|
|
236
|
+
"No NNG URLs configured. Set SEGMENTATION_SINK_URL/KEYPOINTS_SINK_URL "
|
|
237
|
+
"environment variables, or set SessionId for URL derivation."
|
|
238
|
+
)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Transport layer for RocketWelder SDK.
|
|
3
|
+
|
|
4
|
+
Provides transport-agnostic frame sink/source abstractions for protocols.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .frame_sink import IFrameSink, NullFrameSink
|
|
8
|
+
from .frame_source import IFrameSource
|
|
9
|
+
from .nng_transport import NngFrameSink, NngFrameSource
|
|
10
|
+
from .stream_transport import StreamFrameSink, StreamFrameSource
|
|
11
|
+
from .tcp_transport import TcpFrameSink, TcpFrameSource
|
|
12
|
+
from .unix_socket_transport import (
|
|
13
|
+
UnixSocketFrameSink,
|
|
14
|
+
UnixSocketFrameSource,
|
|
15
|
+
UnixSocketServer,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"IFrameSink",
|
|
20
|
+
"IFrameSource",
|
|
21
|
+
"NngFrameSink",
|
|
22
|
+
"NngFrameSource",
|
|
23
|
+
"NullFrameSink",
|
|
24
|
+
"StreamFrameSink",
|
|
25
|
+
"StreamFrameSource",
|
|
26
|
+
"TcpFrameSink",
|
|
27
|
+
"TcpFrameSource",
|
|
28
|
+
"UnixSocketFrameSink",
|
|
29
|
+
"UnixSocketFrameSource",
|
|
30
|
+
"UnixSocketServer",
|
|
31
|
+
]
|