Index: nng-trial/Makefile ================================================================== --- nng-trial/Makefile +++ nng-trial/Makefile @@ -1,5 +1,8 @@ nng-test : nng-test.scm csc nng-test.scm test : nng-test ./nng-test do-test + +clean : + rm -f .runners/* NBFAKE* Index: nng-trial/nng-test.scm ================================================================== --- nng-trial/nng-test.scm +++ nng-trial/nng-test.scm @@ -1,6 +1,11 @@ -(import (chicken io) +(module nng-test * + +(import scheme + (chicken io) + (chicken base) + (chicken time) (chicken file) (chicken file posix) (chicken string) (chicken process-context) (chicken process-context posix) @@ -15,11 +20,12 @@ directory-utils ) (define help "Usage: nng-test COMMAND where COMMAND is one of: - dotest : run the basic req/rep test + do-test : run the basic req/rep test + run tcp://host:port : start test server - start several in same dir ") (define address-tcp-1 "tcp://localhost:5555") (define address-tcp-2 "tcp://localhost:6666") @@ -59,21 +65,10 @@ (test "inproc req-rep" "message" (req-rep-test address-inproc-1))) (test-exit)) -;; talking to self here... -;; -(define (send-n-messages n srvdat) - (let* ((name (srv-name srvdat))) - (let loop ((i 0)) - (if (< i n) - (begin - (nng-send (srv-req srvdat) (conc name "-" i)) - (print "received: "(nng-recv (srv-rep srvdat))) - (loop (+ i 1))))))) - ;; this should be run in a thread (define (run-listener-responder socket myaddr) (let loop ((status 'running)) (let* ((msg (nng-recv socket)) (response (process-message msg))) @@ -110,44 +105,53 @@ ((msg) (conc msg " 0")) (else "hello 0")))) -(define (close-srv srvdat) - (nng-close! (srv-rep srvdat))) - -(match - (command-line-arguments) - (("do-test")(do-test)) - ((run myaddr) - ;; start listener - ;; put myaddr into file by host-pid in .runners - ;; for 1 minute - ;; get all in .runners - ;; call each with a message - ;; - (let* ((socket (make-listening-reply-socket myaddr)) - (rfile (conc ".runners/"(get-host-name)"-"(current-process-id))) - (th1 (make-thread (lambda () - (run-listener-responder socket myaddr) - (delete-file* rfile) - (exit)) - "responder"))) - (if (not (and (file-exists? ".runners") - (directory? ".runners"))) - (create-directory ".runners" #t)) - (with-output-to-file rfile - (lambda () - (print myaddr))) - (thread-start! th1) - (let loop ((entries '())) - (if (null? entries) - (loop (glob ".runners/*")) - (let* ((entry (car entries)) - (destaddr (with-input-from-file entry read-line))) - (call *channels* (conc "hello-from-"destaddr) destaddr) - (thread-sleep! 0.25) - (loop (cdr entries))))))) - ((cmd)(print "ERROR: command "cmd", not recognised.\n\n"help)) - (else - (print help))) +(define (main) + (match + (command-line-arguments) + (("do-test")(do-test)) + ((run myaddr) + ;; start listener + ;; put myaddr into file by host-pid in .runners + ;; for 1 minute + ;; get all in .runners + ;; call each with a message + ;; + (let* ((endtimes (+ (current-seconds) 20)) ;; run for 20 seconds + (socket (make-listening-reply-socket myaddr)) + (rfile (conc ".runners/"(get-host-name)"-"(current-process-id))) + (th1 (make-thread (lambda () + (run-listener-responder socket myaddr) + ) + "responder"))) + (if (not (and (file-exists? ".runners") + (directory? ".runners"))) + (create-directory ".runners" #t)) + (with-output-to-file rfile + (lambda () + (print myaddr))) + (thread-start! th1) + (let loop ((entries '())) + (if (> (current-seconds) endtimes) + (begin + (delete-file* rfile) + (sleep 1) + (exit)) + (if (null? entries) + (loop (glob ".runners/*")) + (let* ((entry (car entries)) + (destaddr (with-input-from-file entry read-line))) + (call *channels* (conc "hello-from-"destaddr) destaddr) + ;; (thread-sleep! 0.025) + (loop (cdr entries)))))))) + ((cmd)(print "ERROR: command "cmd", not recognised.\n\n"help)) + (else + (print help)))) + +) ;; end module + +(import nng-test) +(main) +