Check-in [bac484c8c0]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added nanomsg
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: bac484c8c0193d3d402b0815ab57ca999030015c
User & Date: ovenpasta@pizzahack.eu 2016-08-17 06:26:37
Context
2016-08-17
07:41
nanomsg error handling and other fixes check-in: 590cb5e905 user: ovenpasta@pizzahack.eu tags: trunk
06:26
added nanomsg check-in: bac484c8c0 user: ovenpasta@pizzahack.eu tags: trunk
2016-07-24
21:32
fixed some tests check-in: 6cb462881c user: ovenpasta@pizzahack.eu tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added nanomsg.sls.











































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!r6rs
(library 
 (nanomsg) 
 (export 
  nanomsg-library-init
  nn-error nn-bind nn-send nn-recv nn-connect nn-poll nn-close
  nn-socket nn-assert nn-shutdown nn-freemsg nn-recvmsg nn-sendmsg
  nn-strerror nn-setsockopt nn-setsockopt/int
  nn-getsockopt nn-get-statistic nn-device nn-symbol

  NN_MSG

  NN_SOCKADDR_MAX
  NN_SOL_SOCKET
  NN_LINGER
  NN_SNDBUF
  NN_RCVBUF
  NN_SNDTIMEO
  NN_RCVTIMEO
  NN_RECONNECT_IVL
  NN_RECONNECT_IVL_MAX
  NN_SNDPRIO
  NN_RCVPRIO
  NN_SNDFD
  NN_RCVFD
  NN_DOMAIN
  NN_PROTOCOL
  NN_IPV4ONLY
  NN_SOCKET_NAME
  NN_RCVMAXSIZE
  NN_MAXTTL
  NN_DONTWAIT
  NN_POLLIN
  NN_POLLOUT
  NN_STAT_ESTABLISHED_CONNECTIONS
  NN_STAT_ACCEPTED_CONNECTIONS
  NN_STAT_DROPPED_CONNECTIONS
  NN_STAT_BROKEN_CONNECTIONS
  NN_STAT_CONNECT_ERRORS
  NN_STAT_BIND_ERRORS
  NN_STAT_ACCEPT_ERRORS
  NN_STAT_CURRENT_CONNECTIONS
  NN_STAT_INPROGRESS_CONNECTIONS
  NN_STAT_CURRENT_EP_ERRORS
  NN_STAT_MESSAGES_SENT
  NN_STAT_MESSAGES_RECEIVED
  NN_STAT_BYTES_SENT
  NN_STAT_BYTES_RECEIVED
  NN_STAT_CURRENT_SND_PRIORITY
					;NN_PROTO_PAIR
  NN_PAIR
					;NN_PROTO_PUBSUB
  NN_PUB
  NN_SUB
  NN_SUB_SUBSCRIBE
  NN_SUB_UNSUBSCRIBE
					;NN_PROTO_REQREP
  NN_REQ
  NN_REP
  NN_REQ_RESEND_IVL
  NN_TCP
  NN_TCP_NODELAY
					;NN_PROTO_PIPELINE
  NN_PUSH
  NN_PULL
					;NN_PROTO_BUS
					;NN_PROTO_SURVEY
  NN_SURVEYOR
  NN_RESPONDENT
  NN_SURVEYOR_DEADLINE
  NN_INPROC
  NN_IPC
					;NN_IPC_SEC_ATTR
					;NN_IPC_OUTBUFSZ
					;NN_IPC_INBUFSZ
  NN_WS
  NN_WS_MSG_TYPE
  NN_WS_MSG_TYPE_TEXT
  NN_WS_MSG_TYPE_BINARY
  NN_BUS
  
  NN_NS_NAMESPACE
  NN_NS_VERSION
  NN_NS_DOMAIN
  NN_NS_TRANSPORT
  NN_NS_PROTOCOL
  NN_NS_OPTION_LEVEL
  NN_NS_SOCKET_OPTION
  NN_NS_TRANSPORT_OPTION
  NN_NS_OPTION_TYPE
  NN_NS_OPTION_UNIT
  NN_NS_FLAG
  NN_NS_ERROR
  NN_NS_LIMIT
  NN_NS_EVENT
  NN_NS_STATISTIC
  NN_TYPE_NONE
  NN_TYPE_INT
  NN_TYPE_STR
  NN_UNIT_NONE
  NN_UNIT_BYTES
  NN_UNIT_MILLISECONDS
  NN_UNIT_PRIORITY
  NN_UNIT_BOOLEAN
  NN_UNIT_MESSAGES
  NN_UNIT_COUNTER

  AF_SP AF_SP_RAW

  NN_NOTSUP
  NN_EPROTONOSUPPORT
  NN_ENOBUFS
  NN_ENETDOWN
  NN_EADDRINUSE
  NN_EADDRNOTAVAIL
  NN_ECONNREFUSED
  NN_EINPROGRESS
  NN_ENOTSOCK
  NN_EAFNOSUPPORT
  NN_EPROTO
  NN_EAGAIN
  NN_EBADF
  NN_EINVAL
  NN_EMFILE
  NN_EFAULT
  NN_EACCES
  NN_EACCESS
  NN_ENETRESET
  NN_ENETUNREACH
  NN_EHOSTUNREACH
  NN_ENOTCONN
  NN_EMSGSIZE
  NN_ETIMEDOUT
  NN_ECONNABORTED
  NN_ECONNRESET
  NN_ENOPROTOOPT
  NN_EISCONN
  NN_ESOCKTNOSUPPORT
  NN_ETERM
  NN_EFSM


  EADDRINUSE
  EADDRNOTAVAIL
  EAFNOSUPPORT
  EAGAIN
  EBADF
  ECONNREFUSED
  EFAULT
  EFSM
  EINPROGRESS
  EINTR
  EINVAL
  EMFILE
  ENAMETOOLONG
  ENETDOWN
  ENOBUFS
  ENODEV
  ENOMEM
  ENOPROTOOPT
  ENOTSOCK
  ENOTSUP
  EPROTO
  EPROTONOSUPPORT
  ETERM
  ETIMEDOUT
  EACCES
  ECONNABORTED
  ECONNRESET
  EHOSTUNREACH
  EMSGSIZE
  ENETRESET
  ENETUNREACH
  ENOTCONN

  NN_VERSION_CURRENT
  NN_VERSION_REVISION
  NN_VERSION_AGE
  ) ;export
 (import (ffi-utils) (chezscheme))
 
 (define (nanomsg-library-init . t)
   (load-shared-object (if (null? t) "libnanomsg.so" (car t))))

 (define-syntax define-nn-func
   (lambda (x)
     (syntax-case x ()
       [(_ ret-type name ((arg-name arg-type) ...) c-name) 
	(with-syntax 
	 ([function-ftype 
	   (datum->syntax #'name 
			  (string->symbol 
			   (string-append 
			    (symbol->string 
			     (syntax->datum #'name)) "-ft")))] )
	 #`(begin
	     (define (name arg-name ...) 
	       (define-ftype function-ftype (function (arg-type ...) ret-type))
	       (let* ([function-fptr  (make-ftype-pointer function-ftype c-name)]
		      [function       (ftype-ref function-ftype () function-fptr)])
		 (let ([result (function arg-name ...)])
		   result)))))])))
 
 
 (define-syntax nn-error
   (syntax-rules ()
     ((_ name n )
      (define-syntax name (identifier-syntax (+ 156384712 n))))))
 
 (nn-error  NN_NOTSUP          1)
 (nn-error  NN_EPROTONOSUPPORT 2)
 (nn-error  NN_ENOBUFS         3)
 (nn-error  NN_ENETDOWN        4)
 (nn-error  NN_EADDRINUSE      5)
 (nn-error  NN_EADDRNOTAVAIL   6)
 (nn-error  NN_ECONNREFUSED    7)
 (nn-error  NN_EINPROGRESS     8)
 (nn-error  NN_ENOTSOCK        9)
 (nn-error  NN_EAFNOSUPPORT   10)
 (nn-error  NN_EPROTO         11)
 (nn-error  NN_EAGAIN         12)
 (nn-error  NN_EBADF          13)
 (nn-error  NN_EINVAL         14)
 (nn-error  NN_EMFILE         15)
 (nn-error  NN_EFAULT         16)
 (nn-error  NN_EACCES         17)
 (nn-error  NN_EACCESS        17)
 (nn-error  NN_ENETRESET      18)
 (nn-error  NN_ENETUNREACH    19)
 (nn-error  NN_EHOSTUNREACH   20)
 (nn-error  NN_ENOTCONN       21)
 (nn-error  NN_EMSGSIZE       22)
 (nn-error  NN_ETIMEDOUT      23)
 (nn-error  NN_ECONNABORTED   24)
 (nn-error  NN_ECONNRESET     25)
 (nn-error  NN_ENOPROTOOPT    26)
 (nn-error  NN_EISCONN        27)
 (nn-error  NN_ESOCKTNOSUPPORT 28)
 (nn-error  NN_ETERM          53)
 (nn-error  NN_EFSM           54)

 (define NN_MSG -1)
 (define-syntax nn-define
   (syntax-rules ()
     ((_ name n) 
      (define-syntax name (identifier-syntax n)))))

 (define-nn-func int nn-errno () "nn_errno")

 (define-nn-func int nn-socket ((domain int) (protocol int))
   "nn_socket")

 (define-nn-func string nn-symbol ((index int) (value (* int)))
   "nn_symbol")

 ;; THIS WAS/CAN BE USED TO GENERATE THE nn-define stuff in nanomsg/symbols.ss
 ;; TODO: can this be a macro that we call after loading the library?
 ;; but still. it will not be possible to export these because we cannot
 ;; dynamically add export entries? 

 (define (nn-gen-symbols)
   (define ptr (make-ftype-pointer int (foreign-alloc (ftype-sizeof int))))
   (let loop ([i 0])
     (let ([sym (nn-symbol i ptr)])
       (if sym
	   (begin
	     (printf "(nn-define ~d ~d)~n" sym (ftype-ref int () ptr))
	     (loop (+ 1 i))))))
   (foreign-free (ftype-pointer-address ptr)))

 (include "nanomsg/symbols.ss")

 (define-nn-func int nn-close ((s int)) "nn_close")

 (define-nn-func int nn-setsockopt ((s int) (level int) (option int)
				    (optval void*) (optval-len size_t))
   "nn_setsockopt")


(define (nn-setsockopt/int s level option optval)
  (define o #f)
  (define r #f)
  (dynamic-wind 
      (lambda ()
	(set! o (make-ftype-pointer int (foreign-alloc (ftype-sizeof int)))))
      (lambda () 
	(ftype-set! int () o optval)
	(set! r (nn-setsockopt s level option (ftype-pointer-address o)
			       (ftype-sizeof int))))
      (lambda ()
	(if o (foreign-free (ftype-pointer-address o)))))
  r)

 (define-nn-func int nn-getsockopt ((s int) (level int) (option int)
				    (optval void*) (optval-len (* size_t)))
   "nn_getsockopt")

 (define-nn-func int nn-bind ((s int) (addr string)) "nn_bind")

 (define-nn-func int nn-connect ((s int) (addr string)) "nn_connect")

 (define-nn-func int nn-shutdown ((s int) (how int)) "nn_shutdown")

 (define-nn-func int nn-send ((s int) (buf u8*) (len size_t) (flags int))
   "nn_send")

 (define-nn-func int nn-recv% ((s int) (buf void*) (len size_t) (flags int))
   "nn_recv")

 (define (char*->string fptr . bytes)
   (let f ([i 0])
     (let ([c (ftype-ref char () fptr i)])
       (if (or (char=? c #\nul) (and bytes (>= (+ 1 i) (car bytes))))
	   (make-string i)
	   (let ([str (f (fx+ i 1))])
	     (string-set! str i c)
	     str)))))

 (define (char*->bytevector fptr bytes)
   (let f ([i 0])
     (let ([c (ftype-ref char () fptr i)])
       (if (>= i  bytes)
	   (make-bytevector i)
	   (let ([bb (f (fx+ i 1))])
	     (bytevector-u8-set! bb i (char->integer c))
	     bb)))))

 (define-syntax cast
   (syntax-rules ()
     [(_ ftype fptr)
      (make-ftype-pointer ftype
			  (ftype-pointer-address fptr))]))

 (define (nn-recv s buf len flags)
   (define b #f)
   (define r #f)
   (dynamic-wind 
       (lambda ()
	 (set! b (make-ftype-pointer void* (foreign-alloc (ftype-sizeof void*))))
	 (set! r (nn-recv% s (ftype-pointer-address b) len flags)))
       (lambda ()
	 (if (> r 0)
	     (let ([c (make-ftype-pointer char (ftype-ref void* () b))])
	       (set-box! buf (char*->bytevector c r)))
	     (set-box! buf #f)))
       (lambda ()
	 (if (> r 0)
	     (nn-freemsg (ftype-ref void* () b)))
	 (if b (foreign-free (ftype-pointer-address b)))))
   r)

 (define-nn-func int nn-sendmsg ((s int) (msghdr (* nn-msghdr)) (flags int))
   "nn_sendmsg")

 (define-nn-func int nn-freemsg ((msg void*)) "nn_freemsg")

 (define-ftype nn-iovec
   (struct 
    (iov_base void*) 
    (iov_len size_t)))

 (define-ftype nn-msghdr
   (struct
    (msg_iov (* nn-iovec))
    (msg_iovlen int) 
    (msg_control void*)
    (msg_controllen size_t)))

 (define-ftype nn-cmsghdr
   (struct 
    (cmsg_len size_t) 
    (cmsg_level int)
    (cmsg_type int)))

 (define-nn-func int nn-recvmsg ((s int) (msghdr (* nn-cmsghdr)) (flags int))
   "nn_recvmsg")

 (define-ftype nn-pollfd 
   (struct (fd int) (events short) (revents short)))

 (define-nn-func int nn-poll ((fds (* nn-pollfd)) (nfds int) (timeout int))
   "nn_poll")


 (define-nn-func int nn-device ((s1 int) (s2 int)) "nn_device")

 (define-nn-func int nn-get-statistic ((s int) (stat int)) 
   "nn_get_statistic")

 (define-ftype nn_req_handle 
   (union
    (i int)
    (ptr void*)))

 (define-flags nn_protocol
   (pair NN_PAIR)
   (pub  NN_PUB)  (sub  NN_SUB)
   (pull NN_PULL) (push NN_PUSH)
   (req  NN_REQ)  (rep  NN_REP)
   (surveyor NN_SURVEYOR)  (respondent NN_RESPONDENT)
   (bus NN_BUS))

 ;; nanomsg domain (AF_SP)
 (define-flags nn_domain
   (sp AF_SP)
   (raw AF_SP_RAW))

 ;; ==================== socket flags

 (define-nn-func string nn-strerror ((errno int)) "nn_strerror")

 ;; let val pass unless it is negative, in which case gulp with the nn
 ;; error-string. on EAGAIN, return #f.
 (define (nn-assert val)
   (if (< val 0)
       (if (= (nn-errno)
	      NN_EAGAIN
	      #f ;; signal EGAIN with #f, other errors will throw
	      (error (nn-strerror (nn-errno)) val))
	   val)))
 );;library

Added nanomsg/bus.















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /usr/bin/env scheme-script
; -*- mode: scheme -*-

; Getting Started with 'nanomsg'
; Survey
; from https://github.com/dysinger/nanomsg-examples#bus
#!chezscheme

(import (nanomsg) (chezscheme))

(nanomsg-library-init)

(define (sleep-s sec)
  (sleep (make-time 'time-duration 0 sec)))


(define (node argc argv)
  (define sock #f)
  (define r #f)
  (dynamic-wind 
      (lambda ()
	(set! sock (nn-socket AF_SP NN_BUS))
	(assert (>= sock 0)))
      (lambda ()
	(assert (>= (nn-bind sock (cadr argv)) 0))
	(sleep-s 1)
	(when (>= argc 2)
	      (let loop ([x 2])
		(define n (nn-connect sock (list-ref argv x)))
		(assert (>= n 0))
		(if (< (+ x 1) argc)
		    (loop (+ x 1)))))
	(sleep-s 1)
	
	(assert (>= (nn-setsockopt/int sock NN_SOL_SOCKET NN_RCVTIMEO 100) 0))

	(let* ([sz-n (string-length (car argv))]
	       [n (car argv)])
	  (printf "~d: SENDING ~d ONTO BUS~n" n n)
	  (assert (= (nn-send sock (string->utf8 n) sz-n 0) sz-n)))
	(let loop ()
	  (let* ([buf (box #t)]
		 [recv (nn-recv sock buf NN_MSG 0)])
	    (when (>= recv 0)
		  (printf "~d RECEIVED ~d FROM BUS~n" 
			  (car argv) (utf8->string (unbox buf))))
	    (loop))))
      (lambda ()
	(if sock (set! r (nn-shutdown sock 0))))
  r)

(define argv (command-line-arguments))
(define argc (length argv))

(cond
 [(and (>= argc 2) 
       (node argc argv))]
 [else
  (printf "Usage: bus <NODE_NAME> <URL> <URL> ...'~n")])



#!eof

./bus node0 ipc:///tmp/node0.ipc ipc:///tmp/node1.ipc ipc:///tmp/node2.ipc & node0=$!
./bus node1 ipc:///tmp/node1.ipc ipc:///tmp/node2.ipc ipc:///tmp/node3.ipc & node1=$!
./bus node2 ipc:///tmp/node2.ipc ipc:///tmp/node3.ipc & node2=$!
./bus node3 ipc:///tmp/node3.ipc ipc:///tmp/node0.ipc & node3=$!
sleep 5
kill $node0 $node1 $node2 $node3

Added nanomsg/pair.



















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /usr/bin/env scheme-script
; -*- mode: scheme -*-

; Getting Started with 'nanomsg'
; Pair
; from https://github.com/dysinger/nanomsg-examples#pair

#!chezscheme

(import (nanomsg) (chezscheme))

(nanomsg-library-init)

(define node0-name "node0")
(define node1-name "node1")

(define (send-name sock name)
 (define sz-n (string-length name))
 (printf "~s: SENDING '~s'~n" name name)
 (nn-send sock (string->utf8 name) sz-n 0))

(define (recv-name sock name)
  (let*  ([buf (box #t)]
	  [result (nn-recv sock buf NN_MSG 0)])
    (when (> result 0)
	  (printf "~d: RECEIVED '~d'~n" name (utf8->string (unbox buf))))
    result))

(define (sleep-s sec)
  (sleep (make-time 'time-duration 0 sec)))
	

(define (send-recv sock name)
  (assert (>= (nn-setsockopt/int sock NN_SOL_SOCKET NN_RCVTIMEO 100) 0))
 
  (let loop ()
    (recv-name sock name)
    (sleep-s 1)
    (send-name sock name)
    (loop)))
	
(define (node0 url)
  (define sock (nn-socket AF_SP NN_PAIR))
  (assert (>= sock 0))
  (assert (>= (nn-bind sock url) 0))
  (send-recv sock node0-name)
  (nn-shutdown sock 0))

(define (node1 url)
  (define sock (nn-socket AF_SP NN_PAIR))
  (assert (>= sock 0))
  (assert (>= (nn-connect sock url) 0))
  (send-recv sock node1-name)
  (nn-shutdown sock 0))

(define argv (command-line-arguments))
(define argc (length argv))

(cond
 [(and (> argc 1) (string=? node0-name (car argv)))
  (node0 (cadr argv))]
 [(and (> argc 1) (string=? node1-name (car argv)))
  (node1 (cadr argv))]
 [else
  (printf "Usage: pair ~d|~d <URL> <ARG> ...'~n" node0-name node1-name)])


#!eof

./pair node0 ipc:///tmp/pair.ipc & node0=$!
./pair node1 ipc:///tmp/pair.ipc & node1=$!
sleep 3
kill $node0 $node1

Added nanomsg/pipeline.

















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /usr/bin/env scheme-script
; -*- mode: scheme -*-

; Getting Started with 'nanomsg'
; Pipeline (A One-Way Pipe)
; from https://github.com/dysinger/nanomsg-examples#pipeline

#!chezscheme

(import (nanomsg) (chezscheme))

(nanomsg-library-init)

(define node0-name "node0")
(define node1-name "node1")

(define (node0 url)
  (define sock (nn-socket AF_SP NN_PULL))
  (assert (>= sock 0))
  (assert (>= (nn-bind sock url) 0))
  (let loop ()
    (let ([buf (box #t)])
      (define bytes (nn-recv sock buf NN_MSG 0))
      (assert (>= bytes 0))
      (printf "NODE0: RECEIVED '~d'~n" (utf8->string (unbox buf))))
    (loop)))


(define (node1 url msg)
  (define sz-msg (string-length msg))
  (define sock (nn-socket AF_SP NN_PUSH))
  (assert (>= sock 0))
  (assert (>= (nn-connect sock url) 0))
  (printf "NODE1: SENDING '~d'~n" msg)
  (let ([bytes (nn-send sock (string->utf8 msg) sz-msg 0)])
    (assert (= bytes sz-msg))
    (nn-shutdown sock 0)))

(define argv (command-line-arguments))
(define argc (length argv))

(cond
 [(and (> argc 1) (string=? node0-name (car argv)))
  (node0 (cadr argv))]
 [(and (> argc 2) (string=? node1-name (car argv)))
  (node1 (cadr argv) (caddr argv))]
 [else
  (printf "Usage: pipeline ~d|~d <URL> <ARG> ...'~n" node0-name node1-name)])


#!eof

./pipeline node0 ipc:///tmp/pipeline.ipc & node0=$! && sleep 1
./pipeline node1 ipc:///tmp/pipeline.ipc "Hello, World!"
./pipeline node1 ipc:///tmp/pipeline.ipc "Goodbye."
kill $node0

Added nanomsg/pubsub.





































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /usr/bin/env scheme-script
; -*- mode: scheme -*-

; Getting Started with 'nanomsg'
; Pub/Sub
; from https://github.com/dysinger/nanomsg-examples#code-3
#!chezscheme

(import (nanomsg) (chezscheme))

(nanomsg-library-init)

(define server-name "server")
(define client-name "client")

(define date date-and-time)

(define (sleep-s sec)
  (sleep (make-time 'time-duration 0 sec)))

(define (server url)
  (define sock (nn-socket AF_SP NN_PUB))
  (assert (>= sock 0))
  (assert (>= (nn-bind sock url) 0))
  (let loop ()
    (let* ([d (date)]
	   [sz-d (string-length d)])
      (printf "SERVER: PUBLISHING DATE ~d~n" d)
      (assert (= (nn-send sock (string->utf8 d) sz-d 0) sz-d))
      (sleep-s 1)
      (loop)))
  (nn-shutdown sock 0))

(define (client url name)
  (define sock (nn-socket AF_SP NN_SUB))
  (assert (>= sock 0))
  (assert (>= (nn-setsockopt sock NN_SUB NN_SUB_SUBSCRIBE 0 0)))
  (assert (>= (nn-connect sock url) 0))
  (let loop ()
    (let* ([buf (box #t)]
	   [bytes (nn-recv sock buf NN_MSG 0)])
      (assert (>= bytes 0))
      (printf "CLIENT (~d): RECEIVED ~d~n" name (utf8->string (unbox buf)))
      (loop)))
  (nn-shutdown sock 0))

(define argv (command-line-arguments))
(define argc (length argv))

(cond
 [(and (> argc 1) (string=? server-name (car argv)))
  (server (cadr argv))]
 [(and (> argc 2) (string=? client-name (car argv)))
  (client (cadr argv) (caddr argv))]
 [else
  (printf "Usage: pubsub ~d|~d <URL> <ARG> ...'~n" server-name client-name)])


#!eof

./pubsub server ipc:///tmp/pubsub.ipc & server=$! && sleep 1
./pubsub client ipc:///tmp/pubsub.ipc client0 & client0=$!
./pubsub client ipc:///tmp/pubsub.ipc client1 & client1=$!
./pubsub client ipc:///tmp/pubsub.ipc client2 & client2=$!
sleep 5
kill $server $client0 $client1 $client2

Added nanomsg/reqrep.











































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /usr/bin/env scheme-script
; -*- mode: scheme -*-

; Getting Started with 'nanomsg'
; Request/Reply (I ask, you answer)
; from https://github.com/dysinger/nanomsg-examples#requestreply

#!chezscheme

(import (nanomsg) (chezscheme))

(nanomsg-library-init)

(define node0-name "node0")
(define node1-name "node1")
(define date-name "DATE")

(define date date-and-time)

(define (node0 url)
  (define sz-date (string-length date-name))
  (define sock (nn-socket AF_SP NN_REP))
  (assert (>= sock 0))
  (assert (>= (nn-bind sock url) 0))
  (let loop ()
    (let ([buf (box #t)])
      (define bytes (nn-recv sock buf NN_MSG 0))
      (assert (>= bytes 0))
      (when (string=? date-name (utf8->string (unbox buf)))
	    (let* ([d (date)]
		   [sz-d (string-length d)])
	      (printf "NODE0: RECEIVED DATE REQUEST~n")
	      (printf "NODE0: SENDING DATE ~d~n" d)
	      (assert (= (nn-send sock (string->utf8 d) sz-d 0)
			 sz-d))))
      (loop)))
  (nn-shutdown sock 0))

(define (node1 url)
  (define sz-date (string-length date-name))
  (define sock (nn-socket AF_SP NN_REQ))
  (assert (>= sock 0))
  (assert (>= (nn-connect sock url) 0))
  (printf "NODE1: SENDING DATE REQUEST '~d'~n" date-name)
  (let ([bytes (nn-send sock (string->utf8 date-name) sz-date 0)]
	[buf (box #t)])
    (assert (= bytes sz-date))
    (let ([bytes (nn-recv sock buf NN_MSG 0)])
      (assert (>= bytes 0))
      (printf "NODE1: RECEIVED DATE ~d~n" (utf8->string (unbox buf)))))
  (nn-shutdown sock 0))

(define argv (command-line-arguments))
(define argc (length argv))

(cond
 [(and (> argc 1) (string=? node0-name (car argv)))
  (node0 (cadr argv))]
 [(and (> argc 1) (string=? node1-name (car argv)))
  (node1 (cadr argv))]
 [else
  (printf "Usage: reqrep ~d|~d <URL> <ARG> ...'~n" node0-name node1-name)])


#!eof

./reqrep node0 ipc:///tmp/reqrep.ipc & node0=$! && sleep 1
./reqrep node1 ipc:///tmp/reqrep.ipc
kill $node0

Added nanomsg/survey.





















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /usr/bin/env scheme-script
; -*- mode: scheme -*-

; Getting Started with 'nanomsg'
; Survey
; from https://github.com/dysinger/nanomsg-examples#survey
#!chezscheme

(import (nanomsg) (chezscheme))

(nanomsg-library-init)

(define server-name "server")
(define client-name "client")
(define date-name "DATE")

(define date date-and-time)

(define (sleep-s sec)
  (sleep (make-time 'time-duration 0 sec)))

(define (server url)
  (define sock #f)
  (define r #f)
  (dynamic-wind 
      (lambda ()
	(set! sock (nn-socket AF_SP NN_SURVEYOR))
	(assert (>= sock 0)))
      (lambda ()
	(assert (>= (nn-bind sock url) 0))
	(sleep-s 1)
	(let* ([sz-d (string-length date-name)])
	  (printf "SERVER: SENDING DATE SURVEY REQUEST~n")
	  (assert (= (nn-send sock (string->utf8 date-name) sz-d 0) sz-d)))
	(let loop ()
	  (let* ([buf (box #t)]
		 [bytes (nn-recv sock buf NN_MSG 0)])
	    (unless (= bytes NN_ETIMEDOUT)
		    (when (>= bytes 0)
			  (printf "SERVER: RECEIVED ~d SURVEY RESPONSE~n" 
				  (utf8->string (unbox buf))))
		    (loop)))))
      (lambda ()
	(set! r (nn-shutdown sock 0))))
  r)

(define (client url name)
  (define sock #f)
  (define r #f)
  (dynamic-wind 
      (lambda ()
	(set! sock (nn-socket AF_SP NN_RESPONDENT))
	(assert (>= sock 0)))
      (lambda ()
	(assert (>= (nn-connect sock url) 0))
	(let loop ()
	  (let* ([buf (box #t)]
		 [bytes (nn-recv sock buf NN_MSG 0)])
	    (when (>= bytes 0)
		  (printf "CLIENT (~d): RECEIVED ~d SURVEY REQUEST~n" 
			  name (utf8->string (unbox buf)))
		  (printf "CLIENT (~d): SENDING DATE SURVEY RESPONSE~n" 
			  name)
		  (let* ([d (date)] [sz-d (string-length d)])
		    (assert (= (nn-send sock (string->utf8 d) sz-d 0))))
		  (loop)))))
      (lambda ()
	(set! r (nn-shutdown sock 0))))
    r)

(define argv (command-line-arguments))
(define argc (length argv))

(cond
 [(and (> argc 1) (string=? server-name (car argv)))
  (server (cadr argv))]
 [(and (> argc 2) (string=? client-name (car argv)))
  (client (cadr argv) (caddr argv))]
 [else
  (printf "Usage: survey ~d|~d <URL> <ARG> ...'~n" server-name client-name)])


#!eof

./survey server ipc:///tmp/survey.ipc & server=$!
./survey client ipc:///tmp/survey.ipc client0 & client0=$!
./survey client ipc:///tmp/survey.ipc client1 & client1=$!
./survey client ipc:///tmp/survey.ipc client2 & client2=$!
sleep 3
kill $server $client0 $client1 $client2

Added nanomsg/symbols.ss.





















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
(nn-define NN_NS_NAMESPACE 0)
(nn-define NN_NS_VERSION 1)
(nn-define NN_NS_DOMAIN 2)
(nn-define NN_NS_TRANSPORT 3)
(nn-define NN_NS_PROTOCOL 4)
(nn-define NN_NS_OPTION_LEVEL 5)
(nn-define NN_NS_SOCKET_OPTION 6)
(nn-define NN_NS_TRANSPORT_OPTION 7)
(nn-define NN_NS_OPTION_TYPE 8)
(nn-define NN_NS_OPTION_UNIT 9)
(nn-define NN_NS_FLAG 10)
(nn-define NN_NS_ERROR 11)
(nn-define NN_NS_LIMIT 12)
(nn-define NN_NS_EVENT 13)
(nn-define NN_NS_STATISTIC 14)
(nn-define NN_TYPE_NONE 0)
(nn-define NN_TYPE_INT 1)
(nn-define NN_TYPE_STR 2)
(nn-define NN_UNIT_NONE 0)
(nn-define NN_UNIT_BYTES 1)
(nn-define NN_UNIT_MILLISECONDS 2)
(nn-define NN_UNIT_PRIORITY 3)
(nn-define NN_UNIT_BOOLEAN 4)
(nn-define NN_UNIT_COUNTER 6)
(nn-define NN_UNIT_MESSAGES 5)
(nn-define NN_VERSION_CURRENT 5)
(nn-define NN_VERSION_REVISION 0)
(nn-define NN_VERSION_AGE 0)
(nn-define AF_SP 1)
(nn-define AF_SP_RAW 2)
(nn-define NN_INPROC -1)
(nn-define NN_IPC -2)
(nn-define NN_TCP -3)
(nn-define NN_WS -4)
(nn-define NN_PAIR 16)
(nn-define NN_PUB 32)
(nn-define NN_SUB 33)
(nn-define NN_REP 49)
(nn-define NN_REQ 48)
(nn-define NN_PUSH 80)
(nn-define NN_PULL 81)
(nn-define NN_SURVEYOR 98)
(nn-define NN_RESPONDENT 99)
(nn-define NN_BUS 112)
(nn-define NN_SOCKADDR_MAX 128)
(nn-define NN_SOL_SOCKET 0)
(nn-define NN_LINGER 1)
(nn-define NN_SNDBUF 2)
(nn-define NN_RCVBUF 3)
(nn-define NN_RCVMAXSIZE 16)
(nn-define NN_SNDTIMEO 4)
(nn-define NN_RCVTIMEO 5)
(nn-define NN_RECONNECT_IVL 6)
(nn-define NN_RECONNECT_IVL_MAX 7)
(nn-define NN_SNDPRIO 8)
(nn-define NN_RCVPRIO 9)
(nn-define NN_SNDFD 10)
(nn-define NN_RCVFD 11)
(nn-define NN_DOMAIN 12)
(nn-define NN_PROTOCOL 13)
(nn-define NN_IPV4ONLY 14)
(nn-define NN_SOCKET_NAME 15)
(nn-define NN_MAXTTL 17)
(nn-define NN_SUB_SUBSCRIBE 1)
(nn-define NN_SUB_UNSUBSCRIBE 2)
(nn-define NN_REQ_RESEND_IVL 1)
(nn-define NN_SURVEYOR_DEADLINE 1)
(nn-define NN_TCP_NODELAY 1)
(nn-define NN_WS_MSG_TYPE 1)
(nn-define NN_DONTWAIT 1)
(nn-define NN_WS_MSG_TYPE_TEXT 1)
(nn-define NN_WS_MSG_TYPE_BINARY 2)
(nn-define NN_POLLIN 1)
(nn-define NN_POLLOUT 2)
(nn-define EADDRINUSE 98)
(nn-define EADDRNOTAVAIL 99)
(nn-define EAFNOSUPPORT 97)
(nn-define EAGAIN 11)
(nn-define EBADF 9)
(nn-define ECONNREFUSED 111)
(nn-define EFAULT 14)
(nn-define EFSM 156384766)
(nn-define EINPROGRESS 115)
(nn-define EINTR 4)
(nn-define EINVAL 22)
(nn-define EMFILE 24)
(nn-define ENAMETOOLONG 36)
(nn-define ENETDOWN 100)
(nn-define ENOBUFS 105)
(nn-define ENODEV 19)
(nn-define ENOMEM 12)
(nn-define ENOPROTOOPT 92)
(nn-define ENOTSOCK 88)
(nn-define ENOTSUP 95)
(nn-define EPROTO 71)
(nn-define EPROTONOSUPPORT 93)
(nn-define ETERM 156384765)
(nn-define ETIMEDOUT 110)
(nn-define EACCES 13)
(nn-define ECONNABORTED 103)
(nn-define ECONNRESET 104)
(nn-define EHOSTUNREACH 113)
(nn-define EMSGSIZE 90)
(nn-define ENETRESET 102)
(nn-define ENETUNREACH 101)
(nn-define ENOTCONN 107)
(nn-define NN_STAT_ESTABLISHED_CONNECTIONS 101)
(nn-define NN_STAT_ACCEPTED_CONNECTIONS 102)
(nn-define NN_STAT_DROPPED_CONNECTIONS 103)
(nn-define NN_STAT_BROKEN_CONNECTIONS 104)
(nn-define NN_STAT_CONNECT_ERRORS 105)
(nn-define NN_STAT_BIND_ERRORS 106)
(nn-define NN_STAT_ACCEPT_ERRORS 107)
(nn-define NN_STAT_MESSAGES_SENT 301)
(nn-define NN_STAT_MESSAGES_RECEIVED 302)
(nn-define NN_STAT_BYTES_SENT 303)
(nn-define NN_STAT_BYTES_RECEIVED 304)
(nn-define NN_STAT_CURRENT_CONNECTIONS 201)
(nn-define NN_STAT_INPROGRESS_CONNECTIONS 202)
(nn-define NN_STAT_CURRENT_SND_PRIORITY 401)
(nn-define NN_STAT_CURRENT_EP_ERRORS 203)