Files
hakorune/src/boxes/simple_intent_box.rs

221 lines
6.1 KiB
Rust
Raw Normal View History

/*! 🔗 SimpleIntentBox - P2P通信Box
*
* ## 📝
* Box
*
* 使
*
* ## 🛠
* - `send(target, message)` -
* - `on(event, callback)` -
* - `emit(event, data)` -
* - `connect(nodeId)` -
* - `disconnect(nodeId)` -
* - `getConnectedNodes()` -
* - `setNodeId(id)` - ID設定
* - `broadcast(message)` -
*
* ## 💡 使
* ```nyash
* local intent
* intent = new SimpleIntentBox()
*
* // ノード設定
* intent.setNodeId("node1")
*
* // リスナー登録
* intent.on("message", "handleMessage")
* intent.on("join", "handleNodeJoin")
*
* // メッセージ送信
* intent.send("node2", "Hello from node1!")
*
* // ブロードキャスト
* intent.broadcast("System announcement")
* ```
*
* ## 🎮 -
* ```nyash
* static box ChatNode {
* init { intent, username, messages }
*
* main() {
* me.intent = new SimpleIntentBox()
* me.username = "User1"
* me.messages = []
*
* // ノード初期化
* me.intent.setNodeId(me.username)
* me.setupEventHandlers()
*
* // チャットルームに参加
* me.joinChatRoom()
* }
*
* setupEventHandlers() {
* // メッセージ受信
* me.intent.on("chat_message", "onChatMessage")
* // ユーザー参加
* me.intent.on("user_joined", "onUserJoined")
* // ユーザー退出
* me.intent.on("user_left", "onUserLeft")
* }
*
* sendMessage(text) {
* local msg
* msg = new MapBox()
* msg.set("from", me.username)
* msg.set("text", text)
* msg.set("timestamp", new TimeBox().now())
*
* me.intent.broadcast("chat_message", msg)
* }
*
* onChatMessage(sender, message) {
* me.messages.push(message)
* print("[" + message.get("from") + "] " + message.get("text"))
* }
* }
* ```
*
* ## 🌐
* ```nyash
* static box DistributedWorker {
* init { intent, node_id, tasks }
*
* main() {
* me.intent = new SimpleIntentBox()
* me.node_id = "worker_" + RandomBox.randInt(1000, 9999)
* me.tasks = []
*
* me.intent.setNodeId(me.node_id)
* me.registerAsWorker()
* }
*
* registerAsWorker() {
* // タスク受信リスナー
* me.intent.on("task_assign", "processTask")
* // 結果送信完了リスナー
* me.intent.on("result_received", "onResultReceived")
*
* // ワーカー登録通知
* me.intent.broadcast("worker_ready", me.node_id)
* }
*
* processTask(coordinator, task) {
* print("Processing task: " + task.get("id"))
*
* // 重い計算処理...
* local result
* result = heavyCalculation(task.get("data"))
*
* // 結果を送信
* me.intent.send(coordinator, result)
* }
* }
* ```
*
* ## 🎯
* ```nyash
* static box GameClient {
* init { intent, player_id, game_state }
*
* main() {
* me.intent = new SimpleIntentBox()
* me.player_id = "player_" + me.generateId()
* me.game_state = new MapBox()
*
* me.connectToGame()
* }
*
* connectToGame() {
* me.intent.setNodeId(me.player_id)
*
* // ゲームイベント
* me.intent.on("player_move", "onPlayerMove")
* me.intent.on("game_update", "onGameUpdate")
* me.intent.on("player_joined", "onPlayerJoined")
*
* // ゲーム参加
* me.intent.broadcast("join_game", me.player_id)
* }
*
* movePlayer(x, y) {
* local move_data
* move_data = new MapBox()
* move_data.set("player", me.player_id)
* move_data.set("x", x)
* move_data.set("y", y)
*
* me.intent.broadcast("player_move", move_data)
* }
* }
* ```
*
* ##
* -
* -
* -
* - P2P実装は将来バージョンで提供予定
*/
🎨 feat: EguiBox GUI開発基盤完成 + パーサー無限ループバグ修正 ## 🚀 主要機能追加 ### EguiBox - GUI開発基盤 - Windows版GUIメモ帳アプリ (simple_notepad.rs, nyash_notepad_jp.rs) - 日本語フォント対応 (NotoSansJP-VariableFont_wght.ttf) - BMPアイコン表示システム (c_drive_icon.bmp) - Windowsエクスプローラー風アプリ (nyash_explorer.rs) - アイコン抽出システム (test_icon_extraction.rs) ### ビジュアルプログラミング準備 - NyashFlow プロジェクト設計完成 (NYASHFLOW_PROJECT_HANDOVER.md) - ビジュアルノードプロトタイプ基盤 - WebAssembly対応準備 ## 🔧 重大バグ修正 ### パーサー無限ループ問題 (3引数メソッド呼び出し) - 原因: メソッドパラメータ解析ループの予約語処理不備 - 修正: src/parser/mod.rs - 非IDENTIFIERトークンのエラーハンドリング追加 - 効果: "from"等の予約語で適切なエラー報告、ハング→瞬時エラー ### MapBoxハング問題調査 - MapBox+3引数メソッド呼び出し組み合わせ問題特定 - バグレポート作成 (MAPBOX_HANG_BUG_REPORT.md) - 事前評価vs必要時評価の設計問題明確化 ## 🧹 コード品質向上 - box_methods.rs を8モジュールに機能分離 - 一時デバッグコード全削除 (eprintln\!, unsafe等) - 構文チェック通過確認済み ## 📝 ドキュメント整備 - CLAUDE.md にGUI開発セクション追加 - Gemini/ChatGPT先生相談ログ保存 (sessions/) - 段階的デバッグ手法確立 ## 🎯 次の目標 - must_advance\!マクロ実装 (無限ループ早期検出) - コマンド引数でデバッグ制御 (--debug-fuel) - MapBox問題の根本修正 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-10 07:54:03 +09:00
use crate::box_trait::{NyashBox, StringBox, BoolBox};
use std::any::Any;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
#[derive(Debug)]
pub struct SimpleIntentBox {
id: u64,
// ードID -> コールバック関数のマップ
listeners: Arc<Mutex<HashMap<String, Vec<String>>>>, // 仮実装
}
impl SimpleIntentBox {
pub fn new() -> Self {
static mut COUNTER: u64 = 0;
let id = unsafe {
COUNTER += 1;
COUNTER
};
SimpleIntentBox {
id,
listeners: Arc::new(Mutex::new(HashMap::new())),
}
}
}
impl NyashBox for SimpleIntentBox {
fn to_string_box(&self) -> StringBox {
StringBox::new("IntentBox")
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_intent) = other.as_any().downcast_ref::<SimpleIntentBox>() {
BoolBox::new(self.id == other_intent.id)
} else {
BoolBox::new(false)
}
}
fn type_name(&self) -> &'static str {
"IntentBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
// IntentBoxは共有されるので、新しいインスタンスを作らない
Box::new(SimpleIntentBox {
id: self.id,
listeners: self.listeners.clone(),
})
}
fn as_any(&self) -> &dyn Any {
self
}
fn box_id(&self) -> u64 {
self.id
}
}