Files
hakorune/test_p2p_basic.nyash
Moe Charm 21eceed324 🚀 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>
2025-08-11 05:11:52 +09:00

61 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 🧪 P2PBox基本機能テスト
// IntentBoxとP2PBoxの基本的な動作を検証
print("=== P2PBox Basic Test ===")
// 1. IntentBoxの作成
print("\n1. Creating IntentBox...")
local world
world = new IntentBox()
print("✅ IntentBox created: " + world.type())
// 2. P2PBoxードの作成
print("\n2. Creating P2PBox nodes...")
local alice
local bob
local charlie
alice = new P2PBox("alice", world)
bob = new P2PBox("bob", world)
charlie = new P2PBox("charlie", world)
print("✅ Alice created: " + alice.getNodeId())
print("✅ Bob created: " + bob.getNodeId())
print("✅ Charlie created: " + charlie.getNodeId())
// 3. リスナー登録テスト
print("\n3. Testing listener registration...")
bob.on("greeting", "bob_greeting_handler")
charlie.on("greeting", "charlie_greeting_handler")
print("✅ Listeners registered")
// 4. 直接送信テスト
print("\n4. Testing direct send...")
alice.send("greeting", "Hello Bob!", "bob")
alice.send("greeting", "Hello Charlie!", "charlie")
print("✅ Messages sent")
// 5. ブロードキャストテスト
print("\n5. Testing broadcast...")
alice.broadcast("announcement", "Hello everyone!")
print("✅ Broadcast sent")
// 6. リスナー解除テスト
print("\n6. Testing listener removal...")
local result
result = bob.off("greeting")
print("✅ Bob's listener removed: " + result)
// 7. 解除後の送信テスト
print("\n7. Testing send after listener removal...")
alice.send("greeting", "Hello again Bob!", "bob")
print("✅ Message sent (Bob should not receive)")
// 8. 複数リスナーテスト
print("\n8. Testing multiple listeners...")
charlie.on("data", "charlie_data_handler1")
charlie.on("data", "charlie_data_handler2")
alice.send("data", "Test data", "charlie")
print("✅ Multiple listeners tested")
print("\n=== Test Complete ===")