Index: ulex/ulex.scm ================================================================== --- ulex/ulex.scm +++ ulex/ulex.scm @@ -101,26 +101,32 @@ ))) ;; struct for keeping track of our world (defstruct udat + ;; captain info (captain-address #f) (captain-host #f) (captain-port #f) (captain-pid #f) (ulex-dir (conc (get-environment-variable "HOME") "/.ulex")) (cpkts-dir (conc (get-environment-variable "HOME") "/.ulex/pkts")) (cpkt-spec *captain-pktspec*) + ;; this processes info (my-cpkt-key #f) ;; put Z card here when I create a pkt for myself as captain (my-address #f) (my-hostname #f) (my-port #f) (my-pid (current-process-id)) - (serv-listener #f) + ;; server and handler thread + (serv-listener #f) ;; this processes server info (handler-thread #f) (handlers (make-hash-table)) (outgoing-conns (make-hash-table)) ;; host:port -> conn + ;; app info + (appname #f) + (dbtypes (make-hash-table)) ;; this should be an alist but hash is easier. dbtype => [ initproc syncproc ] ) ;; struct for keeping track of others we are talking to (defstruct peer @@ -301,45 +307,91 @@ (define (register-handler udata key proc) (hash-table-set! (udat-handlers udata) key proc)) ;;====================================================================== -;; Ulex db +;; Generic db handling ;;====================================================================== +(defstruct dbconn + (inmem #f) + (conn #f) + (sync #f) ;; sync proc + (init #f) ;; init proc + (lastsync (current-seconds)) + ) + +(defstruct dbinfo + (initproc #f) + (syncproc #f)) + +;; open inmem and disk database +;; init with initproc +;; return db struct +;; +;; appname; megatest, ulex or something else. +;; +(define (setup-db-connection udata fname-in appname dbtype) + (let* ((is-ulex (eq? appname 'ulex)) + (dbinf (if is-ulex ;; ulex is a built-in special case + (make-dbinfo initproc: ulexdb-init syncproc: ulexdb-sync) + (hash-table-ref/default (udat-dbtypes udata) dbtype #f))) + (initproc (dbinfo-initproc dbinf)) + (syncproc (dbinfo-syncproc dbinf)) + (fname (if is-ulex + (conc (udat-ulex-dir udata) "/ulex.db") + fname-in)) + (inmem-db (open-and-initdb udata #f 'inmem (dbinfo-initproc dbinf))) + (disk-db (open-and-initdb udata fname 'disk (dbinfo-initproc dbinf)))) + (make-dbconn inmem: inmem-db conn: disk-db sync: syncproc init: initproc))) + ;; dest='inmem or 'disk ;; -(define (open-ulexdb udata dest) - (let* ((dbfile (conc (udat-ulex-dir udata) "/ulex.db")) - (dbexists (file-exists? dbfile)) +(define (open-and-initdb udata filename dest init-proc) + (let* ((inmem (eq? dest 'inmem)) + (dbfile (if inmem + ":INMEM:" + filename)) + (dbexists (if inmem #t (file-exists? dbfile))) (db (sqlite3:open-database dbfile))) (sqlite3:set-busy-handler! db (sqlite3:make-busy-timeout 136000)) (if (not dbexists) - (sqlite3:with-transaction - db - (lambda () - (for-each - (lambda (stmt) - (if stmt (sqlite3:execute db stmt))) - `("CREATE TABLE IF NOT EXISTS processes + (init-proc db)) + db)) + + +;;====================================================================== +;; Ulex db +;;====================================================================== + +(define (ulexdb-init db inmem) + (sqlite3:with-transaction + db + (lambda () + (for-each + (lambda (stmt) + (if stmt (sqlite3:execute db stmt))) + `("CREATE TABLE IF NOT EXISTS processes (id INTEGER PRIMARY KEY, host TEXT NOT NULL, ipadr TEXT NOT NULL, port INTEGER NOT NULL, pid INTEGER NOT NULL, regtime INTEGER DEFAULT (strftime('%s','now')), last_update INTEGER DEFAULT (strftime('%s','now')));" - (if (eq? dest 'inmem) - "CREATE TRIGGER IF NOT EXISTS update_proces_trigger AFTER UPDATE ON processes + (if inmem + "CREATE TRIGGER IF NOT EXISTS update_proces_trigger AFTER UPDATE ON processes FOR EACH ROW BEGIN UPDATE processes SET last_update=(strftime('%s','now')) WHERE id=old.id; END;" - #f)))))) - db)) - + #f)))))) + +;; open databases, do initial sync +(define (ulexdb-sync dbconndat udata) + #f) ;;====================================================================== ;; connection setup and management functions ;;======================================================================