forked from ovenpasta/thunderchez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey
executable file
·86 lines (72 loc) · 2.17 KB
/
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
#! /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 eid #f)
(dynamic-wind
(lambda ()
(set! sock (nn-socket AF_SP NN_SURVEYOR)))
(lambda ()
(set! eid (nn-bind sock url))
(sleep-s 1)
(printf "SERVER: SENDING DATE SURVEY REQUEST~n")
(nn-send sock (string->utf8 date-name) 0)
(let loop ()
(let* ([buf (box #t)]
[bytes (guard (x [(= (nn-errno) ETIMEDOUT) #f])
(nn-recv sock buf NN_MSG 0))])
(unless bytes
(printf "SERVER: RECEIVED ~d SURVEY RESPONSE~n"
(utf8->string (unbox buf)))
(loop)))))
(lambda ()
(if eid (nn-shutdown sock eid)))))
(define (client url name)
(define sock #f)
(define eid #f)
(dynamic-wind
(lambda ()
(set! sock (nn-socket AF_SP NN_RESPONDENT)))
(lambda ()
(set! eid (nn-connect sock url))
(let loop ()
(let* ([buf (box #t)]
[bytes (guard (x [else #f])
(nn-recv sock buf NN_MSG 0))])
(when bytes
(printf "CLIENT (~d): RECEIVED ~d SURVEY REQUEST~n"
name (utf8->string (unbox buf)))
(printf "CLIENT (~d): SENDING DATE SURVEY RESPONSE~n"
name)
(nn-send sock (string->utf8 (date)) 0)
(loop)))))
(lambda ()
(if eid (nn-shutdown sock eid)))))
(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