-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
77 lines (64 loc) · 1.4 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/mohabusama/nodnod/client"
"github.com/mohabusama/nodnod/stats"
)
type Node struct {
name string
address string
client *client.Client
}
func newNode(address string) *Node {
return &Node{
address: address,
client: client.NewClient(address),
}
}
func (n *Node) Connect() {
if n.client.Connected() {
log.Debug("Not connecting, Node already connected!")
return
}
if err := n.client.Connect(); err == nil {
log.Info("Established connection with node: ", n.address)
// This should be able to load the name!
n.Stat()
} else {
log.Warnf("Failed to connect to node: %s. Error: %s", n.address, err)
}
}
func (n *Node) Stat() (*stats.NodeStats, error) {
if st, err := n.client.Stat(); err != nil {
return nil, err
} else {
// Return stats.NodeStat from stats.Stats
for nodeName, nodeStats := range st {
if n.name == "" {
n.name = nodeName
log.Info("Connected to node: ", n.String())
}
return &nodeStats, nil
}
}
return &stats.NodeStats{}, nil
}
func (n *Node) Address() string {
return n.address
}
func (n *Node) Name() string {
if n.name == "" {
return n.address
}
return n.name
}
func (n *Node) Connected() bool {
return n.client.Connected()
}
func (n *Node) String() string {
return fmt.Sprintf("[%s : %s]", n.Name(), n.address)
}
func (n *Node) Disconnect() {
n.client.Disconnect()
}