-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.go
185 lines (171 loc) · 4.19 KB
/
service.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
package bw2bind
import (
"fmt"
"os"
"strings"
"sync"
"time"
)
// Reregister interfaces/services every ten seconds
const RegistrationInterval = 10
func handleErr(err error) {
if err != nil {
fmt.Println("Service encountered error: ", err)
os.Exit(1)
}
}
type Service struct {
cl *BW2Client
name string
baseuri string
ifaces []*Interface
mu *sync.Mutex
errorHandler func(error)
}
type Interface struct {
svc *Service
prefix string
name string
auto bool
last time.Time
}
func (cl *BW2Client) RegisterService(baseuri string, name string) *Service {
baseuri = strings.TrimSuffix(baseuri, "/")
rv := &Service{cl: cl, baseuri: baseuri, name: name, mu: &sync.Mutex{}}
go rv.registerLoop()
return rv
}
func (cl *BW2Client) RegisterServiceNoHb(baseuri string, name string) *Service {
baseuri = strings.TrimSuffix(baseuri, "/")
rv := &Service{cl: cl, baseuri: baseuri, name: name, mu: &sync.Mutex{}}
return rv
}
func (s *Service) registerLoop() {
//Initial delay is lower
time.Sleep(1 * time.Second)
for {
if err := s.cl.SetMetadata(s.baseuri+"/"+s.name, "lastalive", time.Now().String()); err != nil {
if s.errorHandler != nil {
s.errorHandler(err)
} else {
handleErr(err)
}
} else {
s.mu.Lock()
for _, i := range s.ifaces {
if i.auto {
if err := i.updateRegistration(); err != nil {
if s.errorHandler != nil {
s.errorHandler(err)
} else {
handleErr(err)
}
break
}
}
}
s.mu.Unlock()
}
time.Sleep(RegistrationInterval * time.Second)
}
}
func (s *Service) FullURI() string {
return s.baseuri + "/" + s.name
}
func (s *Service) RegisterInterface(prefix string, name string) *Interface {
prefix = strings.TrimSuffix(prefix, "/")
prefix = strings.TrimPrefix(prefix, "/")
rv := &Interface{
svc: s,
prefix: prefix,
name: name,
auto: true,
}
s.mu.Lock()
s.ifaces = append(s.ifaces, rv)
s.mu.Unlock()
return rv
}
// Registers an interface that will only publish a heartbeat when the interface
// is published on
func (s *Service) RegisterInterfaceHeartbeatOnPub(prefix string, name string) *Interface {
prefix = strings.TrimSuffix(prefix, "/")
prefix = strings.TrimPrefix(prefix, "/")
rv := &Interface{
svc: s,
prefix: prefix,
name: name,
auto: false,
}
s.mu.Lock()
s.ifaces = append(s.ifaces, rv)
s.mu.Unlock()
return rv
}
func (s *Service) SetMetadata(key, val string) error {
return s.cl.SetMetadata(s.FullURI(), key, val)
}
func (s *Service) SetErrorHandler(f func(error)) {
s.errorHandler = f
}
func (ifc *Interface) FullURI() string {
return ifc.svc.FullURI() + "/" + ifc.prefix + "/" + ifc.name
}
func (ifc *Interface) SignalURI(signal string) string {
return ifc.FullURI() + "/signal/" + signal
}
func (ifc *Interface) SlotURI(slot string) string {
return ifc.FullURI() + "/slot/" + slot
}
func (ifc *Interface) SetMetadata(key, val string) error {
return ifc.svc.cl.SetMetadata(ifc.FullURI(), key, val)
}
func (ifc *Interface) GetMetadataKey(key string) (string, error) {
dat, _, err := ifc.svc.cl.GetMetadataKey(ifc.FullURI(), key)
return dat.Value, err
}
func (ifc *Interface) updateRegistration() error {
return ifc.SetMetadata("lastalive", time.Now().String())
}
func (ifc *Interface) PublishSignal(signal string, poz ...PayloadObject) error {
if !ifc.auto && time.Now().Sub(ifc.last) > RegistrationInterval*time.Second {
ifc.updateRegistration()
ifc.last = time.Now()
}
return ifc.svc.cl.Publish(&PublishParams{
URI: ifc.SignalURI(signal),
AutoChain: true,
PayloadObjects: poz,
Persist: true,
})
}
func (ifc *Interface) SubscribeSlot(slot string, cb func(*SimpleMessage)) error {
rc, err := ifc.svc.cl.Subscribe(&SubscribeParams{
URI: ifc.SlotURI(slot),
AutoChain: true,
})
if err != nil {
return err
}
go func() {
for sm := range rc {
cb(sm)
}
}()
return nil
}
func (ifc *Interface) SubscribeSlotH(slot string, cb func(*SimpleMessage)) (string, error) {
rc, handle, err := ifc.svc.cl.SubscribeH(&SubscribeParams{
URI: ifc.SlotURI(slot),
AutoChain: true,
})
if err != nil {
return "", err
}
go func() {
for sm := range rc {
cb(sm)
}
}()
return handle, nil
}