Artifact Content
Not logged in

Artifact c3a7129577312fa79c835392687d5bc04c73983a:


#! /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