2017-05-08 19:13:29 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-05-30 16:39:14 +02:00
|
|
|
"time"
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2017-05-29 22:55:38 +02:00
|
|
|
"github.com/genofire/golang-lib/log"
|
2017-05-30 16:39:14 +02:00
|
|
|
"github.com/genofire/golang-lib/worker"
|
2017-05-08 19:13:29 +02:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
const channelBufSize = 100
|
|
|
|
|
|
|
|
type Client struct {
|
2017-05-09 01:03:44 +02:00
|
|
|
ip string
|
|
|
|
ws *websocket.Conn
|
|
|
|
ch chan *Message
|
|
|
|
writeQuit chan bool
|
|
|
|
readQuit chan bool
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(ip string, ws *websocket.Conn) *Client {
|
|
|
|
|
|
|
|
if ws == nil {
|
2017-05-09 01:03:44 +02:00
|
|
|
log.Log.Panic("ws cannot be nil")
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
2017-05-09 01:03:44 +02:00
|
|
|
ws: ws,
|
|
|
|
ch: make(chan *Message, channelBufSize),
|
|
|
|
writeQuit: make(chan bool),
|
|
|
|
readQuit: make(chan bool),
|
|
|
|
ip: ip,
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Write(msg *Message) {
|
|
|
|
select {
|
|
|
|
case c.ch <- msg:
|
|
|
|
default:
|
2017-05-12 21:32:10 +02:00
|
|
|
clientsMutex.Lock()
|
2017-05-09 01:03:44 +02:00
|
|
|
delete(clients, c.ip)
|
2017-05-12 21:32:10 +02:00
|
|
|
clientsMutex.Unlock()
|
2017-05-09 01:03:44 +02:00
|
|
|
log.HTTP(c.ws.Request()).Error("client disconnected")
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:03:44 +02:00
|
|
|
func (c *Client) Close() {
|
|
|
|
c.writeQuit <- true
|
|
|
|
c.readQuit <- true
|
|
|
|
log.HTTP(c.ws.Request()).Info("client disconnecting...")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen Write and Read request via chanel
|
|
|
|
func (c *Client) Listen() {
|
|
|
|
go c.listenWrite()
|
2017-05-15 21:59:48 +02:00
|
|
|
c.Write(&Message{Type: MessageTypeStats, Body: stats})
|
2017-05-30 16:39:14 +02:00
|
|
|
c.publishAllData()
|
2017-05-09 01:03:44 +02:00
|
|
|
c.listenRead()
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 16:39:14 +02:00
|
|
|
func (c *Client) publishAllData() {
|
2017-05-08 19:13:29 +02:00
|
|
|
for _, node := range nodes.List {
|
2017-05-30 02:16:46 +02:00
|
|
|
c.Write(&Message{Type: MessageTypeSystemNode, Node: node})
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
2017-05-30 02:16:46 +02:00
|
|
|
for _, node := range nodes.Current {
|
|
|
|
c.Write(&Message{Type: MessageTypeCurrentNode, Node: node})
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
2017-05-30 16:39:14 +02:00
|
|
|
for _, cmd := range commands.List {
|
|
|
|
c.Write(&Message{Type: MessageTypeCommand, Command: cmd})
|
|
|
|
}
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 21:59:48 +02:00
|
|
|
func (c *Client) handleMessage(msg *Message) {
|
2017-05-09 01:03:44 +02:00
|
|
|
switch msg.Type {
|
2017-05-30 02:16:46 +02:00
|
|
|
case MessageTypeSystemNode:
|
2017-05-09 01:03:44 +02:00
|
|
|
nodes.UpdateNode(msg.Node)
|
2017-05-30 16:39:14 +02:00
|
|
|
break
|
|
|
|
case MessageTypeCommand:
|
|
|
|
cmd := commands.AddCommand(msg.Command)
|
|
|
|
w := worker.NewWorker(time.Millisecond*300, func() {
|
|
|
|
SendAll(Message{Type: MessageTypeCommand, Command: cmd})
|
|
|
|
})
|
|
|
|
go w.Start()
|
|
|
|
go cmd.Run(func() {
|
|
|
|
w.Close()
|
|
|
|
SendAll(Message{Type: MessageTypeCommand, Command: cmd})
|
|
|
|
})
|
|
|
|
break
|
2017-05-09 01:03:44 +02:00
|
|
|
}
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen write request via chanel
|
|
|
|
func (c *Client) listenWrite() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-c.ch:
|
|
|
|
websocket.JSON.Send(c.ws, msg)
|
|
|
|
|
2017-05-09 01:03:44 +02:00
|
|
|
case <-c.writeQuit:
|
|
|
|
close(c.ch)
|
|
|
|
close(c.writeQuit)
|
2017-05-12 21:32:10 +02:00
|
|
|
clientsMutex.Lock()
|
2017-05-09 01:03:44 +02:00
|
|
|
delete(clients, c.ip)
|
2017-05-12 21:32:10 +02:00
|
|
|
clientsMutex.Unlock()
|
2017-05-08 19:13:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen read request via chanel
|
|
|
|
func (c *Client) listenRead() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
2017-05-09 01:03:44 +02:00
|
|
|
case <-c.readQuit:
|
|
|
|
close(c.readQuit)
|
2017-05-12 21:32:10 +02:00
|
|
|
clientsMutex.Lock()
|
2017-05-09 01:03:44 +02:00
|
|
|
delete(clients, c.ip)
|
2017-05-12 21:32:10 +02:00
|
|
|
clientsMutex.Unlock()
|
2017-05-08 19:13:29 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
var msg Message
|
|
|
|
err := websocket.JSON.Receive(c.ws, &msg)
|
|
|
|
if err == io.EOF {
|
2017-05-09 01:03:44 +02:00
|
|
|
close(c.readQuit)
|
|
|
|
c.writeQuit <- true
|
|
|
|
return
|
2017-05-08 19:13:29 +02:00
|
|
|
} else if err != nil {
|
|
|
|
log.HTTP(c.ws.Request()).Error(err)
|
|
|
|
} else {
|
2017-05-15 21:59:48 +02:00
|
|
|
c.handleMessage(&msg)
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|