fix bug
This commit is contained in:
parent
dc5569cea5
commit
c1845d038c
|
@ -9,6 +9,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
//Node struct
|
||||
type Node struct {
|
||||
Firstseen time.Time `json:"firstseen"`
|
||||
Lastseen time.Time `json:"lastseen"`
|
||||
|
@ -17,6 +18,7 @@ type Node struct {
|
|||
Neighbours interface{} `json:"neighbours"`
|
||||
}
|
||||
|
||||
//Nodes struct: cache DB of Node's structs
|
||||
type Nodes struct {
|
||||
Version int `json:"version"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
|
@ -24,6 +26,7 @@ type Nodes struct {
|
|||
sync.Mutex
|
||||
}
|
||||
|
||||
//NewNodes create Nodes structs (cache DB)
|
||||
func NewNodes() *Nodes {
|
||||
nodes := &Nodes{
|
||||
Version: 1,
|
||||
|
@ -33,17 +36,18 @@ func NewNodes() *Nodes {
|
|||
return nodes
|
||||
}
|
||||
|
||||
func (nodes *Nodes) Get(nodeId string) *Node {
|
||||
//Get a Node by nodeid
|
||||
func (nodes *Nodes) Get(nodeID string) *Node {
|
||||
now := time.Now()
|
||||
|
||||
nodes.Lock()
|
||||
node, _ := nodes.List[nodeId]
|
||||
node, _ := nodes.List[nodeID]
|
||||
|
||||
if node == nil {
|
||||
node = &Node{
|
||||
Firstseen: now,
|
||||
}
|
||||
nodes.List[nodeId] = node
|
||||
nodes.List[nodeID] = node
|
||||
}
|
||||
nodes.Unlock()
|
||||
|
||||
|
@ -52,6 +56,7 @@ func (nodes *Nodes) Get(nodeId string) *Node {
|
|||
return node
|
||||
}
|
||||
|
||||
//Saver to save the cached DB to json file
|
||||
func (nodes *Nodes) Saver(outputFile string, saveInterval time.Duration) {
|
||||
c := time.Tick(saveInterval)
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ const (
|
|||
maxDataGramSize int = 8192
|
||||
)
|
||||
|
||||
//Response of the
|
||||
//Response of the responed request
|
||||
type Response struct {
|
||||
Address net.UDPAddr
|
||||
Raw []byte
|
||||
|
|
|
@ -15,7 +15,7 @@ type Client struct {
|
|||
id int
|
||||
ws *websocket.Conn
|
||||
server *Server
|
||||
ch chan *struct{}
|
||||
ch chan interface{}
|
||||
doneCh chan bool
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ func NewClient(ws *websocket.Conn, server *Server) *Client {
|
|||
}
|
||||
|
||||
maxID++
|
||||
ch := make(chan *struct{}, channelBufSize)
|
||||
ch := make(chan interface{}, channelBufSize)
|
||||
doneCh := make(chan bool)
|
||||
|
||||
return &Client{maxID, ws, server, ch, doneCh}
|
||||
|
@ -43,7 +43,7 @@ func (c *Client) GetConnection() *websocket.Conn {
|
|||
}
|
||||
|
||||
//Write send the msg informations to the clients
|
||||
func (c *Client) Write(msg *struct{}) {
|
||||
func (c *Client) Write(msg interface{}) {
|
||||
select {
|
||||
case c.ch <- msg:
|
||||
default:
|
||||
|
|
|
@ -13,17 +13,17 @@ type Server struct {
|
|||
clients map[int]*Client
|
||||
addCh chan *Client
|
||||
delCh chan *Client
|
||||
sendAllCh chan *struct{}
|
||||
sendAllCh chan interface{}
|
||||
closeCh chan bool
|
||||
errCh chan error
|
||||
}
|
||||
|
||||
//NewServer creates a new node server
|
||||
//NewServer creates a new server
|
||||
func NewServer(pattern string) *Server {
|
||||
clients := make(map[int]*Client)
|
||||
addCh := make(chan *Client)
|
||||
delCh := make(chan *Client)
|
||||
sendAllCh := make(chan *struct{})
|
||||
sendAllCh := make(chan interface{})
|
||||
closeCh := make(chan bool)
|
||||
errCh := make(chan error)
|
||||
|
||||
|
@ -38,22 +38,22 @@ func NewServer(pattern string) *Server {
|
|||
}
|
||||
}
|
||||
|
||||
//Add a node listen client
|
||||
//Add a listen client
|
||||
func (s *Server) Add(c *Client) {
|
||||
s.addCh <- c
|
||||
}
|
||||
|
||||
//Del a node listen client
|
||||
//Del a listen client
|
||||
func (s *Server) Del(c *Client) {
|
||||
s.delCh <- c
|
||||
}
|
||||
|
||||
//SendAll to all listen clients refreshed information of a node
|
||||
func (s *Server) SendAll(node *struct{}) {
|
||||
s.sendAllCh <- node
|
||||
//SendAll to all listen clients a msg
|
||||
func (s *Server) SendAll(msg interface{}) {
|
||||
s.sendAllCh <- msg
|
||||
}
|
||||
|
||||
//Close stops node server
|
||||
//Close stops server
|
||||
func (s *Server) Close() {
|
||||
s.closeCh <- true
|
||||
}
|
||||
|
@ -63,9 +63,9 @@ func (s *Server) Err(err error) {
|
|||
s.errCh <- err
|
||||
}
|
||||
|
||||
func (s *Server) sendAll(node *struct{}) {
|
||||
func (s *Server) sendAll(msg interface{}) {
|
||||
for _, c := range s.clients {
|
||||
c.Write(node)
|
||||
c.Write(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,8 +106,8 @@ func (s *Server) Listen() {
|
|||
delete(s.clients, c.id)
|
||||
|
||||
// broadcast message for all clients
|
||||
case node := <-s.sendAllCh:
|
||||
s.sendAll(node)
|
||||
case msg := <-s.sendAllCh:
|
||||
s.sendAll(msg)
|
||||
|
||||
case err := <-s.errCh:
|
||||
log.Println("Error:", err.Error())
|
||||
|
|
Loading…
Reference in New Issue