-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
173 lines (147 loc) · 3.18 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
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
package pigato
import (
"encoding/json"
"fmt"
zmq "github.com/pebbe/zmq4"
pgtlib "github.com/prdn/pigato-go/lib"
"log"
"math/rand"
"runtime"
"time"
)
func randSeq() string {
return fmt.Sprintf("%06X%06X", rand.Intn(0x10000), rand.Intn(0x10000))
}
type PigatoClient struct {
broker string
identity string
verbose bool
reqs map[string]PigatoReq
out chan PigatoReq
ctx PigatoCtx
}
type PigatoReq struct {
Reply interface{}
Cb PigatoHandler
Msg []string
}
type PigatoCtx struct {
poller *zmq.Poller
client *zmq.Socket
}
type PigatoHandler func(reply interface{})
func (pcli *PigatoClient) ConnectToBroker() (err error) {
if pcli.ctx.client != nil {
pcli.ctx.client.Close()
pcli.ctx.client = nil
}
pcli.ctx.client, err = zmq.NewSocket(zmq.DEALER)
pcli.identity = randSeq()
pcli.ctx.client.SetIdentity(pcli.identity)
if err != nil {
if pcli.verbose {
log.Println("E: ConnectToBroker() creating socket failed")
}
return
}
pcli.ctx.poller = zmq.NewPoller()
pcli.ctx.poller.Add(pcli.ctx.client, zmq.POLLIN)
if pcli.verbose {
log.Printf("I: connecting to broker at %s...", pcli.broker)
}
err = pcli.ctx.client.Connect(pcli.broker)
if err != nil && pcli.verbose {
log.Println("E: ConnectToBroker() failed to connect to broker", pcli.broker)
}
return
}
func NewPigatoClient(broker string, verbose bool) (pcli *PigatoClient, err error) {
pcli = &PigatoClient{
broker: broker,
verbose: verbose,
}
err = pcli.ConnectToBroker()
runtime.SetFinalizer(pcli, (*PigatoClient).Close)
pcli.reqs = make(map[string]PigatoReq)
pcli.out = make(chan PigatoReq, 100)
go func() {
for {
pcli.Flush()
time.Sleep(time.Millisecond * 10)
}
}()
return
}
func (pcli *PigatoClient) Close() (err error) {
if pcli.ctx.client != nil {
err = pcli.ctx.client.Close()
pcli.ctx.client = nil
}
return
}
func (pcli *PigatoClient) Request(service string, pld interface{}, rep interface{}, cb PigatoHandler) {
req := make([]string, 6, 6)
rid := pcli.identity + randSeq()
val, _ := json.Marshal(pld)
req[5] = ""
req[4] = string(val)
req[3] = rid
req[2] = service
req[1] = pgtlib.W_REQUEST
req[0] = pgtlib.C_CLIENT
if pcli.verbose {
log.Printf("I: send request to '%s' service: %q\n", service, val)
}
preq := PigatoReq{Cb: cb, Msg: req, Reply: rep}
pcli.reqs[rid] = preq
pcli.out <- preq
}
func (pcli *PigatoClient) Flush() {
for {
evts, _ := pcli.ctx.client.GetEvents()
if evts&zmq.POLLIN != 0 {
msg := []string{}
msg, err := pcli.ctx.client.RecvMessage(0)
if msg == nil {
break
}
if err != nil {
log.Printf("%s", err)
break
}
if len(msg) < 3 {
break
}
if msg[0] != pgtlib.C_CLIENT {
break
}
rid := msg[3]
if req, exists := pcli.reqs[rid]; exists {
delete(pcli.reqs, rid)
if len(msg) >= 4 {
_ = json.Unmarshal([]byte(msg[5]), req.Reply)
}
req.Cb(req.Reply)
}
} else {
break
}
}
var served bool
served = false
for {
evts, _ := pcli.ctx.client.GetEvents()
if evts&zmq.POLLOUT != 0 {
select {
case req := <-pcli.out:
served = true
_, _ = pcli.ctx.client.SendMessage(req.Msg)
default:
served = false
}
}
if !served {
break
}
}
}