Repository navigation
Expand file tree
/
Copy pathshared_framed.py
More file actions
552 lines (493 loc) · 22.5 KB
/
Copy pathshared_framed.py
File metadata and controls
552 lines (493 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
"""Public framed transport facade over the shared listener."""
from __future__ import annotations
import asyncio
from typing import Any, Callable
from ...link_models import EybondLinkRoute, LinkRoute
from ...models import CollectorInfo
from ..protocol import EybondHeader
from .common import _bounded_write_timeout, _copy_collector_info
from .connections import _CollectorConnection
from .listener import (
_LISTENERS,
_LISTENERS_LOCK,
_SharedEybondListener,
_acquire_shared_payload_listener,
_release_shared_listener,
)
class SharedEybondTransport:
"""One per-entry transport facade backed by a shared TCP listener."""
def __init__(
self,
*,
host: str,
port: int,
request_timeout: float,
heartbeat_interval: float,
collector_ip: str,
collector_pn: str = "",
collector_session_protocol: str = "",
collector_identity_strategy: str = "",
collector_raw_passthrough_bootstrap: str = "",
collector_raw_passthrough_frame_format: str = "",
collector_raw_passthrough_min_interval_ms: int = 0,
) -> None:
self._host = host
self._port = int(port)
self._request_timeout = request_timeout
self._write_timeout = _bounded_write_timeout(request_timeout)
self._heartbeat_interval = float(heartbeat_interval)
self._collector_ip = collector_ip
self._collector_pn = str(collector_pn or "").strip()
# DURABLE confirmed protocol owner value. Only the runtime passes the
# validated confirmed protocol here; onboarding never does, so an
# inferred/expected hint can never register a durable confirmed owner.
self._collector_session_protocol = str(collector_session_protocol or "").strip().lower()
# Registry-mediated claim: the runtime injects a resolver that returns
# the registry-chosen session id for this collector, so the claim targets
# exactly the owned session instead of re-deriving ownership by IP/PN.
self._claimed_session_provider: Any = None
self._collector_identity_strategy = str(collector_identity_strategy or "").strip().lower()
self._collector_raw_passthrough_bootstrap = (
str(collector_raw_passthrough_bootstrap or "").strip().lower()
)
self._collector_raw_passthrough_frame_format = (
str(collector_raw_passthrough_frame_format or "").strip().lower()
)
self._collector_raw_passthrough_min_interval_ms = max(
0,
int(collector_raw_passthrough_min_interval_ms or 0),
)
self._listener: _SharedEybondListener | None = None
self._connection_watcher_callback: Callable[[str], None] | None = None
self._connection_watcher_token: int | None = None
def set_connection_watcher(self, callback: Callable[[str], None] | None) -> None:
"""Fire ``callback(remote_ip)`` whenever this collector dials back in.
Registered on the shared listener once the transport starts; safe to
call before ``start()``.
"""
if self._listener is not None and self._connection_watcher_token is not None:
self._listener.remove_connection_watcher(self._connection_watcher_token)
self._connection_watcher_token = None
self._connection_watcher_callback = callback
if callback is not None and self._listener is not None:
self._connection_watcher_token = self._listener.add_connection_watcher(
self._collector_ip,
callback,
)
@property
def connected(self) -> bool:
connection = self._connection(create_placeholder=False)
return connection.connected if connection is not None else False
@property
def collector_info(self) -> CollectorInfo:
connection = self._connection(create_placeholder=False)
if connection is not None:
return connection.collector_info
return _copy_collector_info(
CollectorInfo(remote_ip=self._collector_ip, collector_pn=self._collector_pn)
)
async def start(self) -> None:
if self._listener is not None:
return
self._listener = await _acquire_shared_payload_listener(
self._host,
self._port,
self._collector_ip,
self._collector_pn,
self._collector_session_protocol,
)
if self._connection_watcher_callback is not None and self._connection_watcher_token is None:
self._connection_watcher_token = self._listener.add_connection_watcher(
self._collector_ip,
self._connection_watcher_callback,
)
self._connection(create_placeholder=bool(self._collector_ip))
async def stop(self, *, preserve_session_id: str = "") -> None:
"""Release this facade, optionally leaving one exact socket observable.
The preservation hook is for exact-session lifecycle boundaries: a
strongly identified session may have to survive scan-to-admission,
runtime reload/rebuild, or unload-to-removal finalization. It never
preserves by route or peer address.
"""
if self._listener is None:
return
preserve_session_id = str(preserve_session_id or "").strip()
listener = self._listener
self._listener = None
if self._connection_watcher_token is not None:
listener.remove_connection_watcher(self._connection_watcher_token)
self._connection_watcher_token = None
await _release_shared_listener(
listener,
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
collector_session_protocol=self._collector_session_protocol,
close_payload=True,
close_pending=True,
unregister_payload_owner=True,
unregister_payload_pn_owner=True,
unregister_session_protocol_owner=True,
preserve_session_id=preserve_session_id,
)
async def async_snapshot_shared_connection(self) -> _CollectorConnection | None:
if not self._collector_ip and not self._collector_pn:
return None
async with _LISTENERS_LOCK:
listener = _LISTENERS.get((self._host, self._port))
if listener is None:
return None
if self._collector_pn:
connection = listener._connection_by_collector_pn(
self._collector_pn,
listener._connections_by_pn,
)
else:
connection = listener._connections.get(self._collector_ip)
if connection is None or not connection.connected:
return None
return connection
def session_inventory_diagnostics(self) -> dict[str, object]:
if self._listener is None:
return {
"pending_session_count": 0,
"recent_session_count": 0,
"duplicate_peer_ip_count": 0,
"duplicate_peer_ips": [],
"sessions": [],
}
return self._listener.session_inventory_diagnostics()
@property
def listener_key(self) -> str:
"""Return a stable, public identity for the shared listener this uses.
Runtime code dedups transports that share one listener by this key
instead of ``id(transport._listener)``, so it never touches listener
internals.
"""
return f"{self._host}:{self._port}"
def observed_collector_sessions(self) -> tuple[dict[str, object], ...]:
"""Return raw observed inbound sessions on this listener (public facade).
Runtime session ownership/negotiation reads through this instead of
reaching into the listener's private ``_session_inventory``. Each dict is
stamped with the listener port so the callback session registry can build
a SessionHandle without knowing listener internals.
"""
if self._listener is None:
return ()
sessions: list[dict[str, object]] = []
for session in self._listener.discovered_collector_sessions():
if not isinstance(session, dict):
continue
enriched = dict(session)
enriched.setdefault("listener_port", int(self._port))
sessions.append(enriched)
return tuple(sessions)
async def async_disconnect_if_new_shared_connection(
self,
snapshot: _CollectorConnection | None,
) -> None:
if not self._collector_ip and not self._collector_pn:
return
async with _LISTENERS_LOCK:
listener = _LISTENERS.get((self._host, self._port))
if listener is None:
return
if self._collector_pn:
connection = listener._connection_by_collector_pn(
self._collector_pn,
listener._connections_by_pn,
)
else:
connection = listener._connections.get(self._collector_ip)
if connection is None or connection is snapshot:
return
await connection.disconnect()
async def disconnect(self) -> None:
connection = self._connection(create_placeholder=False)
if connection is None:
return
await connection.disconnect()
def set_collector_ip(self, collector_ip: str) -> None:
self._collector_ip = collector_ip
self._connection(create_placeholder=bool(self._collector_ip))
def set_claimed_session_provider(self, provider: Any) -> None:
"""Set the runtime's registry-mediated claimed-session-id resolver."""
self._claimed_session_provider = provider
def set_confirmed_session_protocol(self, protocol: str) -> None:
"""Set the CONFIRMED session-protocol owner on the shared listener.
Idempotent. This is the durable-probe-permission counterpart of
``set_negotiated_wire`` (live activation): a confirmed protocol owner lets
the listener send a safe identity probe to a SILENT session of this
collector. It is distinct from the live wire. Only a confirmed wire
(``eybond_framed`` / ``at_text``) may own; anything else clears the owner.
The inferred/expected cloud-family protocol must never reach this method.
Before the listener is acquired this only stores the value (applied at
``start()``). Once acquired it dynamically unregisters the old owner and
registers the new one WITHOUT rebuilding the TCP listener; re-setting the
same value is a no-op; ``stop()`` unregisters exactly the current value
once (no double unregister / leak).
"""
normalized = str(protocol or "").strip().lower()
if normalized not in ("at_text", "eybond_framed"):
normalized = ""
if normalized == self._collector_session_protocol:
return
listener = self._listener
if listener is not None:
if self._collector_session_protocol:
listener.unregister_session_protocol_owner(
self._collector_session_protocol
)
if normalized:
listener.register_session_protocol_owner(normalized)
self._collector_session_protocol = normalized
def _resolve_claimed_session_id(self) -> str:
provider = self._claimed_session_provider
if not callable(provider):
return ""
try:
return str(provider() or "").strip()
except Exception:
return ""
async def wait_until_connected(self, timeout: float) -> bool:
if self._listener is None:
return False
deadline = asyncio.get_running_loop().time() + timeout
while True:
listener = self._listener
if listener is None:
return False
connection = self._connection(create_placeholder=bool(self._collector_ip))
if connection is not None and connection.connected:
return True
# Re-resolved every iteration: the runtime's registry-mediated
# provider may learn (or lose) the claimed session while we wait.
claimed_session_id = self._resolve_claimed_session_id()
if self._collector_ip or self._collector_pn or claimed_session_id:
pending = await listener.pop_pending_socket_for_route(
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
session_protocol=self._collector_session_protocol,
session_id=claimed_session_id,
)
if pending is not None:
connection = await listener.activate_pending_connection(
pending,
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
heartbeat_interval=self._heartbeat_interval,
write_timeout=self._write_timeout,
)
if connection.connected:
return True
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
if connection is not None and (
self._collector_ip or self._collector_pn or claimed_session_id
):
ok = await connection.wait_until_connected(timeout=min(0.1, remaining))
if ok:
return True
continue
await asyncio.sleep(min(0.1, remaining))
async def wait_until_heartbeat(self, timeout: float) -> bool:
if self._listener is None:
return False
deadline = asyncio.get_running_loop().time() + timeout
while True:
connection = self._connection(create_placeholder=bool(self._collector_ip))
if connection is not None and connection.connected:
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
return await connection.wait_until_heartbeat(timeout=remaining)
claimed_session_id = self._resolve_claimed_session_id()
if self._collector_ip or self._collector_pn or claimed_session_id:
pending = await self._listener.pop_pending_socket_for_route(
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
session_protocol=self._collector_session_protocol,
session_id=claimed_session_id,
)
if pending is not None:
connection = await self._listener.activate_pending_connection(
pending,
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
heartbeat_interval=self._heartbeat_interval,
write_timeout=self._write_timeout,
)
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
return await connection.wait_until_heartbeat(timeout=remaining)
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
await asyncio.sleep(min(0.1, remaining))
async def wait_until_liveness(self, timeout: float) -> bool:
"""Wait for recent correlated traffic on the exact selected session."""
if self._listener is None:
return False
deadline = asyncio.get_running_loop().time() + timeout
while True:
connection = self._connection(create_placeholder=bool(self._collector_ip))
if connection is not None and connection.connected:
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
return await connection.wait_until_liveness(timeout=remaining)
claimed_session_id = self._resolve_claimed_session_id()
if self._collector_ip or self._collector_pn or claimed_session_id:
pending = await self._listener.pop_pending_socket_for_route(
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
session_protocol=self._collector_session_protocol,
session_id=claimed_session_id,
)
if pending is not None:
connection = await self._listener.activate_pending_connection(
pending,
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
heartbeat_interval=self._heartbeat_interval,
write_timeout=self._write_timeout,
)
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
return await connection.wait_until_liveness(timeout=remaining)
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
return False
await asyncio.sleep(min(0.1, remaining))
async def async_send_forward(
self,
payload: bytes,
*,
devcode: int,
collector_addr: int,
) -> bytes:
connection = await self._active_connection_for_send()
return await connection.async_send_forward(
payload,
devcode=devcode,
collector_addr=collector_addr,
request_timeout=self._request_timeout,
)
async def async_send_auxiliary_read(
self,
payload: bytes,
*,
request_timeout: float,
) -> bytes:
"""Delegate the internal read-only side channel; never infer from magic.
Ownership, documented-query gating, and framing stay on the socket
connection. This facade only resolves the active connection so callers
need not touch ``connections.py``. Driver/catalog admission for live PV
remains separate work.
"""
connection = await self._active_connection_for_send()
return await connection.async_send_auxiliary_read(
payload,
request_timeout=request_timeout,
)
async def async_send_payload(
self,
payload: bytes,
*,
route: LinkRoute,
) -> bytes:
if not isinstance(route, EybondLinkRoute):
raise TypeError(f"unsupported_link_route:{route.family}")
return await self.async_send_forward(
payload,
devcode=route.devcode,
collector_addr=route.collector_addr,
)
async def async_send_collector(
self,
*,
fcode: int,
payload: bytes = b"",
devcode: int = 0,
collector_addr: int = 1,
) -> tuple[EybondHeader, bytes]:
connection = await self._active_connection_for_send()
return await connection.async_send_collector(
fcode=fcode,
payload=payload,
devcode=devcode,
collector_addr=collector_addr,
request_timeout=self._request_timeout,
)
async def _active_connection_for_send(self) -> _CollectorConnection:
connection = self._connection(create_placeholder=bool(self._collector_ip))
if connection is not None and connection.connected:
return connection
if self._listener is None:
raise ConnectionError("collector_not_connected")
claimed_session_id = self._resolve_claimed_session_id()
if self._collector_ip or self._collector_pn or claimed_session_id:
pending = await self._listener.pop_pending_socket_for_route(
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
session_protocol=self._collector_session_protocol,
session_id=claimed_session_id,
)
if pending is not None:
return await self._listener.activate_pending_connection(
pending,
collector_ip=self._collector_ip,
collector_pn=self._collector_pn,
heartbeat_interval=self._heartbeat_interval,
write_timeout=self._write_timeout,
)
if connection is None or not connection.connected:
raise ConnectionError("collector_not_connected")
return connection
def _connection(self, *, create_placeholder: bool) -> _CollectorConnection | None:
if self._listener is None:
return None
claimed_session_id = self._resolve_claimed_session_id()
if claimed_session_id:
connection = self._listener.payload_connection_for_session(
claimed_session_id
)
if connection is not None:
return connection
if not self._collector_ip and not self._collector_pn:
# A session-pinned-ONLY transport (the callback identity read):
# the claimed socket either resolves or there is nothing this
# transport may talk to. The "current connection" fallback below
# hands back an arbitrary live socket -- exactly the
# substitution a session claim exists to prevent. A claimed but
# still-parked socket is activated by the pop path, not here.
return None
if self._collector_pn:
connection = self._listener.ensure_connection(
"",
self._heartbeat_interval,
self._write_timeout,
self._collector_pn,
)
if connection is not None:
return connection
if create_placeholder:
return self._listener.ensure_connection(
self._collector_ip,
self._heartbeat_interval,
self._write_timeout,
self._collector_pn,
)
if self._collector_ip:
return self._listener.ensure_connection(
self._collector_ip,
self._heartbeat_interval,
self._write_timeout,
self._collector_pn,
)
return self._listener.current_connection(
heartbeat_interval=self._heartbeat_interval,
write_timeout=self._write_timeout,
)