43 lines
955 B
Plaintext
43 lines
955 B
Plaintext
|
|
// P2PBoxの概念実証 - 純粋なNyashコードのみ
|
||
|
|
|
||
|
|
// シンプルなメッセージ配信
|
||
|
|
box MessageBus {
|
||
|
|
init { handlers }
|
||
|
|
|
||
|
|
constructor() {
|
||
|
|
me.handlers = new MapBox()
|
||
|
|
}
|
||
|
|
|
||
|
|
// メッセージ送信(登録されたハンドラーに配信)
|
||
|
|
send(type, data, from) {
|
||
|
|
local list
|
||
|
|
list = me.handlers.get(type)
|
||
|
|
|
||
|
|
if (list != null) {
|
||
|
|
// とりあえずprintで確認
|
||
|
|
print(from + " -> " + type + ": " + data)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ハンドラー登録(今は登録だけ)
|
||
|
|
on(type, nodeId) {
|
||
|
|
me.handlers.set(type, nodeId)
|
||
|
|
print("Registered: " + nodeId + " listens to " + type)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// テスト実行
|
||
|
|
print("=== Message Bus Test ===")
|
||
|
|
|
||
|
|
local bus
|
||
|
|
bus = new MessageBus()
|
||
|
|
|
||
|
|
// 登録
|
||
|
|
bus.on("hello", "Bob")
|
||
|
|
bus.on("bye", "Alice")
|
||
|
|
|
||
|
|
// 送信
|
||
|
|
bus.send("hello", "Hi there!", "Alice")
|
||
|
|
bus.send("bye", "See you!", "Bob")
|
||
|
|
|
||
|
|
print("Done!")
|