2025-08-12 01:35:36 +00:00
|
|
|
|
/*! 📦 IntentBox - Structured Message Box
|
2025-08-11 05:11:52 +09:00
|
|
|
|
*
|
|
|
|
|
|
* ## 📝 概要
|
2025-08-12 01:35:36 +00:00
|
|
|
|
* IntentBoxは構造化メッセージを表現するBoxです。
|
|
|
|
|
|
* P2P通信において、メッセージの種類(name)と内容(payload)を
|
|
|
|
|
|
* 明確に分離して管理します。
|
|
|
|
|
|
*
|
|
|
|
|
|
* ## 🏗️ 設計
|
|
|
|
|
|
* - **name**: メッセージの種類 ("chat.message", "file.share"等)
|
|
|
|
|
|
* - **payload**: JSON形式の任意データ
|
|
|
|
|
|
* - **Arc<Mutex>**: 他のBoxと統一されたメモリ管理パターン
|
2025-08-11 05:11:52 +09:00
|
|
|
|
*
|
|
|
|
|
|
* ## 🛠️ 利用可能メソッド
|
2025-08-12 01:35:36 +00:00
|
|
|
|
* - `new(name, payload)` - 構造化メッセージを作成
|
|
|
|
|
|
* - `getName()` - メッセージ名を取得
|
|
|
|
|
|
* - `getPayload()` - ペイロードを取得
|
|
|
|
|
|
* - `setPayload(data)` - ペイロードを更新
|
2025-08-11 05:11:52 +09:00
|
|
|
|
*
|
|
|
|
|
|
* ## 💡 使用例
|
|
|
|
|
|
* ```nyash
|
2025-08-12 01:35:36 +00:00
|
|
|
|
* // チャットメッセージ
|
|
|
|
|
|
* local msg = new IntentBox("chat.message", {
|
|
|
|
|
|
* text: "Hello P2P!",
|
|
|
|
|
|
* from: "alice"
|
|
|
|
|
|
* })
|
2025-08-11 05:11:52 +09:00
|
|
|
|
*
|
2025-08-12 01:35:36 +00:00
|
|
|
|
* // ファイル共有メッセージ
|
|
|
|
|
|
* local file_msg = new IntentBox("file.share", {
|
|
|
|
|
|
* filename: "document.pdf",
|
|
|
|
|
|
* size: 1024000
|
2025-08-11 05:11:52 +09:00
|
|
|
|
* })
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-08-11 12:09:14 +09:00
|
|
|
|
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
|
2025-08-11 05:11:52 +09:00
|
|
|
|
use std::any::Any;
|
2025-08-15 01:00:21 +00:00
|
|
|
|
use std::sync::RwLock;
|
2025-08-16 17:39:04 +09:00
|
|
|
|
use std::fmt::Debug;
|
2025-08-11 05:11:52 +09:00
|
|
|
|
|
2025-08-15 01:00:21 +00:00
|
|
|
|
/// IntentBox - 構造化メッセージBox (RwLock pattern)
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct IntentBox {
|
2025-08-11 12:09:14 +09:00
|
|
|
|
base: BoxBase,
|
2025-08-12 01:35:36 +00:00
|
|
|
|
/// メッセージの種類 ("chat.message", "file.share"等)
|
2025-08-15 01:00:21 +00:00
|
|
|
|
name: RwLock<String>,
|
2025-08-12 01:35:36 +00:00
|
|
|
|
/// 任意のJSONデータ
|
2025-08-15 01:00:21 +00:00
|
|
|
|
payload: RwLock<serde_json::Value>,
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-15 01:00:21 +00:00
|
|
|
|
impl Clone for IntentBox {
|
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
|
let name_val = self.name.read().unwrap().clone();
|
|
|
|
|
|
let payload_val = self.payload.read().unwrap().clone();
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
|
base: BoxBase::new(), // New unique ID for clone
|
|
|
|
|
|
name: RwLock::new(name_val),
|
|
|
|
|
|
payload: RwLock::new(payload_val),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-11 05:11:52 +09:00
|
|
|
|
|
2025-08-15 01:00:21 +00:00
|
|
|
|
impl IntentBox {
|
2025-08-12 01:35:36 +00:00
|
|
|
|
/// 新しいIntentBoxを作成
|
2025-08-15 01:00:21 +00:00
|
|
|
|
pub fn new(name: String, payload: serde_json::Value) -> Self {
|
|
|
|
|
|
IntentBox {
|
2025-08-11 12:09:14 +09:00
|
|
|
|
base: BoxBase::new(),
|
2025-08-15 01:00:21 +00:00
|
|
|
|
name: RwLock::new(name),
|
|
|
|
|
|
payload: RwLock::new(payload),
|
|
|
|
|
|
}
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
/// メッセージ名を取得
|
2025-08-15 01:00:21 +00:00
|
|
|
|
pub fn get_name(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
let name = self.name.read().unwrap().clone();
|
|
|
|
|
|
Box::new(StringBox::new(name))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
/// ペイロードを取得
|
2025-08-15 01:00:21 +00:00
|
|
|
|
pub fn get_payload(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
let payload = self.payload.read().unwrap().clone();
|
|
|
|
|
|
Box::new(StringBox::new(payload.to_string()))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
/// ペイロードを更新
|
2025-08-15 01:00:21 +00:00
|
|
|
|
pub fn set_payload(&self, payload: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
|
|
|
|
|
|
let payload_str = payload.to_string_box().value;
|
|
|
|
|
|
match serde_json::from_str(&payload_str) {
|
|
|
|
|
|
Ok(json_val) => {
|
|
|
|
|
|
*self.payload.write().unwrap() = json_val;
|
|
|
|
|
|
Box::new(BoolBox::new(true))
|
|
|
|
|
|
},
|
|
|
|
|
|
Err(_) => Box::new(BoolBox::new(false))
|
|
|
|
|
|
}
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl NyashBox for IntentBox {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
2025-08-15 14:29:47 +09:00
|
|
|
|
Box::new(self.clone())
|
|
|
|
|
|
}
|
2025-08-15 04:29:41 +00:00
|
|
|
|
|
|
|
|
|
|
/// 仮実装: clone_boxと同じ(後で修正)
|
|
|
|
|
|
fn share_box(&self) -> Box<dyn NyashBox> {
|
|
|
|
|
|
self.clone_box()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-11 05:11:52 +09:00
|
|
|
|
fn to_string_box(&self) -> StringBox {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
let name = self.name.read().unwrap().clone();
|
|
|
|
|
|
StringBox::new(format!("IntentBox[{}]", name))
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
|
|
|
|
|
if let Some(other_intent) = other.as_any().downcast_ref::<IntentBox>() {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
BoolBox::new(self.base.id == other_intent.base.id)
|
2025-08-11 05:11:52 +09:00
|
|
|
|
} else {
|
|
|
|
|
|
BoolBox::new(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
|
|
"IntentBox"
|
|
|
|
|
|
}
|
2025-08-11 12:09:14 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl BoxCore for IntentBox {
|
2025-08-11 05:11:52 +09:00
|
|
|
|
fn box_id(&self) -> u64 {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
self.base.id
|
2025-08-11 15:01:11 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn parent_type_id(&self) -> Option<std::any::TypeId> {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
self.base.parent_type_id
|
2025-08-11 12:09:14 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
let name = self.name.read().unwrap().clone();
|
|
|
|
|
|
write!(f, "IntentBox[{}]", name)
|
2025-08-11 12:09:14 +09:00
|
|
|
|
}
|
2025-08-11 15:01:11 +09:00
|
|
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
2025-08-11 12:09:14 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-15 01:00:21 +00:00
|
|
|
|
impl std::fmt::Display for IntentBox {
|
2025-08-11 12:09:14 +09:00
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2025-08-15 01:00:21 +00:00
|
|
|
|
self.fmt_box(f)
|
2025-08-11 05:11:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|