Megatest

Check-in [1b8dcc586b]
Login
Overview
Comment:Switched back to tcp6
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v2.0001-ulex-one-shot
Files: files | file ages | folders
SHA1: 1b8dcc586bd6bbddb8dd55e075ed29c2f42e71f5
User & Date: matt on 2022-01-12 17:49:05
Other Links: branch diff | manifest | tags
Context
2022-01-13
06:18
wip check-in: fb2aca7823 user: matt tags: v2.0001-ulex-one-shot
2022-01-12
17:49
Switched back to tcp6 check-in: 1b8dcc586b user: matt tags: v2.0001-ulex-one-shot
16:52
Sync up with v2.0001 check-in: d7b4fe7a7f user: matt tags: v2.0001-ulex-one-shot
Changes

Modified ulex-simple/ulex.scm from [eb641ce140] to [3a037ef6f7].

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
	chicken.base
	chicken.file
	chicken.time
	chicken.condition
	chicken.string
	chicken.sort
	chicken.pretty-print
	chicken.tcp
	
	address-info
	mailbox
	matchable
	;; queues
	regex
	regex-case
	s11n
	srfi-1
	srfi-18
	srfi-4
	srfi-69
	system-information
	;; tcp6
	typed-records
	tcp-server
	
	)

;; udat struct, used by both caller and callee
;; instantiated as uconn by convention
;;
(defstruct udat







|













|

|







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
	chicken.base
	chicken.file
	chicken.time
	chicken.condition
	chicken.string
	chicken.sort
	chicken.pretty-print
	;; chicken.tcp
	
	address-info
	mailbox
	matchable
	;; queues
	regex
	regex-case
	s11n
	srfi-1
	srfi-18
	srfi-4
	srfi-69
	system-information
	tcp6
	typed-records
	;; tcp-server
	
	)

