@@ -13,6 +13,7 @@ import (
1313 "runtime"
1414 "runtime/pprof"
1515 "strings"
16+ "time"
1617
1718 blcuPackage "github.com/HyperloopUPV-H8/h9-backend/internal/blcu"
1819 "github.com/HyperloopUPV-H8/h9-backend/internal/common"
@@ -30,6 +31,9 @@ import (
3031 vehicle_models "github.com/HyperloopUPV-H8/h9-backend/internal/vehicle/models"
3132 "github.com/HyperloopUPV-H8/h9-backend/internal/ws_handle"
3233 "github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
34+ "github.com/HyperloopUPV-H8/h9-backend/pkg/broker"
35+ connection_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/connection"
36+ data_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/data"
3337 "github.com/HyperloopUPV-H8/h9-backend/pkg/transport"
3438 "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/sniffer"
3539 "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/tcp"
@@ -40,6 +44,7 @@ import (
4044 "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/protection"
4145 "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/state"
4246 "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/presentation"
47+ "github.com/HyperloopUPV-H8/h9-backend/pkg/websocket"
4348 "github.com/fatih/color"
4449 "github.com/google/gopacket/layers"
4550 "github.com/google/gopacket/pcap"
@@ -147,6 +152,24 @@ func main() {
147152 var blcu blcuPackage.BLCU
148153 blcuAddr , useBlcu := info .Addresses .Boards ["BLCU" ]
149154
155+ // <--- broker --->
156+ broker := broker .New ()
157+ broker .SetAPI (& brokerAPI {
158+ OnUserPush : func (push abstraction.BrokerPush ) {},
159+ })
160+
161+ dataTopic := data_topic .NewUpdateTopic (time .Second / 10 )
162+ defer dataTopic .Stop ()
163+ connectionTopic := connection_topic .NewUpdateTopic ()
164+
165+ broker .AddTopic (data_topic .UpdateName , dataTopic )
166+ broker .AddTopic (connection_topic .UpdateName , connectionTopic )
167+
168+ connections := make (chan * websocket.Client )
169+ upgrader := websocket .NewUpgrader (connections )
170+ pool := websocket .NewPool (connections )
171+ broker .SetPool (pool )
172+
150173 // <--- transport --->
151174 orders := make (map [abstraction.PacketId ]struct {})
152175 for _ , board := range podData .Boards {
@@ -159,14 +182,17 @@ func main() {
159182
160183 transp := transport .NewTransport ()
161184
162- transp .SetAPI (& TransportAPI {
185+ transp .SetAPI (& transportAPI {
163186 OnNotification : func (notification abstraction.TransportNotification ) {
164187 packet := notification .(transport.PacketNotification )
165188
166189 switch p := packet .Packet .(type ) {
167190 case * data.Packet :
168191 update := updateFactory .NewUpdate (p )
169- dataTransfer .Update (update )
192+ err := broker .Push (data_topic .NewPush (& update ))
193+ if err != nil {
194+ fmt .Println (err )
195+ }
170196
171197 err = loggerHandler .PushRecord (& data_logger.Record {
172198 Packet : p ,
@@ -226,7 +252,7 @@ func main() {
226252 },
227253
228254 OnConnectionUpdate : func (target abstraction.TransportTarget , isConnected bool ) {
229- connectionTransfer . Update ( string (target ), isConnected )
255+ connectionTopic . Push ( connection_topic . NewConnection ( string (target ), isConnected ) )
230256 },
231257 })
232258
@@ -327,7 +353,7 @@ func main() {
327353 ProgramableBoards : uploadableBords ,
328354 }
329355
330- serverHandler , err := server .New (& websocketBroker , endpointData , config .Server )
356+ serverHandler , err := server .New (upgrader , endpointData , config .Server )
331357 if err != nil {
332358 trace .Fatal ().Err (err ).Msg ("Error creating server" )
333359 panic (err )
@@ -542,16 +568,16 @@ func getOps(units utils.Units) data.ConversionDescriptor {
542568 return output
543569}
544570
545- type TransportAPI struct {
571+ type transportAPI struct {
546572 OnNotification func (abstraction.TransportNotification )
547573 OnConnectionUpdate func (abstraction.TransportTarget , bool )
548574}
549575
550- func (api * TransportAPI ) Notification (notification abstraction.TransportNotification ) {
576+ func (api * transportAPI ) Notification (notification abstraction.TransportNotification ) {
551577 api .OnNotification (notification )
552578}
553579
554- func (api * TransportAPI ) ConnectionUpdate (target abstraction.TransportTarget , isConnected bool ) {
580+ func (api * transportAPI ) ConnectionUpdate (target abstraction.TransportTarget , isConnected bool ) {
555581 api .OnConnectionUpdate (target , isConnected )
556582}
557583
@@ -605,3 +631,11 @@ func getTCPFilter(addrs []net.IP, serverPort uint16, clientPort uint16) string {
605631 filter := fmt .Sprintf ("(%s) and (%s) and (%s) and (%s) and (%s) and (%s)" , ports , notSynFinRst , notJustAck , nonZeroPayload , srcAddressesStr , dstAddressesStr )
606632 return filter
607633}
634+
635+ type brokerAPI struct {
636+ OnUserPush func (abstraction.BrokerPush )
637+ }
638+
639+ func (api * brokerAPI ) UserPush (push abstraction.BrokerPush ) {
640+ api .OnUserPush (push )
641+ }
0 commit comments