forked from ovenpasta/thunderchez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reqrep
executable file
·60 lines (47 loc) · 1.56 KB
/
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
#! /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 sock (nn-socket AF_SP NN_REP))
(define eid (nn-bind sock url))
(let loop ()
(let ([buf (box #t)])
(define bytes (nn-recv sock buf NN_MSG 0))
(when (string=? date-name (utf8->string (unbox buf)))
(let* ([d (date)])
(printf "NODE0: RECEIVED DATE REQUEST~n")
(printf "NODE0: SENDING DATE ~d~n" d)
(nn-send sock (string->utf8 d) 0)))
(loop)))
(nn-shutdown sock eid))
(define (node1 url)
(define sock (nn-socket AF_SP NN_REQ))
(define eid (nn-connect sock url))
(printf "NODE1: SENDING DATE REQUEST '~d'~n" date-name)
(nn-send sock (string->utf8 date-name) 0)
(let ([buf (box #t)])
(nn-recv sock buf NN_MSG 0)
(printf "NODE1: RECEIVED DATE ~d~n" (utf8->string (unbox buf))))
(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: 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