go-waku/waku/v2/rpc/relay.go

137 lines
3.2 KiB
Go
Raw Normal View History

2021-11-06 10:49:47 +00:00
package rpc
import (
"fmt"
2021-11-06 10:49:47 +00:00
"net/http"
2021-11-18 14:20:58 +00:00
"sync"
2021-11-06 10:49:47 +00:00
"github.com/status-im/go-waku/waku/v2/node"
2021-11-18 14:20:58 +00:00
"github.com/status-im/go-waku/waku/v2/protocol"
2021-11-06 10:49:47 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/status-im/go-waku/waku/v2/protocol/relay"
)
type RelayService struct {
node *node.WakuNode
2021-11-18 14:20:58 +00:00
messages map[string][]*pb.WakuMessage
messagesMutex sync.RWMutex
ch chan *protocol.Envelope
quit chan bool
2021-11-06 10:49:47 +00:00
}
type RelayMessageArgs struct {
Topic string `json:"topic,omitempty"`
Message pb.WakuMessage `json:"message,omitempty"`
}
type TopicsArgs struct {
Topics []string `json:"topics,omitempty"`
}
2021-11-18 14:20:58 +00:00
type TopicArgs struct {
Topic string `json:"topic,omitempty"`
}
type MessagesReply struct {
Messages []*pb.WakuMessage `json:"messages,omitempty"`
}
func NewRelayService(node *node.WakuNode) *RelayService {
return &RelayService{
node: node,
messages: make(map[string][]*pb.WakuMessage),
quit: make(chan bool),
}
}
func (r *RelayService) addEnvelope(envelope *protocol.Envelope) {
r.messagesMutex.Lock()
defer r.messagesMutex.Unlock()
if _, ok := r.messages[envelope.PubsubTopic()]; !ok {
return
}
r.messages[envelope.PubsubTopic()] = append(r.messages[envelope.PubsubTopic()], envelope.Message())
}
func (r *RelayService) Start() {
r.ch = make(chan *protocol.Envelope, 1024)
r.node.Broadcaster().Register(r.ch)
for {
select {
case <-r.quit:
return
case envelope := <-r.ch:
r.addEnvelope(envelope)
}
}
}
func (r *RelayService) Stop() {
r.quit <- true
r.node.Broadcaster().Unregister(r.ch)
close(r.ch)
}
2021-11-06 10:49:47 +00:00
func (r *RelayService) PostV1Message(req *http.Request, args *RelayMessageArgs, reply *SuccessReply) error {
_, err := r.node.Relay().Publish(req.Context(), &args.Message, (*relay.Topic)(&args.Topic))
if err != nil {
log.Error("Error publishing message:", err)
reply.Success = false
reply.Error = err.Error()
} else {
reply.Success = true
}
return nil
}
func (r *RelayService) PostV1Subscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
ctx := req.Context()
for _, topic := range args.Topics {
_, err := r.node.Relay().Subscribe(ctx, (*relay.Topic)(&topic))
if err != nil {
log.Error("Error subscribing to topic:", topic, "err:", err)
reply.Success = false
reply.Error = err.Error()
return nil
}
2021-11-18 14:20:58 +00:00
r.messages[topic] = make([]*pb.WakuMessage, 0)
2021-11-06 10:49:47 +00:00
}
reply.Success = true
return nil
}
func (r *RelayService) DeleteV1Subscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
ctx := req.Context()
for _, topic := range args.Topics {
err := r.node.Relay().Unsubscribe(ctx, (relay.Topic)(topic))
if err != nil {
log.Error("Error unsubscribing from topic:", topic, "err:", err)
reply.Success = false
reply.Error = err.Error()
return nil
}
2021-11-18 14:20:58 +00:00
delete(r.messages, topic)
2021-11-06 10:49:47 +00:00
}
reply.Success = true
return nil
}
2021-11-18 14:20:58 +00:00
func (r *RelayService) GetV1Messages(req *http.Request, args *TopicArgs, reply *MessagesReply) error {
r.messagesMutex.Lock()
defer r.messagesMutex.Unlock()
if _, ok := r.messages[args.Topic]; !ok {
return fmt.Errorf("topic %s not subscribed", args.Topic)
}
reply.Messages = r.messages[args.Topic]
r.messages[args.Topic] = make([]*pb.WakuMessage, 0)
return nil
}