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