-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmq-support.rkt
55 lines (48 loc) · 1.44 KB
/
zmq-support.rkt
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
#lang racket
(provide (all-defined-out))
(require ffi/unsafe
(prefix-in zmq: "../zeromq/net/zmq.rkt"))
;; Creates a context and passes it to the func
;; when the function returns, it closes the context
(define (call-with-context func)
(let ([context (zmq:context 1)])
(dynamic-wind
void
(lambda ()
(func context) (void))
(lambda ()
(zmq:context-close! context)))))
;; Creates a context and passes it to the func
;; when the function returns, it closes the context
(define (call-with-socket context type func )
(let ([socket (zmq:socket context type)])
(dynamic-wind
void
(lambda ()
(func socket) (void))
(lambda ()
(zmq:socket-close! socket)))))
(define (call-with-req-socket context func)
(call-with-socket
context 'REQ
(lambda (socket)
(func socket))))
(define (call-with-rep-socket context func)
(call-with-socket
context 'REP
(lambda (socket)
(func socket))))
;; using zmq_proxy to create shared queue
;;http://api.zeromq.org/3-2:zmq-proxy
(define (call-with-shared-queue context func)
(let ([router-socket (zmq:socket context 'DEALER)]
[dealer-socket (zmq:socket context 'ROUTER)])
(dynamic-wind
void
(lambda ()
(func router-socket dealer-socket)
(zmq:proxy! router-socket dealer-socket)
(void))
(lambda ()
(zmq:socket-close! router-socket)
(zmq:socket-close! dealer-socket)))))