158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
(define (server:launch mode proc)
(let* ((start-time (current-seconds))
(rep (server:start-nmsg mode))
(last-msg (current-seconds))
(th1 (make-thread
(lambda ()
(let loop ()
(let ((pktdat (nmsg:recv rep)))
(set! last-msg (current-seconds))
(print "received: " pktdat)
(if (not (eof-object? pktdat))
(begin
(proc pktdat)
(loop))))))
"message handler"))
(th2 (make-thread
(lambda ()
|
|
|
|
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
(define (server:launch mode proc)
(let* ((start-time (current-seconds))
(rep (server:start-nmsg mode))
(last-msg (current-seconds))
(th1 (make-thread
(lambda ()
(let loop ()
(let ((pktdat (server:receive rep)))
(set! last-msg (current-seconds))
;; (print "received: " pktdat)
(if (not (eof-object? pktdat))
(begin
(proc pktdat)
(loop))))))
"message handler"))
(th2 (make-thread
(lambda ()
|
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
|
(host (alist-ref 'hostname servpkt))
(ip (alist-ref 'ip servpkt))
(hkey (alist-ref 'Z servpkt))
(addr (conc (or ip host) ":" port)) ;; fall back to host if ip not provided
(myport (area-port *area-info*))
(myhost (area-myaddr *area-info*))
(mykey (area-pktid *area-info*))
(msg (alist->pkt `((hostname . ,myhost)
(port . ,myport)
(servkey . ,hkey) ;; server looks at this to ensure message is for them
(hostkey . ,mykey)
(format . ,dtype) ;; formating of the message
(data . ,data))
*pktspec*)))
(if (and port host)
(begin
(print "sending " msg " to " addr)
(nmsg:open-send-receive addr msg))
#f)))
;; is the server alive?
;;
(define (server:ping servpkt)
(let* ((start-time (current-milliseconds))
(res (server:send servpkt "ping" "t")))
(cons (- (current-milliseconds) start-time)
(equal? res "got ping"))))
|
>
>
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
|
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
|
(host (alist-ref 'hostname servpkt))
(ip (alist-ref 'ip servpkt))
(hkey (alist-ref 'Z servpkt))
(addr (conc (or ip host) ":" port)) ;; fall back to host if ip not provided
(myport (area-port *area-info*))
(myhost (area-myaddr *area-info*))
(mykey (area-pktid *area-info*))
(msg (with-output-to-string
(lambda ()
(write `((hostname . ,myhost)
(port . ,myport)
(servkey . ,hkey) ;; server looks at this to ensure message is for them
(hostkey . ,mykey)
(format . ,dtype) ;; formating of the message
(data . ,data))
;; *pktspec*
;; ptype: 'data))
)))))
(print "msg: " msg)
(if (and port host)
(begin
(print "sending " msg " to " addr)
(nmsg:open-send-receive addr msg))
#f)))
;; get the response
;;
(define (server:receive rep)
(let ((instr (nmsg:recv rep)))
(if (string? instr)
(with-input-from-string instr read)
instr)))
;; is the server alive?
;;
(define (server:ping servpkt)
(let* ((start-time (current-milliseconds))
(res (server:send servpkt "ping" "t")))
(cons (- (current-milliseconds) start-time)
(equal? res "got ping"))))
|