2014-09-15 09:03:44 +04:00
|
|
|
|
// 通知.
|
2014-09-15 10:24:40 +04:00
|
|
|
|
package notification
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2014-09-16 11:06:52 +04:00
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
|
"time"
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
|
"strconv"
|
2014-09-15 14:03:52 +04:00
|
|
|
|
"github.com/b3log/wide/event"
|
2014-09-15 19:20:01 +04:00
|
|
|
|
"github.com/b3log/wide/i18n"
|
2014-09-19 15:21:13 +04:00
|
|
|
|
"github.com/b3log/wide/session"
|
2014-09-16 11:06:52 +04:00
|
|
|
|
"github.com/b3log/wide/util"
|
2014-09-15 09:03:44 +04:00
|
|
|
|
"github.com/golang/glog"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
)
|
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
|
const (
|
|
|
|
|
Error = "ERROR" // 通知.严重程度:ERROR
|
|
|
|
|
Warn = "WARN" // 通知.严重程度:WARN
|
|
|
|
|
Info = "INFO" // 通知.严重程度:INFO
|
|
|
|
|
|
|
|
|
|
Setup = "Setup" // 通知.类型:安装
|
|
|
|
|
)
|
|
|
|
|
|
2014-09-15 09:03:44 +04:00
|
|
|
|
// 通知结构.
|
|
|
|
|
type Notification struct {
|
2014-09-15 19:20:01 +04:00
|
|
|
|
event *event.Event
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Severity string `json:"severity"`
|
|
|
|
|
Message string `json:"message"`
|
2014-09-15 09:03:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-15 10:24:40 +04:00
|
|
|
|
// 通知通道.
|
2014-09-16 13:32:55 +04:00
|
|
|
|
// <sid, *util.WSChannel>
|
2014-09-16 11:06:52 +04:00
|
|
|
|
var notificationWSs = map[string]*util.WSChannel{}
|
2014-09-15 19:20:01 +04:00
|
|
|
|
|
2014-09-16 07:36:21 +04:00
|
|
|
|
// 用户事件处理:将事件转为通知,并通过通知通道推送给前端.
|
|
|
|
|
// 当用户事件队列接收到事件时将会调用该函数进行处理.
|
2014-09-15 19:20:01 +04:00
|
|
|
|
func event2Notification(e *event.Event) {
|
|
|
|
|
if nil == notificationWSs[e.Sid] {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsChannel := notificationWSs[e.Sid]
|
|
|
|
|
|
|
|
|
|
var notification Notification
|
|
|
|
|
|
|
|
|
|
switch e.Code {
|
|
|
|
|
case event.EvtCodeGocodeNotFound:
|
|
|
|
|
notification = Notification{event: e, Type: Setup, Severity: Error}
|
|
|
|
|
case event.EvtCodeIDEStubNotFound:
|
|
|
|
|
notification = Notification{event: e, Type: Setup, Severity: Error}
|
|
|
|
|
default:
|
|
|
|
|
glog.Warningf("Can't handle event[code=%d]", e.Code)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-16 07:36:21 +04:00
|
|
|
|
// 消息国际化处理
|
|
|
|
|
notification.Message = i18n.Get(wsChannel.Request, "notification_"+strconv.Itoa(e.Code)).(string)
|
2014-09-15 19:20:01 +04:00
|
|
|
|
|
|
|
|
|
wsChannel.Conn.WriteJSON(¬ification)
|
2014-09-16 11:06:52 +04:00
|
|
|
|
|
2014-09-16 11:11:14 +04:00
|
|
|
|
// 更新通道最近使用时间
|
2014-09-16 11:06:52 +04:00
|
|
|
|
wsChannel.Time = time.Now()
|
2014-09-15 19:20:01 +04:00
|
|
|
|
}
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
2014-09-16 11:06:52 +04:00
|
|
|
|
// 建立通知通道.
|
2014-09-15 09:03:44 +04:00
|
|
|
|
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
2014-09-17 06:54:57 +04:00
|
|
|
|
sid := r.URL.Query()["sid"][0]
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
|
wSession := session.WideSessions.Get(sid)
|
|
|
|
|
if nil == wSession {
|
|
|
|
|
glog.Errorf("Session [%s] not found", sid)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
|
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
|
2014-09-16 11:06:52 +04:00
|
|
|
|
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
|
2014-09-15 14:03:52 +04:00
|
|
|
|
|
2014-09-15 19:20:01 +04:00
|
|
|
|
notificationWSs[sid] = &wsChan
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
|
|
|
|
ret := map[string]interface{}{"output": "Notification initialized", "cmd": "init-notification"}
|
2014-09-15 14:03:52 +04:00
|
|
|
|
wsChan.Conn.WriteJSON(&ret)
|
|
|
|
|
|
2014-09-16 18:39:22 +04:00
|
|
|
|
glog.V(4).Infof("Open a new [Notification] with session [%s], %d", sid, len(notificationWSs))
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
2014-09-19 15:21:13 +04:00
|
|
|
|
// 添加用户事件处理器
|
|
|
|
|
wSession.EventQueue.AddHandler(event.HandleFunc(event2Notification))
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
|
|
|
|
input := map[string]interface{}{}
|
|
|
|
|
|
|
|
|
|
for {
|
2014-09-15 14:03:52 +04:00
|
|
|
|
if err := wsChan.Conn.ReadJSON(&input); err != nil {
|
2014-09-15 09:03:44 +04:00
|
|
|
|
if err.Error() == "EOF" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err.Error() == "unexpected EOF" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
|
glog.Error("Notification WS ERROR: " + err.Error())
|
2014-09-15 09:03:44 +04:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output := ""
|
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
|
ret = map[string]interface{}{"output": output, "cmd": "notification-output"}
|
2014-09-15 09:03:44 +04:00
|
|
|
|
|
2014-09-15 14:03:52 +04:00
|
|
|
|
if err := wsChan.Conn.WriteJSON(&ret); err != nil {
|
|
|
|
|
glog.Error("Notification WS ERROR: " + err.Error())
|
2014-09-15 09:03:44 +04:00
|
|
|
|
return
|
|
|
|
|
}
|
2014-09-16 11:11:14 +04:00
|
|
|
|
|
|
|
|
|
wsChan.Time = time.Now()
|
2014-09-15 09:03:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|