Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE1] Send rumor state message to new WS connection #1954

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading