Index: ulex/ulex.scm ================================================================== --- ulex/ulex.scm +++ ulex/ulex.scm @@ -221,18 +221,21 @@ (define (udat-captain-host-port udata) (if (and (udat-captain-address udata)(udat-captain-port udata)) (conc (udat-captain-address udata) ":" (udat-captain-port udata)) #f)) +(define (udat-get-peer udata host-port) + (hash-table-ref/default (udat-peers udata) host-port #f)) + ;; struct for keeping track of others we are talking to (defstruct peer (addr-port #f) (hostname #f) (pid #f) - (inp #f) - (oup #f) + ;; (inp #f) + ;; (oup #f) (dbs '()) ;; list of databases this peer is currently handling ) (defstruct work (peer-dat #f) @@ -360,69 +363,65 @@ (define (get-peer-dat udata host-port #!optional (hostname #f)(pid #f)) ;; I'm currently very fuzzy on whether it makes sense to be reusing the outgoing connections. ;; at the other end of the line I think the reciever has closed the ports - thus each message ;; requires new connection? - (let* ((pdat (or #f #;(hash-table-ref/default (udat-outgoing-conns udata) host-port #f) + (let* ((pdat (or (udat-get-peer udata host-port) (handle-exceptions ;; ERROR - MAKE THIS EXCEPTION HANDLER MORE SPECIFIC exn #f (let ((npdat (make-peer addr-port: host-port))) (if hostname (peer-hostname-set! npdat hostname)) (if pid (peer-pid-set! npdat pid)) - (let-values (((ninp noup)(tcp-connect host-port))) - (peer-inp-set! npdat ninp) - (peer-oup-set! npdat noup)) - (hash-table-set! (udat-outgoing-conns udata) host-port npdat) npdat))))) pdat)) -(define (get-peer-ports udata host-port #!optional (hostname #f)(pid #f)) - (let ((pdat (get-peer-dat udata host-port hostname pid))) - (if pdat - (values (peer-inp pdat)(peer-oup pdat)) - (values #f #f)))) - ;; send structured data to recipient ;; ;; NOTE: qrykey is what was called the "cookie" previously ;; ;; retval tells send to expect and wait for return data (one line) and return it or time out ;; this is for ping where we don't want to necessarily have set up our own server yet. ;; (define (send udata host-port handler qrykey data #!key (hostname #f)(pid #f)(params '())(retval #f)) - (let-values (((inp oup)(get-peer-ports udata host-port hostname pid))) - ;; - ;; CONTROL LINE: (note: removed the hostname - I don't think it adds much value - ;; - ;; handlerkey host:port pid qrykey params ... - ;; - (if (and inp oup) - (let* ((myhost (udat-my-address udata)) - (myport (udat-my-port udata)) - (dat (conc - handler " " - (udat-my-address udata) ":" (udat-my-port udata) " " - ;; (udat-my-hostname udata) " " - (udat-my-pid udata) " " - qrykey - (if (null? params) "" (conc " " (string-intersperse params " ")))))) - (if (and myhost myport) - (begin - (write-line dat oup) - (write-line data oup) - ;; (print "Sent dat: " dat " data: " data) - (if retval - (read-line inp) - #t)) - (begin - (print "ERROR: send called but no receiver has been setup. Please call setup first!") - #f)) - ;; NOTE: DO NOT BE TEMPTED TO LOOK AT ANY DATA ON INP HERE! - ;; (there is a listener for handling that) - ) - #f))) ;; #f means failed to connect and send + (handle-exceptions ;; ERROR - MAKE THIS EXCEPTION HANDLER MORE SPECIFIC + exn + #f + (let-values (((inp oup)(tcp-connect host-port))) + ;; + ;; CONTROL LINE: + ;; handlerkey host:port pid qrykey params ... + ;; + (let ((res + (if (and inp oup) + (let* ((myhost (udat-my-address udata)) + (myport (udat-my-port udata)) + (dat (conc + handler " " + (udat-my-address udata) ":" (udat-my-port udata) " " + ;; (udat-my-hostname udata) " " + (udat-my-pid udata) " " + qrykey + (if (null? params) "" (conc " " (string-intersperse params " ")))))) + (if (and myhost myport) + (begin + (write-line dat oup) + (write-line data oup) + ;; (print "Sent dat: " dat " data: " data) + (if retval + (read-line inp) + #t)) + (begin + (print "ERROR: send called but no receiver has been setup. Please call setup first!") + #f)) + ;; NOTE: DO NOT BE TEMPTED TO LOOK AT ANY DATA ON INP HERE! + ;; (there is a listener for handling that) + ) + #f))) ;; #f means failed to connect and send + (close-input-port inp) + (close-output-port oup) + res)))) ;; send a request to the given host-port and register a mailbox in udata ;; wait for the mailbox data and return it ;; (define (send-receive udata host-port handler qrykey data #!key (hostname #f)(pid #f)(params '())(timeout 20))