summaryrefslogtreecommitdiff
path: root/src/helsinki.at/rhimportd/ctrlWebSocket.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-30 20:50:46 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-30 20:50:46 (GMT)
commitc37f07d1416b7e371db7b32799df53df39210aa6 (patch)
tree2637c7f3515cec3524382d79565725cf972d1ac3 /src/helsinki.at/rhimportd/ctrlWebSocket.go
parentb44f626200669a57a2bc012d2203c36f587b425b (diff)
improved response handling at websocket interface
Diffstat (limited to 'src/helsinki.at/rhimportd/ctrlWebSocket.go')
-rw-r--r--src/helsinki.at/rhimportd/ctrlWebSocket.go97
1 files changed, 72 insertions, 25 deletions
diff --git a/src/helsinki.at/rhimportd/ctrlWebSocket.go b/src/helsinki.at/rhimportd/ctrlWebSocket.go
index ea7bf24..ca1092c 100644
--- a/src/helsinki.at/rhimportd/ctrlWebSocket.go
+++ b/src/helsinki.at/rhimportd/ctrlWebSocket.go
@@ -75,27 +75,74 @@ func newWebSocketRequestData(conf *rhimport.Config) *webSocketRequestData {
return rd
}
-type webSocketResponseData struct {
- ResponseCode int `json:"RESPONSE_CODE"`
- Type string `json:"TYPE"`
- ErrorString string `json:"ERROR_STRING"`
- Id string `json:"ID"`
- RefId string `json:"REFERENCE_ID"`
- ProgressStep int `json:"PROGRESS_STEP"`
- ProgressStepName string `json:"PROGRESS_STEP_NAME"`
- Progress float64 `json:"PROGRESS"`
- Cart uint `json:"CART_NUMBER"`
- Cut uint `json:"CUT_NUMBER"`
+type webSocketResponseBaseData struct {
+ ResponseCode int `json:"RESPONSE_CODE"`
+ Type string `json:"TYPE"`
+ ErrorString string `json:"ERROR_STRING"`
+ Id string `json:"ID"`
+ RefId string `json:"REFERENCE_ID"`
}
-func sendWebSocketResponse(ws *websocket.Conn, rd *webSocketResponseData) {
- if err := ws.WriteJSON(*rd); err != nil {
+type webSocketResponseProgressData struct {
+ webSocketResponseBaseData
+ Step int `json:"PROGRESS_STEP"`
+ StepName string `json:"PROGRESS_STEP_NAME"`
+ Progress float64 `json:"PROGRESS"`
+}
+
+type webSocketResponseDoneData struct {
+ webSocketResponseBaseData
+ Cart uint `json:"CART_NUMBER"`
+ Cut uint `json:"CUT_NUMBER"`
+}
+
+func sendWebSocketResponse(ws *websocket.Conn, rd interface{}) {
+ if err := ws.WriteJSON(rd); err != nil {
rhdl.Println("WebScoket Client", ws.RemoteAddr(), "write error:", err)
}
}
-func sendWebSocketErrorResponse(ws *websocket.Conn, id string, code int, err_str string) {
- sendWebSocketResponse(ws, &webSocketResponseData{ResponseCode: code, Type: "ERROR", ErrorString: err_str, Id: id})
+func sendWebSocketAckResponse(ws *websocket.Conn, code int, err_str, id, refid string) {
+ rd := &webSocketResponseBaseData{}
+ rd.ResponseCode = code
+ rd.Type = "ACK"
+ rd.ErrorString = err_str
+ rd.Id = id
+ rd.RefId = refid
+ sendWebSocketResponse(ws, rd)
+}
+
+func sendWebSocketErrorResponse(ws *websocket.Conn, code int, err_str string) {
+ rd := &webSocketResponseBaseData{}
+ rd.ResponseCode = code
+ rd.Type = "ERROR"
+ rd.ErrorString = err_str
+ sendWebSocketResponse(ws, rd)
+}
+
+func sendWebSocketProgressResponse(ws *websocket.Conn, id, refid string, step int, step_name string, progress float64) {
+ rd := &webSocketResponseProgressData{}
+ rd.ResponseCode = http.StatusOK
+ rd.Type = "PROGRESS"
+ rd.ErrorString = "OK"
+ rd.Id = id
+ rd.RefId = refid
+ rd.Step = step
+ rd.StepName = step_name
+ rd.Progress = progress
+ sendWebSocketResponse(ws, rd)
+}
+
+func sendWebSocketDoneResponse(ws *websocket.Conn, code int, err_str, id, refid string, cart, cut uint) {
+ rd := &webSocketResponseDoneData{}
+ rd.ResponseCode = code
+ rd.Type = "DONE"
+ rd.ErrorString = err_str
+ rd.Id = id
+ rd.RefId = refid
+ rd.Cart = cart
+ rd.Cut = cut
+ sendWebSocketResponse(ws, rd)
}
type webSocketSession struct {
@@ -195,39 +242,39 @@ func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.
switch reqdata.Command {
case "new":
if session.id != "" {
- sendWebSocketErrorResponse(ws, "", http.StatusBadRequest, "This connection already handles a session")
+ sendWebSocketErrorResponse(ws, http.StatusBadRequest, "This connection already handles a session")
} else {
code, errstring := session.startNewSession(&reqdata, conf, rddb, sessions)
if code != http.StatusOK {
- sendWebSocketErrorResponse(ws, "", code, errstring)
+ sendWebSocketErrorResponse(ws, code, errstring)
} else {
- sendWebSocketResponse(ws, &webSocketResponseData{ResponseCode: code, Type: "ACK", Id: session.id, RefId: session.refId})
+ sendWebSocketAckResponse(ws, code, "OK", session.id, session.refId)
}
}
case "cancel":
if session.id == "" {
- sendWebSocketErrorResponse(ws, "", http.StatusBadRequest, "This connection doesn't handle any session")
+ sendWebSocketErrorResponse(ws, http.StatusBadRequest, "This connection doesn't handle any session")
} else {
session.session.Cancel()
}
case "reconnect":
if session.id != "" {
- sendWebSocketErrorResponse(ws, "", http.StatusBadRequest, "This connection already handles a session")
+ sendWebSocketErrorResponse(ws, http.StatusBadRequest, "This connection already handles a session")
} else {
code, errstring := session.reconnectSession(&reqdata, sessions)
if code != http.StatusOK {
- sendWebSocketErrorResponse(ws, "", code, errstring)
+ sendWebSocketErrorResponse(ws, code, errstring)
} else {
- sendWebSocketResponse(ws, &webSocketResponseData{ResponseCode: code, Type: "ACK", Id: session.id, RefId: session.refId})
+ sendWebSocketAckResponse(ws, code, "OK", session.id, session.refId)
}
}
default:
- sendWebSocketErrorResponse(ws, "", http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command))
+ sendWebSocketErrorResponse(ws, http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command))
}
case p := <-session.progresschan:
- sendWebSocketResponse(ws, &webSocketResponseData{http.StatusOK, "PROGRESS", "", session.id, session.refId, p.Step, p.StepName, p.Progress * 100, 0, 0})
+ sendWebSocketProgressResponse(ws, session.id, session.refId, p.Step, p.StepName, p.Progress*100)
case d := <-session.donechan:
- sendWebSocketResponse(ws, &webSocketResponseData{d.ResponseCode, "DONE", d.ErrorString, session.id, session.refId, 0, "", 100.0, d.Cart, d.Cut})
+ sendWebSocketDoneResponse(ws, d.ResponseCode, d.ErrorString, session.id, session.refId, d.Cart, d.Cut)
// TODO: send close message at this point?
}
}