aboutsummaryrefslogtreecommitdiff
path: root/gnu/gnunet/nse
diff options
context:
space:
mode:
authorMaxime Devos <maximedevos@telenet.be>2021-09-10 14:07:47 +0200
committerMaxime Devos <maximedevos@telenet.be>2021-09-21 12:21:00 +0200
commitcea7cfd7416b33b664defea4bbacf78114d7e08a (patch)
tree89d195a48488182718a2536d8ab052988a2f269b /gnu/gnunet/nse
parentf23603f013098a4b699948fe659ab7f157be678f (diff)
downloadgnunet-scheme-cea7cfd7416b33b664defea4bbacf78114d7e08a.tar.gz
gnunet-scheme-cea7cfd7416b33b664defea4bbacf78114d7e08a.zip
nse/client: Allow disconnecting.
* gnu/gnunet/nse/client.scm (<server>)[request-close-condition]: New field (disconnect!): New procedure. (reconnect): Add 'request-close-condition' argument. (reconnect)[request-close-handler]: New procedure. (reconnect): Spawn 'request-close-handler'. (connect)[request-close-condition]: New variable. (connect): Use new variable. * tests/utils.scm: Export 'call-with-temporary-directory'. * tests/network-size.scm ("close, not connected --> all fibers stop, no callbacks called"): New test.
Diffstat (limited to 'gnu/gnunet/nse')
-rw-r--r--gnu/gnunet/nse/client.scm41
1 files changed, 32 insertions, 9 deletions
diff --git a/gnu/gnunet/nse/client.scm b/gnu/gnunet/nse/client.scm
index f19f06c..92e8d77 100644
--- a/gnu/gnunet/nse/client.scm
+++ b/gnu/gnunet/nse/client.scm
@@ -32,7 +32,7 @@
32 estimate:timestamp 32 estimate:timestamp
33 server? 33 server?
34 connect 34 connect
35 ;; TODO: disconnect 35 disconnect!
36 estimate) 36 estimate)
37 (import (only (rnrs base) 37 (import (only (rnrs base)
38 begin define quote lambda case values expt = else apply) 38 begin define quote lambda case values expt = else apply)
@@ -45,7 +45,9 @@
45 (only (fibers) 45 (only (fibers)
46 spawn-fiber) 46 spawn-fiber)
47 (only (fibers conditions) 47 (only (fibers conditions)
48 make-condition wait signal-condition!) 48 make-condition wait wait-operation signal-condition!)
49 (only (fibers operations)
50 choice-operation perform-operation)
49 (only (gnu extractor enum) 51 (only (gnu extractor enum)
50 symbol-value value->index) 52 symbol-value value->index)
51 (only (guile) 53 (only (guile)
@@ -60,7 +62,7 @@
60 message-handler 62 message-handler
61 message-handlers) 63 message-handlers)
62 (only (gnu gnunet mq) 64 (only (gnu gnunet mq)
63 send-message!) 65 send-message! close-queue!)
64 (only (gnu gnunet mq-impl stream) 66 (only (gnu gnunet mq-impl stream)
65 connect/fibers) 67 connect/fibers)
66 (gnu gnunet message protocols) 68 (gnu gnunet message protocols)
@@ -76,7 +78,9 @@
76 (opaque #t)) 78 (opaque #t))
77 79
78 (define-record-type (<server> %make-server server?) 80 (define-record-type (<server> %make-server server?)
79 (fields (immutable estimate/box server-estimate/box))) ; atomic box of flonum 81 (fields (immutable estimate/box server-estimate/box) ; atomic box of flonum
82 (immutable request-close-condition
83 server-request-close-condition)))
80 84
81 (define (estimate server) 85 (define (estimate server)
82 "Return the current estimate of the number of peers on the network, 86 "Return the current estimate of the number of peers on the network,
@@ -114,8 +118,14 @@ Maybe +inf.0 as well?"
114timestamp." 118timestamp."
115 (%estimate:timestamp estimate)) 119 (%estimate:timestamp estimate))
116 120
121 (define (disconnect! server)
122 "Asynchronuously disconnect from the NSE server and stop reconnecting,
123even if not connected. This is an idempotent operation."
124 (signal-condition! (server-request-close-condition server)))
125
117 ;; See 'connect'. 126 ;; See 'connect'.
118 (define* (reconnect estimate/box config #:key updated connected disconnected 127 (define* (reconnect estimate/box request-close-condition config #:key
128 updated connected disconnected
119 (spawn spawn-fiber) #:rest rest) 129 (spawn spawn-fiber) #:rest rest)
120 (define (handle-estimate! estimate-slice) 130 (define (handle-estimate! estimate-slice)
121 (define estimate 131 (define estimate
@@ -144,6 +154,7 @@ timestamp."
144 (value->index (symbol-value message-type msg:nse:start))) 154 (value->index (symbol-value message-type msg:nse:start)))
145 (send-message! mq s)) 155 (send-message! mq s))
146 (define mq-defined (make-condition)) 156 (define mq-defined (make-condition))
157 (define mq-closed (make-condition))
147 (define (error-handler error) 158 (define (error-handler error)
148 (case error 159 (case error
149 ;; TODO report input errors? 160 ;; TODO report input errors?
@@ -161,11 +172,22 @@ timestamp."
161 ;; it is possible that 'connected' is called twice without 172 ;; it is possible that 'connected' is called twice without
162 ;; a call to 'disconnected' in-between, which would presumably 173 ;; a call to 'disconnected' in-between, which would presumably
163 ;; be confusing. 174 ;; be confusing.
175 (signal-condition! mq-closed)
164 (when disconnected (disconnected)) 176 (when disconnected (disconnected))
165 (apply reconnect estimate/box config rest)))) 177 (apply reconnect estimate/box request-close-condition config rest))))
178 ;; Only started after 'mq' is defined, so no need to wait for
179 ;; 'mq-defined'.
180 (define (request-close-handler)
181 (perform-operation
182 (choice-operation
183 (wait-operation request-close-condition)
184 ;; Make sure the fiber exits after a reconnect.
185 (wait-operation mq-closed)))
186 (close-queue! mq))
166 (define mq (connect/fibers config "nse" handlers error-handler 187 (define mq (connect/fibers config "nse" handlers error-handler
167 #:spawn spawn)) 188 #:spawn spawn))
168 (signal-condition! mq-defined)) 189 (signal-condition! mq-defined)
190 (spawn-fiber request-close-handler))
169 191
170 (define* (connect config #:key updated connected disconnected 192 (define* (connect config #:key updated connected disconnected
171 (spawn spawn-fiber) #:rest rest) 193 (spawn spawn-fiber) #:rest rest)
@@ -181,5 +203,6 @@ shortly after calling @var{disconnected}.
181 203
182The procedures @var{updated}, @var{connected} and @var{disconnected} are optional." 204The procedures @var{updated}, @var{connected} and @var{disconnected} are optional."
183 (define estimate/box (make-atomic-box #f)) 205 (define estimate/box (make-atomic-box #f))
184 (apply reconnect estimate/box config rest) 206 (define request-close-condition (make-condition))
185 (%make-server estimate/box)))) 207 (apply reconnect estimate/box request-close-condition config rest)
208 (%make-server estimate/box request-close-condition))))