127 lines
4.4 KiB
Plaintext
127 lines
4.4 KiB
Plaintext
|
|
// 🔥 P2PBox Phase 3: デリゲーション・override革命!
|
|||
|
|
// 目標: P2PBoxデリゲーション実装・Everything is Box哲学の完全実証
|
|||
|
|
|
|||
|
|
local console
|
|||
|
|
console = new ConsoleBox()
|
|||
|
|
console.log("🔥 Phase 3: デリゲーション・override革命開始!")
|
|||
|
|
|
|||
|
|
// 3.1 P2PBoxデリゲーション - ChatNodeBox実装
|
|||
|
|
console.log("\n📦 3.1 ChatNodeBox(P2PBoxデリゲーション)実装")
|
|||
|
|
|
|||
|
|
// Nyashのデリゲーション構文:box Child from Parent
|
|||
|
|
box ChatNodeBox from P2PBox {
|
|||
|
|
init { chatHistory, messageCount }
|
|||
|
|
|
|||
|
|
pack(nodeId, transport) {
|
|||
|
|
from P2PBox.pack(nodeId, transport) // 親の初期化
|
|||
|
|
me.chatHistory = new ArrayBox()
|
|||
|
|
me.messageCount = 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3.2 send()メソッドのoverride - ログ機能追加
|
|||
|
|
override send(to, intent) {
|
|||
|
|
console = new ConsoleBox()
|
|||
|
|
console.log("📤 [ChatNode] Sending: " + intent.getName() + " to " + to)
|
|||
|
|
|
|||
|
|
// 履歴に記録
|
|||
|
|
local logEntry
|
|||
|
|
logEntry = "SENT:" + intent.getName() + ":" + to
|
|||
|
|
me.chatHistory.push(logEntry)
|
|||
|
|
me.messageCount = me.messageCount + 1
|
|||
|
|
|
|||
|
|
// 親のsend()を呼び出し
|
|||
|
|
return from P2PBox.send(to, intent)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3.3 getNodeId()のoverride - 拡張情報付き
|
|||
|
|
override getNodeId() {
|
|||
|
|
local baseId
|
|||
|
|
baseId = from P2PBox.getNodeId()
|
|||
|
|
return "[Chat]" + baseId + "(" + me.messageCount + "msgs)"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3.4 独自メソッド追加
|
|||
|
|
getChatStats() {
|
|||
|
|
return "Messages: " + me.messageCount + ", History: " + me.chatHistory.length()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
getLastMessage() {
|
|||
|
|
if me.chatHistory.length() > 0 {
|
|||
|
|
return me.chatHistory.get(me.chatHistory.length() - 1)
|
|||
|
|
} else {
|
|||
|
|
return "No messages"
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3.5 デリゲーション動作テスト
|
|||
|
|
console.log("\n🚀 3.5 ChatNodeBox作成・動作テスト")
|
|||
|
|
|
|||
|
|
local chatAlice
|
|||
|
|
local regularBob
|
|||
|
|
|
|||
|
|
chatAlice = new ChatNodeBox("alice", "inprocess")
|
|||
|
|
regularBob = new P2PBox("bob", "inprocess")
|
|||
|
|
|
|||
|
|
console.log("✅ ChatNodeBox作成完了")
|
|||
|
|
console.log("ChatAlice ID: " + chatAlice.getNodeId()) // override版
|
|||
|
|
console.log("Regular Bob ID: " + regularBob.getNodeId()) // 通常版
|
|||
|
|
|
|||
|
|
// 3.6 override機能テスト
|
|||
|
|
console.log("\n🔄 3.6 Override send()メソッドテスト")
|
|||
|
|
|
|||
|
|
local chatMsg1
|
|||
|
|
local chatMsg2
|
|||
|
|
|
|||
|
|
chatMsg1 = new IntentBox("chat", "Hello from ChatNode!")
|
|||
|
|
chatMsg2 = new IntentBox("info", "System message")
|
|||
|
|
|
|||
|
|
console.log("送信テスト1:")
|
|||
|
|
local result1
|
|||
|
|
result1 = chatAlice.send("bob", chatMsg1) // override版send
|
|||
|
|
console.log("送信結果: " + result1)
|
|||
|
|
|
|||
|
|
console.log("\n送信テスト2:")
|
|||
|
|
local result2
|
|||
|
|
result2 = chatAlice.send("bob", chatMsg2) // override版send
|
|||
|
|
console.log("送信結果: " + result2)
|
|||
|
|
|
|||
|
|
// 3.7 独自メソッドテスト
|
|||
|
|
console.log("\n📊 3.7 ChatNodeBox独自機能テスト")
|
|||
|
|
|
|||
|
|
console.log("Chat統計: " + chatAlice.getChatStats())
|
|||
|
|
console.log("最新メッセージ: " + chatAlice.getLastMessage())
|
|||
|
|
console.log("更新されたID: " + chatAlice.getNodeId()) // カウント更新確認
|
|||
|
|
|
|||
|
|
// 3.8 通常P2PBoxとの比較テスト
|
|||
|
|
console.log("\n⚖️ 3.8 ChatNodeBox vs 通常P2PBox比較")
|
|||
|
|
|
|||
|
|
local normalMsg
|
|||
|
|
normalMsg = new IntentBox("normal", "Normal P2P message")
|
|||
|
|
|
|||
|
|
console.log("通常P2PBox送信:")
|
|||
|
|
local normalResult
|
|||
|
|
normalResult = regularBob.send("alice", normalMsg) // 通常版send
|
|||
|
|
console.log("通常送信結果: " + normalResult)
|
|||
|
|
|
|||
|
|
console.log("\nChatNodeBox送信:")
|
|||
|
|
local chatResult
|
|||
|
|
chatResult = chatAlice.send("bob", normalMsg) // override版send
|
|||
|
|
console.log("Chat送信結果: " + chatResult)
|
|||
|
|
|
|||
|
|
// 3.9 Revolution成果確認
|
|||
|
|
console.log("\n🎉 3.9 Everything is Box + デリゲーション革命成果")
|
|||
|
|
|
|||
|
|
console.log("✅ P2PBoxデリゲーション: 成功")
|
|||
|
|
console.log("✅ Override機能: send()・getNodeId()正常動作")
|
|||
|
|
console.log("✅ 独自メソッド: getChatStats()・getLastMessage()追加")
|
|||
|
|
console.log("✅ 親機能継承: isReachable()など全て利用可能")
|
|||
|
|
console.log("✅ ログ機能: 送信履歴記録・カウント機能")
|
|||
|
|
|
|||
|
|
console.log("\n🌟 最終統計:")
|
|||
|
|
console.log("- 送信メッセージ総数: " + chatAlice.getChatStats())
|
|||
|
|
console.log("- ノードID拡張表示: " + chatAlice.getNodeId())
|
|||
|
|
console.log("- 最新ログエントリ: " + chatAlice.getLastMessage())
|
|||
|
|
|
|||
|
|
console.log("\n🔥🔥🔥 Phase 3: デリゲーション・override革命完全達成! 🔥🔥🔥")
|
|||
|
|
console.log("Everything is Box哲学 + P2P通信 + デリゲーション = 完全統合成功!")
|