2025-08-12 01:35:36 +00:00
|
|
|
|
/*! 📡 P2P通信メソッド実装 (NEW ARCHITECTURE)
|
2025-08-11 05:11:52 +09:00
|
|
|
|
* IntentBoxとP2PBoxのNyashインタープリター統合
|
2025-08-12 01:35:36 +00:00
|
|
|
|
* Arc<Mutex>パターン対応版
|
2025-08-11 05:11:52 +09:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
use crate::interpreter::core::NyashInterpreter;
|
|
|
|
|
|
use crate::interpreter::core::RuntimeError;
|
|
|
|
|
|
use crate::ast::ASTNode;
|
2025-08-16 17:39:04 +09:00
|
|
|
|
use crate::box_trait::{NyashBox, StringBox};
|
2025-08-12 08:12:26 +00:00
|
|
|
|
use crate::boxes::{IntentBox};
|
2025-08-11 05:11:52 +09:00
|
|
|
|
|
|
|
|
|
|
impl NyashInterpreter {
|
2025-08-15 03:08:09 +00:00
|
|
|
|
/// IntentBoxのメソッド実行 (RwLock版)
|
2025-08-11 05:11:52 +09:00
|
|
|
|
pub(in crate::interpreter) fn execute_intent_box_method(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
intent_box: &IntentBox,
|
|
|
|
|
|
method: &str,
|
|
|
|
|
|
_arguments: &[ASTNode],
|
2025-08-15 03:08:09 +00:00
|
|
|
|
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
2025-08-11 05:11:52 +09:00
|
|
|
|
match method {
|
2025-08-12 01:35:36 +00:00
|
|
|
|
// メッセージ名取得
|
|
|
|
|
|
"getName" | "name" => {
|
2025-08-15 03:08:09 +00:00
|
|
|
|
Ok(intent_box.get_name())
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
// ペイロード取得(JSON文字列として)
|
|
|
|
|
|
"getPayload" | "payload" => {
|
2025-08-15 03:08:09 +00:00
|
|
|
|
Ok(intent_box.get_payload())
|
2025-08-12 01:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 型情報取得
|
|
|
|
|
|
"getType" | "type" => {
|
|
|
|
|
|
Ok(Box::new(StringBox::new("IntentBox")))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_ => Err(RuntimeError::UndefinedVariable {
|
|
|
|
|
|
name: format!("IntentBox method '{}' not found", method),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 08:12:26 +00:00
|
|
|
|
// P2PBoxのメソッド実行 (Arc<Mutex>版) - Temporarily disabled
|
|
|
|
|
|
/*
|
2025-08-11 05:11:52 +09:00
|
|
|
|
pub(in crate::interpreter) fn execute_p2p_box_method(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
p2p_box: &P2PBox,
|
|
|
|
|
|
method: &str,
|
|
|
|
|
|
arguments: &[ASTNode],
|
|
|
|
|
|
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
2025-08-12 01:35:36 +00:00
|
|
|
|
let data = p2p_box.lock().map_err(|_| RuntimeError::UndefinedVariable {
|
|
|
|
|
|
name: "Failed to lock P2PBox".to_string(),
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
2025-08-11 05:11:52 +09:00
|
|
|
|
match method {
|
|
|
|
|
|
// ノードID取得
|
|
|
|
|
|
"getNodeId" | "getId" => {
|
2025-08-12 01:35:36 +00:00
|
|
|
|
Ok(Box::new(StringBox::new(data.get_node_id().to_string())))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
// トランスポート種類取得
|
|
|
|
|
|
"getTransportType" | "transport" => {
|
|
|
|
|
|
Ok(Box::new(StringBox::new(data.get_transport_type())))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
// ノード到達可能性確認
|
|
|
|
|
|
"isReachable" => {
|
|
|
|
|
|
if arguments.is_empty() {
|
|
|
|
|
|
return Err(RuntimeError::UndefinedVariable {
|
|
|
|
|
|
name: "isReachable requires node_id argument".to_string(),
|
2025-08-11 05:11:52 +09:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
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)))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
// send メソッド実装
|
|
|
|
|
|
"send" => {
|
|
|
|
|
|
if arguments.len() < 2 {
|
|
|
|
|
|
return Err(RuntimeError::UndefinedVariable {
|
|
|
|
|
|
name: "send requires (to, intent) arguments".to_string(),
|
2025-08-11 05:11:52 +09:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
let to_result = self.execute_expression(&arguments[0])?;
|
|
|
|
|
|
let to = to_result.to_string_box().value;
|
2025-08-11 05:11:52 +09:00
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
let intent_result = self.execute_expression(&arguments[1])?;
|
2025-08-11 05:11:52 +09:00
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
// 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(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_ => Err(RuntimeError::UndefinedVariable {
|
|
|
|
|
|
name: format!("P2PBox method '{}' not found", method),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-12 08:12:26 +00:00
|
|
|
|
*/
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|