mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-27 15:16:05 +00:00
feature/filter (#63)
* simplified filter * add filter * eol * Update waku_protocol2.nim * trigger GitHub actions * comment * fix import * oops * and * init filters * import tables
This commit is contained in:
parent
e920fd212e
commit
0799ef9c16
25
waku/protocol/v2/filter.nim
Normal file
25
waku/protocol/v2/filter.nim
Normal file
@ -0,0 +1,25 @@
|
||||
import
|
||||
tables
|
||||
|
||||
type
|
||||
|
||||
FilterMessageHandler* = proc(msg: seq[byte]) {.gcsafe, closure.}
|
||||
|
||||
Filter* = object
|
||||
topics: seq[string] # @TODO TOPIC
|
||||
handler: FilterMessageHandler
|
||||
|
||||
Filters* = Table[string, Filter]
|
||||
|
||||
proc init*(T: type Filter, topics: seq[string], handler: FilterMessageHandler): T =
|
||||
result = T(
|
||||
topics: topics,
|
||||
handler: handler
|
||||
)
|
||||
|
||||
proc notify*(filters: var Filters, topic: string, msg: seq[byte]) {.gcsafe.} =
|
||||
for filter in filters.mvalues:
|
||||
if filter.topics.len > 0 and topic notin filter.topics:
|
||||
continue
|
||||
|
||||
filter.handler(msg)
|
@ -5,6 +5,8 @@
|
||||
|
||||
import strutils
|
||||
import chronos, chronicles
|
||||
import ./filter
|
||||
import tables
|
||||
import libp2p/protocols/pubsub/pubsub,
|
||||
libp2p/protocols/pubsub/pubsubpeer,
|
||||
libp2p/protocols/pubsub/floodsub,
|
||||
@ -28,6 +30,8 @@ type
|
||||
text*: string
|
||||
gossip_enabled*: bool
|
||||
|
||||
filters: Filters
|
||||
|
||||
method init(w: WakuSub) =
|
||||
debug "init"
|
||||
proc handler(conn: Connection, proto: string) {.async.} =
|
||||
@ -44,6 +48,7 @@ method init(w: WakuSub) =
|
||||
|
||||
# XXX: Handler hijack GossipSub here?
|
||||
w.handler = handler
|
||||
w.filters = initTable[string, Filter]()
|
||||
w.codec = WakuSubCodec
|
||||
|
||||
method initPubSub*(w: WakuSub) =
|
||||
@ -82,6 +87,10 @@ method subscribeTopic*(w: WakuSub,
|
||||
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
||||
info "Hit NOOP handler", topic
|
||||
|
||||
# Currently we are using the libp2p topic here.
|
||||
# This will need to be change to a Waku topic.
|
||||
w.filters.notify(topic, data)
|
||||
|
||||
debug "subscribeTopic", topic=topic, subscribe=subscribe, peerId=peerId
|
||||
|
||||
if w.gossip_enabled:
|
||||
|
Loading…
x
Reference in New Issue
Block a user