Skip to content

Commit

Permalink
feature/filter (#63)
Browse files Browse the repository at this point in the history
* simplified filter

* add filter

* eol

* Update waku_protocol2.nim

* trigger GitHub actions

* comment

* fix import

* oops

* and

* init filters

* import tables
  • Loading branch information
decanus authored Jul 22, 2020
1 parent a83ed55 commit fd7a623
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions waku/protocol/v2/filter.nim
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions waku/protocol/v2/waku_protocol2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.} =
Expand All @@ -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) =
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit fd7a623

Please sign in to comment.