🚀 feat: P2PBox/IntentBox実装 - NyaMesh通信基盤の第一歩
## 🎯 概要 NyaMeshプロジェクトの基盤となるP2PBox/IntentBoxを実装。 シンプルなsend/onインターフェースで通信ノード間のメッセージングを実現。 ## ✨ 新機能 - **IntentBox**: 通信世界を定義するコンテナ - Transportトレイトで通信方式を抽象化 - LocalTransport実装(プロセス内通信) - 将来のWebSocket/SharedMemory拡張に対応 - **P2PBox**: 通信ノードの実装 - send(intent, data, target) - 特定ノードへ送信 - broadcast(intent, data) - 全ノードへ配信 - on(intent, callback) - リスナー登録 - off(intent) - リスナー解除 - 同一intentに複数リスナー登録可能 ## 🔧 技術詳細 - Arc<Mutex>パターンで完全なスレッドセーフティ - Arc<P2PBoxInner>構造でBox型システムとの整合性確保 - インタープリター完全統合(new/メソッド呼び出し) ## 🧪 テスト - test_p2p_basic.nyash - 基本機能検証 - test_p2p_message_types.nyash - 各種データ型対応 - test_p2p_edge_cases.nyash - エラー処理 - test_p2p_callback_demo.nyash - 実用例 ## 📝 TODO (将来拡張) - WebSocket/SharedMemoryトランスポート - コールバック実行(MethodBox統合待ち) - ノード登録管理システム 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
137
src/interpreter/methods/p2p_methods.rs
Normal file
137
src/interpreter/methods/p2p_methods.rs
Normal file
@ -0,0 +1,137 @@
|
||||
/*! 📡 P2P通信メソッド実装
|
||||
* IntentBoxとP2PBoxのNyashインタープリター統合
|
||||
*/
|
||||
|
||||
use crate::interpreter::core::NyashInterpreter;
|
||||
use crate::interpreter::core::RuntimeError;
|
||||
use crate::ast::ASTNode;
|
||||
use crate::box_trait::{NyashBox, StringBox};
|
||||
use crate::boxes::{IntentBox, P2PBox};
|
||||
|
||||
impl NyashInterpreter {
|
||||
/// IntentBoxのメソッド実行
|
||||
pub(in crate::interpreter) fn execute_intent_box_method(
|
||||
&mut self,
|
||||
intent_box: &IntentBox,
|
||||
method: &str,
|
||||
_arguments: &[ASTNode],
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
match method {
|
||||
// 基本情報取得
|
||||
"getType" | "type" => {
|
||||
Ok(Box::new(StringBox::new("IntentBox")))
|
||||
}
|
||||
|
||||
// メッセージ処理(テスト用)
|
||||
"processMessages" => {
|
||||
let messages = intent_box.process_messages();
|
||||
Ok(Box::new(StringBox::new(format!("Processed {} messages", messages.len()))))
|
||||
}
|
||||
|
||||
_ => Err(RuntimeError::UndefinedVariable {
|
||||
name: format!("IntentBox method '{}' not found", method),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// P2PBoxのメソッド実行
|
||||
pub(in crate::interpreter) fn execute_p2p_box_method(
|
||||
&mut self,
|
||||
p2p_box: &P2PBox,
|
||||
method: &str,
|
||||
arguments: &[ASTNode],
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
match method {
|
||||
// ノードID取得
|
||||
"getNodeId" | "getId" => {
|
||||
Ok(Box::new(StringBox::new(p2p_box.get_node_id())))
|
||||
}
|
||||
|
||||
// メッセージ送信
|
||||
"send" => {
|
||||
if arguments.len() < 3 {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: "send requires 3 arguments: intent, data, target".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let intent = self.execute_expression(&arguments[0])?;
|
||||
let data = self.execute_expression(&arguments[1])?;
|
||||
let target = self.execute_expression(&arguments[2])?;
|
||||
|
||||
if let Some(intent_str) = intent.as_any().downcast_ref::<StringBox>() {
|
||||
if let Some(target_str) = target.as_any().downcast_ref::<StringBox>() {
|
||||
return Ok(p2p_box.send(&intent_str.value, data, &target_str.value));
|
||||
}
|
||||
}
|
||||
|
||||
Err(RuntimeError::TypeError {
|
||||
message: "send requires string arguments for intent and target".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
// ブロードキャスト
|
||||
"broadcast" => {
|
||||
if arguments.len() < 2 {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: "broadcast requires 2 arguments: intent, data".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let intent = self.execute_expression(&arguments[0])?;
|
||||
let data = self.execute_expression(&arguments[1])?;
|
||||
|
||||
if let Some(intent_str) = intent.as_any().downcast_ref::<StringBox>() {
|
||||
return Ok(p2p_box.broadcast(&intent_str.value, data));
|
||||
}
|
||||
|
||||
Err(RuntimeError::TypeError {
|
||||
message: "broadcast requires string argument for intent".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
// リスナー登録
|
||||
"on" => {
|
||||
if arguments.len() < 2 {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: "on requires 2 arguments: intent, callback".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let intent = self.execute_expression(&arguments[0])?;
|
||||
let callback = self.execute_expression(&arguments[1])?;
|
||||
|
||||
if let Some(intent_str) = intent.as_any().downcast_ref::<StringBox>() {
|
||||
return Ok(p2p_box.on(&intent_str.value, callback));
|
||||
}
|
||||
|
||||
Err(RuntimeError::TypeError {
|
||||
message: "on requires string argument for intent".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
// リスナー解除
|
||||
"off" => {
|
||||
if arguments.is_empty() {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: "off requires 1 argument: intent".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let intent = self.execute_expression(&arguments[0])?;
|
||||
|
||||
if let Some(intent_str) = intent.as_any().downcast_ref::<StringBox>() {
|
||||
return Ok(p2p_box.off(&intent_str.value));
|
||||
}
|
||||
|
||||
Err(RuntimeError::TypeError {
|
||||
message: "off requires string argument for intent".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
_ => Err(RuntimeError::UndefinedVariable {
|
||||
name: format!("P2PBox method '{}' not found", method),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user