2015-01-17 01:37:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
|
2015-01-21 21:59:08 +00:00
|
|
|
"github.com/khlieng/name_pending/storage"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
irc map[string]*IRC
|
|
|
|
ircLock sync.Mutex
|
|
|
|
|
|
|
|
ws map[string]*WebSocket
|
|
|
|
wsLock sync.Mutex
|
|
|
|
out chan []byte
|
|
|
|
|
|
|
|
user storage.User
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSession() *Session {
|
|
|
|
return &Session{
|
|
|
|
irc: make(map[string]*IRC),
|
|
|
|
ws: make(map[string]*WebSocket),
|
|
|
|
out: make(chan []byte, 32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) getIRC(server string) (*IRC, bool) {
|
|
|
|
s.ircLock.Lock()
|
|
|
|
irc, ok := s.irc[server]
|
2015-02-09 00:00:30 +00:00
|
|
|
s.ircLock.Unlock()
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
return irc, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) setIRC(server string, irc *IRC) {
|
|
|
|
s.ircLock.Lock()
|
|
|
|
s.irc[server] = irc
|
|
|
|
s.ircLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-02-09 00:00:30 +00:00
|
|
|
func (s *Session) deleteIRC(server string) {
|
|
|
|
s.ircLock.Lock()
|
|
|
|
delete(s.irc, server)
|
|
|
|
s.ircLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) numIRC() int {
|
|
|
|
s.ircLock.Lock()
|
|
|
|
n := len(s.irc)
|
|
|
|
s.ircLock.Unlock()
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
func (s *Session) setWS(addr string, ws *websocket.Conn) {
|
|
|
|
socket := NewWebSocket(ws)
|
|
|
|
go socket.write()
|
|
|
|
|
|
|
|
s.wsLock.Lock()
|
|
|
|
s.ws[addr] = socket
|
|
|
|
s.wsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) deleteWS(addr string) {
|
|
|
|
s.wsLock.Lock()
|
|
|
|
delete(s.ws, addr)
|
|
|
|
s.wsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) sendJSON(t string, v interface{}) {
|
|
|
|
data, _ := json.Marshal(v)
|
|
|
|
raw := json.RawMessage(data)
|
|
|
|
res, _ := json.Marshal(WSResponse{Type: t, Response: &raw})
|
|
|
|
|
|
|
|
s.out <- res
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:38:51 +00:00
|
|
|
func (s *Session) sendError(err error, server string) {
|
|
|
|
s.sendJSON("error", Error{
|
|
|
|
Server: server,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
func (s *Session) write() {
|
|
|
|
for res := range s.out {
|
|
|
|
s.wsLock.Lock()
|
|
|
|
for _, ws := range s.ws {
|
2015-01-29 23:38:51 +00:00
|
|
|
ws.Out <- res
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
s.wsLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|