/
client.go
188 lines (165 loc) · 6.27 KB
/
client.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
186
187
188
/*
Copyright © 2021 Ci4Rail GmbH <engineering@ci4rail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package client provides the API for io4edge I/O devices
package client
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/ci4rail/io4edge-client-go/transport"
"github.com/ci4rail/io4edge-client-go/transport/socket"
"google.golang.org/protobuf/proto"
)
const defaultServiceSearchTimeout = 8 * time.Second
// If is a interface for the Client
type If interface {
Command(cmd proto.Message, res proto.Message, timeout time.Duration) error
ReadMessage(res proto.Message, timeout time.Duration) error
}
// FunctionInfo is an interface to query properties of the io4edge function
type FunctionInfo interface {
// NetAddress returns the IP address (or host name) and the default port of the function
NetAddress() (host string, port int, err error)
// FuncClass returns the class of the io4edge function: e.g. core/datalogger/controlio/ttynvt
FuncClass() (class string, err error)
// Security tells whether function channels use encryption (no/tls)
Security() (security string, err error)
// AuxPort returns the protocol of the aux port (tcp/udp) and the port
// returns error if no aux port for function
AuxPort() (protcol string, port int, err error)
// AuxSchema returns the schema name of the aux channel
// returns error if no aux port for function
AuxSchemaID() (schemaID string, err error)
}
// Client represents a client for an io4edge function
type Client struct {
Ch *Channel
FuncInfo FunctionInfo
}
// NewClient creates a new client for an io4edge function
func NewClient(c *Channel, funcInfo FunctionInfo) *Client {
return &Client{Ch: c, FuncInfo: funcInfo}
}
// NewClientFromSocketAddress creates a new function client from a socket with the specified address.
func NewClientFromSocketAddress(address string) (*Client, error) {
return newClientFromSocketAddress(address, NewFuncInfoDefault(address))
}
func newClientFromSocketAddress(address string, funcInfo FunctionInfo) (*Client, error) {
ch, err := createChannel(address)
if err != nil {
return nil, err
}
c := NewClient(ch, funcInfo)
return c, nil
}
func createChannel(address string) (*Channel, error) {
t, err := socket.NewSocketConnection(address)
if err != nil {
return nil, errors.New("can't create connection: " + err.Error())
}
ms := transport.NewFramedStreamFromTransport(t)
ch := NewChannel(ms)
return ch, nil
}
// NewClientFromService creates a new function client from a socket with a address, which was acquired from the specified service.
// The timeout specifies the maximal time waiting for a service to show up. If 0, use default timeout
func NewClientFromService(serviceAddr string, timeout time.Duration) (*Client, error) {
instance, service, err := ParseInstanceAndService(serviceAddr)
if err != nil {
return nil, err
}
if timeout == 0 {
timeout = defaultServiceSearchTimeout
}
svcInfo, err := GetServiceInfo(instance, service, timeout)
if err != nil {
return nil, err
}
ipAddrPort := svcInfo.GetIPAddressPort()
c, err := newClientFromSocketAddress(ipAddrPort, svcInfo)
return c, err
}
// NewClientFromUniversalAddress creates a new function client from addrOrService.
// If addrOrService is of the form "host:port", it creates the client from that host/port,
// otherwise it assumes addrOrService is the instance name of an mdns service.
// If service is non-empty and addrOrService is a mdns instance name, it is appended to the addrOrService.
// .e.g. if addrOrService is "iou01-sn01-binio" and service is "_io4edge_binaryIoTypeA._tcp", the mdns instance
// name "iou01-sn01-binio._io4edge_binaryIoTypeA._tcp" is used.
// The timeout specifies the maximal time waiting for a service to show up. Not used for "host:port"
func NewClientFromUniversalAddress(addrOrService string, service string, timeout time.Duration) (*Client, error) {
var c *Client
var err error
if _, _, err = netAddressSplit(addrOrService); err == nil {
c, err = NewClientFromSocketAddress(addrOrService)
} else {
if service != "" {
addrOrService = fmt.Sprintf("%s.%s", addrOrService, service)
}
c, err = NewClientFromService(addrOrService, timeout)
}
return c, err
}
// Close terminates the underlying connection to the service
func (c *Client) Close() {
c.Ch.Close()
}
// RestartChannel restarts the connection to the device
// May be used to reestablish a connection after a device reset or after network disconnection of the device (e.g. WLAN roaming)
func (c *Client) RestartChannel() error {
host, port, err := c.FuncInfo.NetAddress()
if err != nil {
return err
}
address := fmt.Sprintf("%s:%d", host, port)
ch, err := createChannel(address)
if err != nil {
return err
}
c.Ch = ch
return nil
}
// Command issues a command cmd to a channel, waits for the devices response and returns it in res
func (c *Client) Command(cmd proto.Message, res proto.Message, timeout time.Duration) error {
err := c.Ch.WriteMessage(cmd)
if err != nil {
return err
}
err = c.Ch.ReadMessage(res, timeout)
if err != nil {
return errors.New("Failed to receive device response: " + err.Error())
}
return err
}
// ReadMessage reads the next message from the channel and unmarshalles it
func (c *Client) ReadMessage(res proto.Message, timeout time.Duration) error {
err := c.Ch.ReadMessage(res, timeout)
if err != nil {
return errors.New("Failed to read forever: " + err.Error())
}
return nil
}
// netAddressSplit splits addr to host and port
// example: addr="myhost.example.com:1234" -> host="myhost.example.com", port=1234
func netAddressSplit(addr string) (host string, port int, err error) {
fields := strings.Split(addr, ":")
if len(fields) != 2 {
return "", 0, errors.New("invalid address " + addr)
}
port, err = strconv.Atoi(fields[1])
if err != nil {
return "", 0, errors.New("invalid port in " + addr)
}
return fields[0], port, nil
}