-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
71 lines (55 loc) · 1.49 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
package ipc
import (
"context"
"time"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/ipc/internal/flisten"
"github.com/ActiveState/cli/internal/logging"
)
type Client struct {
sockpath *SockPath
dialer *flisten.Dial
}
func NewClient(n *SockPath) *Client {
logging.Debug("Initializing ipc client with socket: %s", n)
return &Client{
sockpath: n,
dialer: flisten.NewDial(),
}
}
func (c *Client) SockPath() *SockPath {
return c.sockpath
}
func (c *Client) Request(ctx context.Context, key string) (string, error) {
spath := c.sockpath.String()
conn, err := c.dialer.DialContext(ctx, network, spath)
if err != nil {
err = asServerDownError(err)
return "", errs.Wrap(err, "Cannot connect to ipc via %q", spath)
}
defer conn.Close()
_, err = conn.Write([]byte(key))
if err != nil {
return "", errs.Wrap(err, "Failed to write to server connection")
}
buf := make([]byte, msgWidth)
n, err := conn.Read(buf)
if err != nil {
return "", errs.Wrap(err, "Failed to read from server connection")
}
msg := string(buf[:n])
return msg, nil
}
func (c *Client) PingServer(ctx context.Context) (time.Duration, error) {
start := time.Now()
if _, err := getPing(ctx, c); err != nil {
return 0, errs.Wrap(err, "Failed to complete ping request")
}
return time.Since(start), nil
}
func (c *Client) StopServer(ctx context.Context) error {
if _, err := getStop(ctx, c); err != nil {
return errs.Wrap(err, "Failed to complete stop request")
}
return nil
}