2022-06-10 11:30:51 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
logScope: topics = "rest_api_relay_topiccache"
|
|
|
|
|
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 PubSubTopicString = string
|
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-08-30 13:57:45 +00:00
|
|
|
type TopicCache* = MessageCache[PubSubTopicString]
|
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-08-30 13:57:45 +00:00
|
|
|
let handler = proc(topic: string, data: seq[byte]): Future[void] {.async, closure.} =
|
2022-06-10 11:30:51 +00:00
|
|
|
trace "Topic handler triggered", topic=topic
|
|
|
|
|
|
|
|
# Add message to current cache
|
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
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
|