-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
235 lines (194 loc) · 4.94 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// A gozmq server that either responds to requests (REP) with static
// data or makes repeated requests (REQ) with static data to a
// specified ZMQ server.
package main
import (
"flag"
zmq "github.com/alecthomas/gozmq"
poller "github.com/tchap/gozmq-poller"
"log"
"os"
"os/signal"
"time"
)
func main() {
var (
socketUrl string
socketType string
replyText string
verbose bool
delay int
nreplies int
)
flag.StringVar(&socketUrl, "socket", "", "the mock server socket: ipc:///tmp/foo.sock, tcp://1.2.3.4:9999, etc")
flag.StringVar(&socketType, "type", "REP", "the type of socket (currently supported: REP (default), REQ)")
flag.StringVar(&replyText, "reply", "", "the text to return whenever any request is made")
flag.BoolVar(&verbose, "verbose", false, "set true to log all input (REP) or output (REQ)")
flag.IntVar(&delay, "delay", 0, "the number of milliseconds to sleep between messages")
flag.IntVar(&nreplies, "n", 0, "the number of replies or requests to send (default: 0 = unlimited)")
flag.Parse()
if socketUrl == "" {
log.Println("main", "you must specify a socket")
flag.PrintDefaults()
os.Exit(1)
}
if replyText == "" {
log.Println("main", "you must specify reply text")
flag.PrintDefaults()
os.Exit(1)
}
if nreplies < 0 {
nreplies = 0
}
// OK, we're all set. Get ready to start the server and to wait for
// a signal to end it.
done := make(chan bool, 1)
finishedClosing := make(chan bool, 1)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Println("main", "caught signal, going down")
done <- true
<-done
os.Exit(0)
}()
var err error
switch socketType {
case "REP":
err = startServerRep(socketUrl, replyText, verbose, delay, done, finishedClosing, nreplies)
case "REQ":
err = startServerReq(socketUrl, replyText, verbose, delay, done, finishedClosing, nreplies)
default:
log.Println("main", "only supports REP and REQ")
flag.PrintDefaults()
os.Exit(1)
}
if err != nil {
log.Fatalln("startServer", err.Error())
os.Exit(1)
}
<-finishedClosing
}
type zmqMessage struct {
Payload []byte
}
// Start a simple "request" sender. This just sends the same request
// payload repeatedly to a server at the specified socketUrl until
// a value is passed to the done channel or the number of requests
// exceeds nrequests. (nrequests = 0 means unlimited requests).
func startServerReq(socketUrl string, payload string, verbose bool, delay int, done chan bool, finishedClosing chan bool, nrequests int) (err error) {
zmqContext, err := zmq.NewContext()
if err != nil {
return
}
zmqSocket, err := zmqContext.NewSocket(zmq.REQ)
if err != nil {
return
}
err = zmqSocket.Connect(socketUrl)
if err != nil {
return
}
go func() {
n := 0
for {
select {
case <-done:
zmqSocket.Close()
zmqContext.Close()
finishedClosing <- true
return
default:
err := zmqSocket.Send([]byte(payload), 0)
if err != nil {
log.Println("zmqSocket.Send", err.Error())
done <- true
break
}
if verbose {
log.Println("> request", string(payload))
}
if delay > 0 {
time.Sleep(time.Duration(delay) * time.Millisecond)
}
reply, err := zmqSocket.Recv(0)
if err != nil {
log.Println("zmqSocket.Recv", err.Error())
done <- true
break
}
if verbose {
log.Println("< reply", string(reply))
}
n++
if nrequests > 0 && n >= nrequests {
done <- true
}
}
}
}()
return
}
// Start a simple "reply" server. This responds to every incoming
// request at the specified socket with the reply payload until
// a value is passed to the done channel or there have been at least
// nreplies sent.
func startServerRep(socketUrl string, replyText string, verbose bool, delay int, done chan bool, finishedClosing chan bool, nreplies int) (err error) {
zmqContext, err := zmq.NewContext()
if err != nil {
return
}
zmqSocket, err := zmqContext.NewSocket(zmq.REP)
if err != nil {
return
}
err = zmqSocket.Bind(socketUrl)
if err != nil {
return
}
pf := poller.NewFactory(zmqContext)
zmqPoller, err := pf.NewPoller(zmq.PollItems{
0: zmq.PollItem{Socket: zmqSocket, Events: zmq.POLLIN},
})
if err != nil {
return
}
n := 0
go func() {
pollCh := zmqPoller.Poll()
for {
select {
case <-done:
zmqPoller.Close()
zmqSocket.Close()
zmqContext.Close()
finishedClosing <- true
return
case <-pollCh:
inputPayload, err := zmqSocket.Recv(0)
if err != nil {
log.Println("zmqSocket.Recv", err.Error())
done <- true
break
}
if verbose {
log.Println("< request", string(inputPayload))
}
if delay > 0 {
time.Sleep(time.Duration(delay) * time.Millisecond)
}
zmqSocket.Send([]byte(replyText), 0)
if verbose {
log.Println("> reply", string(replyText))
}
n++
if nreplies > 0 && n >= nreplies {
done <- true
}
pollCh = zmqPoller.Poll()
}
}
}()
return
}