︙ | | | ︙ | |
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
typed-records srfi-69 srfi-1
srfi-4 regex-case
(prefix sqlite3 sqlite3:)
foreign
tcp6
;; ulex-netutil
hostinfo)
;;======================================================================
;; network utilities
;;======================================================================
(define (rate-ip ipaddr)
(regex-case ipaddr
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
typed-records srfi-69 srfi-1
srfi-4 regex-case
(prefix sqlite3 sqlite3:)
foreign
tcp6
;; ulex-netutil
hostinfo)
;;======================================================================
;; KEY FUNCTIONS - THESE ARE TOO BE EXPOSED AND USED
;;======================================================================
;; connection setup and management functions
;; This is the basic setup command. Must always be
;; called before connecting to a db using connect.
;;
;; find or become the captain
;; setup and return a ulex object
;;
(define (setup)
(let* ((udata (make-udat))
(cpkts (get-all-captain-pkts udata)) ;; read captain pkts
(captn (get-winning-pkt cpkts)))
(if captn
(let* ((port (alist-ref 'port captn))
(host (alist-ref 'host captn))
(ipaddr (alist-ref 'ipaddr captn))
(pid (alist-ref 'pid captn))
(Z (alist-ref 'Z captn)))
(udat-captain-address-set! udata ipaddr)
(udat-captain-host-set! udata host)
(udat-captain-port-set! udata port)
(udat-captain-pid-set! udata pid)
(if (ping udata ipaddr port)
udata
(begin
(remove-captain-pkt udata captn)
(setup))))
(setup-as-captain udata)) ;; this saves the thread to captain-thread and starts the thread
))
;; connect to a specific dbfile
(define (connect udata dbfname)
udata)
(define (ping udata host-port)
(let ((cookie (make-cookie udata)))
(send udata host-port 'ping "just pinging" (current-seconds))
;; (mailbox-rec
))
;;======================================================================
;; network utilities
;;======================================================================
(define (rate-ip ipaddr)
(regex-case ipaddr
|
︙ | | | ︙ | |
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
(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
(addr-port #f)
(hostname #f)
|
>
>
|
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
(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 ]
;; cookies
(cnum 0) ;; cookie num
)
;; struct for keeping track of others we are talking to
(defstruct peer
(addr-port #f)
(hostname #f)
|
︙ | | | ︙ | |
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
(let ((ad (string->number (alist-ref 'D a)))
(bd (string->number (alist-ref 'D b))))
(if (eq? a b)
(let ((az (alist-ref 'Z a))
(bz (alist-ref 'Z b)))
(string>=? az bz))
(> ad bd))))))))
;; create a tcp listener and return a populated udat struct with
;; my port, address, hostname, pid etc.
;; return #f if fail to find a port to allocate.
;;
(define (start-server-find-port udata #!optional (port 4242))
(handle-exceptions
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
(let ((ad (string->number (alist-ref 'D a)))
(bd (string->number (alist-ref 'D b))))
(if (eq? a b)
(let ((az (alist-ref 'Z a))
(bz (alist-ref 'Z b)))
(string>=? az bz))
(> ad bd))))))))
;; remove pkt associated with captn (the Z key .pkt)
;;
(define (remove-captain-pkt udata captn)
(let ((Z (alist-ref 'Z captn))
(cpktdir (udat-cpkts-dir udata)))
(delete-file* (conc cpktdir "/" Z ".pkt"))))
;;======================================================================
;; server primitives
;;======================================================================
(define (make-cookie udata)
(let ((newcnum (+ (udat-cnum udata))))
(udat-cnum-set! udata newcnum)
(conc (udat-my-address udata) ":"
(udat-my-port udata) "-"
(udat-my-pid udata) "-"
newcnum)))
;; create a tcp listener and return a populated udat struct with
;; my port, address, hostname, pid etc.
;; return #f if fail to find a port to allocate.
;;
(define (start-server-find-port udata #!optional (port 4242))
(handle-exceptions
|
︙ | | | ︙ | |
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
npdat))))
pdat))
(define (get-peer-ports udata host-port #!optional (hostname #f)(pid #f))
(let ((pdat (get-peer-dat udata host-port hostname pid)))
(values (peer-inp pdat)(peer-oup pdat))))
;; send back ack, amusing I suppose that this looks almost like what
;; tcp itself does ...
;;
(define (reply udata host-port handler qrykey data #!optional (hostname #f)(pid #f))
(let-values (((inp oup)(get-peer-ports udata host-port hostname pid)))
(write-line (conc
handler " "
(udat-my-address udata) ":" (udat-my-port udata) " "
(udat-my-hostname udata) " "
(udat-my-pid udata) " "
qrykey)
oup)
(write-line data oup))) ;; we must send a second line - for the ack let it be the qrykey
(define (add-to-work-queue udata . blah)
#f)
;; send back ack
;;
(define (send-ack udata host-port qrykey #!optional (hostname #f)(pid #f))
(reply udata "ack" qrykey qrykey hostname pid)) ;; we must send a second line - for the ack let it be the qrykey
;;
;;
(define (ulex-handler udata)
(let* ((serv-listener (udat-serv-listener udata)))
(let-values (((inp oup)(tcp-accept serv-listener)))
;; data comes as two lines
;; handlerkey resp-addr:resp-port hostname pid qrykey [dbpath/dbfile.db]
;; data
(let loop ((state 'start))
(let* ((controldat (read-line inp))
(data (read-line inp)))
(match (string-split controldat)
((handlerkey host:port hostname pid qrykey params ...)
(case (string->symbol handlerkey)
((ack)(print "Got ack!"))
((ping)
(let* ((proc (hash-table-ref/default (udat-handlers udata) 'ping #f))
(val (if proc (proc) "gotping")))
(reply udata host:port "version" qrykey val)))
((rucaptain)
(reply udata host:port "iamcaptain" qrykey (if (udat-my-cpkt-key udata)
"yes"
"no")))
(else
(send-ack udata host:port qrykey hostname pid)
(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))
;;======================================================================
;; Generic db handling
;;======================================================================
(defstruct dbconn
(inmem #f)
(conn #f)
(sync #f) ;; sync proc
(init #f) ;; init proc
|
|
|
>
|
>
>
>
>
|
|
|
>
|
>
>
>
|
>
>
|
|
|
|
|
|
>
>
>
>
>
>
|
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
npdat))))
pdat))
(define (get-peer-ports udata host-port #!optional (hostname #f)(pid #f))
(let ((pdat (get-peer-dat udata host-port hostname pid)))
(values (peer-inp pdat)(peer-oup pdat))))
;; send structured data to recipient
;;
;; NOTE: qrykey is what was called the "cookie" previously
;;
(define (send udata host-port handler qrykey data #!key (hostname #f)(pid #f)(params '()))
(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 ...
;;
(write-line (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 " "))))
oup)
(write-line data oup)
;; NOTE: DO NOT BE TEMPTED TO LOOK AT ANY DATA ON INP HERE!
;; (there is a listener for handling that)
))
(define (add-to-work-queue udata . blah)
#f)
;; send back ack - this is tcp we are talking about, do we really need an ack?
;;
;; NOTE: No need to send back host:port of self - that is locked in by qrykey
;;
(define (send-ack udata host-port qrykey) ;; #!optional (hostname #f)(pid #f))
(send udata host-port "ack" qrykey qrykey)) ;; we must send a second line - for the ack let it be the qrykey
;;
;;
(define (ulex-handler udata)
(let* ((serv-listener (udat-serv-listener udata)))
(let-values (((inp oup)(tcp-accept serv-listener)))
;; data comes as two lines
;; handlerkey resp-addr:resp-port hostname pid qrykey [dbpath/dbfile.db]
;; data
(let loop ((state 'start))
(let* ((controldat (read-line inp))
(data (read-line inp)))
(match (string-split controldat)
((handlerkey host:port pid qrykey cookie params ...)
(case (string->symbol handlerkey)
((ack)(print "Got ack!"))
((ping)
(let* ((proc (hash-table-ref/default (udat-handlers udata) 'ping #f))
(val (if proc (proc) "gotping")))
(send udata host:port "version" qrykey cookie val)))
((rucaptain)
(send udata host:port "iamcaptain" qrykey (if (udat-my-cpkt-key udata)
"yes"
"no")))
(else
(send-ack udata host:port qrykey)
(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))
;;======================================================================
;; Generic db handling
;; setup a inmem db instance
;; open connection to on-disk db
;; sync on-disk db to inmem
;; get lock in on-disk db for dbowner of this db
;; put sync-proc, init-proc, on-disk handle, inmem handle in dbconn stuct
;; return the stuct
;;======================================================================
(defstruct dbconn
(inmem #f)
(conn #f)
(sync #f) ;; sync proc
(init #f) ;; init proc
|
︙ | | | ︙ | |
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
#f))))))
;; open databases, do initial sync
(define (ulexdb-sync dbconndat udata)
#f)
;;======================================================================
;; connection setup and management functions
;;======================================================================
;; find or become the captain, return a ulex object
;;
(define (setup)
(let* ((udata (make-udat))
(cpkts (get-all-captain-pkts udata)) ;; read captain pkts
(captn (get-winning-pkt cpkts)))
(if captn
(let* ((port (alist-ref 'port captn))
(host (alist-ref 'host captn))
(ipaddr (alist-ref 'ipaddr captn))
(pid (alist-ref 'pid captn)))
(udat-captain-address-set! udata ipaddr)
(udat-captain-host-set! udata host)
(udat-captain-port-set! udata port)
(udat-captain-pid-set! udata pid)
;;(if (ping-captain udata)
;; udata
;; (begin
;; (remove-captain-pkt udata captn)
;; (setup)))
udata)
(setup-as-captain udata)) ;; this saves the thread to captain-thread and starts the thread
))
(define (connect udata dbfname)
udata)
) ;; END OF ULEX
;;; ;;======================================================================
;;; ;; D E B U G H E L P E R S
;;; ;;======================================================================
;;;
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
#f))))))
;; open databases, do initial sync
(define (ulexdb-sync dbconndat udata)
#f)
) ;; END OF ULEX
;;; ;;======================================================================
;;; ;; D E B U G H E L P E R S
;;; ;;======================================================================
;;;
|
︙ | | | ︙ | |