2014-09-15 10:24:40 +04:00
|
|
|
// 事件处理.
|
|
|
|
package event
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
import "github.com/golang/glog"
|
2014-09-15 10:24:40 +04:00
|
|
|
|
|
|
|
const (
|
2014-09-15 19:20:01 +04:00
|
|
|
EvtCodeGOPATHNotFound = iota // 事件代码:找不到环境变量 $GOPATH
|
|
|
|
EvtCodeGOROOTNotFound // 事件代码:找不到环境变量 $GOROOT
|
|
|
|
EvtCodeGocodeNotFound // 事件代码:找不到 gocode
|
|
|
|
EvtCodeIDEStubNotFound // 事件代码:找不到 IDE stub
|
2014-09-15 10:24:40 +04:00
|
|
|
)
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
// 事件队列最大长度.
|
2014-09-15 14:03:52 +04:00
|
|
|
const MaxQueueLength = 10
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
// 事件结构.
|
|
|
|
type Event struct {
|
|
|
|
Code int `json:"code"` // 事件代码
|
|
|
|
Sid string `json:"sid"` // 用户会话 id
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:24:40 +04:00
|
|
|
// 全局事件队列.
|
|
|
|
// 入队的事件将分发到每个用户的通知队列.
|
2014-09-15 14:03:52 +04:00
|
|
|
var EventQueue = make(chan int, MaxQueueLength)
|
2014-09-15 10:24:40 +04:00
|
|
|
|
|
|
|
// 用户事件队列.
|
2014-09-15 19:24:58 +04:00
|
|
|
// <sid, chan>
|
2014-09-15 14:03:52 +04:00
|
|
|
var UserEventQueues = map[string]chan int{}
|
2014-09-15 10:24:40 +04:00
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
// 用户事件处理器集.
|
2014-09-15 19:24:58 +04:00
|
|
|
// <sid, *Handlers>
|
2014-09-15 19:20:01 +04:00
|
|
|
var UserEventHandlers = map[string]*Handlers{}
|
|
|
|
|
2014-09-15 10:24:40 +04:00
|
|
|
// 加载事件处理.
|
|
|
|
func Load() {
|
|
|
|
go func() {
|
2014-09-15 14:03:52 +04:00
|
|
|
for event := range EventQueue {
|
2014-09-15 10:24:40 +04:00
|
|
|
glog.V(5).Info("收到全局事件 [%d]", event)
|
|
|
|
|
|
|
|
// 将事件分发到每个用户的事件队列里
|
2014-09-15 14:03:52 +04:00
|
|
|
for _, userQueue := range UserEventQueues {
|
2014-09-15 10:24:40 +04:00
|
|
|
userQueue <- event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-09-15 14:03:52 +04:00
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
// 初始化一个用户事件队列.
|
|
|
|
func InitUserQueue(sid string, handlers ...Handler) {
|
2014-09-15 14:03:52 +04:00
|
|
|
// FIXME: 会话过期后需要销毁对应的用户事件队列
|
|
|
|
|
|
|
|
q := UserEventQueues[sid]
|
|
|
|
if nil != q {
|
2014-09-15 19:20:01 +04:00
|
|
|
return
|
2014-09-15 14:03:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
q = make(chan int, MaxQueueLength)
|
|
|
|
UserEventQueues[sid] = q
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
if nil == UserEventHandlers[sid] {
|
|
|
|
UserEventHandlers[sid] = new(Handlers)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, handler := range handlers {
|
|
|
|
UserEventHandlers[sid].add(handler)
|
|
|
|
}
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
go func() {
|
2014-09-15 19:20:01 +04:00
|
|
|
for evtCode := range q {
|
|
|
|
glog.V(5).Infof("Session [%s] received a event [%d]", sid, evtCode)
|
|
|
|
|
|
|
|
// 将事件交给事件处理器进行处理
|
|
|
|
for _, handler := range *UserEventHandlers[sid] {
|
|
|
|
e := Event{Code: evtCode, Sid: sid}
|
|
|
|
handler.Handle(&e)
|
|
|
|
}
|
2014-09-15 14:03:52 +04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-09-15 19:20:01 +04:00
|
|
|
|
2014-09-15 19:24:58 +04:00
|
|
|
// 事件处理接口.
|
2014-09-15 19:20:01 +04:00
|
|
|
type Handler interface {
|
|
|
|
Handle(event *Event)
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:24:58 +04:00
|
|
|
// 函数指针包装.
|
2014-09-15 19:20:01 +04:00
|
|
|
type HandleFunc func(event *Event)
|
|
|
|
|
2014-09-15 19:24:58 +04:00
|
|
|
// 事件处理默认实现.
|
2014-09-15 19:20:01 +04:00
|
|
|
func (fn HandleFunc) Handle(event *Event) {
|
|
|
|
fn(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Handlers []Handler
|
|
|
|
|
|
|
|
func (handlers *Handlers) add(handler Handler) {
|
|
|
|
*handlers = append(*handlers, handler)
|
|
|
|
}
|