From a80e953614315a561da8f1fd62e17bb9d41f7922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20F=C3=ADsica?= Date: Sat, 21 Jun 2025 13:23:44 +0200 Subject: [PATCH 1/4] Send a confirmation message to the frontend --- backend/pkg/broker/topics/message/update.go | 12 ++++++++++++ backend/pkg/vehicle/vehicle.go | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/pkg/broker/topics/message/update.go b/backend/pkg/broker/topics/message/update.go index f9291f74b..ab3d73092 100644 --- a/backend/pkg/broker/topics/message/update.go +++ b/backend/pkg/broker/topics/message/update.go @@ -7,12 +7,15 @@ import ( "github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction" "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics" + "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/data" "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/protection" "github.com/HyperloopUPV-H8/h9-backend/pkg/websocket" "github.com/google/uuid" ws "github.com/gorilla/websocket" ) +var _ = data.Packet{} + const UpdateName abstraction.BrokerTopic = "message/update" const SubscribeName abstraction.BrokerTopic = "message/update" @@ -128,8 +131,17 @@ func (push *pushStruct) Data(boardID abstraction.BoardId, idToBoard map[abstract Name: string(data.Name), Timestamp: data.Timestamp, } + case *data.Packet: + return wrapper{ + Kind: "info", + Payload: "Order Sent", + Board: string(idToBoard[boardID]), + Name: string(data.Id()), + Timestamp: protection.NowTimestamp(), + } } return wrapper{} + } type wrapper struct { diff --git a/backend/pkg/vehicle/vehicle.go b/backend/pkg/vehicle/vehicle.go index b067bef3c..fa27b381d 100644 --- a/backend/pkg/vehicle/vehicle.go +++ b/backend/pkg/vehicle/vehicle.go @@ -54,6 +54,12 @@ func (vehicle *Vehicle) UserPush(push abstraction.BrokerPush) error { return err } + err = vehicle.broker.Push(message_topic.Push(packet, 1)) // TODO: Get board ID, it's currently hardcoded + if err != nil { + fmt.Fprintf(os.Stderr, "error sending info packet to the frontend: %v\n", err) + return err + } + err = vehicle.transport.SendMessage(transport.NewPacketMessage(packet)) if err != nil { fmt.Fprintf(os.Stderr, "error sending packet: %v\n", err) @@ -66,7 +72,6 @@ func (vehicle *Vehicle) UserPush(push abstraction.BrokerPush) error { To: vehicle.idToBoardName[uint16(packet.Id())], Timestamp: packet.Timestamp(), }) - if err != nil && !errors.Is(err, logger.ErrLoggerNotRunning{}) { fmt.Fprintln(os.Stderr, "Error pushing record to logger: ", err) } From e36c93e9363b7d570ddbeabb6a6103f96930223a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20F=C3=ADsica?= Date: Sat, 21 Jun 2025 18:52:22 +0200 Subject: [PATCH 2/4] Changed Push to use board name instead of board id --- .../pkg/broker/topics/message/message_test.go | 4 ++-- backend/pkg/broker/topics/message/update.go | 18 ++++++++---------- backend/pkg/vehicle/notification.go | 2 +- backend/pkg/vehicle/vehicle.go | 4 ++-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/backend/pkg/broker/topics/message/message_test.go b/backend/pkg/broker/topics/message/message_test.go index 2c920bad5..c160b1ed5 100644 --- a/backend/pkg/broker/topics/message/message_test.go +++ b/backend/pkg/broker/topics/message/message_test.go @@ -39,7 +39,7 @@ func TestMessageTopic_Push(t *testing.T) { messageTopic.SetPool(pool) // Simulate sending a download request - request := data.Push("test", 0) + request := data.Push("test", "test_board") err = messageTopic.Push(request) if err != nil { t.Fatal("Error pushing download request:", err) @@ -84,7 +84,7 @@ func TestMessageTopic_ClientMessage(t *testing.T) { messageTopic.SetAPI(api) packet := protection.NewPacket(0, protection.OkSeverity) - payload := data.Push(packet, 0) + payload := data.Push(packet, "test_board") payloadBytes, _ := json.Marshal(payload) messageTopic.ClientMessage(websocket.ClientId{0}, &websocket.Message{ Topic: data.SubscribeName, diff --git a/backend/pkg/broker/topics/message/update.go b/backend/pkg/broker/topics/message/update.go index ab3d73092..49d829260 100644 --- a/backend/pkg/broker/topics/message/update.go +++ b/backend/pkg/broker/topics/message/update.go @@ -22,7 +22,6 @@ const SubscribeName abstraction.BrokerTopic = "message/update" type Update struct { subscribersMx *sync.Mutex subscribers map[websocket.ClientId]struct{} - idToBoard map[abstraction.BoardId]string pool *websocket.Pool api abstraction.BrokerAPI } @@ -31,7 +30,6 @@ func NewUpdateTopic(idToBoard map[abstraction.BoardId]string) *Update { return &Update{ subscribersMx: &sync.Mutex{}, subscribers: make(map[websocket.ClientId]struct{}), - idToBoard: idToBoard, } } @@ -45,7 +43,7 @@ func (update *Update) Push(p abstraction.BrokerPush) error { return topics.ErrUnexpectedPush{Push: p} } - raw, err := json.Marshal(push.Data(push.boardId, update.idToBoard)) + raw, err := json.Marshal(push.Data(push.boardName)) if err != nil { return err } @@ -103,19 +101,19 @@ func (update *Update) SetAPI(api abstraction.BrokerAPI) { } type pushStruct struct { - data any - boardId abstraction.BoardId + data any + boardName string } -func Push(data any, boardId abstraction.BoardId) *pushStruct { - return &pushStruct{data: data, boardId: boardId} +func Push(data any, boardName string) *pushStruct { + return &pushStruct{data: data, boardName: boardName} } func (push *pushStruct) Topic() abstraction.BrokerTopic { return UpdateName } -func (push *pushStruct) Data(boardID abstraction.BoardId, idToBoard map[abstraction.BoardId]string) wrapper { +func (push *pushStruct) Data(boardName string) wrapper { switch data := push.data.(type) { case *protection.Packet: return wrapper{ @@ -127,7 +125,7 @@ func (push *pushStruct) Data(boardID abstraction.BoardId, idToBoard map[abstract Kind: string(data.Data.Name()), Data: data.Data, }, - Board: string(idToBoard[boardID]), + Board: boardName, Name: string(data.Name), Timestamp: data.Timestamp, } @@ -135,7 +133,7 @@ func (push *pushStruct) Data(boardID abstraction.BoardId, idToBoard map[abstract return wrapper{ Kind: "info", Payload: "Order Sent", - Board: string(idToBoard[boardID]), + Board: boardName, Name: string(data.Id()), Timestamp: protection.NowTimestamp(), } diff --git a/backend/pkg/vehicle/notification.go b/backend/pkg/vehicle/notification.go index 72fde59c3..ffc4ddfa4 100644 --- a/backend/pkg/vehicle/notification.go +++ b/backend/pkg/vehicle/notification.go @@ -84,7 +84,7 @@ func (vehicle *Vehicle) handlePacketNotification(notification transport.PacketNo case *protection.Packet: boardId := vehicle.ipToBoardId[strings.Split(notification.From, ":")[0]] - err := vehicle.broker.Push(message_topic.Push(p, boardId)) + err := vehicle.broker.Push(message_topic.Push(p, vehicle.idToBoardName[uint16(p.Id())])) if err != nil { vehicle.trace.Error().Stack().Err(err).Msg("broker push") return errors.Join(fmt.Errorf("update protection to frontend (%s protection with id %d and kind %d from %s to %s)", p.Severity(), p.Id(), p.Kind, notification.From, notification.To), err) diff --git a/backend/pkg/vehicle/vehicle.go b/backend/pkg/vehicle/vehicle.go index fa27b381d..66500761d 100644 --- a/backend/pkg/vehicle/vehicle.go +++ b/backend/pkg/vehicle/vehicle.go @@ -54,7 +54,7 @@ func (vehicle *Vehicle) UserPush(push abstraction.BrokerPush) error { return err } - err = vehicle.broker.Push(message_topic.Push(packet, 1)) // TODO: Get board ID, it's currently hardcoded + err = vehicle.broker.Push(message_topic.Push(packet, vehicle.idToBoardName[uint16(packet.Id())])) if err != nil { fmt.Fprintf(os.Stderr, "error sending info packet to the frontend: %v\n", err) return err @@ -183,5 +183,5 @@ func (vehicle *Vehicle) notifyError(name string, err error) { packet.Data = &protection.ErrorHandler{ Error: err.Error(), } - vehicle.broker.Push(message_topic.Push(packet, 255)) + vehicle.broker.Push(message_topic.Push(packet, "Error")) } From 0816d2a9abc8485ea5f398a9582fed3256f1f68e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20F=C3=ADsica?= Date: Sat, 21 Jun 2025 18:55:18 +0200 Subject: [PATCH 3/4] Show confirmation message after actually sending the order --- backend/pkg/vehicle/vehicle.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/pkg/vehicle/vehicle.go b/backend/pkg/vehicle/vehicle.go index 66500761d..e2e1ec262 100644 --- a/backend/pkg/vehicle/vehicle.go +++ b/backend/pkg/vehicle/vehicle.go @@ -54,15 +54,15 @@ func (vehicle *Vehicle) UserPush(push abstraction.BrokerPush) error { return err } - err = vehicle.broker.Push(message_topic.Push(packet, vehicle.idToBoardName[uint16(packet.Id())])) + err = vehicle.transport.SendMessage(transport.NewPacketMessage(packet)) if err != nil { - fmt.Fprintf(os.Stderr, "error sending info packet to the frontend: %v\n", err) + fmt.Fprintf(os.Stderr, "error sending packet: %v\n", err) return err } - err = vehicle.transport.SendMessage(transport.NewPacketMessage(packet)) + err = vehicle.broker.Push(message_topic.Push(packet, vehicle.idToBoardName[uint16(packet.Id())])) if err != nil { - fmt.Fprintf(os.Stderr, "error sending packet: %v\n", err) + fmt.Fprintf(os.Stderr, "error sending info packet to the frontend: %v\n", err) return err } From cef55f0b534c2322fa2505868e0f696f5d651563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20F=C3=ADsica?= Date: Sat, 21 Jun 2025 19:27:52 +0200 Subject: [PATCH 4/4] Remove boardIdToBoard --- backend/cmd/main.go | 7 ++----- backend/pkg/broker/topics/message/message_test.go | 4 ++-- backend/pkg/broker/topics/message/update.go | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index ff2a9fc57..fce569d08 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -181,11 +181,8 @@ func main() { connectionTopic := connection_topic.NewUpdateTopic() orderTopic := order_topic.NewSendTopic() loggerTopic := logger_topic.NewEnableTopic() - boardIdToBoard := make(map[abstraction.BoardId]string) - for name, id := range adj.Info.BoardIds { - boardIdToBoard[abstraction.BoardId(id)] = name - } - messageTopic := message_topic.NewUpdateTopic(boardIdToBoard) + + messageTopic := message_topic.NewUpdateTopic() stateOrderTopic := order_topic.NewState(idToBoard, trace.Logger) broker.AddTopic(data_topic.UpdateName, dataTopic) diff --git a/backend/pkg/broker/topics/message/message_test.go b/backend/pkg/broker/topics/message/message_test.go index c160b1ed5..520af3e1c 100644 --- a/backend/pkg/broker/topics/message/message_test.go +++ b/backend/pkg/broker/topics/message/message_test.go @@ -34,7 +34,7 @@ func TestMessageTopic_Push(t *testing.T) { client := websocket.NewClient(c) clientChan <- client - messageTopic := data.NewUpdateTopic(map[abstraction.BoardId]string{}) + messageTopic := data.NewUpdateTopic() messageTopic.SetAPI(api) messageTopic.SetPool(pool) @@ -80,7 +80,7 @@ func TestMessageTopic_ClientMessage(t *testing.T) { logger := zerolog.New(os.Stdout).With().Timestamp().Logger() api := broker.New(logger) - messageTopic := data.NewUpdateTopic(map[abstraction.BoardId]string{}) + messageTopic := data.NewUpdateTopic() messageTopic.SetAPI(api) packet := protection.NewPacket(0, protection.OkSeverity) diff --git a/backend/pkg/broker/topics/message/update.go b/backend/pkg/broker/topics/message/update.go index 49d829260..1adecd145 100644 --- a/backend/pkg/broker/topics/message/update.go +++ b/backend/pkg/broker/topics/message/update.go @@ -26,7 +26,7 @@ type Update struct { api abstraction.BrokerAPI } -func NewUpdateTopic(idToBoard map[abstraction.BoardId]string) *Update { +func NewUpdateTopic() *Update { return &Update{ subscribersMx: &sync.Mutex{}, subscribers: make(map[websocket.ClientId]struct{}),