2019-09-10 02:15:52 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-09-10 02:15:52 +00:00
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
import tables, sets
|
2019-09-12 02:10:38 +00:00
|
|
|
import chronos, chronicles
|
2019-09-10 02:15:52 +00:00
|
|
|
import pubsubpeer,
|
|
|
|
../protocol,
|
|
|
|
../../connection,
|
|
|
|
../../peerinfo
|
|
|
|
|
|
|
|
export PubSubPeer
|
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
|
|
|
topic = "PubSub"
|
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
type
|
2019-09-12 02:10:38 +00:00
|
|
|
TopicHandler* = proc (topic: string,
|
2019-09-24 16:16:39 +00:00
|
|
|
data: seq[byte]):
|
|
|
|
Future[void] {.closure, gcsafe.}
|
|
|
|
|
|
|
|
TopicPair* = tuple[topic: string, handler: TopicHandler]
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
Topic* = object
|
|
|
|
name*: string
|
2019-09-24 16:16:39 +00:00
|
|
|
handler*: seq[TopicHandler]
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
PubSub* = ref object of LPProtocol
|
|
|
|
peerInfo*: PeerInfo
|
|
|
|
topics*: Table[string, Topic] # local topics
|
|
|
|
peers*: Table[string, PubSubPeer] # peerid to peer map
|
|
|
|
peerTopics*: Table[string, HashSet[string]] # topic to remote peer map
|
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
method subscribeToPeer*(p: PubSub, conn: Connection) {.base, async, gcsafe.} =
|
2019-09-10 02:15:52 +00:00
|
|
|
## subscribe to a peer to send/receive pubsub messages
|
|
|
|
discard
|
|
|
|
|
2019-09-24 16:16:39 +00:00
|
|
|
method unsubscribe*(p: PubSub,
|
|
|
|
topics: seq[TopicPair]) {.base, async, gcsafe.} =
|
2019-09-10 02:15:52 +00:00
|
|
|
## unsubscribe from a list of ``topic`` strings
|
2019-09-24 16:16:39 +00:00
|
|
|
for t in topics:
|
|
|
|
for i, h in p.topics[t.topic].handler:
|
|
|
|
if h == t.handler:
|
|
|
|
p.topics[t.topic].handler.del(i)
|
|
|
|
|
|
|
|
method unsubscribe*(p: PubSub,
|
|
|
|
topic: string,
|
|
|
|
handler: TopicHandler): Future[void] {.base, gcsafe.} =
|
|
|
|
## unsubscribe from a ``topic`` string
|
|
|
|
result = p.unsubscribe(@[(topic, handler)])
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-09-24 16:16:39 +00:00
|
|
|
method subscribe*(p: PubSub,
|
|
|
|
topic: string,
|
2019-09-10 02:15:52 +00:00
|
|
|
handler: TopicHandler)
|
|
|
|
{.base, async, gcsafe.} =
|
|
|
|
## subscribe to a topic
|
|
|
|
##
|
|
|
|
## ``topic`` - a string topic to subscribe to
|
|
|
|
##
|
|
|
|
## ``handler`` - is a user provided proc
|
|
|
|
## that will be triggered
|
|
|
|
## on every received message
|
|
|
|
##
|
2019-09-24 16:16:39 +00:00
|
|
|
if not p.topics.contains(topic):
|
|
|
|
p.topics[topic] = Topic(name: topic)
|
|
|
|
|
|
|
|
p.topics[topic].handler.add(handler)
|
2019-09-10 02:15:52 +00:00
|
|
|
|
|
|
|
method publish*(p: PubSub, topic: string, data: seq[byte]) {.base, async, gcsafe.} =
|
|
|
|
## publish to a ``topic``
|
|
|
|
discard
|