Skip to content

Commit

Permalink
Feat: Refactor chun router to close #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabt234 committed Jun 4, 2024
1 parent 800bcf2 commit 3b8b607
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions Routines/ChunkRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
ReadBufferSize: 8096,
WriteBufferSize: 8096,
}

///
Expand All @@ -25,6 +25,7 @@ routine that shall handle that chunk
type ChunkTypeToChannelMap struct {
mu sync.Mutex // Mutex to protect access to the map
chunkTypeRoutingMap map[string](chan string) // Map of chunk type string and channel key value pairs
RoutineWaitGroup sync.WaitGroup
}

/*
Expand All @@ -42,7 +43,6 @@ func (s *ChunkTypeToChannelMap) SendChunkToWebSocket(loggingChannel chan map[zer
} else {
// or drop data and return false
// operate on the router itself
loggingChannel <- CreateLogMessage(zerolog.InfoLevel, "Here")
s.RegisterChunkOnWebSocket(loggingChannel, chunkTypeKey, router)
return channelExists
}
Expand Down Expand Up @@ -104,14 +104,55 @@ func (s *ChunkTypeToChannelMap)RegisterChunkOnWebSocket(loggingChannel chan map[

s.chunkTypeRoutingMap[chunkTypeString] = make(chan string, 100)

router.GET("/DataTypes/"+chunkTypeString, func(c *gin.Context) {
// Upgrade the HTTP request into a websocket
WebSocketConnection, _ := upgrader.Upgrade(c.Writer, c.Request, nil)
defer WebSocketConnection.Close()
// When you get this HTTP request open the websocket
// This permenantly add this to the http
// Router as we are using a reference
router.GET("/DataTypes/"+chunkTypeString, func(c *gin.Context) {

// Upgrade the HTTP request into a websocket
WebSocketConnection, err := upgrader.Upgrade(c.Writer, c.Request, nil)
defer WebSocketConnection.Close()

if err != nil {
loggingChannel <- CreateLogMessage(zerolog.ErrorLevel, "Error upgrading to WebSocket:"+ err.Error())
return
}

s.RoutineWaitGroup.Add(2)
go s.HandleReceivedSignals(loggingChannel, WebSocketConnection);
go s.HandleSignalTransmissions(loggingChannel ,WebSocketConnection, chunkTypeString )
s.RoutineWaitGroup.Wait()

for {
var dataString, _ = s.GetChannelData(chunkTypeString)
WebSocketConnection.WriteMessage(websocket.TextMessage, []byte(dataString))
}
})
}

func (s *ChunkTypeToChannelMap)HandleReceivedSignals(loggingChannel chan map[zerolog.Level]string, WebSocketConnection *websocket.Conn) {

defer s.RoutineWaitGroup.Done()

for{
_, _, err := WebSocketConnection.ReadMessage()
if err != nil {
loggingChannel <- CreateLogMessage(zerolog.WarnLevel, "Issue reading message to WebSocket:"+ err.Error())
WebSocketConnection.Close()
break;
}
}

}

func (s *ChunkTypeToChannelMap)HandleSignalTransmissions(loggingChannel chan map[zerolog.Level]string, WebSocketConnection *websocket.Conn, chunkTypeString string) {

defer s.RoutineWaitGroup.Done()

for {
var dataString, _ = s.GetChannelData(chunkTypeString)
err := WebSocketConnection.WriteMessage(websocket.TextMessage, []byte(dataString))
if err != nil {
loggingChannel <- CreateLogMessage(zerolog.WarnLevel, "Issue writing message to WebSocket:"+ err.Error())
WebSocketConnection.Close()
break
}
}

}

0 comments on commit 3b8b607

Please sign in to comment.