-
Notifications
You must be signed in to change notification settings - Fork 25
/
pair
executable file
·69 lines (53 loc) · 1.59 KB
/
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
#! /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)
(printf "~s: SENDING '~s'~n" name name)
(nn-send sock (string->utf8 name) 0))
(define (recv-name sock name)
(let* ([buf (box #t)]
[result (guard (x [(= (nn-errno) ETIMEDOUT) -1])
(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)
(nn-setsockopt/int sock NN_SOL_SOCKET NN_RCVTIMEO 100)
(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))
(define eid (nn-bind sock url))
(send-recv sock node0-name)
(nn-shutdown sock eid))
(define (node1 url)
(define sock (nn-socket AF_SP NN_PAIR))
(define eid (nn-connect sock url))
(send-recv sock node1-name)
(nn-shutdown sock eid))
(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