wide/event/events.go

118 lines
2.7 KiB
Go
Raw Normal View History

2014-10-29 13:15:18 +03:00
// Event manipulations.
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-10-29 13:15:18 +03:00
EvtCodeGOPATHNotFound = iota // event code: not found $GOPATH env variable
EvtCodeGOROOTNotFound // event code: not found $GOROOT env variable
EvtCodeGocodeNotFound // event code: not found gocode
EvtCodeIDEStubNotFound // event code: not found ide_stub
EvtCodeServerInternalError // event code: server internal error
2014-09-15 10:24:40 +04:00
)
2014-10-29 13:15:18 +03:00
// Max length of queue.
2014-09-15 14:03:52 +04:00
const MaxQueueLength = 10
2014-10-29 13:15:18 +03:00
// Event.
2014-09-15 19:20:01 +04:00
type Event struct {
2014-10-29 13:15:18 +03:00
Code int `json:"code"` // event code
Sid string `json:"sid"` // wide session id related
Data interface{} `json:"data"` // event data
2014-09-15 19:20:01 +04:00
}
2014-10-29 13:15:18 +03:00
// Global event queue.
2014-09-25 09:37:59 +04:00
//
2014-10-29 13:15:18 +03:00
// Every event in this queue will be dispatched to each user event queue.
2014-10-28 19:04:46 +03:00
var EventQueue = make(chan *Event, MaxQueueLength)
2014-09-15 10:24:40 +04:00
2014-10-29 13:15:18 +03:00
// User event queue.
2014-09-19 15:21:13 +04:00
type UserEventQueue struct {
2014-10-29 13:15:18 +03:00
Sid string // wide session id related
Queue chan *Event // queue
Handlers []Handler // event handlers
2014-09-19 15:21:13 +04:00
}
type Queues map[string]*UserEventQueue
2014-09-15 10:24:40 +04:00
2014-10-29 13:15:18 +03:00
// User event queues.
2014-09-25 09:37:59 +04:00
//
2014-09-19 15:21:13 +04:00
// <sid, *UserEventQueue>
var UserEventQueues = Queues{}
2014-09-15 19:20:01 +04:00
2014-10-29 13:15:18 +03:00
// Load initializes the event handling.
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-10-29 13:15:18 +03:00
glog.V(5).Infof("Received a global event [code=%d]", event.Code)
2014-09-15 10:24:40 +04:00
2014-10-29 13:15:18 +03:00
// dispatch the event to each user event queue
2014-09-15 14:03:52 +04:00
for _, userQueue := range UserEventQueues {
2014-10-28 19:04:46 +03:00
event.Sid = userQueue.Sid
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-10-29 13:15:18 +03:00
// AddHandler adds the specified handlers to user event queues.
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-10-29 13:15:18 +03:00
// New initializes a user event queue with the specified wide session id.
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,
2014-10-28 19:04:46 +03:00
Queue: make(chan *Event, MaxQueueLength),
2014-09-15 19:20:01 +04:00
}
2014-09-19 15:21:13 +04:00
ueqs[sid] = q
2014-10-29 13:15:18 +03:00
go func() { // start listening
2014-10-28 19:04:46 +03:00
for evt := range q.Queue {
glog.V(5).Infof("Session [%s] received a event [%d]", sid, evt.Code)
2014-09-15 19:20:01 +04:00
2014-10-29 13:15:18 +03:00
// process event by each handlers
2014-09-19 15:21:13 +04:00
for _, handler := range q.Handlers {
2014-10-28 19:04:46 +03:00
handler.Handle(evt)
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-10-29 13:15:18 +03:00
// Close closes a user event queue with the specified wide session id.
2014-09-20 06:39:29 +04:00
func (ueqs Queues) Close(sid string) {
q := ueqs[sid]
if nil == q {
return
}
delete(ueqs, sid)
}
2014-10-29 13:15:18 +03:00
// Type of event handler.
2014-09-15 19:20:01 +04:00
type Handler interface {
Handle(event *Event)
}
2014-10-29 13:15:18 +03:00
// Type of handler function.
2014-09-15 19:20:01 +04:00
type HandleFunc func(event *Event)
2014-10-29 13:15:18 +03:00
// Default implementation of event handling.
2014-09-15 19:20:01 +04:00
func (fn HandleFunc) Handle(event *Event) {
fn(event)
}