;;;; remote-mailbox-client.scm ;;;; Kon Lovett, Sep '09 ;; Issues ;; ;; - Currently the client input port is unused. (module remote-mailbox-client (;export ;;common ;parameters default-remote-mailbox-tcp-port-no default-remote-mailbox-hostname ;;client ;parameters default-remote-mailbox-connect ;operations remote-mailbox remote-mailbox? remote-mailbox-name remote-mailbox-hostname remote-mailbox-tcp-port remote-mailbox-connected? drop-remote-mailbox! drop-remote-mailboxes! remote-mailboxes remote-mailbox-send!) (import scheme chicken) (use tcp (only srfi-18 make-mutex mutex-name) (only data-structures conc) (only moremacros ->boolean define-warning-parameter) (only type-errors warning-argument-type) mailbox synch lookup-table-synch (only type-checks check-procedure define-check+error-type) remote-mailbox-adapter remote-mailbox-packet remote-mailbox-common) ;;; Utilities ;;; Support (define-type remote-mailbox (struct remote-mailbox)) (define-type mailbox-name symbol) (define-type hostname string) (: remote-mailbox-name (remote-mailbox -> mailbox-name)) (: remote-mailbox-hostname (remote-mailbox -> hostname)) ; (define-record-type remote-mailbox (*make-remote-mailbox name hstnam prtnum serializer connect mutex input output) *remote-mailbox? (name remote-mailbox-name remote-mailbox-name-set!) (hstnam remote-mailbox-hostname) (prtnum remote-mailbox-tcp-port) (serializer remote-mailbox-serializer) (connect remote-mailbox-connect) (mutex remote-mailbox-mutex) (input remote-mailbox-input-port remote-mailbox-input-port-set!) (output remote-mailbox-output-port remote-mailbox-output-port-set!) ) (define (invalidate-remote-mailbox! rmb) (remote-mailbox-name-set! rmb #f) ) (define (valid-remote-mailbox? rmb) (->boolean (remote-mailbox-name rmb)) ) (define (remote-mailbox-key rmb) (mutex-name (remote-mailbox-mutex rmb)) ) (define +remote-mailbox-key->remote-mailbox+ (make-dict/synch)) (define (make-remote-mailbox-key name hostname tcp-port) (conc name #\@ hostname (if tcp-port (conc #\: tcp-port) "")) ) (define (*remote-mailbox name hostname tcp-port connect) (let ((key (make-remote-mailbox-key name hostname tcp-port))) (dict-indempotent-ref!/synch +remote-mailbox-key->remote-mailbox+ key (lambda (def) (*make-remote-mailbox name hostname tcp-port (serializer) connect (make-mutex key) #f #f))) ) ) (define (*remote-mailbox-connected? rmb) (->boolean (remote-mailbox-output-port rmb)) ) (define (connection/remote-mailbox rmb) (if (*remote-mailbox-connected? rmb) (remote-mailbox-output-port rmb) ;else make a connection (let-values ( ((in out) (let ( (connect (remote-mailbox-connect rmb)) (tcp-port (remote-mailbox-tcp-port rmb)) ) ;Allow hostname to carry service/portno (if (not tcp-port) (connect (remote-mailbox-hostname rmb)) (connect (remote-mailbox-hostname rmb) tcp-port) ) ) ) ) (remote-mailbox-input-port-set! rmb in) (remote-mailbox-output-port-set! rmb out) out )) ) (define (close-remote-mailbox-connection! rmb) (close-input-port (remote-mailbox-input-port rmb)) (remote-mailbox-input-port-set! rmb #f) (close-output-port (remote-mailbox-output-port rmb)) (remote-mailbox-output-port-set! rmb #f) (invalidate-remote-mailbox! rmb) ) (define (*drop-remote-mailbox! rmb) (record/synch remote-mailbox rmb (close-remote-mailbox-connection! rmb) (dict-delete!/synch +remote-mailbox-key->remote-mailbox+ (remote-mailbox-key rmb)) ) ) ;;; Exported ;; Parameters (define (remote-mailbox-connector? x) (or (not x) (procedure? x)) ) (: default-remote-mailbox-connect (#!optional (or boolean procedure) -> procedure)) ; (define-warning-parameter default-remote-mailbox-connect tcp-connect remote-mailbox-connector ;ugh, automagic identifier injection (unless obj (set! obj tcp-connect)) ) ;; Operations (: remote-mailbox (mailbox-name #!rest -> remote-mailbox)) ; (define (remote-mailbox name #!key (hostname (default-remote-mailbox-hostname)) (tcp-port (default-remote-mailbox-tcp-port-no)) (connect (default-remote-mailbox-connect))) (*remote-mailbox (check-mailbox-name 'remote-mailbox name 'name) (check-hostname 'remote-mailbox hostname 'hostname) (or tcp-port (check-tcp-port-no 'remote-mailbox tcp-port 'tcp-port)) (check-procedure 'remote-mailbox connect 'connect)) ) (: remote-mailbox? (* -> boolean : remote-mailbox)) ; (define (remote-mailbox? obj) (and (*remote-mailbox? obj) (valid-remote-mailbox? obj)) ) (define-check+error-type remote-mailbox) (: remote-mailbox-connected? (remote-mailbox -> boolean)) ; (define (remote-mailbox-connected? rmb) (*remote-mailbox-connected? (check-remote-mailbox 'remote-mailbox-connected? rmb)) ) (: remote-mailboxes (-> (list-of remote-mailbox))) ; (define (remote-mailboxes) (dict-values/synch +remote-mailbox-key->remote-mailbox+) ) (: drop-remote-mailbox! (remote-mailbox -> void)) ; (define (drop-remote-mailbox! rmb) (*drop-remote-mailbox! (check-remote-mailbox 'drop-remote-mailbox! rmb)) ) (: drop-remote-mailboxes! (-> void)) ; (define (drop-remote-mailboxes!) (for-each (cut *drop-remote-mailbox! <>) (remote-mailboxes)) ) (: remote-mailbox-send! (remote-mailbox * -> void)) ; (define (remote-mailbox-send! rmb val) (record/synch remote-mailbox (check-remote-mailbox 'remote-mailbox-send! rmb) (let ( (out (connection/remote-mailbox rmb)) (req (make-remote-mailbox-packet (remote-mailbox-name rmb) val)) ) (parameterize ((serializer (remote-mailbox-serializer rmb))) (serialize req out) ) ) ) ) ) ;module remote-mailbox-client