168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
;; 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
exn
(if (< port 65535)(connect-server-find-port udata (+ port 1)) #f)
(connect-server udata port)))
(define (connect-server udata port)
;; (tcp-listener-socket LISTENER)(socket-name so)
;; sockaddr-address, sockaddr-port, sockaddr->string
(let* ((tlsn (tcp-listen port 1000 #f)) ;; (tcp-listen TCPPORT [BACKLOG [HOST]])
(addr (get-my-best-address))) ;; (hostinfo-addresses (host-information (current-hostname)))
|
|
|
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
;; 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
exn
(if (< port 65535)(start-server-find-port udata (+ port 1)) #f)
(connect-server udata port)))
(define (connect-server udata port)
;; (tcp-listener-socket LISTENER)(socket-name so)
;; sockaddr-address, sockaddr-port, sockaddr->string
(let* ((tlsn (tcp-listen port 1000 #f)) ;; (tcp-listen TCPPORT [BACKLOG [HOST]])
(addr (get-my-best-address))) ;; (hostinfo-addresses (host-information (current-hostname)))
|
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
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
;; send back ack
;;
(define (send-ack udata host-port qrykey #!optional (hostname #f)(pid #f))
(reply udata "ack" qrykey oup 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
|
|
>
>
>
|
|
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
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
|
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
((ack)(print "Got ack!"))
((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 (get-peer-dat udata host:port) handlerkey data)))
(else (print "BAD DATA? handler=" handler " data=" data)))))
(loop state)))))
;;======================================================================
;; connection setup and management functions
;;======================================================================
;; find or become the captain, return a ulex object
|
|
|
|
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
((ack)(print "Got ack!"))
((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)))))
;;======================================================================
;; connection setup and management functions
;;======================================================================
;; find or become the captain, return a ulex object
|