-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
169 lines (148 loc) · 4.25 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"log"
"time"
"github.com/nats-io/nats.go"
)
type Node struct {
name string
attr map[string]int
main func(*Node)
logs []Packet
nc *nats.EncodedConn
ntpClient *NTPClient
}
func NewNode(name string, nc *nats.EncodedConn, ntpClient *NTPClient, main func(*Node)) *Node {
node := &Node{
name: name,
attr: map[string]int{"rate": 1, "paused": 1, "alive": 1},
main: main,
nc: nc,
ntpClient: ntpClient,
}
// SETUP own setters and getters
nc.Subscribe(fmt.Sprintf("%s.get", name), node.get_srv_cb)
nc.Subscribe(fmt.Sprintf("%s.get.log", name), node.get_srv_log_cb)
nc.Subscribe(fmt.Sprintf("%s.set", name), node.set_srv_cb)
nc.Subscribe(fmt.Sprintf("%s.set.log", name), node.set_srv_log_cb)
return node
}
func (n *Node) get_srv_cb(subj, reply string, msg GetRequest) {
if val, ok := n.attr[msg.Name]; ok {
resp := &GetResponse{Success: true, Data: val}
err := n.nc.Publish(reply, resp)
if err != nil {
log.Fatal(err)
}
fmt.Println("Getting", msg.Name, "by", msg.Author)
} else {
resp := &GetResponse{Success: false, Reason: fmt.Sprintf("Trying to write non-existing field \"%s\"", msg.Name)}
err := n.nc.Publish(reply, resp)
if err != nil {
log.Fatal(err)
}
}
}
func (n *Node) get_srv_log_cb(subj, reply string, _ SetRequest) {
timeNow := time.Now().Format("060102_1504")
fileName := fmt.Sprintf("%s.csv", timeNow)
save(n.logs, fileName)
n.nc.Publish(reply, n.logs)
}
func (n *Node) set_srv_cb(subj, reply string, msg SetRequest) {
if _, ok := n.attr[msg.Name]; ok {
n.attr[msg.Name] = msg.Data
n.nc.Publish(reply, &SetResponse{Success: true})
fmt.Println("Setting", msg.Name, "to", msg.Data, "by", msg.Author)
} else {
resp := &SetResponse{Success: false, Reason: fmt.Sprintf("Trying to write non-existing field \"%s\"", msg.Name)}
n.nc.Publish(reply, resp)
}
}
func (n *Node) set_srv_log_cb(subj, reply string, msg []Packet) {
n.logs = msg
n.nc.Publish(reply, &SetResponse{Success: true})
}
func (n *Node) kill(remote_names ...string) error {
for _, remote_name := range remote_names {
_, err := n.remote_set(remote_name, "alive", 0)
if err != nil {
return fmt.Errorf("%w to \"%s\"", err, remote_name)
}
}
return nil
}
func (n *Node) pause(remote_names ...string) error {
for _, remote_name := range remote_names {
_, err := n.remote_set(remote_name, "paused", 1)
if err != nil {
return fmt.Errorf("%w to \"%s\"", err, remote_name)
}
}
return nil
}
func (n *Node) unpause(remote_names ...string) error {
for _, remote_name := range remote_names {
_, err := n.remote_set(remote_name, "paused", 0)
if err != nil {
return fmt.Errorf("%w to \"%s\"", err, remote_name)
}
}
return nil
}
func (n *Node) remote_get(remote_name string, attr_name string) (GetResponse, error) {
req := &GetRequest{Author: n.name, Name: attr_name}
var resp GetResponse
err := n.nc.Request(fmt.Sprintf("%s.get", remote_name), req, &resp, time.Second)
if err != nil {
return GetResponse{}, err
}
return resp, nil
}
func (n *Node) remote_get_log(remote_name string) ([]Packet, error) {
req := &GetRequest{Author: n.name}
var resp []Packet
err := n.nc.Request(fmt.Sprintf("%s.get.log", remote_name), req, &resp, time.Second)
if err != nil {
return []Packet{}, err
}
return resp, nil
}
func (n *Node) remote_set(remote_name string, attr_name string, value int) (SetResponse, error) {
req := &SetRequest{Author: n.name, Name: attr_name, Data: value}
var resp SetResponse
err := n.nc.Request(fmt.Sprintf("%s.set", remote_name), req, &resp, time.Second)
if err != nil {
return SetResponse{}, err
}
return resp, nil
}
func (n *Node) remote_set_log(remote_name string, value []Packet) (SetResponse, error) {
var resp SetResponse
err := n.nc.Request(fmt.Sprintf("%s.set.log", remote_name), value, &resp, time.Second)
if err != nil {
return SetResponse{}, err
}
return resp, nil
}
func (n *Node) isAlive() bool {
val, _ := n.attr["alive"]
return val != 0
}
func (n *Node) isPaused() bool {
val, _ := n.attr["paused"]
return val != 0
}
func (n *Node) run() {
for {
if val, _ := n.attr["alive"]; val == 0 {
break
}
if val, _ := n.attr["paused"]; val == 0 {
n.main(n)
}
rate, _ := n.attr["rate"]
time.Sleep(time.Duration(1e9 / rate))
}
}