From a37f02747e587b25d45de98d175427d4ccb5cdf7 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Wed, 30 Dec 2015 22:11:56 +0100
Subject: session store list works now (needs auth check)


diff --git a/src/helsinki.at/rhimportd/ctrlWebSocket.go b/src/helsinki.at/rhimportd/ctrlWebSocket.go
index ca1092c..da2978e 100644
--- a/src/helsinki.at/rhimportd/ctrlWebSocket.go
+++ b/src/helsinki.at/rhimportd/ctrlWebSocket.go
@@ -83,6 +83,11 @@ type webSocketResponseBaseData struct {
 	RefId        string `json:"REFERENCE_ID"`
 }
 
+type webSocketResponseListData struct {
+	webSocketResponseBaseData
+	Sessions map[string]string `json:"SESSIONS"`
+}
+
 type webSocketResponseProgressData struct {
 	webSocketResponseBaseData
 	Step     int     `json:"PROGRESS_STEP"`
@@ -102,21 +107,30 @@ func sendWebSocketResponse(ws *websocket.Conn, rd interface{}) {
 	}
 }
 
-func sendWebSocketAckResponse(ws *websocket.Conn, code int, err_str, id, refid string) {
+func sendWebSocketErrorResponse(ws *websocket.Conn, code int, err_str string) {
 	rd := &webSocketResponseBaseData{}
 	rd.ResponseCode = code
-	rd.Type = "ACK"
+	rd.Type = "ERROR"
 	rd.ErrorString = err_str
-	rd.Id = id
-	rd.RefId = refid
 	sendWebSocketResponse(ws, rd)
 }
 
-func sendWebSocketErrorResponse(ws *websocket.Conn, code int, err_str string) {
+func sendWebSocketAckResponse(ws *websocket.Conn, code int, id, refid string) {
 	rd := &webSocketResponseBaseData{}
 	rd.ResponseCode = code
-	rd.Type = "ERROR"
-	rd.ErrorString = err_str
+	rd.Type = "ACK"
+	rd.ErrorString = "OK"
+	rd.Id = id
+	rd.RefId = refid
+	sendWebSocketResponse(ws, rd)
+}
+
+func sendWebSocketListResponse(ws *websocket.Conn, sessions map[string]string) {
+	rd := &webSocketResponseListData{}
+	rd.ResponseCode = http.StatusOK
+	rd.Type = "LIST"
+	rd.ErrorString = "OK"
+	rd.Sessions = sessions
 	sendWebSocketResponse(ws, rd)
 }
 
@@ -248,7 +262,7 @@ func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.
 					if code != http.StatusOK {
 						sendWebSocketErrorResponse(ws, code, errstring)
 					} else {
-						sendWebSocketAckResponse(ws, code, "OK", session.id, session.refId)
+						sendWebSocketAckResponse(ws, code, session.id, session.refId)
 					}
 				}
 			case "cancel":
@@ -265,9 +279,16 @@ func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.
 					if code != http.StatusOK {
 						sendWebSocketErrorResponse(ws, code, errstring)
 					} else {
-						sendWebSocketAckResponse(ws, code, "OK", session.id, session.refId)
+						sendWebSocketAckResponse(ws, code, session.id, session.refId)
 					}
 				}
+			case "list":
+				list, code, errstring := sessions.List(reqdata.UserName, reqdata.Password, true) // TODO: set this to false as soon as the interface is working
+				if code != http.StatusOK {
+					sendWebSocketErrorResponse(ws, code, errstring)
+				} else {
+					sendWebSocketListResponse(ws, list)
+				}
 			default:
 				sendWebSocketErrorResponse(ws, http.StatusBadRequest, fmt.Sprintf("unknown command '%s'", reqdata.Command))
 			}
diff --git a/test/socket.html b/test/socket.html
index 7c3a149..ab631ac 100644
--- a/test/socket.html
+++ b/test/socket.html
@@ -23,7 +23,7 @@
         $('#rawmsg').text("");
         this.sock = new WebSocket("ws://localhost:4080/public/socket");
         this.sock.onmessage = function (event) {
-          $('#rawmsg').append(event.data);
+          $('#rawmsg').append(event.data + "\n");
           msg = $.parseJSON(event.data)
           switch (msg.TYPE) {
             case "ACK":
@@ -31,19 +31,26 @@
                $('#buttonrun').attr('disabled','disabled')
                $('#buttonreconnect').attr('disabled','disabled')
                $('#buttoncancel').removeAttr('disabled')
+               $('#buttonlist').removeAttr('disabled')
                break;
             case "DONE":
                init();
                break;
           }
         }
+
         this.sock_onopen = function() {
           this.sock.send(JSON.stringify(this.req));
         }
         this.sock.onopen = this.sock_onopen.bind(this);
+
         this.cancel = function() {
           this.sock.send(JSON.stringify({COMMAND: "cancel"}));
         }
+
+        this.list = function() {
+          this.sock.send(JSON.stringify({COMMAND: "list", LOGIN_NAME: "heslinki", PASSWORD: "12423"}));
+        }
       }
 
       var s;
@@ -71,11 +78,16 @@
         s.cancel();
       }
 
+      function list() {
+        s.list();
+      }
+
       function init() {
         $('#sessionid').val("");
         $('#buttonrun').removeAttr('disabled')
         $('#buttonreconnect').removeAttr('disabled')
         $('#buttoncancel').attr('disabled','disabled')
+        $('#buttonlist').attr('disabled','disabled')
       }
 </script>
   </head>
@@ -84,9 +96,10 @@
     <h1>Radio Helsinki Rivendell Importer:</h1>
 
     <button id="buttonrun" onclick="run()">start</button>
-    <button id="buttoncancel" onclick="cancel()">cancel</button>
     <input id="sessionid" type="text" size="45" label="ID">
     <button id="buttonreconnect" onclick="reconnect()">reconnect</button>
+    <button id="buttoncancel" onclick="cancel()">cancel</button>
+    <button id="buttonlist" onclick="list()">list</button>
     <div id="rawmsg" class="data"></div>
   </body>
 </html>
-- 
cgit v0.10.2