Implement complete P2P communication system with modern architecture
Co-authored-by: moe-charm <217100418+moe-charm@users.noreply.github.com>
This commit is contained in:
@ -1,32 +1,42 @@
|
||||
/*! 📡 P2P通信メソッド実装
|
||||
/*! 📡 P2P通信メソッド実装 (NEW ARCHITECTURE)
|
||||
* IntentBoxとP2PBoxのNyashインタープリター統合
|
||||
* Arc<Mutex>パターン対応版
|
||||
*/
|
||||
|
||||
use crate::interpreter::core::NyashInterpreter;
|
||||
use crate::interpreter::core::RuntimeError;
|
||||
use crate::ast::ASTNode;
|
||||
use crate::box_trait::{NyashBox, StringBox};
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use crate::boxes::{IntentBox, P2PBox};
|
||||
use crate::method_box::MethodBox;
|
||||
|
||||
impl NyashInterpreter {
|
||||
/// IntentBoxのメソッド実行
|
||||
/// IntentBoxのメソッド実行 (Arc<Mutex>版)
|
||||
pub(in crate::interpreter) fn execute_intent_box_method(
|
||||
&mut self,
|
||||
intent_box: &IntentBox,
|
||||
method: &str,
|
||||
_arguments: &[ASTNode],
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let data = intent_box.lock().map_err(|_| RuntimeError::UndefinedVariable {
|
||||
name: "Failed to lock IntentBox".to_string(),
|
||||
})?;
|
||||
|
||||
match method {
|
||||
// 基本情報取得
|
||||
"getType" | "type" => {
|
||||
Ok(Box::new(StringBox::new("IntentBox")))
|
||||
// メッセージ名取得
|
||||
"getName" | "name" => {
|
||||
Ok(Box::new(StringBox::new(data.name.clone())))
|
||||
}
|
||||
|
||||
// メッセージ処理(テスト用)
|
||||
"processMessages" => {
|
||||
let messages = intent_box.process_messages();
|
||||
Ok(Box::new(StringBox::new(format!("Processed {} messages", messages.len()))))
|
||||
// ペイロード取得(JSON文字列として)
|
||||
"getPayload" | "payload" => {
|
||||
let payload_str = data.payload.to_string();
|
||||
Ok(Box::new(StringBox::new(payload_str)))
|
||||
}
|
||||
|
||||
// 型情報取得
|
||||
"getType" | "type" => {
|
||||
Ok(Box::new(StringBox::new("IntentBox")))
|
||||
}
|
||||
|
||||
_ => Err(RuntimeError::UndefinedVariable {
|
||||
@ -35,79 +45,68 @@ impl NyashInterpreter {
|
||||
}
|
||||
}
|
||||
|
||||
/// P2PBoxのメソッド実行
|
||||
/// P2PBoxのメソッド実行 (Arc<Mutex>版)
|
||||
pub(in crate::interpreter) fn execute_p2p_box_method(
|
||||
&mut self,
|
||||
p2p_box: &P2PBox,
|
||||
method: &str,
|
||||
arguments: &[ASTNode],
|
||||
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
||||
let data = p2p_box.lock().map_err(|_| RuntimeError::UndefinedVariable {
|
||||
name: "Failed to lock P2PBox".to_string(),
|
||||
})?;
|
||||
|
||||
match method {
|
||||
// ノードID取得
|
||||
"getNodeId" | "getId" => {
|
||||
Ok(Box::new(StringBox::new(p2p_box.get_node_id())))
|
||||
Ok(Box::new(StringBox::new(data.get_node_id().to_string())))
|
||||
}
|
||||
|
||||
// メッセージ送信
|
||||
"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(),
|
||||
})
|
||||
// トランスポート種類取得
|
||||
"getTransportType" | "transport" => {
|
||||
Ok(Box::new(StringBox::new(data.get_transport_type())))
|
||||
}
|
||||
|
||||
// リスナー登録
|
||||
"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" => {
|
||||
// ノード到達可能性確認
|
||||
"isReachable" => {
|
||||
if arguments.is_empty() {
|
||||
return Err(RuntimeError::InvalidOperation {
|
||||
message: "off requires 1 argument: intent".to_string(),
|
||||
return Err(RuntimeError::UndefinedVariable {
|
||||
name: "isReachable requires node_id argument".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));
|
||||
let node_id_result = self.execute_expression(&arguments[0])?;
|
||||
let node_id = node_id_result.to_string_box().value;
|
||||
let reachable = data.is_reachable(&node_id);
|
||||
Ok(Box::new(BoolBox::new(reachable)))
|
||||
}
|
||||
|
||||
// send メソッド実装
|
||||
"send" => {
|
||||
if arguments.len() < 2 {
|
||||
return Err(RuntimeError::UndefinedVariable {
|
||||
name: "send requires (to, intent) arguments".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Err(RuntimeError::TypeError {
|
||||
message: "off requires string argument for intent".to_string(),
|
||||
})
|
||||
let to_result = self.execute_expression(&arguments[0])?;
|
||||
let to = to_result.to_string_box().value;
|
||||
|
||||
let intent_result = self.execute_expression(&arguments[1])?;
|
||||
|
||||
// IntentBoxかチェック
|
||||
if let Some(intent_box) = intent_result.as_any().downcast_ref::<IntentBox>() {
|
||||
match data.send(&to, intent_box.clone()) {
|
||||
Ok(_) => Ok(Box::new(StringBox::new("sent"))),
|
||||
Err(e) => Err(RuntimeError::UndefinedVariable {
|
||||
name: format!("Send failed: {:?}", e),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Err(RuntimeError::UndefinedVariable {
|
||||
name: "Second argument must be an IntentBox".to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
_ => Err(RuntimeError::UndefinedVariable {
|
||||
|
||||
Reference in New Issue
Block a user