chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt
This commit is contained in:
@ -1,16 +1,16 @@
|
||||
/*! 🚌 MessageBus - Process-wide Message Routing Singleton
|
||||
*
|
||||
*
|
||||
* ## 📝 概要
|
||||
* MessageBusは、プロセス内でのメッセージルーティングを管理する
|
||||
* シングルトンコンポーネントです。すべてのP2PBoxノードが共有し、
|
||||
* ローカル通信の高速配送を実現します。
|
||||
*
|
||||
*
|
||||
* ## 🏗️ 設計
|
||||
* - **Singleton Pattern**: プロセス内で唯一のインスタンス
|
||||
* - **Node Registry**: 登録されたノードの管理
|
||||
* - **Handler Management**: イベントハンドラーの管理
|
||||
* - **Async Safe**: Arc<Mutex>による並行アクセス対応
|
||||
*
|
||||
*
|
||||
* ## 🚀 機能
|
||||
* - ノードの登録・解除
|
||||
* - メッセージルーティング
|
||||
@ -19,9 +19,9 @@
|
||||
*/
|
||||
|
||||
use crate::boxes::IntentBox;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::collections::HashMap;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// Intent処理ハンドラーの型
|
||||
pub type IntentHandler = Box<dyn Fn(IntentBox, &str) + Send + Sync>;
|
||||
@ -40,20 +40,21 @@ impl BusEndpoint {
|
||||
handlers: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// イベントハンドラーを追加
|
||||
pub fn add_handler(&self, intent_name: &str, handler: IntentHandler) {
|
||||
let mut handlers = self.handlers.lock().unwrap();
|
||||
handlers.entry(intent_name.to_string())
|
||||
handlers
|
||||
.entry(intent_name.to_string())
|
||||
.or_insert_with(Vec::new)
|
||||
.push(handler);
|
||||
}
|
||||
|
||||
|
||||
/// メッセージを配送
|
||||
pub fn deliver(&self, intent: IntentBox, from: &str) {
|
||||
let handlers = self.handlers.lock().unwrap();
|
||||
let intent_name = intent.get_name().to_string_box().value;
|
||||
|
||||
|
||||
if let Some(intent_handlers) = handlers.get(&intent_name) {
|
||||
for handler in intent_handlers {
|
||||
handler(intent.clone(), from);
|
||||
@ -95,27 +96,32 @@ impl MessageBusData {
|
||||
nodes: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// ノードを登録
|
||||
pub fn register_node(&mut self, id: String, endpoint: BusEndpoint) {
|
||||
self.nodes.insert(id, endpoint);
|
||||
}
|
||||
|
||||
|
||||
/// ノードを解除
|
||||
pub fn unregister_node(&mut self, id: &str) -> bool {
|
||||
self.nodes.remove(id).is_some()
|
||||
}
|
||||
|
||||
|
||||
/// ノードが存在するかチェック
|
||||
pub fn node_exists(&self, id: &str) -> bool {
|
||||
self.nodes.contains_key(id)
|
||||
}
|
||||
|
||||
|
||||
/// メッセージをルーティング
|
||||
pub fn route(&self, to: &str, intent: IntentBox, from: &str) -> Result<(), SendError> {
|
||||
if let Some(endpoint) = self.nodes.get(to) {
|
||||
if std::env::var("NYASH_DEBUG_P2P").unwrap_or_default() == "1" {
|
||||
eprintln!("[MessageBus] route {} -> {} intent={}", from, to, intent.get_name().to_string_box().value);
|
||||
eprintln!(
|
||||
"[MessageBus] route {} -> {} intent={}",
|
||||
from,
|
||||
to,
|
||||
intent.get_name().to_string_box().value
|
||||
);
|
||||
}
|
||||
endpoint.deliver(intent, from);
|
||||
Ok(())
|
||||
@ -123,7 +129,7 @@ impl MessageBusData {
|
||||
Err(SendError::NodeNotFound(format!("Node '{}' not found", to)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 登録されたノード一覧を取得
|
||||
pub fn get_nodes(&self) -> Vec<String> {
|
||||
self.nodes.keys().cloned().collect()
|
||||
@ -143,9 +149,8 @@ impl MessageBusData {
|
||||
}
|
||||
|
||||
/// グローバルMessageBusシングルトン
|
||||
static GLOBAL_MESSAGE_BUS: Lazy<MessageBus> = Lazy::new(|| {
|
||||
Arc::new(Mutex::new(MessageBusData::new()))
|
||||
});
|
||||
static GLOBAL_MESSAGE_BUS: Lazy<MessageBus> =
|
||||
Lazy::new(|| Arc::new(Mutex::new(MessageBusData::new())));
|
||||
|
||||
impl MessageBusData {
|
||||
/// グローバルMessageBusへのアクセス
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
/*! 📡 Messaging Module - P2P Communication Infrastructure
|
||||
*
|
||||
*
|
||||
* This module provides the core messaging infrastructure for P2P communication
|
||||
* in Nyash, implementing the MessageBus singleton pattern for local message routing.
|
||||
*/
|
||||
|
||||
pub mod message_bus;
|
||||
|
||||
pub use message_bus::{MessageBus, MessageBusData, BusEndpoint, IntentHandler, SendError};
|
||||
pub use message_bus::{BusEndpoint, IntentHandler, MessageBus, MessageBusData, SendError};
|
||||
|
||||
Reference in New Issue
Block a user