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