forked from bluenviron/goroslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceclient.go
182 lines (150 loc) · 3.69 KB
/
serviceclient.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
package goroslib
import (
"fmt"
"reflect"
"time"
"github.com/aler9/goroslib/pkg/prototcp"
"github.com/aler9/goroslib/pkg/serviceproc"
)
// ServiceClientConf is the configuration of a ServiceClient.
type ServiceClientConf struct {
// parent node.
Node *Node
// name of the service from which providers will be obtained.
Name string
// an instance of the service type.
Srv interface{}
// (optional) enable keep-alive packets, that are
// useful when there's a firewall between nodes.
EnableKeepAlive bool
}
// ServiceClient is a ROS service client, an entity that can send requests to
// service providers and receive responses.
type ServiceClient struct {
conf ServiceClientConf
srvReq interface{}
srvRes interface{}
conn *prototcp.Conn
}
// NewServiceClient allocates a ServiceClient. See ServiceClientConf for the options.
func NewServiceClient(conf ServiceClientConf) (*ServiceClient, error) {
if conf.Node == nil {
return nil, fmt.Errorf("Node is empty")
}
if conf.Name == "" {
return nil, fmt.Errorf("Name is empty")
}
if conf.Srv == nil {
return nil, fmt.Errorf("Srv is empty")
}
srvReq, srvRes, err := serviceproc.RequestResponse(conf.Srv)
if err != nil {
return nil, err
}
return &ServiceClient{
conf: conf,
srvReq: srvReq,
srvRes: srvRes,
}, nil
}
// Close closes a ServiceClient and shuts down all its operations.
func (sc *ServiceClient) Close() error {
if sc.conn != nil {
sc.conn.Close()
}
return nil
}
// Call sends a request to a service provider and reads a response.
func (sc *ServiceClient) Call(req interface{}, res interface{}) error {
if reflect.TypeOf(req) != reflect.PtrTo(reflect.TypeOf(sc.srvReq)) {
panic("wrong req")
}
if reflect.TypeOf(res) != reflect.PtrTo(reflect.TypeOf(sc.srvRes)) {
panic("wrong res")
}
connCreatedInThisCall := false
if sc.conn == nil {
err := sc.createConn()
if err != nil {
return err
}
connCreatedInThisCall = true
}
err := sc.conn.WriteMessage(req)
if err != nil {
sc.conn.Close()
sc.conn = nil
// if the connection was created previously, it could be damaged or
// linked to an invalid provider.
// do another try.
if !connCreatedInThisCall {
return sc.Call(req, res)
}
return err
}
err = sc.conn.ReadServiceResState()
if err != nil {
sc.conn.Close()
sc.conn = nil
// if the connection was created previously, it could be damaged or
// linked to an invalid provider.
// do another try.
if !connCreatedInThisCall {
return sc.Call(req, res)
}
return err
}
err = sc.conn.ReadMessage(res)
if err != nil {
sc.conn.Close()
sc.conn = nil
return err
}
return nil
}
func (sc *ServiceClient) createConn() error {
res, err := sc.conf.Node.apiMasterClient.LookupService(
sc.conf.Node.absoluteTopicName(sc.conf.Name))
if err != nil {
return fmt.Errorf("lookupService: %v", err)
}
srvMD5, err := serviceproc.MD5(sc.conf.Srv)
if err != nil {
return err
}
address, err := urlToAddress(res.URL)
if err != nil {
return err
}
conn, err := prototcp.NewClient(address)
if err != nil {
return err
}
if sc.conf.EnableKeepAlive {
conn.NetConn().SetKeepAlive(true)
conn.NetConn().SetKeepAlivePeriod(60 * time.Second)
}
err = conn.WriteHeader(&prototcp.HeaderServiceClient{
Callerid: sc.conf.Node.absoluteName(),
Md5sum: srvMD5,
Persistent: 1,
Service: sc.conf.Node.absoluteTopicName(sc.conf.Name),
})
if err != nil {
conn.Close()
return err
}
var outHeader prototcp.HeaderServiceProvider
err = conn.ReadHeader(&outHeader)
if err != nil {
conn.Close()
return err
}
if outHeader.Md5sum != srvMD5 {
conn.Close()
return fmt.Errorf("wrong md5sum: expected %s, got %s",
srvMD5, outHeader.Md5sum)
}
sc.conn = conn
return nil
}