forked from shinkuan/Akagi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_bus.rs
More file actions
117 lines (98 loc) · 4.61 KB
/
Copy pathevent_bus.rs
File metadata and controls
117 lines (98 loc) · 4.61 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
//! In-process broadcast buses connecting Akagi's subsystems.
//!
//! Five buses, all `tokio::sync::broadcast::Sender`-typed:
//!
//! - [`MjaiBus`]: every `MjaiEvent` parsed by a platform bridge is fanned
//! out here. Producers: bridge → proxy handler. Consumers: `BotManager`,
//! `ipc` forwarder, future HUD/storage/WS server.
//! - [`BotResponseBus`]: every `BotResponse` from the active `BotRunner`.
//! Producer: `BotManager`. Consumers: `ipc` forwarder, future HUD /
//! external WS / replay recorder.
//! - [`BotStatusBus`]: lifecycle of the active bot subprocess
//! (`Idle/Loading/Ready/Error/Stopped`). Producer: `BotManager`.
//! Consumer: `ipc` forwarder (UI loading spinner).
//! - [`CaptureStatusBus`]: lifecycle of the active capture backend
//! (`Stopped/Starting/Running/Error` × `kind: Mitm | Chromium`).
//! Producer: `ipc::commands` / capture supervisor. Consumer: `ipc`
//! forwarder.
//! - [`NotifyBus`]: ad-hoc toast notifications. Any subsystem may push;
//! `ipc` forwards to the frontend as `notify` events.
//!
//! Channel capacity is fixed-size — slow consumers see `RecvError::Lagged`
//! rather than blocking the producer. That's the right trade-off for a
//! real-time analyzer: if the HUD falls behind, drop and resync rather
//! than stall the proxy.
use crate::analysis::result::AnalysisResult;
use crate::bot::BotResponse;
use crate::schema::{BotStatus, CaptureStatus, HistoryEvent, MjaiEvent, Notification};
use tokio::sync::broadcast;
/// Fan-out for `MjaiEvent`s from platform bridges.
pub type MjaiBus = broadcast::Sender<MjaiEvent>;
/// Fan-out for `BotResponse`s from the active bot.
pub type BotResponseBus = broadcast::Sender<BotResponse>;
/// Fan-out for `BotStatus` lifecycle transitions.
pub type BotStatusBus = broadcast::Sender<BotStatus>;
/// Fan-out for `CaptureStatus` lifecycle transitions.
pub type CaptureStatusBus = broadcast::Sender<CaptureStatus>;
/// Fan-out for transient `Notification`s pushed at the user.
pub type NotifyBus = broadcast::Sender<Notification>;
/// Fan-out for `AnalysisResult`s produced after each game-state update.
/// Producer: `analysis::runner`. Consumers: `ipc` forwarder, future HUD.
pub type AnalysisBus = broadcast::Sender<AnalysisResult>;
/// Post-tracker fan-out: each `MjaiEvent` re-emitted *after* the
/// `GameTracker` has applied it to the engine state. Subscribers can rely
/// on the live game-state mirror being current when this fires (vs. the
/// raw `MjaiBus` where ordering against the tracker is racy).
pub type PostTrackerBus = broadcast::Sender<MjaiEvent>;
/// Fan-out for game-history lifecycle events. Producer:
/// `crate::history::recorder` (on each finalised game / deletion).
/// Consumer: `ipc` forwarder, which emits `history-recorded` to the
/// frontend.
pub type HistoryBus = broadcast::Sender<HistoryEvent>;
/// Default capacity. Live pacing produces ~1 second of mjai events at a time
/// (start_kyoku + 13 tehai + a few tsumo/dahai pairs), which is tiny. The
/// sizing constraint is the **one-shot GameRestore replay**: on reconnect the
/// bridge emits an entire kyoku's events in a single synchronous burst (the
/// CDP send loop does not yield between `send`s), and consumers treat overflow
/// as `Lagged → skip`. A skipped mid-hand `dahai`/`pon` would silently corrupt
/// the game-state tracker with no self-heal until the next kyoku, so the buffer
/// must comfortably exceed a worst-case full-kyoku event count (~a few hundred).
pub const DEFAULT_CAPACITY: usize = 1024;
/// Smaller buffer for status / notification streams — these are bursty
/// but low-rate; 64 is plenty.
pub const STATUS_CAPACITY: usize = 64;
pub fn mjai_bus() -> MjaiBus {
// Drop the placeholder receiver — real consumers subscribe later via
// `Sender::subscribe`. The sender stays alive as long as anyone holds
// a clone of it.
let (tx, _rx) = broadcast::channel(DEFAULT_CAPACITY);
tx
}
pub fn bot_response_bus() -> BotResponseBus {
let (tx, _rx) = broadcast::channel(DEFAULT_CAPACITY);
tx
}
pub fn bot_status_bus() -> BotStatusBus {
let (tx, _rx) = broadcast::channel(STATUS_CAPACITY);
tx
}
pub fn capture_status_bus() -> CaptureStatusBus {
let (tx, _rx) = broadcast::channel(STATUS_CAPACITY);
tx
}
pub fn notify_bus() -> NotifyBus {
let (tx, _rx) = broadcast::channel(STATUS_CAPACITY);
tx
}
pub fn analysis_bus() -> AnalysisBus {
let (tx, _rx) = broadcast::channel(DEFAULT_CAPACITY);
tx
}
pub fn post_tracker_bus() -> PostTrackerBus {
let (tx, _rx) = broadcast::channel(DEFAULT_CAPACITY);
tx
}
pub fn history_bus() -> HistoryBus {
let (tx, _rx) = broadcast::channel(STATUS_CAPACITY);
tx
}