Skip to content

Commit dc723ef

Browse files
committed
Implement connection topic
1 parent d33c815 commit dc723ef

3 files changed

Lines changed: 118 additions & 5 deletions

File tree

backend/cmd/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/HyperloopUPV-H8/h9-backend/internal/ws_handle"
3838
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
3939
"github.com/HyperloopUPV-H8/h9-backend/pkg/broker"
40+
connection_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/connection"
4041
data_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/data"
4142
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport"
4243
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/sniffer"
@@ -161,10 +162,12 @@ func main() {
161162
OnUserPush: func(push abstraction.BrokerPush) {},
162163
})
163164

164-
dataTopic := data_topic.NewUpdateTopic(time.Second / 60)
165+
dataTopic := data_topic.NewUpdateTopic(time.Second / 10)
165166
defer dataTopic.Stop()
167+
connectionTopic := connection_topic.NewUpdateTopic()
166168

167169
broker.AddTopic(data_topic.UpdateName, dataTopic)
170+
broker.AddTopic(connection_topic.UpdateName, connectionTopic)
168171

169172
connections := make(chan *websocket.Client)
170173
upgrader := websocket.NewUpgrader(connections)
@@ -226,7 +229,7 @@ func main() {
226229
},
227230

228231
OnConnectionUpdate: func(target abstraction.TransportTarget, isConnected bool) {
229-
connectionTransfer.Update(string(target), isConnected)
232+
connectionTopic.Push(connection_topic.NewConnection(string(target), isConnected))
230233
},
231234
})
232235

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package data
2+
3+
import "github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
4+
5+
type Connection struct {
6+
Name string `json:"name"`
7+
IsConnected bool `json:"isConnected"`
8+
}
9+
10+
func NewConnection(name string, isConnected bool) *Connection {
11+
return &Connection{
12+
Name: name,
13+
IsConnected: isConnected,
14+
}
15+
}
16+
17+
func (connection *Connection) Topic() abstraction.BrokerTopic {
18+
return UpdateName
19+
}

backend/pkg/broker/topics/connection/update.go

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,118 @@
11
package data
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"sync"
7+
48
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
9+
"github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics"
510
"github.com/HyperloopUPV-H8/h9-backend/pkg/websocket"
11+
"github.com/google/uuid"
12+
ws "github.com/gorilla/websocket"
613
)
714

815
const UpdateName abstraction.BrokerTopic = "connection/update"
16+
const SubscribeName abstraction.BrokerTopic = "connection/update"
917

1018
type Update struct {
11-
pool *websocket.Pool
12-
api abstraction.BrokerAPI
19+
subscriberMx *sync.Mutex
20+
subscribers map[websocket.ClientId]struct{}
21+
connectionMx *sync.Mutex
22+
connections map[string]Connection
23+
pool *websocket.Pool
24+
api abstraction.BrokerAPI
25+
}
26+
27+
func NewUpdateTopic() *Update {
28+
topic := &Update{
29+
subscriberMx: &sync.Mutex{},
30+
subscribers: make(map[websocket.ClientId]struct{}),
31+
connectionMx: &sync.Mutex{},
32+
connections: make(map[string]Connection),
33+
}
34+
35+
return topic
1336
}
1437

1538
func (update *Update) Topic() abstraction.BrokerTopic {
1639
return UpdateName
1740
}
1841

1942
func (update *Update) Push(push abstraction.BrokerPush) error {
43+
connection, ok := push.(*Connection)
44+
if !ok {
45+
return topics.ErrUnexpectedPush{Push: push}
46+
}
47+
48+
update.connectionMx.Lock()
49+
defer update.connectionMx.Unlock()
50+
update.connections[connection.Name] = *connection
51+
52+
rawPayload, err := json.Marshal(update.connections)
53+
if err != nil {
54+
return err
55+
}
56+
57+
message := websocket.Message{
58+
Topic: UpdateName,
59+
Payload: rawPayload,
60+
}
61+
62+
update.subscriberMx.Lock()
63+
defer update.subscriberMx.Unlock()
64+
flagged := make([]websocket.ClientId, 0, len(update.subscribers))
65+
for id := range update.subscribers {
66+
err := update.pool.Write(id, message)
67+
if err != nil {
68+
flagged = append(flagged, id)
69+
}
70+
}
71+
72+
for _, id := range flagged {
73+
update.pool.Disconnect(id, ws.CloseNormalClosure, "client disconnected")
74+
delete(update.subscribers, id)
75+
fmt.Printf("connection/update unsubscribed %s\n", uuid.UUID(id).String())
76+
}
77+
2078
return nil
2179
}
2280

2381
func (update *Update) Pull(request abstraction.BrokerRequest) (abstraction.BrokerResponse, error) {
24-
return nil, nil
82+
return nil, topics.ErrOpNotSupported{}
83+
}
84+
85+
func (update *Update) ClientMessage(id websocket.ClientId, message *websocket.Message) {
86+
update.subscriberMx.Lock()
87+
defer update.subscriberMx.Unlock()
88+
89+
switch message.Topic {
90+
case SubscribeName:
91+
update.subscribers[id] = struct{}{}
92+
fmt.Printf("connection/update subscribed %s\n", uuid.UUID(id).String())
93+
94+
update.connectionMx.Lock()
95+
defer update.connectionMx.Unlock()
96+
rawPayload, err := json.Marshal(update.connections)
97+
if err != nil {
98+
fmt.Println(err)
99+
}
100+
101+
message := websocket.Message{
102+
Topic: UpdateName,
103+
Payload: rawPayload,
104+
}
105+
106+
err = update.pool.Write(id, message)
107+
if err != nil {
108+
fmt.Println(err)
109+
}
110+
111+
default:
112+
update.pool.Disconnect(id, ws.CloseUnsupportedData, "unsupported topic")
113+
delete(update.subscribers, id)
114+
fmt.Printf("connection/update unsubscribed %s\n", uuid.UUID(id).String())
115+
}
25116
}
26117

27118
func (update *Update) SetPool(pool *websocket.Pool) {

0 commit comments

Comments
 (0)