fix/libp2p-protocol-inheritance (#173)

This commit is contained in:
Dean Eigenmann 2020-09-18 15:28:19 +02:00 committed by GitHub
parent b25c1e10f2
commit 255e762536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 15 deletions

View File

@ -74,25 +74,22 @@ proc init*(T: type MessagePush, buffer: seq[byte]): ProtoResult[T] =
ok(push)
proc init*(T: type WakuFilter): T =
var ws = WakuFilter(subscribers: newSeq[Subscriber](0))
# From my understanding we need to set up filters,
# then on every message received we need the handle function to send it to the connection
# if the peer subscribed.
method init*(wf: WakuFilter) =
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
var message = await conn.readLp(64*1024)
var res = FilterRequest.init(message)
if res.isErr:
return
ws.subscribers.add(Subscriber(connection: conn, filter: res.value))
wf.subscribers.add(Subscriber(connection: conn, filter: res.value))
# @TODO THIS IS A VERY ROUGH EXPERIMENT
ws.handler = handle
ws.codec = WakuFilterCodec
result = ws
wf.handler = handle
wf.codec = WakuFilterCodec
proc init*(T: type WakuFilter): T =
new result
result.init()
proc subscription*(proto: WakuFilter): MessageNotificationSubscription =
## Returns a Filter for the specific protocol

View File

@ -61,9 +61,7 @@ proc query(w: WakuStore, query: HistoryQuery): HistoryResponse =
if msg.contentTopic in query.topics:
result.messages.insert(msg)
proc init*(T: type WakuStore): T =
var ws = WakuStore()
method init*(ws: WakuStore) =
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
var message = await conn.readLp(64*1024)
var rpc = HistoryQuery.init(message)
@ -78,7 +76,10 @@ proc init*(T: type WakuStore): T =
ws.handler = handle
ws.codec = WakuStoreCodec
result = ws
proc init*(T: type WakuStore): T =
new result
result.init()
proc subscription*(proto: WakuStore): MessageNotificationSubscription =
## The filter function returns the pubsub filter for the node.