-
Notifications
You must be signed in to change notification settings - Fork 25
/
bus
executable file
·71 lines (56 loc) · 1.68 KB
/
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
#! /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 eids '())
(define r #f)
(dynamic-wind
(lambda ()
(set! sock (nn-socket AF_SP NN_BUS)))
(lambda ()
(nn-bind sock (cadr argv))
(sleep-s 1)
(when (>= argc 2)
(let loop ([x 2])
(define n (nn-connect sock (list-ref argv x)))
(set! eids (cons n eids))
(if (< (+ x 1) argc)
(loop (+ x 1)))))
(sleep-s 1)
(nn-setsockopt/int sock NN_SOL_SOCKET NN_RCVTIMEO 100)
(let* ([n (car argv)])
(printf "~d: SENDING ~d ONTO BUS~n" n n)
(nn-send sock (string->utf8 n) 0))
(let loop ()
(let* ([buf (box #t)]
[recv (guard (x [(= (nn-errno) ETIMEDOUT) -1])
(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 ()
(for-each (lambda (eid)
(nn-shutdown sock eid)) eids))))
(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