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-19 15:21:13 +04:00
|
|
|
type UserEventQueue struct {
|
|
|
|
Sid string // 关联的会话 id
|
|
|
|
Queue chan int // 队列
|
|
|
|
Handlers []Handler // 事件处理器集
|
|
|
|
}
|
|
|
|
|
|
|
|
type Queues map[string]*UserEventQueue
|
2014-09-15 10:24:40 +04:00
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
// 用户事件队列集.
|
|
|
|
// <sid, *UserEventQueue>
|
|
|
|
var UserEventQueues = Queues{}
|
2014-09-15 19:20:01 +04:00
|
|
|
|
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-19 15:21:13 +04:00
|
|
|
userQueue.Queue <- event
|
2014-09-15 10:24:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-09-15 14:03:52 +04:00
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
// 为用户队列添加事件处理器.
|
|
|
|
func (uq *UserEventQueue) AddHandler(handlers ...Handler) {
|
|
|
|
for _, handler := range handlers {
|
|
|
|
uq.Handlers = append(uq.Handlers, handler)
|
2014-09-15 14:03:52 +04:00
|
|
|
}
|
2014-09-19 15:21:13 +04:00
|
|
|
}
|
2014-09-15 14:03:52 +04:00
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
// 初始化一个用户事件队列.
|
|
|
|
func (ueqs Queues) New(sid string) *UserEventQueue {
|
|
|
|
q := ueqs[sid]
|
|
|
|
if nil != q {
|
|
|
|
glog.Warningf("Already exist a user queue in session [%s]", sid)
|
2014-09-15 14:03:52 +04:00
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
return q
|
2014-09-15 19:20:01 +04:00
|
|
|
}
|
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
q = &UserEventQueue{
|
|
|
|
Sid: sid,
|
|
|
|
Queue: make(chan int, MaxQueueLength),
|
2014-09-15 19:20:01 +04:00
|
|
|
}
|
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
ueqs[sid] = q
|
|
|
|
|
|
|
|
go func() { // 队列开始监听事件
|
|
|
|
for evtCode := range q.Queue {
|
2014-09-15 19:20:01 +04:00
|
|
|
glog.V(5).Infof("Session [%s] received a event [%d]", sid, evtCode)
|
|
|
|
|
|
|
|
// 将事件交给事件处理器进行处理
|
2014-09-19 15:21:13 +04:00
|
|
|
for _, handler := range q.Handlers {
|
|
|
|
handler.Handle(&Event{Code: evtCode, Sid: sid})
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
}
|
2014-09-15 14:03:52 +04:00
|
|
|
}
|
|
|
|
}()
|
2014-09-19 15:21:13 +04:00
|
|
|
|
|
|
|
return q
|
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)
|
|
|
|
}
|