|
1 | 1 | package data |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + |
4 | 8 | "github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction" |
| 9 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics" |
5 | 10 | "github.com/HyperloopUPV-H8/h9-backend/pkg/websocket" |
| 11 | + "github.com/google/uuid" |
| 12 | + ws "github.com/gorilla/websocket" |
6 | 13 | ) |
7 | 14 |
|
8 | 15 | const UpdateName abstraction.BrokerTopic = "connection/update" |
| 16 | +const SubscribeName abstraction.BrokerTopic = "connection/update" |
9 | 17 |
|
10 | 18 | 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 |
13 | 36 | } |
14 | 37 |
|
15 | 38 | func (update *Update) Topic() abstraction.BrokerTopic { |
16 | 39 | return UpdateName |
17 | 40 | } |
18 | 41 |
|
19 | 42 | 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 | + |
20 | 78 | return nil |
21 | 79 | } |
22 | 80 |
|
23 | 81 | 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 | + } |
25 | 116 | } |
26 | 117 |
|
27 | 118 | func (update *Update) SetPool(pool *websocket.Pool) { |
|
0 commit comments