diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-30 19:58:28 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-30 19:58:28 (GMT) |
commit | 6ff5e0a94f38b4501ffdc7ff51ff6766d03c5a65 (patch) | |
tree | 0b4dcc144e237868b2d3d86c53e3fa1c5e3e3afc /session_store.go | |
parent | f46281a93faf72331178a93ebf67cfd23fa81395 (diff) |
session store and web socket interface now support to set a reference id
Diffstat (limited to 'session_store.go')
-rw-r--r-- | session_store.go | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/session_store.go b/session_store.go index 5025d8c..7fe6585 100644 --- a/session_store.go +++ b/session_store.go @@ -40,11 +40,13 @@ type newSessionResponse struct { type newSessionRequest struct { ctx *ImportContext + refId string response chan newSessionResponse } type getSessionResponse struct { session *SessionChan + refId string responsecode int errorstring string } @@ -52,6 +54,7 @@ type getSessionResponse struct { type getSessionRequest struct { user string id string + refId string response chan getSessionResponse } @@ -66,8 +69,13 @@ type removeSessionRequest struct { response chan removeSessionResponse } +type SessionStoreElement struct { + s *Session + refId string +} + type SessionStore struct { - store map[string]map[string]*Session + store map[string]map[string]*SessionStoreElement quit chan bool done chan bool newChan chan newSessionRequest @@ -83,7 +91,7 @@ func generateSessionId() (string, error) { return base64.RawStdEncoding.EncodeToString(b[:]), nil } -func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { +func (self *SessionStore) new(ctx *ImportContext, refId string) (resp newSessionResponse) { resp.responsecode = http.StatusOK resp.errorstring = "OK" if !ctx.Trusted { @@ -103,10 +111,11 @@ func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { } else { resp.id = id if _, exists := self.store[ctx.UserName]; !exists { - self.store[ctx.UserName] = make(map[string]*Session) + self.store[ctx.UserName] = make(map[string]*SessionStoreElement) } - self.store[ctx.UserName][resp.id] = NewSession(ctx, func() { self.GetInterface().Remove(ctx.UserName, resp.id) }) - resp.session = self.store[ctx.UserName][resp.id].getInterface() + 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() rhdl.Printf("SessionStore: created session for '%s' -> %s", ctx.UserName, resp.id) } return @@ -116,7 +125,8 @@ func (self *SessionStore) get(user, id string) (resp getSessionResponse) { resp.responsecode = http.StatusOK resp.errorstring = "OK" if session, exists := self.store[user][id]; exists { - resp.session = session.getInterface() + resp.session = session.s.getInterface() + resp.refId = session.refId } else { resp.responsecode = http.StatusNotFound resp.errorstring = fmt.Sprintf("SessionStore: session '%s/%s' not found", user, id) @@ -128,7 +138,7 @@ func (self *SessionStore) remove(user, id string) (resp removeSessionResponse) { resp.responsecode = http.StatusOK resp.errorstring = "OK" if session, exists := self.store[user][id]; exists { - go session.Cleanup() // cleanup could take a while -> don't block all the other stuff + go session.s.Cleanup() // cleanup could take a while -> don't block all the other stuff delete(self.store[user], id) rhdl.Printf("SessionStore: removed session '%s/%s'", user, id) if userstore, exists := self.store[user]; exists { @@ -151,7 +161,7 @@ func (self *SessionStore) dispatchRequests() { case <-self.quit: return case req := <-self.newChan: - req.response <- self.new(req.ctx) + req.response <- self.new(req.ctx, req.refId) case req := <-self.getChan: req.response <- self.get(req.user, req.id) case req := <-self.removeChan: @@ -169,10 +179,11 @@ type SessionStoreChan struct { removeChan chan<- removeSessionRequest } -func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, int, string) { +func (self *SessionStoreChan) New(ctx *ImportContext, refId string) (string, *SessionChan, int, string) { res_ch := make(chan newSessionResponse) req := newSessionRequest{} req.ctx = ctx + req.refId = refId req.response = res_ch self.newChan <- req @@ -180,7 +191,7 @@ func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, int return res.id, res.session, res.responsecode, res.errorstring } -func (self *SessionStoreChan) Get(user, id string) (*SessionChan, int, string) { +func (self *SessionStoreChan) Get(user, id string) (*SessionChan, string, int, string) { res_ch := make(chan getSessionResponse) req := getSessionRequest{} req.user = user @@ -189,7 +200,7 @@ func (self *SessionStoreChan) Get(user, id string) (*SessionChan, int, string) { self.getChan <- req res := <-res_ch - return res.session, res.responsecode, res.errorstring + return res.session, res.refId, res.responsecode, res.errorstring } func (self *SessionStoreChan) Remove(user, id string) (int, string) { @@ -227,7 +238,7 @@ func NewSessionStore(conf *Config) (store *SessionStore, err error) { store.quit = make(chan bool) store.done = make(chan bool) - store.store = make(map[string]map[string]*Session) + store.store = make(map[string]map[string]*SessionStoreElement) store.newChan = make(chan newSessionRequest, 10) store.getChan = make(chan getSessionRequest, 10) store.removeChan = make(chan removeSessionRequest, 10) |