-
Notifications
You must be signed in to change notification settings - Fork 62
/
node_funcs_master.go
185 lines (152 loc) · 4.17 KB
/
node_funcs_master.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package goroslib
import (
"fmt"
"net"
)
// InfoNode contains informations about a node.
type InfoNode struct {
PublishedTopics map[string]struct{}
SubscribedTopics map[string]struct{}
ProvidedServices map[string]struct{}
Address string
}
// MasterGetNodes returns all the nodes connected to the master.
func (n *Node) MasterGetNodes() (map[string]*InfoNode, error) {
state, err := n.apiMasterClient.GetSystemState()
if err != nil {
return nil, err
}
ret := make(map[string]*InfoNode)
initEntry := func(node string) {
if _, ok := ret[node]; !ok {
ret[node] = &InfoNode{
PublishedTopics: make(map[string]struct{}),
SubscribedTopics: make(map[string]struct{}),
ProvidedServices: make(map[string]struct{}),
}
}
}
for _, entry := range state.PublishedTopics {
for _, node := range entry.Nodes {
initEntry(node)
ret[node].PublishedTopics[entry.Name] = struct{}{}
}
}
for _, entry := range state.SubscribedTopics {
for _, node := range entry.Nodes {
initEntry(node)
ret[node].SubscribedTopics[entry.Name] = struct{}{}
}
}
for _, entry := range state.ProvidedServices {
for _, node := range entry.Nodes {
initEntry(node)
ret[node].ProvidedServices[entry.Name] = struct{}{}
}
}
for nodeName, info := range ret {
ur, err := n.apiMasterClient.LookupNode(nodeName)
if err != nil {
return nil, fmt.Errorf("lookupNode: %w", err)
}
address, err := urlToAddress(ur)
if err != nil {
return nil, err
}
info.Address = address
}
return ret, nil
}
// MasterGetMachines returns all the machines connected to the master with a node.
func (n *Node) MasterGetMachines() (map[string]struct{}, error) {
// this is like its equivalent in python
// https://docs.ros.org/noetic/api/rosnode/html/rosnode-pysrc.html#get_machines_by_nodes
nodes, err := n.MasterGetNodes()
if err != nil {
return nil, err
}
ret := make(map[string]struct{})
for _, info := range nodes {
host, _, err := net.SplitHostPort(info.Address)
if err != nil {
continue
}
ret[host] = struct{}{}
}
return ret, nil
}
// InfoTopic contains informations about a topic.
type InfoTopic struct {
Type string
Publishers map[string]struct{}
Subscribers map[string]struct{}
}
// MasterGetTopics returns all the topics published by nodes connected to the master.
func (n *Node) MasterGetTopics() (map[string]*InfoTopic, error) {
state, err := n.apiMasterClient.GetSystemState()
if err != nil {
return nil, fmt.Errorf("getSystemState: %w", err)
}
types, err := n.apiMasterClient.GetTopicTypes()
if err != nil {
return nil, fmt.Errorf("getTopicTypes: %w", err)
}
ret := make(map[string]*InfoTopic)
for _, entry := range types {
ret[entry.Name] = &InfoTopic{
Type: entry.Type,
Publishers: make(map[string]struct{}),
Subscribers: make(map[string]struct{}),
}
}
for _, entry := range state.PublishedTopics {
if _, ok := ret[entry.Name]; !ok {
continue
}
for _, node := range entry.Nodes {
ret[entry.Name].Publishers[node] = struct{}{}
}
}
for _, entry := range state.SubscribedTopics {
if _, ok := ret[entry.Name]; !ok {
continue
}
for _, node := range entry.Nodes {
ret[entry.Name].Subscribers[node] = struct{}{}
}
}
return ret, nil
}
// InfoService contains informations about a service.
type InfoService struct {
Providers map[string]struct{}
Address string
}
// MasterGetServices returns all the services provided by nodes connected to the server.
func (n *Node) MasterGetServices() (map[string]*InfoService, error) {
state, err := n.apiMasterClient.GetSystemState()
if err != nil {
return nil, fmt.Errorf("getSystemState: %w", err)
}
ret := make(map[string]*InfoService)
for _, entry := range state.ProvidedServices {
if _, ok := ret[entry.Name]; !ok {
ret[entry.Name] = &InfoService{
Providers: make(map[string]struct{}),
}
}
for _, node := range entry.Nodes {
ret[entry.Name].Providers[node] = struct{}{}
}
ur, err := n.apiMasterClient.LookupService(entry.Name)
if err != nil {
return nil, fmt.Errorf("lookupService: %w", err)
}
address, err := urlToAddress(ur)
if err != nil {
return nil, err
}
ret[entry.Name].Address = address
}
return ret, nil
}