forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ovsclient.go
57 lines (51 loc) · 1.25 KB
/
ovsclient.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
package ovsclient
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"time"
)
// Client is an RPC client for communicating with OVS.
type Client struct {
*rpc.Client
conn net.Conn
}
// New creates a new Client from a connection.
func New(conn net.Conn) *Client {
return &Client{
Client: jsonrpc.NewClient(conn),
conn: conn,
}
}
// DialTimeout dials the provided network and address, and if it responds within
// timeout will return a valid Client.
func DialTimeout(network, addr string, timeout time.Duration) (*Client, error) {
conn, err := net.DialTimeout(network, addr, timeout)
if err != nil {
return nil, err
}
return New(conn), nil
}
// Ping returns nil if the OVS server responded to an "echo" command.
func (c *Client) Ping() error {
var result interface{}
if err := c.Call("echo", []string{"hello"}, &result); err != nil {
return err
}
return nil
}
// WaitForDisconnect will block until the provided connection is closed
// and return an error. This consumes the connection.
func (c *Client) WaitForDisconnect() error {
n, err := io.Copy(ioutil.Discard, c.conn)
if err != nil && err != io.EOF {
return err
}
if n > 0 {
return fmt.Errorf("unexpected bytes read waiting for disconnect: %d", n)
}
return nil
}