2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
libp2p/protocols/pubsub
|
|
|
|
import
|
2022-08-30 13:57:45 +00:00
|
|
|
../../../protocol/waku_message,
|
|
|
|
../../message_cache
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2022-11-03 15:36:24 +00:00
|
|
|
logScope:
|
|
|
|
topics = "waku node rest relay_api"
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
export message_cache
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
##### TopicCache
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
type TopicCacheResult*[T] = MessageCacheResult[T]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2022-11-09 14:00:11 +00:00
|
|
|
type TopicCache* = MessageCache[PubSubTopic]
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
##### Message handler
|
2022-06-10 11:30:51 +00:00
|
|
|
|
2022-08-30 13:57:45 +00:00
|
|
|
type TopicCacheMessageHandler* = Topichandler
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
proc messageHandler*(cache: TopicCache): TopicCacheMessageHandler =
|
|
|
|
|
2022-11-09 14:00:11 +00:00
|
|
|
let handler = proc(pubsubTopic: string, data: seq[byte]): Future[void] {.async, closure.} =
|
|
|
|
trace "PubsubTopic handler triggered", pubsubTopic=pubsubTopic
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
# Add message to current cache
|
2022-11-07 15:24:16 +00:00
|
|
|
let msg = WakuMessage.decode(data)
|
2022-06-10 11:30:51 +00:00
|
|
|
if msg.isErr():
|
2022-11-09 14:00:11 +00:00
|
|
|
debug "WakuMessage received but failed to decode", msg=msg, pubsubTopic=pubsubTopic
|
2022-06-10 11:30:51 +00:00
|
|
|
# TODO: handle message decode failure
|
|
|
|
return
|
|
|
|
|
2022-11-09 14:00:11 +00:00
|
|
|
trace "WakuMessage received", msg=msg, pubsubTopic=pubsubTopic
|
|
|
|
cache.addMessage(PubSubTopic(pubsubTopic), msg.get())
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
handler
|