49 lines
1021 B
Plaintext
49 lines
1021 B
Plaintext
|
|
// 超シンプルなP2PBoxテスト(printだけ)
|
|||
|
|
|
|||
|
|
// IntentBox - メッセージをprintするだけ
|
|||
|
|
box IntentBox {
|
|||
|
|
init { }
|
|||
|
|
|
|||
|
|
constructor() {
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// メッセージを配信(今は単にprintするだけ)
|
|||
|
|
deliver(messageType, data, from) {
|
|||
|
|
print("IntentBox: " + from + " sent " + messageType + " with data: " + data)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// P2PBox - IntentBoxにメッセージを送る
|
|||
|
|
box P2PBox {
|
|||
|
|
init { nodeId, intentBox }
|
|||
|
|
|
|||
|
|
constructor(nodeId, intentBox) {
|
|||
|
|
me.nodeId = nodeId
|
|||
|
|
me.intentBox = intentBox
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// メッセージ送信
|
|||
|
|
send(messageType, data) {
|
|||
|
|
me.intentBox.deliver(messageType, data, me.nodeId)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// テスト
|
|||
|
|
print("=== Simple P2PBox Test ===")
|
|||
|
|
|
|||
|
|
// 1. IntentBox作成
|
|||
|
|
local bus
|
|||
|
|
bus = new IntentBox()
|
|||
|
|
|
|||
|
|
// 2. P2PBox作成
|
|||
|
|
local alice
|
|||
|
|
alice = new P2PBox("Alice", bus)
|
|||
|
|
|
|||
|
|
local bob
|
|||
|
|
bob = new P2PBox("Bob", bus)
|
|||
|
|
|
|||
|
|
// 3. メッセージ送信
|
|||
|
|
alice.send("greeting", "Hello!")
|
|||
|
|
bob.send("reply", "Hi there!")
|
|||
|
|
|
|||
|
|
print("Done!")
|