(define sub (make-socket 'sub))
(define push (make-socket 'push))
(socket-option-set! sub 'subscribe cname)
(connect-socket sub "tcp://localhost:5563")
(connect-socket push "tcp://localhost:5564")
(define (dbaccess cname cmd var val #!key (numtries 20))
(let* ((msg (conc cname ":" cmd ":" (if val (conc var " " val) var)))
(res #f)
(mtx1 (make-mutex))
(do-access (lambda ()
(print "Sending msg: " msg)
(send-message push msg)
(print "Message " msg " sent")
(print "Client " cname " waiting for response to " msg)
(print "Client " cname " received address " (receive-message* sub))
(mutex-lock! mtx1)
(set! res (receive-message* sub))
(mutex-unlock! mtx1))))
(let ((th1 (make-thread do-access "do access"))
(th2 (make-thread (lambda ()
(let ((result #f))
(mutex-lock! mtx1)
(set! result res)
(mutex-unlock! mtx1)
(thread-sleep! 5)
(if (not result)
(if (> numtries 0)
(begin
(print "WARNING: access timed out for " cname ", trying again. Trys remaining=" numtries)
(dbaccess cname cmd var val numtries: (- numtries 1)))
(begin
(print "ERROR: dbaccess timed out. Exiting")
(exit)))))
"timeout thread"))))
(thread-start! th1)
(thread-start! th2)
(thread-join! th1)
res)))