diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-29 14:10:18 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-29 14:10:18 (GMT) |
commit | 4911fe3fd94019fcca730710e0b411e8e5a66847 (patch) | |
tree | c6c5d4c050675c4924991225f0076aabece3bfb7 /src/helsinki.at/rhimportd | |
parent | 485e0b2bdcec3c82d6c0b4d7719808a0dfc9289c (diff) |
moved websocket session handling to same goroutine
Diffstat (limited to 'src/helsinki.at/rhimportd')
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlWebSocket.go | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/src/helsinki.at/rhimportd/ctrlWebSocket.go b/src/helsinki.at/rhimportd/ctrlWebSocket.go index 93134fd..b3f10f6 100644 --- a/src/helsinki.at/rhimportd/ctrlWebSocket.go +++ b/src/helsinki.at/rhimportd/ctrlWebSocket.go @@ -83,7 +83,31 @@ func sendWebSocketErrorResponse(ws *websocket.Conn, code int, err_str string) { } } -func webSocketSessionHandler() { +func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.Conn) { + defer ws.Close() + + select { + case reqdata := <-reqchan: + switch reqdata.Command { + case "new": + sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "new session - not yet implemented") + return + case "reconnect": + if reqdata.UserName == "" { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field LOGIN_NAME") + return + } + if reqdata.Id == "" { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field ID") + return + } + sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "reconnect session - not yet implemented") + return + default: + sendWebSocketErrorResponse(ws, http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command)) + return + } + } } func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *rhimport.SessionStoreChan, trusted bool, w http.ResponseWriter, r *http.Request) { @@ -98,6 +122,8 @@ func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions * return } rhdl.Println("WebSocket Client", ws.RemoteAddr(), "connected") + reqchan := make(chan webSocketRequestData) + go webSocketSessionHandler(reqchan, ws) for { reqdata := newWebSocketRequestData(conf) @@ -109,26 +135,7 @@ func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions * if trusted { reqdata.UserName = r.Header.Get("X-Forwarded-User") } - - switch reqdata.Command { - case "new": - sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "new session - not yet implemented") - return - case "reconnect": - if reqdata.UserName == "" { - sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field LOGIN_NAME") - return - } - if reqdata.Id == "" { - sendWebSocketErrorResponse(ws, http.StatusBadRequest, "missing mandotary field ID") - return - } - sendWebSocketErrorResponse(ws, http.StatusNotImplemented, "get session - not yet implemented") - return - default: - sendWebSocketErrorResponse(ws, http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command)) - return - } + reqchan <- *reqdata } } |