︙ | | | ︙ | |
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
(import scheme
chicken.base
chicken.file
chicken.io
chicken.time
chicken.condition
chicken.string
chicken.sort
chicken.pretty-print
chicken.tcp
address-info
mailbox
|
>
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
(import scheme
chicken.base
chicken.file
chicken.io
chicken.time
chicken.condition
chicken.port
chicken.string
chicken.sort
chicken.pretty-print
chicken.tcp
address-info
mailbox
|
︙ | | | ︙ | |
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
srfi-18
srfi-4
srfi-69
system-information
;; tcp6
tcp-server
typed-records
)
;; udat struct, used by both caller and callee
;; instantiated as uconn by convention
;;
(defstruct udat
;; the listener side
|
>
>
>
>
>
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
srfi-18
srfi-4
srfi-69
system-information
;; tcp6
tcp-server
typed-records
md5
message-digest
(prefix base64 base64:)
z3
)
;; udat struct, used by both caller and callee
;; instantiated as uconn by convention
;;
(defstruct udat
;; the listener side
|
︙ | | | ︙ | |
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
(avail-cmboxes '()) ;; list of (<cookie> . <mbox>) for re-use
;; threads
(numthreads 10)
(cmd-thread #f)
(work-queue-thread #f)
(num-threads-running 0)
)
;;======================================================================
;; listener
;;======================================================================
;; is uconn a ulex connector (listener)
;;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
(avail-cmboxes '()) ;; list of (<cookie> . <mbox>) for re-use
;; threads
(numthreads 10)
(cmd-thread #f)
(work-queue-thread #f)
(num-threads-running 0)
)
;;======================================================================
;; serialization
;; NOTE: I've had problems with read/write and s11n serialize, deserialize
;; thus the inefficient method here
;;======================================================================
(define serializing-method (make-parameter 'complex))
;; NOTE: Can remove the regex and base64 encoding for zmq
(define (obj->string obj)
(case (serializing-method)
((complex)
(string-substitute
(regexp "=") "_"
(base64:base64-encode
(z3:encode-buffer
(with-output-to-string
(lambda ()(serialize obj))))) ;; BB: serialize - this is
;; what causes problems
;; between different builds of
;; megatest communicating.
;; serialize is sensitive to
;; binary image of mtest.
#t))
((write)(with-output-to-string (lambda ()(write obj))))
((s11n) (with-output-to-string (lambda ()(serialize obj))))
(else obj))) ;; rpc
(define (string->obj msg #!key (transport 'http))
(case (serializing-method)
((complex)
(if (string? msg)
(with-input-from-string
(z3:decode-buffer
(base64:base64-decode
(string-substitute
(regexp "_") "=" msg #t)))
(lambda ()(deserialize)))
(begin
(print "ULEX ERROR: cannot translate received data \""msg"\"")
(print-call-chain (current-error-port))
msg))) ;; crude reply for when things go awry
((write)(with-input-from-string msg (lambda ()(read))))
((s11n)(with-input-from-string msg (lambda ()(deserialize))))
(else msg))) ;; rpc
;;======================================================================
;; listener
;;======================================================================
;; is uconn a ulex connector (listener)
;;
|
︙ | | | ︙ | |
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
(define (run-listener handler-proc #!optional (port-suggestion 4242))
(let* ((uconn (make-udat)))
(udat-work-proc-set! uconn handler-proc)
(if (setup-listener uconn port-suggestion)
((make-tcp-server
(udat-socket uconn)
(lambda ()
(let* ((rdat (deserialize)) ;; '(my-host-port qrykey cmd params)
(resp (do-work uconn rdat)))
(serialize resp)))))
(assert #f "ERROR: run-listener called without proper setup."))))
(define (wait-and-close uconn)
(thread-join! (udat-cmd-thread uconn))
(tcp-close (udat-socket uconn)))
;;======================================================================
|
|
|
|
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
(define (run-listener handler-proc #!optional (port-suggestion 4242))
(let* ((uconn (make-udat)))
(udat-work-proc-set! uconn handler-proc)
(if (setup-listener uconn port-suggestion)
((make-tcp-server
(udat-socket uconn)
(lambda ()
(let* ((rdat (string->obj (read)) #;(deserialize)) ;; '(my-host-port qrykey cmd params)
(resp (do-work uconn rdat)))
(write (obj->string resp)) #;(serialize resp)))))
(assert #f "ERROR: run-listener called without proper setup."))))
(define (wait-and-close uconn)
(thread-join! (udat-cmd-thread uconn))
(tcp-close (udat-socket uconn)))
;;======================================================================
|
︙ | | | ︙ | |
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
(isme (equal? host-port my-host-port)) ;; calling myself?
;; dat is a self-contained work block that can be sent or handled locally
(dat (list my-host-port 'qrykey cmd params #;(cons (current-seconds)(current-milliseconds)))))
(cond
(isme (do-work udata dat)) ;; no transmission needed
(else
(handle-exceptions ;; TODO - MAKE THIS EXCEPTION CMD SPECIFIC?
exn
(message exn)
(begin
;; (mutex-lock! *send-mutex*) ;; DOESN'T SEEM TO HELP
(let-values (((inp oup)(tcp-connect host port)))
(let ((res (if (and inp oup)
(begin
(serialize dat oup)
(close-output-port oup)
(deserialize inp))
(begin
(print "ERROR: send called but no receiver has been setup. Please call setup first!")
#f))))
(close-input-port inp)
;; (mutex-unlock! *send-mutex*) ;; DOESN'T SEEM TO HELP
res)))))))) ;; res will always be 'ack unless return-method is direct
;;======================================================================
;; work queues - this is all happening on the listener side
;;======================================================================
;; move the logic to return the result somewhere else?
;;
|
|
>
>
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
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
|
(isme (equal? host-port my-host-port)) ;; calling myself?
;; dat is a self-contained work block that can be sent or handled locally
(dat (list my-host-port 'qrykey cmd params #;(cons (current-seconds)(current-milliseconds)))))
(cond
(isme (do-work udata dat)) ;; no transmission needed
(else
(handle-exceptions ;; TODO - MAKE THIS EXCEPTION CMD SPECIFIC?
exn
(begin
(print "ULEX send-receive: exn="exn)
(message exn))
(begin
;; (mutex-lock! *send-mutex*) ;; DOESN'T SEEM TO HELP
(let-values (((inp oup)(tcp-connect host port)))
(let ((res (if (and inp oup)
(begin
(write (obj->string dat) oup) ;; (write dat oup);; (serialize dat oup)
(close-output-port oup)
(string->obj (read inp))) ;; (deserialize inp))
(begin
(print "ERROR: send called but no receiver has been setup. Please call setup first!")
#f))))
;; (close-output-port oup)
(close-input-port inp)
;; (mutex-unlock! *send-mutex*) ;; DOESN'T SEEM TO HELP
res)))))))) ;; res will always be 'ack unless return-method is direct
;;======================================================================
;; work queues - this is all happening on the listener side
;;======================================================================
;; move the logic to return the result somewhere else?
;;
|
︙ | | | ︙ | |