| } | } | ||||
| type workDone struct { | type workDone struct { | ||||
| openID string //which user | |||||
| consumed int //job done | |||||
| openID string //which user | |||||
| consumed int //job done | |||||
| data openIDSessionData //modified, updated sessiondata | |||||
| } | } | ||||
| //SessionManager manage all sessions | //SessionManager manage all sessions | ||||
| func (m *SessionManager) clearJobDone(d workDone) { | func (m *SessionManager) clearJobDone(d workDone) { | ||||
| s, found := m.sessions[d.openID] | s, found := m.sessions[d.openID] | ||||
| log.Println("SessionMgr/clearJobDone: start clearning jobs ... ") | log.Println("SessionMgr/clearJobDone: start clearning jobs ... ") | ||||
| log.Println(s) | |||||
| log.Println(d) | |||||
| if found { | if found { | ||||
| s.count -= d.consumed | s.count -= d.consumed | ||||
| s.data = d.data //updated session data | |||||
| m.sessions[d.openID] = s //update | m.sessions[d.openID] = s //update | ||||
| if s.count == 0 { //no job to do | if s.count == 0 { //no job to do | ||||
| //remove from memory | //remove from memory | ||||
| //worker thread, cannot change session info | //worker thread, cannot change session info | ||||
| func (m *SessionManager) startJob(openID string) { | func (m *SessionManager) startJob(openID string) { | ||||
| log.Println("SessionMgr/startJob: enter...") | log.Println("SessionMgr/startJob: enter...") | ||||
| jobFinished := workDone{openID, 0} | |||||
| s := m.sessions[openID] | |||||
| jobFinished := workDone{openID, 0, openIDSessionData{}} | |||||
| //process all jobs in the channel | //process all jobs in the channel | ||||
| hasJob := true | hasJob := true | ||||
| for hasJob { | for hasJob { | ||||
| select { | select { | ||||
| case v := <-m.sessions[openID].jobs: | case v := <-m.sessions[openID].jobs: | ||||
| m.checkOpenID(openID, v) | m.checkOpenID(openID, v) | ||||
| s := m.sessions[openID] | |||||
| s.data.incomingMsg(v) //<=== main logic for processing each incoming message | s.data.incomingMsg(v) //<=== main logic for processing each incoming message | ||||
| log.Printf("after incoming message %s", s.data.Procedure) | |||||
| m.sessions[openID] = s | |||||
| log.Printf("procedure after incoming message %s", s.data.Procedure) | |||||
| jobFinished.consumed++ | jobFinished.consumed++ | ||||
| default: | default: | ||||
| hasJob = false | hasJob = false | ||||
| } | } | ||||
| } | } | ||||
| m.done <- jobFinished //notify parent that we have done | |||||
| jobFinished.data = s.data //update session data | |||||
| m.done <- jobFinished //notify parent that we have done | |||||
| return | return | ||||
| } | } | ||||