Skip to content

Commit

Permalink
Merge pull request #1954 from dedis/work-be1-stuart-send-rumorstate-t…
Browse files Browse the repository at this point in the history
…o-new-ws

[BE1] Send rumor state message to new WS connection
  • Loading branch information
sgueissa authored Jun 27, 2024
2 parents be8abce + b1b85cf commit 5fd2e1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (h *Handler) Handle(socket socket.Socket, msg []byte) (*int, error) {
return nil, nil
}

func (h *Handler) SendRumorState() error {
func (h *Handler) createRumorState() ([]byte, error) {
timestamp, err := h.db.GetRumorTimestamp()
if err != nil {
return err
return nil, err
}

id := h.queries.GetNextID()
Expand All @@ -94,14 +94,34 @@ func (h *Handler) SendRumorState() error {

buf, err := json.Marshal(rumorStateMessage)
if err != nil {
return errors.NewJsonMarshalError(err.Error())
return nil, errors.NewJsonMarshalError(err.Error())
}

err = h.queries.AddRumorState(id)
if err != nil {
return nil, err
}

return buf, nil
}

func (h *Handler) SendRumorState() error {
buf, err := h.createRumorState()
if err != nil {
return err
}

h.sockets.SendToRandom(buf)
return nil
}

func (h *Handler) SendRumorStateTo(socket socket.Socket) error {
buf, err := h.createRumorState()
if err != nil {
return err
}

socket.Send(buf)

return nil
}
5 changes: 5 additions & 0 deletions be1-go/internal/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type RumorSender interface {

type RumorStateSender interface {
SendRumorState() error
SendRumorStateTo(socket socket.Socket) error
}

type Hub struct {
Expand Down Expand Up @@ -235,6 +236,10 @@ func New(dbPath string, ownerPubKey kyber.Point, clientAddress, serverAddress st

func (h *Hub) NotifyNewServer(socket socket.Socket) {
h.sockets.Upsert(socket)
err := h.rumorStateSender.SendRumorStateTo(socket)
if err != nil {
h.log.Error().Err(err).Send()
}
}

func (h *Hub) Start() {
Expand Down

0 comments on commit 5fd2e1c

Please sign in to comment.