- jxcontext.Context added

This commit is contained in:
gazebo
2018-10-21 19:09:11 +08:00
parent c818c45a4d
commit 5d2bf9a510

View File

@@ -0,0 +1,78 @@
package jxcontext
import (
"net/http"
"sync"
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
)
type Context struct {
rootTask tasksch.ITask
token string
w http.ResponseWriter
r *http.Request
mapData map[interface{}]interface{}
locker sync.RWMutex
}
func New(rootTask tasksch.ITask, token string, w http.ResponseWriter, r *http.Request) (ctx *Context, errCode string, err error) {
return &Context{
rootTask: rootTask,
token: token,
w: w,
r: r,
mapData: make(map[interface{}]interface{}),
}, "", nil
}
func (ctx *Context) GetUserName() string {
return ctx.token
}
func (ctx *Context) GetUserID() string {
return ctx.token
}
func (ctx *Context) GetRootTask() tasksch.ITask {
ctx.locker.RLock()
defer ctx.locker.RUnlock()
return ctx.rootTask
}
func (ctx *Context) TaskCreated(task tasksch.ITask) bool {
ctx.locker.Lock()
if ctx.rootTask == nil {
ctx.rootTask = task
ctx.locker.Unlock()
return true
}
ctx.locker.Unlock()
ctx.rootTask.AddChild(task)
return false
}
func (ctx *Context) GetResponseWriter() http.ResponseWriter {
return ctx.w
}
func (ctx *Context) GetRequest() *http.Request {
return ctx.r
}
func (ctx *Context) SetValue(key, value interface{}) {
ctx.locker.Lock()
defer ctx.locker.Unlock()
ctx.mapData[key] = value
}
func (ctx *Context) GetValue(key interface{}) interface{} {
ctx.locker.RLock()
defer ctx.locker.RUnlock()
return ctx.mapData[key]
}