;; udat struct, used by both caller and callee
;; instantiated as uconn by convention
;;
(defstruct udat
99
100
101
102
103
104
105

106
107
108
109
110
111
112
  (cnum       0)                 ;; cookie number
  (mboxes     (make-hash-table)) ;; for the replies
  (avail-cmboxes '())            ;; list of (<cookie> . <mbox>) for re-use
  ;; threads
  (numthreads 50)
  (cmd-thread #f)
  (work-queue-thread #f)

  ) 

;; ;; struct for keeping track of others we are talking to
;; ;;
;; (defstruct pdat
;;   (host-port  #f)
;;   (conns      '()) ;; list of pcon structs, pop one off when calling the peer







>







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  (cnum       0)                 ;; cookie number
  (mboxes     (make-hash-table)) ;; for the replies
  (avail-cmboxes '())            ;; list of (<cookie> . <mbox>) for re-use
  ;; threads
  (numthreads 50)
  (cmd-thread #f)
  (work-queue-thread #f)
  (num-threads-running 0)
  ) 

;; ;; struct for keeping track of others we are talking to
;; ;;
;; (defstruct pdat
;;   (host-port  #f)
;;   (conns      '()) ;; list of pcon structs, pop one off when calling the peer
201
202
203
204
205
206
207
208
209
210
211
212
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
247
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
	 ;; dat is a self-contained work block that can be sent or handled locally
	 (dat          (list my-host-port qrykey cmd params))
	 (parts        (string-split host-port ":"))
	 (host         (car parts))
	 (port         (string->number (cadr parts))))
    (if isme
	(ulex-handler udata dat) ;; no transmission needed
	;; (handle-exceptions ;; TODO - MAKE THIS EXCEPTION CMD SPECIFIC?
	;;  exn
	;;  #f
	  (let-values (((inp oup)(tcp-connect host port)))
	    (let ((res (if (and inp oup)
			   (begin
			     (serialize dat oup)
			     (deserialize inp)) ;; yes, we always want an ack
			   (begin
			     (print "ERROR: send called but no receiver has been setup. Please call setup first!")
			     #f))))
	      (close-input-port inp)
	      (close-output-port oup)
	      (mutex-unlock! *send-mutex*)
	      res))))) ;; res will always be 'ack

;; send a request to the given host-port and register a mailbox in udata
;; wait for the mailbox data and return it
;;
(define (send-receive uconn host-port cmd data)
  (send uconn host-port 'qrykey cmd data)
  #;(cond
   ((member cmd '(ping goodbye)) ;; these are immediate
    (send uconn host-port 'ping cmd data))
   (else
    (let* ((cmbox     (get-cmbox uconn)) ;; would it be better to keep a stack of mboxes to reuse?
	   (qrykey    (car cmbox))
	   (mbox      (cdr cmbox))
	   (mbox-time (current-milliseconds))
	   (sres      (send uconn host-port qrykey cmd data))) ;; short res
      sres))))

;;======================================================================
;; responder side
;;======================================================================

;; take a request, rdat, and if not immediate put it in the work queue
;;
;; Reserved cmds; ack ping goodbye response
;;
(define (ulex-handler uconn rdat)
  (assert (list? rdat) "FATAL: ulex-handler give rdat as not list")
  (match rdat ;;  (string-split controldat)
    ((rem-host-port qrykey cmd params)
     ;; (print "ulex-handler got: "rem-host-port" qrykey: "qrykey" cmd: "cmd" params: "params)
     (let ((mbox (hash-table-ref/default (udat-mboxes uconn) qrykey #f)))
       (case cmd
	 ;; ((ack )(print "Got ack! But why? Should NOT get here.") 'ack)
	 ((ping)
	  ;; (print "Got Ping!")
	  ;; (add-to-work-queue uconn rdat)
	 'ack)
	 (else
	  (do-work uconn rdat)))))
    (else
     (print "BAD DATA? controldat=" rdat)
     'ack) ;; send ack anyway?

    ))















;; given an already set up uconn start the cmd-loop
;;
(define (ulex-cmd-loop uconn)
  (let* ((serv-listener (udat-socket uconn))
	 (server        (make-tcp-server

			 serv-listener
			 (lambda ()
			   (let* ((rdat  (read)#;(deserialize)) ;; '(my-host-port qrykey cmd params)
				  (resp  #;(ulex-handler uconn rdat)
				   (do-work uconn rdat)))
			     (if resp
				 #;(serialize resp)
				 (write resp)
				 (begin
				   (print "ULEX ERROR: communication error in ulex-cmd-loop.")

				   resp)))))))


    (server)))







;; add a proc to the cmd list, these are done symetrically (i.e. in all instances)
;; so that the proc can be dereferenced remotely
;;
(define (set-work-handler uconn proc)
  (udat-work-proc-set! uconn proc))








<
<
<
|
|
|
|
|
|
|
|
|
|
|
|





|
<
<
<
<
<
<
<
<
<
<











|

<
<
<
<
<
<
<
<
<
|


<
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>





|
>
|
<
|
|
<
<
|
<
|
<
>
|
>
>
|
>
>
>
>
>
>







202
203
204
205
206
207
208



209
210
211
212
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
247
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
	 ;; dat is a self-contained work block that can be sent or handled locally
	 (dat          (list my-host-port qrykey cmd params))
	 (parts        (string-split host-port ":"))
	 (host         (car parts))
	 (port         (string->number (cadr parts))))
    (if isme
	(ulex-handler udata dat) ;; no transmission needed



	(let-values (((inp oup)(tcp-connect host port)))
	  (let ((res (if (and inp oup)
			 (begin
			   (serialize dat oup)
			   (deserialize inp)) ;; yes, we always want an ack
			 (begin
			   (print "ERROR: send called but no receiver has been setup. Please call setup first!")
			   #f))))
	    (close-input-port inp)
	    (close-output-port oup)
	    (mutex-unlock! *send-mutex*)
	    res)))))

;; send a request to the given host-port and register a mailbox in udata
;; wait for the mailbox data and return it
;;
(define (send-receive uconn host-port cmd data)
  (send uconn host-port 'qrykey cmd data))











;;======================================================================
;; responder side
;;======================================================================

;; take a request, rdat, and if not immediate put it in the work queue
;;
;; Reserved cmds; ack ping goodbye response
;;
(define (ulex-handler uconn rdat)
  (assert (list? rdat) "FATAL: ulex-handler give rdat as not list")
  (match rdat
    ((rem-host-port qrykey cmd params)









     (do-work uconn rdat))
    (else
     (print "BAD DATA? controldat=" rdat)

     'bad-data)
    ))

;; given an already set up uconn start the cmd-loop
;;
#;(define (ulex-cmd-loop uconn)
  (let* ((serv-listener (udat-socket uconn))
	 (server        (make-tcp-server
			 serv-listener
			 (lambda ()
			   (let* ((rdat  (deserialize)) ;; '(my-host-port qrykey cmd params)
				  (resp  (ulex-handler uconn rdat)))
			     (if resp
				 (serialize resp)
				 (write resp)))))))
    (server)))

;; given an already set up uconn start the cmd-loop
;;
(define (ulex-cmd-loop uconn)
  (let* ((serv-listener (udat-socket uconn))
	 (listener      (lambda ()
			  (let loop ((state 'start))
			    (let-values (((inp oup)(tcp-accept serv-listener)))

			      (let* ((rdat  (deserialize inp)) ;; '(my-host-port qrykey cmd params)
				     (resp  (ulex-handler uconn rdat)))


				(serialize resp oup)

				(close-input-port inp)

				(close-output-port oup))
			      (loop state))))))
   ;; start N of them
   (let loop ((thnum   0)
	       (threads '()))
      (if (< thnum 100)
	  (let* ((th (make-thread listener (conc "listener" thnum))))
	    (thread-start! th)
	    (loop (+ thnum 1)
		  (cons th threads)))
	  (map thread-join! threads)))))

;; add a proc to the cmd list, these are done symetrically (i.e. in all instances)
;; so that the proc can be dereferenced remotely
;;
(define (set-work-handler uconn proc)
  (udat-work-proc-set! uconn proc))

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
349
350
351
	      (result (proc rem-host-port qrykey cmd params))
	      (end-time (current-milliseconds))
	      (run-time (- end-time start-time)))
	 result))
      (else
       (print "ERROR: rdat "rdat", did not match rem-host-port qrykey cmd params")
       #f))))
     
;; below was to enable re-use of connections. This seems non-trivial so for
;; now lets open on each call
;;
;; ;; given host-port get or create peer struct
;; ;;
;; (define (udat-get-peer uconn host-port)
;;   (or (hash-table-ref/default (udat-peers uconn) host-port #f)
;;       ;; no peer, so create pdat and init it
;;       
;;       ;; NEED stack of connections, pop and use; inp, oup,
;;       ;; creation_time (remove and create new if over 24hrs old
;;       ;; 
;;       (let ((pdat (make-pdat host-port: host-port)))
;; 	(hash-table-set! (udat-peers uconn) host-port pdat)
;; 	pdat)))
;; 
;; ;; is pcon alive
;; 
;; ;; given host-port and pdat get a pcon
;; ;;
;; (define (pdat-get-pcon pdat host-port)
;;   (let loop ((conns (pdat-conns pdat)))
;;     (if (null? conns) ;; none? make and return - do NOT add - it will be pushed back on list later
;; 	(init-pcon (make-pcon))
;; 	(let* ((conn (pop conns)))
;; 	  
;; ;; given host-port get a pcon struct
;; ;;
;; (define (udat-get-pcon 
      
;;======================================================================
;; misc utils
;;======================================================================

(define (make-cookie uconn)
  (let ((newcnum (+ (udat-cnum uconn) 1)))
    (udat-cnum-set! uconn newcnum)







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







305
306
307
308
309
310
311
312






























313
314
315
316
317
318
319
	      (result (proc rem-host-port qrykey cmd params))
	      (end-time (current-milliseconds))
	      (run-time (- end-time start-time)))
	 result))
      (else
       (print "ERROR: rdat "rdat", did not match rem-host-port qrykey cmd params")
       #f))))
           






























;;======================================================================
;; misc utils
;;======================================================================

(define (make-cookie uconn)
  (let ((newcnum (+ (udat-cnum uconn) 1)))
    (udat-cnum-set! uconn newcnum)

Modified ulex.scm from [f004a2cedd] to [64369b6c76].

16
17
18
19
20
21
22
23
24
;;     You should have received a copy of the GNU General Public License
;;     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.

;;======================================================================

(declare (unit ulex))

(include "ulex/ulex.scm")
;; (include "ulex-simple/ulex.scm")







|
|
16
17
18
19
20
21
22
23
24
;;     You should have received a copy of the GNU General Public License
;;     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.

;;======================================================================

(declare (unit ulex))

;; (include "ulex/ulex.scm")
(include "ulex-simple/ulex.scm")