-
Notifications
You must be signed in to change notification settings - Fork 0
/
gone.go
60 lines (51 loc) · 1.15 KB
/
gone.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
package gone
import (
zmq "github.com/pebbe/zmq4"
)
type Gone struct {
ip string
lockFactory LockFactory
zmqContext *zmq.Context
server *goneServer
dispatcher *goneDispatcher
}
func (g *Gone) buildLock(id string) (GoneLock, error) {
return g.lockFactory(id, g.ip)
}
func (g *Gone) Close() {
g.server.Close()
g.dispatcher.Close()
}
func (g *Gone) SendData(id string, data []byte) error {
return g.dispatcher.SendData(id, data)
}
func (g *Gone) RunOne(id string, runHandler RunHandler) (*GoneRunner, error) {
runner := g.dispatcher.GetRunner(id)
if runner != nil {
return runner, nil
}
lock, err := g.buildLock(id)
if err != nil {
return nil, err
}
runner = newGoneRunner(id, runHandler, lock, g)
g.dispatcher.AddRunner(id, runner)
return runner, nil
}
func NewGone(ip string, lockFactory LockFactory) (*Gone, error) {
context, err := zmq.NewContext()
if err != nil {
return nil, err
}
server, err := NewGoneServer(ip, context)
if err != nil {
return nil, err
}
g := Gone{}
g.ip = ip
g.lockFactory = lockFactory
g.server = server
g.dispatcher = newDispatcher(server, context)
g.zmqContext = context
return &g, nil
}