summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2015-12-30 21:42:59 (GMT)
committerChristian Pointner <equinox@helsinki.at>2015-12-30 21:42:59 (GMT)
commitb48b8d760021bd7a887a24c4bbd239f90c2c2f22 (patch)
tree27c20e906a1dd83734dbdb854fb8863aa0f622c7
parenta37f02747e587b25d45de98d175427d4ccb5cdf7 (diff)
web socket handler is now completed
-rw-r--r--src/helsinki.at/rhimport/session_store.go34
-rw-r--r--src/helsinki.at/rhimportd/ctrlWebSocket.go14
-rw-r--r--src/helsinki.at/rhimportd/main.go98
-rw-r--r--test/socket.html45
4 files changed, 60 insertions, 131 deletions
diff --git a/src/helsinki.at/rhimport/session_store.go b/src/helsinki.at/rhimport/session_store.go
index 0d149c6..9566293 100644
--- a/src/helsinki.at/rhimport/session_store.go
+++ b/src/helsinki.at/rhimport/session_store.go
@@ -89,6 +89,8 @@ type SessionStoreElement struct {
type SessionStore struct {
store map[string]map[string]*SessionStoreElement
+ conf *Config
+ rddb *RdDbChan
quit chan bool
done chan bool
newChan chan newSessionRequest
@@ -109,7 +111,7 @@ func (self *SessionStore) new(ctx *ImportContext, refId string) (resp newSession
resp.responsecode = http.StatusOK
resp.errorstring = "OK"
if !ctx.Trusted {
- if ok, err := ctx.rddb.CheckPassword(ctx.UserName, ctx.Password); err != nil {
+ if ok, err := self.rddb.CheckPassword(ctx.UserName, ctx.Password); err != nil {
resp.responsecode = http.StatusInternalServerError
resp.errorstring = err.Error()
return
@@ -127,6 +129,8 @@ func (self *SessionStore) new(ctx *ImportContext, refId string) (resp newSession
if _, exists := self.store[ctx.UserName]; !exists {
self.store[ctx.UserName] = make(map[string]*SessionStoreElement)
}
+ ctx.conf = self.conf
+ ctx.rddb = self.rddb
s := &SessionStoreElement{NewSession(ctx, func() { self.GetInterface().Remove(ctx.UserName, resp.id) }), refId}
self.store[ctx.UserName][resp.id] = s
resp.session = self.store[ctx.UserName][resp.id].s.getInterface()
@@ -151,18 +155,17 @@ func (self *SessionStore) get(user, id string) (resp getSessionResponse) {
func (self *SessionStore) list(user, password string, trusted bool) (resp listSessionsResponse) {
resp.responsecode = http.StatusOK
resp.errorstring = "OK"
- // TODO: enable this check as soon as the session store handles the rddb itself
- // if !trusted {
- // if ok, err := self.rddb.CheckPassword(user, password); err != nil {
- // resp.responsecode = http.StatusInternalServerError
- // resp.errorstring = err.Error()
- // return
- // } else if !ok {
- // resp.responsecode = http.StatusUnauthorized
- // resp.errorstring = "invalid username and/or password"
- // return
- // }
- // }
+ if !trusted {
+ if ok, err := self.rddb.CheckPassword(user, password); err != nil {
+ resp.responsecode = http.StatusInternalServerError
+ resp.errorstring = err.Error()
+ return
+ } else if !ok {
+ resp.responsecode = http.StatusUnauthorized
+ resp.errorstring = "invalid username and/or password"
+ return
+ }
+ }
resp.sessions = make(map[string]string)
if sessions, exists := self.store[user]; exists {
for id, e := range sessions {
@@ -289,9 +292,10 @@ func (self *SessionStore) Cleanup() {
close(self.removeChan)
}
-func NewSessionStore(conf *Config) (store *SessionStore, err error) {
+func NewSessionStore(conf *Config, rddb *RdDbChan) (store *SessionStore, err error) {
store = new(SessionStore)
-
+ store.conf = conf
+ store.rddb = rddb
store.quit = make(chan bool)
store.done = make(chan bool)
store.store = make(map[string]map[string]*SessionStoreElement)
diff --git a/src/helsinki.at/rhimportd/ctrlWebSocket.go b/src/helsinki.at/rhimportd/ctrlWebSocket.go
index da2978e..4c1bf45 100644
--- a/src/helsinki.at/rhimportd/ctrlWebSocket.go
+++ b/src/helsinki.at/rhimportd/ctrlWebSocket.go
@@ -192,10 +192,10 @@ func webSocketDone(res rhimport.ImportResult, userdata interface{}) bool {
return true
}
-func (self *webSocketSession) startNewSession(reqdata *webSocketRequestData, conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *rhimport.SessionStoreChan) (int, string) {
- ctx := rhimport.NewImportContext(conf, rddb, reqdata.UserName)
+func (self *webSocketSession) startNewSession(reqdata *webSocketRequestData, conf *rhimport.Config, sessions *rhimport.SessionStoreChan) (int, string) {
+ ctx := rhimport.NewImportContext(conf, nil, reqdata.UserName)
ctx.Password = reqdata.Password
- ctx.Trusted = true // TODO: set this to false as soon as the interface is working
+ ctx.Trusted = false
ctx.ShowId = reqdata.ShowId
ctx.ClearShowCarts = reqdata.ClearShowCarts
ctx.GroupName = reqdata.MusicPoolGroup
@@ -243,7 +243,7 @@ func (self *webSocketSession) reconnectSession(reqdata *webSocketRequestData, se
return http.StatusOK, "SUCCESS"
}
-func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.Conn, conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *rhimport.SessionStoreChan) {
+func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.Conn, conf *rhimport.Config, sessions *rhimport.SessionStoreChan) {
defer ws.Close()
session := newWebSocketSession()
@@ -258,7 +258,7 @@ func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.
if session.id != "" {
sendWebSocketErrorResponse(ws, http.StatusBadRequest, "This connection already handles a session")
} else {
- code, errstring := session.startNewSession(&reqdata, conf, rddb, sessions)
+ code, errstring := session.startNewSession(&reqdata, conf, sessions)
if code != http.StatusOK {
sendWebSocketErrorResponse(ws, code, errstring)
} else {
@@ -283,7 +283,7 @@ func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.
}
}
case "list":
- list, code, errstring := sessions.List(reqdata.UserName, reqdata.Password, true) // TODO: set this to false as soon as the interface is working
+ list, code, errstring := sessions.List(reqdata.UserName, reqdata.Password, false)
if code != http.StatusOK {
sendWebSocketErrorResponse(ws, code, errstring)
} else {
@@ -314,7 +314,7 @@ func webSocketHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, sessions *
}
rhdl.Println("WebSocket Client", ws.RemoteAddr(), "connected")
reqchan := make(chan webSocketRequestData)
- go webSocketSessionHandler(reqchan, ws, conf, rddb, sessions)
+ go webSocketSessionHandler(reqchan, ws, conf, sessions)
defer close(reqchan)
for {
diff --git a/src/helsinki.at/rhimportd/main.go b/src/helsinki.at/rhimportd/main.go
index 87fa3ab..21c1246 100644
--- a/src/helsinki.at/rhimportd/main.go
+++ b/src/helsinki.at/rhimportd/main.go
@@ -26,14 +26,11 @@ package main
import (
"flag"
- // "fmt"
"helsinki.at/rhimport"
"log"
- // "net/http"
"os"
"os/signal"
"sync"
- // "time"
// "io/ioutil"
)
@@ -43,99 +40,6 @@ var (
//rhdl = log.New(ioutil.Discard, "[rhimportd-dbg]\t", log.LstdFlags)
)
-// func session_test_progress1(step int, step_name string, progress float64, userdata interface{}) bool {
-// out := userdata.(chan<- string)
-// select {
-// case out <- fmt.Sprintf("CB1 %d, %s: %3.2f%%", step, step_name, progress*100):
-// default:
-// }
-// if step > 1 {
-// return false
-// }
-// return true
-// }
-
-// func session_test_progress2(step int, step_name string, progress float64, userdata interface{}) bool {
-// out := userdata.(chan<- string)
-// select {
-// case out <- fmt.Sprintf("CB2 %d, %s: %3.2f%%", step, step_name, progress*100):
-// default:
-// }
-// return true
-// }
-
-// func session_test_done(res rhimport.ImportResult, userdata interface{}) bool {
-// done := userdata.(chan<- rhimport.ImportResult)
-// done <- res
-// return true
-// }
-
-// func session_test(conf *rhimport.Config, rddb *rhimport.RdDbChan) {
-// sessions, err := rhimport.NewSessionStore(conf)
-// if err != nil {
-// rhl.Println("Error initializing Session Store:", err)
-// return
-// }
-// defer sessions.Cleanup()
-
-// store := sessions.GetInterface()
-
-// ctx := rhimport.NewImportContext(conf, rddb, "heslinki")
-// ctx.Trusted = true
-// ctx.ShowId = 10002
-// ctx.ClearShowCarts = true
-// ctx.SourceUri = "http://www.tonycuffe.com/mp3/tail%20toddle.mp3"
-
-// id, _, code, errstring := store.New(ctx)
-// if code != http.StatusOK {
-// rhl.Printf("MAIN: Error SessionStore.New(): %s", errstring)
-// return
-// }
-
-// var session *rhimport.SessionChan
-// if session, code, errstring = store.Get(ctx.UserName, id); code != http.StatusOK {
-// rhl.Printf("MAIN: Error SessionStore.Get(): %s", errstring)
-// return
-// }
-
-// dch := make(chan rhimport.ImportResult, 1)
-// if err = session.AddDoneHandler((chan<- rhimport.ImportResult)(dch), session_test_done); err != nil {
-// rhl.Printf("MAIN: Error Session.AddDoneHandler(): %s", err)
-// return
-// }
-
-// pch := make(chan string, 10)
-// if err = session.AddProgressHandler((chan<- string)(pch), session_test_progress1); err != nil {
-// rhl.Printf("MAIN: Error Session.AddProgressHandler(): %s", err)
-// }
-// go func() {
-// time.Sleep(3 * time.Second)
-// if err = session.AddProgressHandler((chan<- string)(pch), session_test_progress2); err != nil {
-// rhl.Printf("MAIN: Error Session.AddProgressHandler(): %s", err)
-// }
-// }()
-
-// rhl.Printf("MAIN: calling run for %s/%s", ctx.UserName, id)
-// session.Run(30 * time.Second)
-
-// for {
-// select {
-// case p := <-pch:
-// fmt.Println(p)
-// case r := <-dch:
-// fmt.Printf("Import finished: %+v\n", r)
-// break
-// }
-// }
-
-// rhl.Printf("MAIN: calling remove for %s/%s", ctx.UserName, id)
-// if code, errstring = store.Remove(ctx.UserName, id); code != http.StatusOK {
-// rhl.Printf("MAIN: Error SessionStore.Remove(): %s", errstring)
-// }
-
-// rhl.Printf("MAIN: remove done")
-// }
-
func main() {
web_addr_s := flag.String("web-addr", ":4080", "addr:port to listen on")
telnet_addr_s := flag.String("telnet-addr", ":4023", "addr:port to listen on")
@@ -164,7 +68,7 @@ func main() {
}
defer rddb.Cleanup()
- sessions, err := rhimport.NewSessionStore(conf)
+ sessions, err := rhimport.NewSessionStore(conf, rddb.GetInterface())
if err != nil {
rhl.Println("Error initializing Session Store:", err)
return
diff --git a/test/socket.html b/test/socket.html
index ab631ac..a7504f8 100644
--- a/test/socket.html
+++ b/test/socket.html
@@ -14,15 +14,41 @@
padding: 1em;
font-family: monospace;
margin-top: 1em;
+ margin-bottom: 1em;
}
</style>
<script src="jquery.min.js"></script>
<script type="text/javascript">
+ function SessionList(req) {
+ this.req = req
+ this.sock = new WebSocket("ws://localhost:4080/public/socket");
+ this.sock_onmessage = function (event) {
+ $('#listmsg').text(event.data);
+ this.sock.close();
+ }
+ this.sock.onmessage = this.sock_onmessage.bind(this);
+
+ this.sock_onopen = function() {
+ this.sock.send(JSON.stringify(this.req));
+ }
+ this.sock.onopen = this.sock_onopen.bind(this);
+ }
+
+ var sl;
+
+ function list() {
+ req = { COMMAND: "list",
+ LOGIN_NAME: "heslinki",
+ PASSWORD: "12423" };
+ sl = new SessionList(req);
+ }
+
+
function Session(req) {
this.req = req
$('#rawmsg').text("");
this.sock = new WebSocket("ws://localhost:4080/public/socket");
- this.sock.onmessage = function (event) {
+ this.sock_onmessage = function (event) {
$('#rawmsg').append(event.data + "\n");
msg = $.parseJSON(event.data)
switch (msg.TYPE) {
@@ -31,13 +57,15 @@
$('#buttonrun').attr('disabled','disabled')
$('#buttonreconnect').attr('disabled','disabled')
$('#buttoncancel').removeAttr('disabled')
- $('#buttonlist').removeAttr('disabled')
break;
case "DONE":
+ case "ERROR":
+ this.sock.close();
init();
break;
}
}
+ this.sock.onmessage = this.sock_onmessage.bind(this);
this.sock_onopen = function() {
this.sock.send(JSON.stringify(this.req));
@@ -47,10 +75,6 @@
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;
@@ -78,16 +102,11 @@
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>
@@ -95,11 +114,13 @@
<h1>Radio Helsinki Rivendell Importer:</h1>
+ <button id="buttonlist" onclick="list()">list</button>
+ <div id="listmsg" class="data"></div>
+
<button id="buttonrun" onclick="run()">start</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>