-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
101 lines (82 loc) · 2.14 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
package copper
import (
"net"
)
type clientConn struct {
rpcClient
hmap *HandlerMap
}
var _ Client = &clientConn{}
func newClient(conn net.Conn) *clientConn {
c := &clientConn{
hmap: NewHandlerMap(nil),
}
c.rpcClient.RawConn = NewRawConn(conn, c, false)
return c
}
// NewClient wraps an existing connection and returns a copper client
func NewClient(conn net.Conn) Client {
return newClient(conn)
}
type clientSubscription struct {
owner *clientConn
targetID int64
}
func (sub *clientSubscription) Endpoints() ([]Endpoint, error) {
return sub.owner.getEndpoints(sub.targetID)
}
func (sub *clientSubscription) EndpointChanges() (EndpointChangesStream, error) {
return sub.owner.streamEndpoints(sub.targetID)
}
func (sub *clientSubscription) Open() (Stream, error) {
return sub.owner.openNewStream(sub.targetID)
}
func (sub *clientSubscription) Stop() error {
return sub.owner.unsubscribe(sub.targetID)
}
func (c *clientConn) Subscribe(settings SubscribeSettings) (Subscription, error) {
targetID, err := c.subscribe(settings)
if err != nil {
return nil, err
}
return &clientSubscription{
owner: c,
targetID: targetID,
}, nil
}
type clientPublication struct {
owner *clientConn
targetID int64
}
func (pub *clientPublication) Stop() error {
err := pub.owner.unpublish(pub.targetID)
pub.owner.hmap.Remove(pub.targetID)
return err
}
func (c *clientConn) Publish(name string, settings PublishSettings, handler Handler) (Publication, error) {
targetID := c.hmap.Add(handler)
err := c.publish(targetID, name, settings)
if err != nil {
c.hmap.Remove(targetID)
return nil, err
}
return &clientPublication{
owner: c,
targetID: targetID,
}, nil
}
func (c *clientConn) SetRoute(name string, routes ...Route) error {
return c.setRoute(name, routes...)
}
func (c *clientConn) ListRoutes() ([]string, error) {
return c.listRoutes()
}
func (c *clientConn) LookupRoute(name string) ([]Route, error) {
return c.lookupRoute(name)
}
func (c *clientConn) ServiceChanges() (ServiceChangesStream, error) {
return c.streamServices()
}
func (c *clientConn) ServeCopper(stream Stream) error {
return rpcWrapClient(stream, c.hmap)
}