(import (chicken io)
        (chicken string)
        miscmacros
        nng
        srfi-18
        test)


(define address-tcp-1 "tcp://localhost:5555")
(define address-tcp-2 "tcp://localhost:6666")

(define address-tls+tcp-1 "tls+tcp://localhost:5555")

(define address-inproc-1 "inproc://local1")
(define address-inproc-2 "inproc://local2")

(tls-register!)

(define cert
  (if (tls-enabled?)
      (with-input-from-file "cert.pem"
        (lambda ()
          (read-string)))
      #f))

(define key
  (if (tls-enabled?)
      (with-input-from-file "key.pem"
        (lambda ()
          (read-string)))
      #f))

;;;
;;; Req-Rep
;;;
(define (make-listening-reply-socket address)
  (let ((socket (make-rep-socket)))
    (socket-set! socket 'nng/recvtimeo 2000)
    (nng-listen socket address)
    socket))

(define (make-dialed-request-socket address)
  (let ((socket (make-req-socket)))
    (socket-set! socket 'nng/recvtimeo 2000)
    (nng-dial socket address)
    socket))

(define (req-rep-test address)
  (let ((rep (make-listening-reply-socket address))
        (req (make-dialed-request-socket address)))
    (nng-send req "message")
    (nng-recv rep)
    (nng-send rep "message")
    (begin0
     (nng-recv req)
     (nng-close! rep))))


(test-group "nng"

            (test "tcp req-rep"
                  "message"
                  (req-rep-test address-tcp-1))

            (test "inproc req-rep"
                  "message"
                  (req-rep-test address-inproc-1))

	    )

(test-exit)
