103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
;; struct for keeping track of our world
(defstruct udat
(captain-address #f)
(captain-host #f)
(captain-port #f)
(captain-pid #f)
(cpkts-dir (conc (get-environment-variable "HOME") "/.ulex/pkts"))
(cpkt-spec *captain-pktspec*)
(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))
|
>
|
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
;; struct for keeping track of our world
(defstruct udat
(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*)
(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))
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
(add-to-work-queue udata (get-peer-dat udata host:port) handlerkey data)))
(else (print "BAD DATA? handler=" handlerkey " data=" data)))))
(loop state)))))
;; add a proc to the handler list
(define (register-handler udata key proc)
(hash-table-set! (udat-handlers udata) key proc))
;;======================================================================
;; connection setup and management functions
;;======================================================================
;; find or become the captain, return a ulex object
;;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
(add-to-work-queue udata (get-peer-dat udata host:port) handlerkey data)))
(else (print "BAD DATA? handler=" handlerkey " data=" data)))))
(loop state)))))
;; add a proc to the handler list
(define (register-handler udata key proc)
(hash-table-set! (udat-handlers udata) key proc))
;;======================================================================
;; Ulex db
;;======================================================================
;; dest='inmem or 'disk
;;
(define (open-ulexdb udata dest)
(let* ((dbfile (conc (udat-ulex-dir udata) "/ulex.db"))
(dbexists (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
(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
FOR EACH ROW
BEGIN
UPDATE processes SET last_update=(strftime('%s','now'))
WHERE id=old.id;
END;"
#f))))))
db))
;;======================================================================
;; connection setup and management functions
;;======================================================================
;; find or become the captain, return a ulex object
;;
|