-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserviceclient.go
99 lines (84 loc) · 2.19 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
package bw2bind
import (
"strings"
"sync"
)
type ServiceClient Service
type InterfaceClient Interface
func (cl *BW2Client) NewServiceClient(baseuri string, name string) *ServiceClient {
baseuri = strings.TrimSuffix(baseuri, "/")
return &ServiceClient{cl: cl, baseuri: baseuri, name: name, mu: &sync.Mutex{}}
}
func (sc *ServiceClient) AddInterface(prefix string, name string) *InterfaceClient {
svc := Service(*sc)
ifc := Interface{
svc: &svc,
prefix: prefix,
name: name,
auto: false,
}
sc.mu.Lock()
sc.ifaces = append(sc.ifaces, &ifc)
sc.mu.Unlock()
rv := InterfaceClient(ifc)
return &rv
}
func (sc *ServiceClient) GetMetadata() (map[string]*MetadataTuple, error) {
svc := Service(*sc)
md, _, err := sc.cl.GetMetadata(svc.FullURI())
return md, err
}
func (sc *ServiceClient) GetMetadataKey(key string) (*MetadataTuple, error) {
svc := Service(*sc)
md, _, err := sc.cl.GetMetadataKey(svc.FullURI(), key)
return md, err
}
func (ifclient *InterfaceClient) SignalURI(signal string) string {
ifc := Interface(*ifclient)
return ifc.SignalURI(signal)
}
func (ifclient *InterfaceClient) SlotURI(slot string) string {
ifc := Interface(*ifclient)
return ifc.SlotURI(slot)
}
func (ifclient *InterfaceClient) FullURI() string {
ifc := Interface(*ifclient)
return ifc.FullURI()
}
func (ifclient *InterfaceClient) PublishSlot(slot string, poz ...PayloadObject) error {
return ifclient.svc.cl.Publish(&PublishParams{
URI: ifclient.SlotURI(slot),
AutoChain: true,
PayloadObjects: poz,
})
}
func (ifclient *InterfaceClient) SubscribeSignal(signal string, cb func(*SimpleMessage)) error {
subChan, err := ifclient.svc.cl.Subscribe(&SubscribeParams{
URI: ifclient.SignalURI(signal),
AutoChain: true,
})
if err != nil {
return err
}
go func() {
for sm := range subChan {
cb(sm)
}
}()
return nil
}
func (ifclient *InterfaceClient) SubscribeSignalH(signal string, cb func(*SimpleMessage)) (string, error) {
subChan, handle, err := ifclient.svc.cl.SubscribeH(&SubscribeParams{
URI: ifclient.SignalURI(signal),
AutoChain: true,
})
if err != nil {
return "", err
}
go func() {
for sm := range subChan {
cb(sm)
}
}()
return handle, nil
}