-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ab9c3d
commit f856298
Showing
6 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
library/waku_thread/inter_thread_communication/requests/protocols/filter_request.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import options, std/[strutils, sequtils] | ||
import chronicles, chronos, results | ||
import | ||
../../../../../waku/waku_filter_v2/client, | ||
../../../../../waku/waku_core/message/message, | ||
../../../../../waku/factory/waku, | ||
../../../../../waku/waku_filter_v2/common, | ||
../../../../../waku/waku_core/subscription/push_handler, | ||
../../../../../waku/node/peer_manager/peer_manager, | ||
../../../../../waku/node/waku_node, | ||
../../../../../waku/waku_core/topics/pubsub_topic, | ||
../../../../../waku/waku_core/topics/content_topic, | ||
../../../../alloc | ||
|
||
type FilterMsgType* = enum | ||
SUBSCRIBE | ||
UNSUBSCRIBE | ||
UNSUBSCRIBE_ALL | ||
|
||
type FilterRequest* = object | ||
operation: FilterMsgType | ||
pubsubTopic: cstring | ||
contentTopics: cstring ## comma-separated list of content-topics | ||
filterPushEventCallback: FilterPushHandler ## handles incoming filter pushed msgs | ||
|
||
proc createShared*( | ||
T: type FilterRequest, | ||
op: FilterMsgType, | ||
pubsubTopic: cstring = "", | ||
contentTopics: cstring = "", | ||
filterPushEventCallback: FilterPushHandler = nil, | ||
): ptr type T = | ||
var ret = createShared(T) | ||
ret[].operation = op | ||
ret[].pubsubTopic = pubsubTopic.alloc() | ||
ret[].contentTopics = contentTopics.alloc() | ||
ret[].filterPushEventCallback = filterPushEventCallback | ||
|
||
return ret | ||
|
||
proc destroyShared(self: ptr FilterRequest) = | ||
deallocShared(self[].pubsubTopic) | ||
deallocShared(self[].contentTopics) | ||
deallocShared(self) | ||
|
||
proc process*( | ||
self: ptr FilterRequest, waku: ptr Waku | ||
): Future[Result[string, string]] {.async.} = | ||
defer: | ||
destroyShared(self) | ||
|
||
const FilterOpTimeout = 5.seconds | ||
if waku.node.wakuFilterClient.isNil(): | ||
let errorMsg = "FilterRequest waku.node.wakuFilterClient is nil" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
|
||
case self.operation | ||
of SUBSCRIBE: | ||
waku.node.wakuFilterClient.registerPushHandler(self.filterPushEventCallback) | ||
|
||
let peer = waku.node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: | ||
let errorMsg = | ||
"could not find peer with WakuFilterSubscribeCodec when subscribing" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
|
||
let pubsubTopic = some(PubsubTopic($self[].pubsubTopic)) | ||
let contentTopics = ($(self[].contentTopics)).split(",").mapIt(ContentTopic(it)) | ||
|
||
let subFut = waku.node.filterSubscribe(pubsubTopic, contentTopics, peer) | ||
if not await subFut.withTimeout(FilterOpTimeout): | ||
let errorMsg = "filter subscription timed out" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
of UNSUBSCRIBE: | ||
let peer = waku.node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: | ||
let errorMsg = | ||
"could not find peer with WakuFilterSubscribeCodec when unsubscribing" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
|
||
let pubsubTopic = some(PubsubTopic($self[].pubsubTopic)) | ||
let contentTopics = ($(self[].contentTopics)).split(",").mapIt(ContentTopic(it)) | ||
|
||
let subFut = waku.node.filterUnsubscribe(pubsubTopic, contentTopics, peer) | ||
if not await subFut.withTimeout(FilterOpTimeout): | ||
let errorMsg = "filter un-subscription timed out" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
of UNSUBSCRIBE_ALL: | ||
let peer = waku.node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: | ||
let errorMsg = | ||
"could not find peer with WakuFilterSubscribeCodec when unsubscribing all" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
|
||
let unsubFut = waku.node.filterUnsubscribeAll(peer) | ||
|
||
if not await unsubFut.withTimeout(FilterOpTimeout): | ||
let errorMsg = "filter un-subscription all timed out" | ||
error "fail filter process", error = errorMsg, op = $(self.operation) | ||
return err(errorMsg) | ||
|
||
return ok("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters