This commit is contained in:
Liang Ding 2014-12-07 11:22:05 +08:00
parent e74dcc806d
commit 6e7bfa5040
1 changed files with 27 additions and 22 deletions

View File

@ -1,34 +1,39 @@
// Copyright (c) 2014, B3log // Copyright (c) 2014, B3log
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// //
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Event manipulations. // Package event includes event related manipulations.
package event package event
import "github.com/golang/glog" import "github.com/golang/glog"
const ( const (
EvtCodeGOPATHNotFound = iota // event code: not found $GOPATH env variable // EvtCodeGOPATHNotFound indicates an event: not found $GOPATH env variable
EvtCodeGOROOTNotFound // event code: not found $GOROOT env variable EvtCodeGOPATHNotFound = iota
EvtCodeGocodeNotFound // event code: not found gocode // EvtCodeGOROOTNotFound indicates an event: not found $GOROOT env variable
EvtCodeIDEStubNotFound // event code: not found ide_stub EvtCodeGOROOTNotFound
EvtCodeServerInternalError // event code: server internal error // EvtCodeGocodeNotFound indicates an event: not found gocode
EvtCodeGocodeNotFound
// EvtCodeIDEStubNotFound indicates an event: not found ide_stub
EvtCodeIDEStubNotFound
// EvtCodeServerInternalError indicates an event: server internal error
EvtCodeServerInternalError
) )
// Max length of queue. // Max length of queue.
const MaxQueueLength = 10 const maxQueueLength = 10
// Event. // Event represents an event.
type Event struct { type Event struct {
Code int `json:"code"` // event code Code int `json:"code"` // event code
Sid string `json:"sid"` // wide session id related Sid string `json:"sid"` // wide session id related
@ -38,21 +43,21 @@ type Event struct {
// Global event queue. // Global event queue.
// //
// Every event in this queue will be dispatched to each user event queue. // Every event in this queue will be dispatched to each user event queue.
var EventQueue = make(chan *Event, MaxQueueLength) var EventQueue = make(chan *Event, maxQueueLength)
// User event queue. // UserEventQueue represents a user event queue.
type UserEventQueue struct { type UserEventQueue struct {
Sid string // wide session id related Sid string // wide session id related
Queue chan *Event // queue Queue chan *Event // queue
Handlers []Handler // event handlers Handlers []Handler // event handlers
} }
type Queues map[string]*UserEventQueue type queues map[string]*UserEventQueue
// User event queues. // User event queues.
// //
// <sid, *UserEventQueue> // <sid, *UserEventQueue>
var UserEventQueues = Queues{} var UserEventQueues = queues{}
// Load initializes the event handling. // Load initializes the event handling.
func Load() { func Load() {
@ -78,7 +83,7 @@ func (uq *UserEventQueue) AddHandler(handlers ...Handler) {
} }
// New initializes a user event queue with the specified wide session id. // New initializes a user event queue with the specified wide session id.
func (ueqs Queues) New(sid string) *UserEventQueue { func (ueqs queues) New(sid string) *UserEventQueue {
q := ueqs[sid] q := ueqs[sid]
if nil != q { if nil != q {
glog.Warningf("Already exist a user queue in session [%s]", sid) glog.Warningf("Already exist a user queue in session [%s]", sid)
@ -88,14 +93,14 @@ func (ueqs Queues) New(sid string) *UserEventQueue {
q = &UserEventQueue{ q = &UserEventQueue{
Sid: sid, Sid: sid,
Queue: make(chan *Event, MaxQueueLength), Queue: make(chan *Event, maxQueueLength),
} }
ueqs[sid] = q ueqs[sid] = q
go func() { // start listening go func() { // start listening
for evt := range q.Queue { for evt := range q.Queue {
glog.V(5).Infof("Session [%s] received a event [%d]", sid, evt.Code) glog.V(5).Infof("Session [%s] received an event [%d]", sid, evt.Code)
// process event by each handlers // process event by each handlers
for _, handler := range q.Handlers { for _, handler := range q.Handlers {
@ -108,7 +113,7 @@ func (ueqs Queues) New(sid string) *UserEventQueue {
} }
// Close closes a user event queue with the specified wide session id. // Close closes a user event queue with the specified wide session id.
func (ueqs Queues) Close(sid string) { func (ueqs queues) Close(sid string) {
q := ueqs[sid] q := ueqs[sid]
if nil == q { if nil == q {
return return
@ -117,15 +122,15 @@ func (ueqs Queues) Close(sid string) {
delete(ueqs, sid) delete(ueqs, sid)
} }
// Type of event handler. // Handler represents an event handler.
type Handler interface { type Handler interface {
Handle(event *Event) Handle(event *Event)
} }
// Type of handler function. // Type of handler function.
type HandleFunc func(event *Event) type handleFunc func(event *Event)
// Default implementation of event handling. // Default implementation of event handling.
func (fn HandleFunc) Handle(event *Event) { func (fn handleFunc) Handle(event *Event) {
fn(event) fn(event)
} }