-
Notifications
You must be signed in to change notification settings - Fork 62
/
node_funcs_node.go
135 lines (107 loc) · 2.57 KB
/
node_funcs_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
package goroslib
import (
"fmt"
"time"
"github.com/bluenviron/goroslib/v2/pkg/apislave"
)
// InfoConnection contains information about a connection.
type InfoConnection struct {
ID int
To string
Direction byte
Transport string
Topic string
Connected bool
}
// NodeGetConns returns infos about connections of a node.
func (n *Node) NodeGetConns(nodeName string) ([]InfoConnection, error) {
ur, err := n.apiMasterClient.LookupNode(nodeName)
if err != nil {
return nil, err
}
address, err := urlToAddress(ur)
if err != nil {
return nil, err
}
xcs := apislave.NewClient(address, n.absoluteName(), n.httpClient)
infos, err := xcs.GetBusInfo()
if err != nil {
return nil, err
}
ret := make([]InfoConnection, len(infos))
for i, info := range infos {
if len(info) < 6 {
return nil, fmt.Errorf("invalid entry: %v", info)
}
id, ok := info[0].(int)
if !ok {
return nil, fmt.Errorf("invalid entry: %v", info)
}
counterpart, ok := info[1].(string)
if !ok {
return nil, fmt.Errorf("invalid entry: %v", info)
}
temp, ok := info[2].(string)
if !ok || len(temp) != 1 {
return nil, fmt.Errorf("invalid entry: %v", info)
}
direction := temp[0]
transport, ok := info[3].(string)
if !ok {
return nil, fmt.Errorf("invalid entry: %v", info)
}
topic, ok := info[4].(string)
if !ok {
return nil, fmt.Errorf("invalid entry: %v", info)
}
connected, ok := info[5].(bool)
if !ok {
return nil, fmt.Errorf("invalid entry: %v", info)
}
ret[i] = InfoConnection{
ID: id,
To: counterpart,
Direction: direction,
Transport: transport,
Topic: topic,
Connected: connected,
}
}
return ret, nil
}
// NodePing sends a ping request to a given node, wait for the response and returns
// the elapsed time.
func (n *Node) NodePing(nodeName string) (time.Duration, error) {
ur, err := n.apiMasterClient.LookupNode(nodeName)
if err != nil {
return 0, err
}
address, err := urlToAddress(ur)
if err != nil {
return 0, err
}
xcs := apislave.NewClient(address, n.absoluteName(), n.httpClient)
start := time.Now()
_, err = xcs.GetPid()
if err != nil {
return 0, err
}
return time.Since(start), nil
}
// NodeKill sends a kill request to a given node.
func (n *Node) NodeKill(nodeName string) error {
ur, err := n.apiMasterClient.LookupNode(nodeName)
if err != nil {
return err
}
address, err := urlToAddress(ur)
if err != nil {
return err
}
xcs := apislave.NewClient(address, n.absoluteName(), n.httpClient)
err = xcs.Shutdown("")
if err != nil {
return err
}
return nil
}