Skip to content

Commit

Permalink
Merge pull request #1859 from dedis/work-be1-stuart-fix-connexion-pro…
Browse files Browse the repository at this point in the history
…blem

Fix connexion problem
  • Loading branch information
sgueissa committed May 16, 2024
2 parents 2db8b47 + e759095 commit 3ffd997
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions be1-go/internal/popserver/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (h *Hub) Start() {
go func() {
utils.LogInfo("start the Hub")
for {
utils.LogInfo("waiting for a new message")
select {
case incomingMessage := <-h.messageChan:
utils.LogInfo("start handling a message")
Expand All @@ -65,9 +66,9 @@ func (h *Hub) Start() {
} else {
utils.LogInfo("successfully handled a message")
}
case <-h.closedSockets:
utils.LogInfo("stopping the Sockets")
return
case socketID := <-h.closedSockets:
utils.LogInfo("stopping the Socket " + socketID)
state.UnsubscribeFromAll(socketID)
case <-h.stop:
utils.LogInfo("stopping the Hub")
return
Expand Down
12 changes: 12 additions & 0 deletions be1-go/internal/popserver/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Subscriber interface {
HasChannel(channel string) bool
Subscribe(channel string, socket socket.Socket) *answer.Error
Unsubscribe(channel string, socket socket.Socket) *answer.Error
UnsubscribeFromAll(socketID string)
SendToAll(buf []byte, channel string) *answer.Error
}

Expand Down Expand Up @@ -104,6 +105,17 @@ func Unsubscribe(socket socket.Socket, channel string) *answer.Error {
return subs.Unsubscribe(channel, socket)
}

func UnsubscribeFromAll(socketID string) *answer.Error {
subs, errAnswer := getSubs()
if errAnswer != nil {
return errAnswer
}

subs.UnsubscribeFromAll(socketID)

return nil
}

func SendToAll(buf []byte, channel string) *answer.Error {
subs, errAnswer := getSubs()
if errAnswer != nil {
Expand Down
15 changes: 15 additions & 0 deletions be1-go/internal/popserver/types/subscribers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"golang.org/x/xerrors"
"popstellar/message/answer"
"popstellar/network/socket"
Expand Down Expand Up @@ -65,6 +66,20 @@ func (s *Subscribers) Unsubscribe(channel string, socket socket.Socket) *answer.
return nil
}

func (s *Subscribers) UnsubscribeFromAll(socketID string) {
s.Lock()
defer s.Unlock()

for channel, subs := range s.list {
_, ok := subs[socketID]
if !ok {
continue
}
delete(s.list[channel], socketID)
fmt.Println("unsubscribe from " + channel)
}
}

// SendToAll sends a message to all sockets.
func (s *Subscribers) SendToAll(buf []byte, channel string) *answer.Error {
s.RLock()
Expand Down

0 comments on commit 3ffd997

Please sign in to comment.