Index: apimod.scm ================================================================== --- apimod.scm +++ apimod.scm @@ -344,10 +344,11 @@ ((get-prev-run-ids) (apply db:get-prev-run-ids dbstruct params)) ((get-run-ids-matching-target) (apply db:get-run-ids-matching-target dbstruct params)) ((get-runs-by-patt) (apply db:get-runs-by-patt dbstruct params)) ((get-run-name-from-id) (apply db:get-run-name-from-id dbstruct params)) ((get-main-run-stats) (apply db:get-main-run-stats dbstruct params)) + ((log-to-main) (apply debug:print params)) ((get-var) (apply db:get-var dbstruct params)) ((get-run-stats) (apply db:get-run-stats dbstruct params)) ((get-run-times) (apply db:get-run-times dbstruct params)) ;; STEPS Index: debugprint.scm ================================================================== --- debugprint.scm +++ debugprint.scm @@ -8,20 +8,24 @@ (import scheme chicken.base chicken.string chicken.port chicken.process-context + chicken.process-context.posix + (prefix mtargs args:) srfi-1 + system-information ) ;;====================================================================== ;; debug stuff ;;====================================================================== (define verbosity (make-parameter '())) (define *default-log-port* (current-error-port)) +(define debug:print-logger (make-parameter #f)) ;; se to a proc to call on every logging print (define (debug:setup) (let ((debugstr (or (args:get-arg "-debug") (args:get-arg "-debug-noprop") (get-environment-variable "MT_DEBUG_MODE")))) @@ -99,25 +103,31 @@ ((and (number? vb) (list? n)) (member vb n)) (else #f)))) +(define (debug:handle-remote-logging params) + (if (debug:print-logger) + (apply (debug:print-logger) "REMOTE ("(get-host-name)", pid="(current-process-id)") " params))) + (define (debug:print n e . params) (if (debug:debug-mode n) (with-output-to-port (or e (current-error-port)) (lambda () ;; (if *logging* ;; (db:log-event (apply conc params)) (apply print params) + (debug:handle-remote-logging params) )))) ;; ) (define (debug:print-error n e . params) ;; normal print (if (debug:debug-mode n) (with-output-to-port (if (port? e) e (current-error-port)) (lambda () (apply print "ERROR: " params) + (debug:handle-remote-logging (cons "ERROR: " params)) ))) ;; pass important messages to stderr (if (and (eq? n 0)(not (eq? e (current-error-port)))) (with-output-to-port (current-error-port) (lambda () @@ -127,8 +137,16 @@ (define (debug:print-info n e . params) (if (debug:debug-mode n) (with-output-to-port (if (port? e) e (current-error-port)) (lambda () (apply print "INFO: (" n ") " params) ;; res) + (debug:handle-remote-logging (cons "INFO: " params)) )))) +(define (debug:print-warn n e . params) + (if (debug:debug-mode n) + (with-output-to-port (if (port? e) e (current-error-port)) + (lambda () + (apply print "WARN: (" n ") " params) ;; res) + (debug:handle-remote-logging (cons "WARN: " params)) + )))) ) Index: rmtmod.scm ================================================================== --- rmtmod.scm +++ rmtmod.scm @@ -133,26 +133,26 @@ ) (define (servdat->url sdat) (conc (servdat-host sdat)":"(servdat-port sdat))) - ;; generate entries for ~/.megatestrc with the following ;; ;; grep define ../rmt.scm | grep rmt: |perl -pi -e 's/\(define\s+\((\S+)\W.*$/\1/'|sort -u -(defstruct rmt:remote - (conns (make-hash-table)) ;; apath/dbname => rmt:conn +(defstruct remotedat + (conns (make-hash-table)) ;; apath/dbname => conndat ) -(defstruct rmt:conn +(defstruct conndat (apath #f) (dbname #f) (fullname #f) (hostport #f) (ipaddr #f) (port #f) + (socket #f) (srvpkt #f) (srvkey #f) (lastmsg 0) (expires 0) (inport #f) @@ -169,16 +169,16 @@ ;;====================================================================== ;; S U P P O R T F U N C T I O N S ;;====================================================================== ;; replaces *runremote* -(define *rmt:remote* (make-rmt:remote)) +(define *remotedat* (make-remotedat)) ;; -> http://abc.com:900/ ;; -(define (rmt:conn->uri conn entrypoint) - (conc "http://"(rmt:conn-ipaddr conn)":"(rmt:conn-port conn)"/"entrypoint)) +(define (conndat->uri conn entrypoint) + (conc "http://"(conndat-ipaddr conn)":"(conndat-port conn)"/"entrypoint)) ;; set up the api proc, seems like there should be a better place for this? (define api-proc (make-parameter conc)) (api-proc api:process-request) @@ -187,15 +187,15 @@ ;; ;; else setup a connection ;; ;; if that fails, return '(#f "some reason") ;; NB// convert to raising an exception ;; -(define (rmt:get-conn remote apath dbname) +(define (rmt:get-conn remdat apath dbname) (let* ((fullname (db:dbname->path apath dbname)) ;; we'll switch to full name later - (conn (hash-table-ref/default (rmt:remote-conns remote) dbname #f))) + (conn (hash-table-ref/default (remotedat-conns remdat) dbname #f))) (if (and conn - (< (current-seconds) (rmt:conn-expires conn))) + (< (current-seconds) (conndat-expires conn))) conn #f))) (define (rmt:find-main-server apath dbname) (let* ((pktsdir (get-pkts-dir apath)) @@ -202,69 +202,81 @@ (all-srvpkts (get-all-server-pkts pktsdir *srvpktspec*)) ;; (dbpath (conc apath "/" dbname)) (viable-srvs (get-viable-servers all-srvpkts dbname))) (get-the-server apath viable-srvs))) -;; looks for a connection to main +;; looks for a connection to main, returns if have and not exired +;; creates new otherwise +;; ;; connections for other servers happens by requesting from main ;; ;; TODO: This is unnecessarily re-creating the record in the hash table ;; -(define (rmt:open-main-connection remote apath) - (let* ((dbname (db:run-id->dbname #f)) - (the-srv (rmt:find-main-server apath dbname)) - (start-main-srv (lambda () - ;; srv not ready, delay a little and try again - (api:run-server-process apath dbname) - (thread-sleep! 4) - (rmt:open-main-connection remote apath) ;; TODO: Add limit to number of tries - ))) - (if the-srv ;; yes, we have a server, now try connecting to it - (let* ((srv-addr (server-address the-srv)) - (ipaddr (alist-ref 'ipaddr the-srv)) - (port (alist-ref 'port the-srv)) - (srvkey (alist-ref 'servkey the-srv)) - (fullpath (db:dbname->path apath dbname)) - (srvready (server-ready? ipaddr port srvkey))) - (if srvready - (begin - (hash-table-set! (rmt:remote-conns remote) - dbname ;; fullpath ;; yes, I'd prefer it to be fullpath - FIXME later - (make-rmt:conn - apath: apath - dbname: dbname - fullname: fullpath - hostport: srv-addr - ipaddr: ipaddr - port: port - srvpkt: the-srv - srvkey: srvkey ;; generated by rmt:get-signature on the server side - lastmsg: (current-seconds) - expires: (+ (current-seconds) 60) ;; this needs to be gathered during the ping - )) - #t) - (start-main-srv))) - (start-main-srv)))) - -;; NB// remote is a rmt:remote struct +(define (rmt:open-main-connection remdat apath) + (let* ((fullpath (db:dbname->path apath "/.db/main.db")) + (conn (hash-table-ref/default remdat fullpath))) ;; TODO - create call for this + (if (and conn ;; conn is NOT a socket, just saying ... + (< (current-seconds) (conndat-expires conn))) + conn ;; we are current and good to go - we'll deal elsewhere with a server that was killed or died + ;; Below we will find or create and connect to main + (let* ((dbname (db:run-id->dbname #f)) + (the-srv (rmt:find-main-server apath dbname)) + (start-main-srv (lambda () ;; call IF there is no the-srv found + (api:run-server-process apath dbname) + (thread-sleep! 4) + (rmt:open-main-connection remdat apath) ;; TODO: Add limit to number of tries + ))) + (if (not the-srv) ;; have server, try connecting to it + (start-main-srv) + (let* ((srv-addr (server-address the-srv)) ;; need serv + (ipaddr (alist-ref 'ipaddr the-srv)) + (port (alist-ref 'port the-srv)) + (srvkey (alist-ref 'servkey the-srv)) + (fullpath (db:dbname->path apath dbname)) + (new-the-srv (make-conndat + apath: apath + dbname: dbname + fullname: fullpath + hostport: srv-addr + socket: (open-nn-connection srv-addr) + ipaddr: ipaddr + port: port + srvpkt: the-srv + srvkey: srvkey ;; generated by rmt:get-signature on the server side + lastmsg: (current-seconds) + expires: (+ (current-seconds) 60) ;; this needs to be gathered during the ping + ))) + (hash-table-set! (remotedat-conns remdat) + fullpath ;; dbname ;; fullpath ;; yes, I'd prefer it to be fullpath - FIXME later + new-the-srv))))))) + +;; NB// remdat is a remotedat struct ;; -(define (rmt:general-open-connection remote apath dbname #!key (num-tries 5)) +(define (rmt:general-open-connection remdat apath dbname #!key (num-tries 5)) + (assert (not (equal? dbname ".db/main.db")) "ERROR: general-open-connection should never be called with main as the db") (let* ((mdbname (db:run-id->dbname #f)) - (mconn (rmt:get-conn remote apath mdbname))) + (mconn (rmt:get-conn remdat apath mdbname))) + (if (and mconn + (not (debug:print-logger))) + (if (equal? dbname ".db/main.db") + (debug:print-info 0 *default-log-port* "Not turning on logging to main, I am main!") + (begin + (debug:print-info 0 *default-log-port* "Turning on logging to main, look in logs dir for main log.") + (debug:print-logger rmt:log-to-main)))) (cond ((or (not mconn) ;; no channel open to main? - (< (rmt:conn-expires mconn)(+ (current-seconds) 2))) ;; restablish connection if less than 2 seconds on the lease - (rmt:open-main-connection remote apath) - (rmt:general-open-connection remote apath mdbname)) - ((not (rmt:get-conn remote apath dbname)) ;; no channel open to dbname? - (let* ((res (rmt:send-receive-real remote apath mdbname 'get-server `(,apath ,dbname)))) + (< (conndat-expires mconn)(+ (current-seconds) 2))) ;; restablish connection if less than 2 seconds on the lease + (rmt:open-main-connection remdat apath) + (rmt:general-open-connection remdat apath mdbname)) + ((not (rmt:get-conn remdat apath dbname)) ;; no channel open to dbname? + (let* ((res (rmt:send-receive-real remdat apath mdbname 'get-server `(,apath ,dbname)))) (case res ((server-started) (if (> num-tries 0) (begin (thread-sleep! 2) - (rmt:general-open-connection remote apath dbname num-tries: (- num-tries 1))) + (rmt:general-open-connection remdat apath dbname num-tries: (- num-tries 1))) (begin (debug:print-error 0 *default-log-port* "Failed to start servers needed or open channel to "apath", "dbname) (exit 1)))) (else (if (list? res) ;; server has been registered and the info was returned. pass it on. @@ -275,13 +287,13 @@ ;; ".db/1.db") (match res ((host port servkey pid ipaddr apath dbname) (debug:print-info 0 *default-log-port* "got "res) - (hash-table-set! (rmt:remote-conns remote) + (hash-table-set! (remotedat-conns remdat) dbname - (make-rmt:conn + (make-conndat apath: apath dbname: dbname hostport: (conc host":"port) ipaddr: ipaddr port: port @@ -306,42 +318,44 @@ (define *dbstruct* (make-dbr:dbstruct)) ;; Defaults to current area ;; (define (rmt:send-receive cmd rid params #!key (attemptnum 1)(area-dat #f)) - (if (not *rmt:remote*)(set! *rmt:remote* (make-rmt:remote))) + (if (not *remotedat*)(set! *remotedat* (make-remotedat))) (let* ((apath *toppath*) - (conns *rmt:remote*) + (conns *remotedat*) (dbname (db:run-id->dbname rid))) (if *localmode* (let* ((dbdat (dbr:dbstruct-get-dbdat *dbstruct* dbname)) (indat `((cmd . ,cmd)(params . ,params)))) (api:process-request *dbstruct* indat) ;; (api:process-request dbdat indat) ) (begin - (rmt:general-open-connection conns apath dbname) + (if rid + (rmt:general-open-connection conns apath dbname) + (rmt:open-main-connection conns apath)) (rmt:send-receive-real conns apath dbname cmd params))))) #;(define (rmt:send-receive-setup conn) - (if (not (rmt:conn-inport conn)) - (let-values (((i o) (tcp-connect (rmt:conn-ipaddr conn) - (rmt:conn-port conn)))) - (rmt:conn-inport-set! conn i) - (rmt:conn-outport-set! conn o)))) + (if (not (conndat-inport conn)) + (let-values (((i o) (tcp-connect (conndat-ipaddr conn) + (conndat-port conn)))) + (conndat-inport-set! conn i) + (conndat-outport-set! conn o)))) ;; db is at apath/.db/dbname, rid is an intermediary solution and will be removed ;; sometime in the future ;; -(define (rmt:send-receive-real remote apath dbname cmd params) - (let* ((conn (rmt:get-conn remote apath dbname))) +(define (rmt:send-receive-real remdat apath dbname cmd params) + (let* ((conn (rmt:get-conn remdat apath dbname))) (assert conn "FATAL: rmt:send-receive-real called without the needed channels opened") (let* ((key #f) - (host (rmt:conn-ipaddr conn)) - (port (rmt:conn-port conn)) + (host (conndat-ipaddr conn)) + (port (conndat-port conn)) (payload `((cmd . ,cmd) - (key . ,(rmt:conn-srvkey conn)) + (key . ,(conndat-srvkey conn)) (params . ,params))) (res (open-send-receive-nn (conc host":"port) (sexpr->string payload)))) (string->sexpr res)))) @@ -349,15 +363,15 @@ ;; sometime in the future. ;; ;; Purpose - call the main.db server and request a server be started ;; for the given area path and dbname ;; -;; (define (rmt:send-receive-server-start remote apath dbname) -;; (let* ((conn (rmt:get-conn remote apath dbname))) +;; (define (rmt:send-receive-server-start remdat apath dbname) +;; (let* ((conn (rmt:get-conn remdat apath dbname))) ;; (assert conn "FATAL: Unable to connect to db "apath"/"dbname) ;; #;(let* ((res (with-input-from-request -;; (rmt:conn->uri conn "api") +;; (conndat->uri conn "api") ;; `((params . (,apath ,dbname))) ;; read-string))) ;; (string->sexpr res)))) (define (rmt:print-db-stats) @@ -413,11 +427,11 @@ (define (rmt:start-server run-id) (rmt:send-receive 'start-server 0 (list run-id))) (define (rmt:get-server-info apath dbname) - (rmt:send-receive 'get-server-info 0 (list 0 apath dbname))) + (rmt:send-receive 'get-server-info #f (list #f apath dbname))) ;;====================================================================== ;; M I S C ;;====================================================================== @@ -768,10 +782,13 @@ ) ;; ) (define (rmt:get-main-run-stats run-id) (rmt:send-receive 'get-main-run-stats #f (list run-id))) +(define (rmt:log-to-main . params) + (rmt:send-receive 'log-to-main #f (cons #f params))) + (define (rmt:get-var run-id varname) (rmt:send-receive 'get-var run-id (list run-id varname))) (define (rmt:del-var run-id varname) (rmt:send-receive 'del-var run-id (list run-id varname))) @@ -1514,11 +1531,11 @@ (let* ((sdat *server-info*) ;; we have a run-id server (host (servdat-host sdat)) (port (servdat-port sdat)) (uuid (servdat-uuid sdat))) (if (not (string-match ".db/main.db" (args:get-arg "-db"))) - (let* ((res (rmt:deregister-server *rmt:remote* ;; TODO/BUG: why is this requiring *rmt:remote*? + (let* ((res (rmt:deregister-server *remotedat* ;; TODO/BUG: why is this requiring *remotedat*? *toppath* (servdat-host *server-info*) ;; iface (servdat-port *server-info*) (servdat-uuid *server-info*) (current-process-id) @@ -1589,12 +1606,17 @@ ;; called in megatest.scm, host-port is string hostname:port ;; ;; NOTE: This is NOT called directly from clients as not all transports support a client running ;; in the same process as the server. ;; -(define (server:ping host port server-id #!key (do-exit #f)) - (server-ready? host port server-id)) +;; conn is a conndat record +;; +(define (server:ping conn #!key (do-exit #f)) + (let* ((req (conndat-socket conn)) + (srvkey (conndat-srvkey conn)) + (msg (sexpr->string '(ping ,srvkey)))) + (send-receive-nn req msg))) ;; (server-ready? host port server-id)) ;;====================================================================== ;; http-transportmod.scm contents moved here ;;====================================================================== @@ -1732,34 +1754,34 @@ ;;====================================================================== ;; C L I E N T S ;;====================================================================== -(define (http-transport:get-time-to-cleanup) +(define (rmt:get-time-to-cleanup) (let ((res #f)) (mutex-lock! *http-mutex*) (set! res (> (current-seconds) *http-connections-next-cleanup*)) (mutex-unlock! *http-mutex*) res)) -(define (http-transport:inc-requests-count) +(define (rmt:inc-requests-count) (mutex-lock! *http-mutex*) (set! *http-requests-in-progress* (+ 1 *http-requests-in-progress*)) ;; Use this opportunity to slow things down iff there are too many requests in flight (if (> *http-requests-in-progress* 5) (begin (debug:print-info 0 *default-log-port* "Whoa there buddy, ease up...") (thread-sleep! 1))) (mutex-unlock! *http-mutex*)) -(define (http-transport:dec-requests-count proc) +(define (rmt:dec-requests-count proc) (mutex-lock! *http-mutex*) (proc) (set! *http-requests-in-progress* (- *http-requests-in-progress* 1)) (mutex-unlock! *http-mutex*)) -(define (http-transport:dec-requests-count-and-close-all-connections) +(define (rmt:dec-requests-count-and-close-all-connections) (set! *http-requests-in-progress* (- *http-requests-in-progress* 1)) (let loop ((etime (+ (current-seconds) 5))) ;; give up in five seconds (if (> *http-requests-in-progress* 0) (if (> etime (current-seconds)) (begin @@ -1769,18 +1791,18 @@ "requests still in progress after 5 seconds of waiting. I'm going to pass on cleaning up http connections")) #;(close-idle-connections!))) (set! *http-connections-next-cleanup* (+ (current-seconds) 10)) (mutex-unlock! *http-mutex*)) -(define (http-transport:inc-requests-and-prep-to-close-all-connections) +(define (rmt:inc-requests-and-prep-to-close-all-connections) (mutex-lock! *http-mutex*) (set! *http-requests-in-progress* (+ 1 *http-requests-in-progress*))) ;; careful closing of connections stored in *runremote* ;; -(define (http-transport:close-connections #!key (area-dat #f)) - (debug:print-info 0 *default-log-port* "http-transport:close-connections doesn't do anything now!")) +(define (rmt:close-connections #!key (area-dat #f)) + (debug:print-info 0 *default-log-port* "rmt:close-connections doesn't do anything now!")) ;; (let* ((runremote (or area-dat *runremote*)) ;; (server-dat (if runremote ;; (remote-conndat runremote) ;; #f))) ;; (hash-table-ref/default *runremote* run-id #f))) ;; (if (vector? server-dat) @@ -2087,42 +2109,42 @@ (exit)) (loop start-time (equal? sdat last-sdat) sdat)))))))) -(define (rmt:register-server remote apath iface port server-key dbname) - (rmt:open-main-connection remote apath) ;; we need a channel to main.db - (rmt:send-receive-real remote apath ;; params: host port servkey pid ipaddr dbpath +(define (rmt:register-server remdat apath iface port server-key dbname) + (rmt:open-main-connection remdat apath) ;; we need a channel to main.db + (rmt:send-receive-real remdat apath ;; params: host port servkey pid ipaddr dbpath (db:run-id->dbname #f) 'register-server `(,iface ,port ,server-key ,(current-process-id) ,iface ,apath ,dbname))) -(define (rmt:get-count-servers remote apath) - (rmt:open-main-connection remote apath) ;; we need a channel to main.db - (rmt:send-receive-real remote apath ;; params: host port servkey pid ipaddr dbpath +(define (rmt:get-count-servers remdat apath) + (rmt:open-main-connection remdat apath) ;; we need a channel to main.db + (rmt:send-receive-real remdat apath ;; params: host port servkey pid ipaddr dbpath (db:run-id->dbname #f) 'get-count-servers `(,apath ))) -(define (rmt:deregister-server remote apath iface port server-key dbname) - (rmt:open-main-connection remote apath) ;; we need a channel to main.db - (rmt:send-receive-real remote apath ;; params: host port servkey pid ipaddr dbpath +(define (rmt:deregister-server remdat apath iface port server-key dbname) + (rmt:open-main-connection remdat apath) ;; we need a channel to main.db + (rmt:send-receive-real remdat apath ;; params: host port servkey pid ipaddr dbpath (db:run-id->dbname #f) 'deregister-server `(,iface ,port ,server-key ,(current-process-id) ,iface ,apath ,dbname))) -(define (http-transport:wait-for-stable-interface #!optional (num-tries-allowed 100)) +(define (rmt:wait-for-stable-interface #!optional (num-tries-allowed 100)) ;; wait until *server-info* stops changing (let* ((stime (current-seconds))) (let loop ((last-host #f) (last-port #f) (tries 0)) @@ -2129,17 +2151,17 @@ (let* ((curr-host (and *server-info* (servdat-host *server-info*))) (curr-port (and *server-info* (servdat-port *server-info*)))) ;; first we verify port and interface, update *server-info* in need be. (cond ((> tries num-tries-allowed) - (debug:print 0 *default-log-port* "http-transport:keep-running, giving up after trying for several minutes.") + (debug:print 0 *default-log-port* "rmt:keep-running, giving up after trying for several minutes.") (exit 1)) ((not *server-info*) (thread-sleep! 0.25) (loop curr-host curr-port (+ tries 1))) ((or (not last-host)(not last-port)) - (debug:print 0 *default-log-port* "http-transport:keep-running, still no interface, tries="tries) + (debug:print 0 *default-log-port* "rmt:keep-running, still no interface, tries="tries) (thread-sleep! 0.25) (loop curr-host curr-port (+ tries 1))) ((or (not (equal? last-host curr-host)) (not (equal? last-port curr-port))) (debug:print-info 0 *default-log-port* "WARNING: interface changed, refreshing iface and port info") @@ -2157,11 +2179,11 @@ " AT " (current-seconds) " server signature: " *my-signature* " with "(servdat-trynum *server-info*)" port changes") (flush-output *default-log-port*) #t)))))) -;; run http-transport:keep-running in a parallel thread to monitor that the db is being +;; run rmt:keep-running in a parallel thread to monitor that the db is being ;; used and to shutdown after sometime if it is not. ;; (define (rmt:keep-running dbname) ;; if none running or if > 20 seconds since ;; server last used then start shutdown @@ -2174,12 +2196,12 @@ (is-main (equal? (args:get-arg "-db") ".db/main.db")) (last-access 0) (server-timeout (server:expiration-timeout))) ;; main and run db servers have both got wait logic (could/should merge it) (if is-main - (http-transport:wait-for-server pkts-dir dbname server-key) - (http-transport:wait-for-stable-interface)) + (rmt:wait-for-server pkts-dir dbname server-key) + (rmt:wait-for-stable-interface)) ;; this is our forever loop (let* ((iface (servdat-host *server-info*)) (port (servdat-port *server-info*))) (let loop ((count 0) (bad-sync-count 0) @@ -2196,11 +2218,11 @@ (debug:print 0 *default-log-port* "SERVER: dbprep") (db:setup dbname) ;; sets *dbstruct-db* as side effect (servdat-status-set! *server-info* 'db-opened) ;; IFF I'm not main, call into main and register self (if (not is-main) - (let ((res (rmt:register-server *rmt:remote* + (let ((res (rmt:register-server *remotedat* *toppath* iface port server-key dbname))) (if res ;; we are the server (servdat-status-set! *server-info* 'have-interface-and-db) (let* ((serv-info (rmt:get-server-info *toppath* dbname))) @@ -2261,11 +2283,11 @@ (cond ((and *server-run* (> (+ last-access server-timeout) (current-seconds)) (if is-main - (> (rmt:get-count-servers *rmt:remote* *toppath*) 1) + (> (rmt:get-count-servers *remotedat* *toppath*) 1) #t)) (if (common:low-noise-print 120 "server continuing") (debug:print-info 0 *default-log-port* "Server continuing, seconds since last db access: " (- (current-seconds) last-access))) (loop 0 bad-sync-count (current-milliseconds))) (else @@ -2355,79 +2377,93 @@ (exit 1)) (nng-dial #;nn-bind rep (conc "tcp://*:" portnum))) rep)) -;; open connection to server, send message, close connection -;; -(define (open-send-close-nn host-port msg #!key (timeout 3) ) ;; default timeout is 3 seconds +(define (open-nn-connection host-port) (let ((req (make-req-socket 'req)) - (uri (conc "tcp://" host-port)) - (res #f) - ;; (contacts (alist-ref 'contact attrib)) - ;; (mode (alist-ref 'mode attrib)) - ) + (uri (conc "tcp://" host-port))) (socket-set! req 'nng/recvtimeo 2000) - (handle-exceptions - exn - (let ((emsg ((condition-property-accessor 'exn 'message) exn))) - ;; Send notification - (debug:print 0 *default-log-port* "ERROR: Failed to connect / send to " uri " message was \"" emsg "\"" ) - #f) - (nng-dial req uri) - ;; (print "Connected to the server " ) - (nng-send req msg) - ;; (print "Request Sent") - (let* ((th1 (make-thread (lambda () - (let ((resp (nng-recv req))) - (nng-close! req) - (set! res (if (equal? resp "ok") - #t - #f)))) - "recv thread")) - (th2 (make-thread (lambda () - (thread-sleep! timeout) - (thread-terminate! th1)) - "timer thread"))) - (thread-start! th1) - (thread-start! th2) - (thread-join! th1) - res)))) - -(define (open-send-receive-nn host-port msg #!key (timeout 3) ) ;; default timeout is 3 seconds - (let ((req (make-req-socket)) - (uri (conc "tcp://" host-port)) - (res #f) - ;; (contacts (alist-ref 'contact attrib)) - ;; (mode (alist-ref 'mode attrib)) - ) - (handle-exceptions - exn - (let ((emsg ((condition-property-accessor 'exn 'message) exn))) - ;; Send notification - (debug:print 0 *default-log-port* "ERROR: Failed to connect / send to " uri " message was \"" emsg "\", exn=" exn) - #f) - (nng-dial req uri) - ;; (print "Connected to the server " ) - (nng-send req msg) - ;; (print "Request Sent") - ;; receive code here - ;;(print (nn-recv req)) - (let* ((th1 (make-thread (lambda () - (let ((resp (nng-recv req))) - (nng-close! req) - (print resp) - (set! res resp))) - "recv thread")) - (th2 (make-thread (lambda () - (thread-sleep! timeout) - (thread-terminate! th1)) - "timer thread"))) - (thread-start! th1) - (thread-start! th2) - (thread-join! th1) - res)))) + (nng-dial req uri) + req)) + +(define (send-receive-nn req msg) + (nng-send req msg) + (nng-recv req)) + +(define (close-nn-connection req) + (nng-close! req)) + +;; ;; open connection to server, send message, close connection +;; ;; +;; (define (open-send-close-nn host-port msg #!key (timeout 3) ) ;; default timeout is 3 seconds +;; (let ((req (make-req-socket 'req)) +;; (uri (conc "tcp://" host-port)) +;; (res #f) +;; ;; (contacts (alist-ref 'contact attrib)) +;; ;; (mode (alist-ref 'mode attrib)) +;; ) +;; (socket-set! req 'nng/recvtimeo 2000) +;; (handle-exceptions +;; exn +;; (let ((emsg ((condition-property-accessor 'exn 'message) exn))) +;; ;; Send notification +;; (debug:print 0 *default-log-port* "ERROR: Failed to connect / send to " uri " message was \"" emsg "\"" ) +;; #f) +;; (nng-dial req uri) +;; ;; (print "Connected to the server " ) +;; (nng-send req msg) +;; ;; (print "Request Sent") +;; (let* ((th1 (make-thread (lambda () +;; (let ((resp (nng-recv req))) +;; (nng-close! req) +;; (set! res (if (equal? resp "ok") +;; #t +;; #f)))) +;; "recv thread")) +;; (th2 (make-thread (lambda () +;; (thread-sleep! timeout) +;; (thread-terminate! th1)) +;; "timer thread"))) +;; (thread-start! th1) +;; (thread-start! th2) +;; (thread-join! th1) +;; res)))) +;; +;; (define (open-send-receive-nn host-port msg #!key (timeout 3) ) ;; default timeout is 3 seconds +;; (let ((req (make-req-socket)) +;; (uri (conc "tcp://" host-port)) +;; (res #f) +;; ;; (contacts (alist-ref 'contact attrib)) +;; ;; (mode (alist-ref 'mode attrib)) +;; ) +;; (handle-exceptions +;; exn +;; (let ((emsg ((condition-property-accessor 'exn 'message) exn))) +;; ;; Send notification +;; (debug:print 0 *default-log-port* "ERROR: Failed to connect / send to " uri " message was \"" emsg "\", exn=" exn) +;; #f) +;; (nng-dial req uri) +;; ;; (print "Connected to the server " ) +;; (nng-send req msg) +;; ;; (print "Request Sent") +;; ;; receive code here +;; ;;(print (nn-recv req)) +;; (let* ((th1 (make-thread (lambda () +;; (let ((resp (nng-recv req))) +;; (nng-close! req) +;; ;; (print resp) +;; (set! res resp))) +;; "recv thread")) +;; (th2 (make-thread (lambda () +;; (thread-sleep! timeout) +;; (thread-terminate! th1)) +;; "timer thread"))) +;; (thread-start! th1) +;; (thread-start! th2) +;; (thread-join! th1) +;; res)))) ;;====================================================================== ;; S E R V E R U T I L I T I E S ;;====================================================================== @@ -2511,17 +2547,17 @@ ;; '(/ "loop-test")) ;; (send-response body: (alist-ref 'data ($)) ;; headers: '((content-type text/plain)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ "")) -;; (send-response body: ((http-get-function 'http-transport:main-page)))) +;; (send-response body: ((http-get-function 'rmt:main-page)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ "json_api")) -;; (send-response body: ((http-get-function 'http-transport:main-page)))) +;; (send-response body: ((http-get-function 'rmt:main-page)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ "runs")) -;; (send-response body: ((http-get-function 'http-transport:main-page)))) +;; (send-response body: ((http-get-function 'rmt:main-page)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ any)) ;; (send-response body: "hey there!\n" ;; headers: '((content-type text/plain)))) ;; ((equal? (uri-path (request-uri (current-request))) @@ -2528,16 +2564,16 @@ ;; '(/ "hey")) ;; (send-response body: "hey there!\n" ;; headers: '((content-type text/plain)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ "jquery3.1.0.js")) -;; (send-response body: ((http-get-function 'http-transport:show-jquery)) +;; (send-response body: ((http-get-function 'rmt:show-jquery)) ;; headers: '((content-type application/javascript)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ "test_log")) -;; (send-response body: ((http-get-function 'http-transport:html-test-log) $) +;; (send-response body: ((http-get-function 'rmt:html-test-log) $) ;; headers: '((content-type text/HTML)))) ;; ((equal? (uri-path (request-uri (current-request))) ;; '(/ "dashboard")) -;; (send-response body: ((http-get-function 'http-transport:html-dboard) $) +;; (send-response body: ((http-get-function 'rmt:html-dboard) $) ;; headers: '((content-type text/HTML)))) ;; (else (continue)))))))) Index: tests/unittests/basicserver.scm ================================================================== --- tests/unittests/basicserver.scm +++ tests/unittests/basicserver.scm @@ -21,11 +21,11 @@ ;; Run like this: ;; ;; ./rununittest.sh server 1;(cd simplerun;megatest -stop-server 0) (import rmtmod trace http-client apimod dbmod - launchmod) + launchmod srfi-69) (trace-call-sites #t) (trace ;; db:get-dbdat ;; rmt:find-main-server