-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathmessaging_client.nim
More file actions
82 lines (70 loc) · 3.12 KB
/
Copy pathmessaging_client.nim
File metadata and controls
82 lines (70 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import results, chronos
import chronicles
import brokers/broker_implement, brokers/broker_context
import logos_delivery/api/messaging_client_interface
import
logos_delivery/waku/api/types,
logos_delivery/waku/node/[waku_node, subscription_manager],
logos_delivery/messaging/delivery_service/[recv_service, send_service],
logos_delivery/messaging/delivery_service/send_service/delivery_task
type MessagingClient* = ref object of MessagingClientInterface
node: WakuNode
sendService*: SendService
recvService*: RecvService
started: bool
proc start*(self: MessagingClient): Result[void, string] =
if self.started:
return ok()
self.recvService.startRecvService()
self.sendService.startSendService()
self.started = true
ok()
proc stop*(self: MessagingClient) {.async.} =
if not self.started:
return
await self.sendService.stopSendService()
await self.recvService.stopRecvService()
self.started = false
BrokerImplement MessagingClient of MessagingClientInterface:
proc new*(
T: typedesc[MessagingClient],
node: WakuNode,
sendService: SendService,
recvService: RecvService,
): T =
T(node: node, sendService: sendService, recvService: recvService, started: false)
method subscribe(
self: MessagingClient, contentTopic: ContentTopic
): Future[Result[void, string]] {.async.} =
return self.node.subscriptionManager.subscribe(contentTopic)
method unsubscribe(
self: MessagingClient, contentTopic: ContentTopic
): Future[Result[void, string]] {.async.} =
return self.node.subscriptionManager.unsubscribe(contentTopic)
method send(
self: MessagingClient, envelope: MessageEnvelope
): Future[Result[RequestId, string]] {.async.} =
## High-level messaging API send. Auto-subscribes to the content topic
## (so the local node sees its own gossipsub broadcast), builds a
## `DeliveryTask`, and hands it to the send service. Returns the request
## id the caller can correlate with `MessageSentEvent` / `MessageErrorEvent`.
let isSubbed =
self.node.subscriptionManager.isSubscribed(envelope.contentTopic).valueOr(false)
if not isSubbed:
info "Auto-subscribing to topic on send", contentTopic = envelope.contentTopic
self.node.subscriptionManager.subscribe(envelope.contentTopic).isOkOr:
warn "Failed to auto-subscribe", error = error
return err("Failed to auto-subscribe before sending: " & error)
let requestId = RequestId.new(self.node.rng)
let deliveryTask = DeliveryTask.new(requestId, envelope, self.node.brokerCtx).valueOr:
return err("MessagingClient.send: Failed to create delivery task: " & error)
asyncSpawn self.sendService.send(deliveryTask)
return ok(requestId)
proc new*(T: typedesc[MessagingClient],
ctx: BrokerContext, useP2PReliability: bool, node: WakuNode
): Result[T, string] =
let sendService = SendService.new(useP2PReliability, node).valueOr:
error "Failed to initialize SendService", error = error
return err("Failed to initialize SendService: " & error)
let recvService = RecvService.new(node)
ok(MessagingClient.createUnderContext(ctx, node, sendService, recvService))