Commit | Line | Data |
---|---|---|
cd541405 | 1 | #lang typed/racket/no-check |
4764ff89 | 2 | |
1d753430 | 3 | (require openssl/sha1) |
4764ff89 | 4 | (require racket/date) |
9c464d95 | 5 | (require |
d718efc4 | 6 | net/head |
edadb804 | 7 | net/uri-codec |
8da029e4 | 8 | net/url) |
4764ff89 | 9 | |
3c9c8266 SK |
10 | (require (prefix-in info: "info.rkt")) |
11 | ||
78398948 | 12 | (module+ test |
de3ff448 | 13 | (require rackunit)) |
78398948 | 14 | |
98529d3d SK |
15 | (define-type Url |
16 | net/url-structs:url) | |
17 | ||
18 | (define-type Out-Format | |
19 | (U 'single-line | |
20 | 'multi-line)) | |
21 | ||
22 | (define-type Timeline-Order | |
23 | (U 'old->new | |
24 | 'new->old)) | |
25 | ||
b0ff061a | 26 | (struct Msg |
78142acb | 27 | ([ts-epoch : Integer] |
3877a0c4 | 28 | [ts-orig : String] |
13c11724 | 29 | [nick : (Option String)] |
98529d3d | 30 | [uri : Url] |
13c11724 | 31 | [text : String] |
b0ff061a | 32 | [mentions : (Listof Peer)])) |
9c464d95 | 33 | |
dbc26280 | 34 | (struct Peer |
13c11724 | 35 | ([nick : (Option String)] |
d0a0e073 SK |
36 | [uri : Url]) |
37 | #:transparent) | |
4764ff89 | 38 | |
edadb804 SK |
39 | (: tt-home-dir Path-String) |
40 | (define tt-home-dir (build-path (expand-user-path "~") ".tt")) | |
41 | ||
9a346534 | 42 | (: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α) (Listof β)))) |
78142acb | 43 | (define (concurrent-filter-map num-workers f xs) |
dad4504d | 44 | ; TODO preserve order of elements OR communicate that reorder is expected |
a239a233 | 45 | ; TODO switch from mailboxes to channels |
895a32cf SK |
46 | (define (make-worker id f) |
47 | (define parent (current-thread)) | |
48 | (λ () | |
a9511f7c SK |
49 | (define self : Thread (current-thread)) |
50 | (: work (∀ (α) (-> α))) | |
895a32cf SK |
51 | (define (work) |
52 | (thread-send parent (cons 'next self)) | |
53 | (match (thread-receive) | |
c562bea3 SK |
54 | ['done (thread-send parent (cons 'exit id))] |
55 | [(cons 'unit x) (begin | |
56 | (define y (f x)) | |
57 | (when y (thread-send parent (cons 'result y))) | |
58 | (work))])) | |
895a32cf | 59 | (work))) |
a9511f7c | 60 | (: dispatch (∀ (α β) (-> (Listof Nonnegative-Integer) (Listof α) (Listof β)))) |
895a32cf SK |
61 | (define (dispatch ws xs ys) |
62 | (if (empty? ws) | |
f1493e49 SK |
63 | ys |
64 | (match (thread-receive) | |
c562bea3 SK |
65 | [(cons 'exit w) (dispatch (remove w ws =) xs ys)] |
66 | [(cons 'result y) (dispatch ws xs (cons y ys))] | |
67 | [(cons 'next thd) (match xs | |
68 | ['() (begin | |
69 | (thread-send thd 'done) | |
70 | (dispatch ws xs ys))] | |
71 | [(cons x xs) (begin | |
72 | (thread-send thd (cons 'unit x)) | |
73 | (dispatch ws xs ys))])]))) | |
78142acb | 74 | (define workers (range num-workers)) |
9926c9a9 SK |
75 | (define threads (map (λ (id) (thread (make-worker id f))) workers)) |
76 | (define results (dispatch workers xs '())) | |
895a32cf SK |
77 | (for-each thread-wait threads) |
78 | results) | |
79 | ||
dad4504d | 80 | (module+ test |
de3ff448 SK |
81 | (let* ([f (λ (x) (if (even? x) x #f))] |
82 | [xs (range 11)] | |
83 | [actual (sort (concurrent-filter-map 10 f xs) <)] | |
84 | [expected (sort ( filter-map f xs) <)]) | |
c562bea3 | 85 | (check-equal? actual expected "concurrent-filter-map"))) |
dad4504d | 86 | |
98529d3d | 87 | (: msg-print (-> Out-Format Integer Msg Void)) |
3d042e75 SK |
88 | (define msg-print |
89 | (let* ([colors (vector 36 33)] | |
90 | [n (vector-length colors)]) | |
91 | (λ (out-format color-i msg) | |
01e4c499 | 92 | (let ([color (vector-ref colors (modulo color-i n))] |
b0ff061a SK |
93 | [nick (Msg-nick msg)] |
94 | [uri (url->string (Msg-uri msg))] | |
95 | [text (Msg-text msg)] | |
96 | [mentions (Msg-mentions msg)]) | |
3d042e75 | 97 | (match out-format |
01e4c499 | 98 | ['single-line |
13c11724 SK |
99 | (let ([nick (if nick nick uri)]) |
100 | (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n" | |
101 | (parameterize | |
102 | ([date-display-format 'iso-8601]) | |
b0ff061a | 103 | (date->string (seconds->date (Msg-ts-epoch msg)) #t)) |
13c11724 | 104 | nick color text))] |
01e4c499 | 105 | ['multi-line |
13c11724 SK |
106 | (let ([nick (if nick (string-append nick " ") "")]) |
107 | (printf "~a (~a)~n\033[1;37m<~a~a>\033[0m~n\033[0;~am~a\033[0m~n~n" | |
108 | (parameterize | |
109 | ([date-display-format 'rfc2822]) | |
b0ff061a SK |
110 | (date->string (seconds->date (Msg-ts-epoch msg)) #t)) |
111 | (Msg-ts-orig msg) | |
13c11724 | 112 | nick uri color text))]))))) |
e96264cc | 113 | |
3877a0c4 SK |
114 | (: rfc3339->epoch (-> String (Option Nonnegative-Integer))) |
115 | (define rfc3339->epoch | |
116 | (let ([re (pregexp "^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(:([0-9]{2}))?(\\.[0-9]+)?(Z|([+-])([0-9]{1,2}):?([0-9]{2}))?$")]) | |
117 | (λ (ts) | |
118 | (match (regexp-match re ts) | |
119 | [(list _wholething yyyy mm dd HH MM _:SS SS _fractional tz-whole tz-sign tz-HH tz-MM) | |
120 | (let* | |
121 | ([tz-offset | |
122 | (match* (tz-whole tz-sign tz-HH tz-MM) | |
123 | [("Z" #f #f #f) | |
124 | 0] | |
125 | [(_ (or "-" "+") (? identity h) (? identity m)) | |
126 | (let ([h (string->number h)] | |
127 | [m (string->number m)] | |
128 | ; Reverse to get back to UTC: | |
129 | [op (match tz-sign ["+" -] ["-" +])]) | |
130 | (op 0 (+ (* 60 m) (* 60 (* 60 h)))))] | |
131 | [(a b c d) | |
132 | (log-warning "Impossible TZ string: ~v, components: ~v ~v ~v ~v" tz-whole a b c d) | |
133 | 0])] | |
134 | [ts-orig ts] | |
135 | [local-time? #f] | |
136 | [ts-epoch (find-seconds (if SS (string->number SS) 0) | |
137 | (string->number MM) | |
138 | (string->number HH) | |
139 | (string->number dd) | |
140 | (string->number mm) | |
141 | (string->number yyyy) | |
142 | local-time?)]) | |
143 | (+ ts-epoch tz-offset))] | |
144 | [_ | |
145 | (log-error "Invalid timestamp: ~v" ts) | |
146 | #f])))) | |
147 | ||
13c11724 | 148 | (: str->msg (-> (Option String) Url String (Option Msg))) |
b4689464 | 149 | (define str->msg |
3877a0c4 | 150 | (let ([re (pregexp "^([^\\s\t]+)[\\s\t]+(.*)$")]) |
b4689464 SK |
151 | (λ (nick uri str) |
152 | (with-handlers* | |
153 | ([exn:fail? | |
154 | (λ (e) | |
9c464d95 SK |
155 | (log-error |
156 | "Failed to parse msg: ~v, from: ~v, at: ~v, because: ~v" | |
157 | str nick (url->string uri) e) | |
b4689464 SK |
158 | #f)]) |
159 | (match (regexp-match re str) | |
3877a0c4 SK |
160 | [(list _wholething ts-orig text) |
161 | (let ([ts-epoch (rfc3339->epoch ts-orig)]) | |
162 | (if ts-epoch | |
13c11724 SK |
163 | (let ([mentions |
164 | (filter-map | |
165 | (λ (m) (match (regexp-match #px"@<([^>]+)>" m) | |
166 | [(list _wholething nick-uri) | |
dbc26280 | 167 | (str->peer nick-uri)])) |
13c11724 | 168 | (regexp-match* #px"@<[^\\s]+([\\s]+)?[^>]+>" text))]) |
b0ff061a | 169 | (Msg ts-epoch ts-orig nick uri text mentions)) |
3877a0c4 SK |
170 | (begin |
171 | (log-error | |
172 | "Msg rejected due to invalid timestamp: ~v, nick:~v, uri:~v" | |
173 | str nick (url->string uri)) | |
174 | #f)))] | |
b4689464 | 175 | [_ |
13c11724 | 176 | (log-debug "Non-msg line from nick:~v, line:~a" nick str) |
b4689464 | 177 | #f]))))) |
88d50b3e | 178 | |
63afa259 | 179 | (module+ test |
13c11724 | 180 | ; TODO Test for when missing-nick case |
b4689464 SK |
181 | (let* ([tzs (for*/list ([d '("-" "+")] |
182 | [h '("5" "05")] | |
183 | [m '("00" ":00" "57" ":57")]) | |
184 | (string-append d h m))] | |
185 | [tzs (list* "" "Z" tzs)]) | |
186 | (for* ([n '("fake-nick")] | |
187 | [u '("fake-uri")] | |
188 | [s '("" ":10")] | |
189 | [f '("" ".1337")] | |
190 | [z tzs] | |
191 | [sep (list "\t" " ")] | |
192 | [txt '("foo bar baz" "'jaz poop bear giraffe / tea" "@*\"``")]) | |
193 | (let* ([ts (string-append "2020-11-18T22:22" | |
194 | (if (non-empty-string? s) s ":00") | |
195 | z)] | |
196 | [m (str->msg n u (string-append ts sep txt))]) | |
197 | (check-not-false m) | |
b0ff061a SK |
198 | (check-equal? (Msg-nick m) n) |
199 | (check-equal? (Msg-uri m) u) | |
200 | (check-equal? (Msg-text m) txt) | |
201 | (check-equal? (Msg-ts-orig m) ts (format "Given: ~v" ts)) | |
b4689464 SK |
202 | ))) |
203 | ||
de3ff448 SK |
204 | (let* ([ts "2020-11-18T22:22:09-0500"] |
205 | [tab " "] | |
206 | [text "Lorem ipsum"] | |
207 | [nick "foo"] | |
208 | [uri "bar"] | |
209 | [actual (str->msg nick uri (string-append ts tab text))] | |
b0ff061a | 210 | [expected (Msg 1605756129 ts nick uri text)]) |
c562bea3 | 211 | (check-equal? |
b0ff061a SK |
212 | (Msg-ts-epoch actual) |
213 | (Msg-ts-epoch expected) | |
78142acb | 214 | "str->msg ts-epoch") |
3877a0c4 | 215 | (check-equal? |
b0ff061a SK |
216 | (Msg-ts-orig actual) |
217 | (Msg-ts-orig expected) | |
3877a0c4 | 218 | "str->msg ts-orig") |
c562bea3 | 219 | (check-equal? |
b0ff061a SK |
220 | (Msg-nick actual) |
221 | (Msg-nick expected) | |
c562bea3 SK |
222 | "str->msg nick") |
223 | (check-equal? | |
b0ff061a SK |
224 | (Msg-uri actual) |
225 | (Msg-uri expected) | |
c562bea3 SK |
226 | "str->msg uri") |
227 | (check-equal? | |
b0ff061a SK |
228 | (Msg-text actual) |
229 | (Msg-text expected) | |
c562bea3 | 230 | "str->msg text"))) |
63afa259 | 231 | |
98529d3d | 232 | (: str->lines (-> String (Listof String))) |
e96264cc SK |
233 | (define (str->lines str) |
234 | (string-split str (regexp "[\r\n]+"))) | |
235 | ||
63afa259 | 236 | (module+ test |
de3ff448 | 237 | (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi"))) |
63afa259 | 238 | |
13c11724 | 239 | (: str->msgs (-> (Option String) Url String (Listof Msg))) |
b201e854 | 240 | (define (str->msgs nick uri str) |
3877a0c4 | 241 | (filter-map (λ (line) (str->msg nick uri line)) (filter-comments (str->lines str)))) |
4764ff89 | 242 | |
edadb804 SK |
243 | (: cache-dir Path-String) |
244 | (define cache-dir (build-path tt-home-dir "cache")) | |
245 | ||
d718efc4 SK |
246 | (define cache-object-dir (build-path cache-dir "objects")) |
247 | ||
edadb804 SK |
248 | (: url->cache-file-path-v1 (-> Url Path-String)) |
249 | (define (url->cache-file-path-v1 uri) | |
250 | (define (hash-sha1 str) : (-> String String) | |
251 | (define in (open-input-string str)) | |
252 | (define digest (sha1 in)) | |
253 | (close-input-port in) | |
254 | digest) | |
d718efc4 | 255 | (build-path cache-object-dir (hash-sha1 (url->string uri)))) |
edadb804 SK |
256 | |
257 | (: url->cache-file-path-v2 (-> Url Path-String)) | |
258 | (define (url->cache-file-path-v2 uri) | |
d718efc4 SK |
259 | (build-path cache-object-dir (uri-encode (url->string uri)))) |
260 | ||
261 | (define url->cache-object-path url->cache-file-path-v2) | |
1d753430 | 262 | |
d718efc4 SK |
263 | (define (url->cache-etag-path uri) |
264 | (build-path cache-dir "etags" (uri-encode (url->string uri)))) | |
265 | ||
266 | (define (url->cache-lmod-path uri) | |
267 | (build-path cache-dir "lmods" (uri-encode (url->string uri)))) | |
9c464d95 | 268 | |
98529d3d SK |
269 | ; TODO Return Option |
270 | (: uri-read-cached (-> Url String)) | |
4214c0f3 | 271 | (define (uri-read-cached uri) |
edadb804 SK |
272 | (define path-v1 (url->cache-file-path-v1 uri)) |
273 | (define path-v2 (url->cache-file-path-v2 uri)) | |
274 | (when (file-exists? path-v1) | |
275 | (rename-file-or-directory path-v1 path-v2 #t)) | |
276 | (if (file-exists? path-v2) | |
277 | (file->string path-v2) | |
0e16a46c | 278 | (begin |
9c464d95 | 279 | (log-warning "Cache file not found for URI: ~a" (url->string uri)) |
4214c0f3 SK |
280 | ""))) |
281 | ||
dbc26280 SK |
282 | (: str->peer (String (Option Peer))) |
283 | (define (str->peer str) | |
284 | (log-debug "Parsing peer string: ~v" str) | |
13c11724 SK |
285 | (with-handlers* |
286 | ([exn:fail? | |
287 | (λ (e) | |
288 | (log-error "Invalid URI in string: ~v, exn: ~v" str e) | |
289 | #f)]) | |
290 | (match (string-split str) | |
dbc26280 SK |
291 | [(list u) (Peer #f (string->url u))] |
292 | [(list n u) (Peer n (string->url u))] | |
13c11724 | 293 | [_ |
dbc26280 | 294 | (log-error "Invalid peer string: ~v" str) |
13c11724 SK |
295 | #f]))) |
296 | ||
9c464d95 | 297 | |
98529d3d | 298 | (: filter-comments (-> (Listof String) (Listof String))) |
9c464d95 SK |
299 | (define (filter-comments lines) |
300 | (filter-not (λ (line) (string-prefix? line "#")) lines)) | |
301 | ||
dbc26280 SK |
302 | (: str->peers (-> String (Listof Peer))) |
303 | (define (str->peers str) | |
304 | (filter-map str->peer (filter-comments (str->lines str)))) | |
9c464d95 | 305 | |
dbc26280 | 306 | (: file->peers (-> Path-String (Listof Peer))) |
d0a0e073 SK |
307 | (define (file->peers file-path) |
308 | (if (file-exists? file-path) | |
309 | (str->peers (file->string file-path)) | |
310 | (begin | |
311 | (log-error "File does not exist: ~v" (path->string file-path)) | |
312 | '()))) | |
9c464d95 | 313 | |
9c5e4499 SK |
314 | (define re-rfc2822 |
315 | #px"^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), ([0-9]{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{4}) ([0-2][0-9]):([0-6][0-9]):([0-6][0-9]) GMT") | |
316 | ||
317 | (: b->n (-> Bytes (Option Number))) | |
318 | (define (b->n b) | |
319 | (string->number (bytes->string/utf-8 b))) | |
320 | ||
321 | (: mon->num (-> Bytes Natural)) | |
322 | (define/match (mon->num mon) | |
323 | [(#"Jan") 1] | |
324 | [(#"Feb") 2] | |
325 | [(#"Mar") 3] | |
326 | [(#"Apr") 4] | |
327 | [(#"May") 5] | |
328 | [(#"Jun") 6] | |
329 | [(#"Jul") 7] | |
330 | [(#"Aug") 8] | |
331 | [(#"Sep") 9] | |
332 | [(#"Oct") 10] | |
333 | [(#"Nov") 11] | |
334 | [(#"Dec") 12]) | |
335 | ||
336 | (: rfc2822->epoch (-> Bytes (Option Nonnegative-Integer))) | |
337 | (define (rfc2822->epoch timestamp) | |
338 | (match (regexp-match re-rfc2822 timestamp) | |
339 | [(list _ _ dd mo yyyy HH MM SS) | |
340 | #:when (and dd mo yyyy HH MM SS) | |
341 | (find-seconds (b->n SS) | |
342 | (b->n MM) | |
343 | (b->n HH) | |
344 | (b->n dd) | |
345 | (mon->num mo) | |
346 | (b->n yyyy) | |
347 | #f)] | |
348 | [_ | |
349 | #f])) | |
350 | ||
98529d3d | 351 | (: user-agent String) |
9c464d95 SK |
352 | (define user-agent |
353 | (let* | |
354 | ([prog-name "tt"] | |
3c9c8266 | 355 | [prog-version (info:#%info-lookup 'version)] |
9c464d95 | 356 | [prog-uri "https://github.com/xandkar/tt"] |
dbc26280 | 357 | [user-peer-file (build-path tt-home-dir "me")] |
9c464d95 | 358 | [user |
dbc26280 SK |
359 | (if (file-exists? user-peer-file) |
360 | (match (first (file->peers user-peer-file)) | |
361 | [(Peer #f u) (format "+~a" (url->string u) )] | |
362 | [(Peer n u) (format "+~a; @~a" (url->string u) n)]) | |
9c464d95 SK |
363 | (format "+~a" prog-uri))]) |
364 | (format "~a/~a (~a)" prog-name prog-version user))) | |
365 | ||
d718efc4 SK |
366 | (: header-get (-> (Listof Bytes) Bytes (Option Bytes))) |
367 | (define (header-get headers name) | |
368 | (match (filter-map (curry extract-field name) headers) | |
369 | [(list val) val] | |
370 | [_ #f])) | |
371 | ||
98529d3d | 372 | (: uri-download (-> Url Void)) |
9c464d95 | 373 | (define (uri-download u) |
d718efc4 SK |
374 | (define cached-object-path (url->cache-object-path u)) |
375 | (define cached-etag-path (url->cache-etag-path u)) | |
376 | (define cached-lmod-path (url->cache-lmod-path u)) | |
377 | (log-debug "uri-download ~v into ~v" u cached-object-path) | |
8da029e4 SK |
378 | (define-values (status-line headers body-input) |
379 | ; TODO Timeout. Currently hangs on slow connections. | |
380 | (http-sendrecv/url u #:headers (list (format "User-Agent: ~a" user-agent)))) | |
381 | (log-debug "headers: ~v" headers) | |
382 | (log-debug "status-line: ~v" status-line) | |
383 | (define status | |
384 | (string->number (second (string-split (bytes->string/utf-8 status-line))))) | |
385 | (log-debug "status: ~v" status) | |
386 | ; TODO Handle redirects | |
387 | (match status | |
388 | [200 | |
9c5e4499 SK |
389 | (let* ([etag (header-get headers #"ETag")] |
390 | [lmod (header-get headers #"Last-Modified")] | |
391 | [lmod-curr (if lmod (rfc2822->epoch lmod) #f)] | |
392 | [lmod-prev (if (file-exists? cached-lmod-path) | |
393 | (rfc2822->epoch (file->bytes cached-lmod-path)) | |
394 | #f)]) | |
395 | (log-debug "lmod-curr:~v lmod-prev:~v" lmod-curr lmod-prev) | |
396 | (unless (or (and etag | |
397 | (file-exists? cached-etag-path) | |
398 | (bytes=? etag (file->bytes cached-etag-path)) | |
399 | (begin | |
400 | (log-info "ETags match, skipping the rest of ~v" (url->string u)) | |
401 | #t)) | |
402 | (and lmod-curr | |
403 | lmod-prev | |
404 | (<= lmod-curr lmod-prev) | |
405 | (begin | |
406 | (log-info "Last-Modified <= current skipping the rest of ~v" (url->string u)) | |
407 | #t))) | |
408 | (begin | |
409 | (log-info | |
410 | "Downloading the rest of ~v. ETag: ~a, Last-Modified: ~v" | |
411 | (url->string u) etag lmod) | |
412 | (make-parent-directory* cached-object-path) | |
413 | (make-parent-directory* cached-etag-path) | |
414 | (make-parent-directory* cached-lmod-path) | |
415 | (call-with-output-file cached-object-path | |
416 | (curry copy-port body-input) | |
417 | #:exists 'replace) | |
418 | (when etag | |
419 | (display-to-file etag cached-etag-path #:exists 'replace)) | |
420 | (when lmod | |
421 | (display-to-file lmod cached-lmod-path #:exists 'replace)))) | |
8da029e4 SK |
422 | (close-input-port body-input))] |
423 | [_ | |
424 | (raise status)])) | |
4764ff89 | 425 | |
98529d3d | 426 | (: timeline-print (-> Out-Format (Listof Msg) Void)) |
b201e854 | 427 | (define (timeline-print out-format timeline) |
3d042e75 | 428 | (void (foldl (match-lambda** |
b0ff061a | 429 | [((and m (Msg _ _ nick _ _ _)) (cons prev-nick i)) |
13c11724 | 430 | (let ([i (if (equal? prev-nick nick) i (+ 1 i))]) |
3d042e75 SK |
431 | (msg-print out-format i m) |
432 | (cons nick i))]) | |
433 | (cons "" 0) | |
434 | timeline))) | |
4764ff89 | 435 | |
dbc26280 SK |
436 | (: peer->msgs (-> Peer (Listof Msg))) |
437 | (define (peer->msgs f) | |
438 | (match-define (Peer nick uri) f) | |
439 | (log-info "Reading peer nick:~v uri:~v" nick (url->string uri)) | |
9c464d95 | 440 | (str->msgs nick uri (uri-read-cached uri))) |
4214c0f3 | 441 | |
dbc26280 SK |
442 | (: peer-download (-> Peer Void)) |
443 | (define (peer-download f) | |
444 | (match-define (Peer nick uri) f) | |
2db1b40f | 445 | (define u (url->string uri)) |
dbc26280 | 446 | (log-info "Downloading peer uri:~a" u) |
9a6a9f9a | 447 | (with-handlers |
9c464d95 | 448 | ([exn:fail? |
9a6a9f9a | 449 | (λ (e) |
13c11724 | 450 | (log-error "Network error nick:~v uri:~v exn:~v" nick u e) |
9a6a9f9a SK |
451 | #f)] |
452 | [integer? | |
453 | (λ (status) | |
13c11724 | 454 | (log-error "HTTP error nick:~v uri:~a status:~a" nick u status) |
9a6a9f9a | 455 | #f)]) |
2db1b40f SK |
456 | (define-values (_result _tm-cpu-ms tm-real-ms _tm-gc-ms) |
457 | (time-apply uri-download (list uri))) | |
dbc26280 | 458 | (log-info "Peer downloaded in ~a seconds, uri: ~a" (/ tm-real-ms 1000.0) u))) |
4214c0f3 | 459 | |
dbc26280 SK |
460 | (: timeline-download (-> Integer (Listof Peer) Void)) |
461 | (define (timeline-download num-workers peers) | |
4214c0f3 | 462 | ; TODO No need for map - can just iter |
dbc26280 | 463 | (void (concurrent-filter-map num-workers peer-download peers))) |
9a6a9f9a SK |
464 | |
465 | ; TODO timeline contract : time-sorted list of messages | |
dbc26280 SK |
466 | (: timeline-read (-> Timeline-Order (Listof Peer) (Listof Msg))) |
467 | (define (timeline-read order peers) | |
a4899240 SK |
468 | (define cmp (match order |
469 | ['old->new <] | |
470 | ['new->old >])) | |
dbc26280 | 471 | (sort (append* (filter-map peer->msgs peers)) |
b0ff061a | 472 | (λ (a b) (cmp (Msg-ts-epoch a) (Msg-ts-epoch b))))) |
4764ff89 | 473 | |
d0a0e073 SK |
474 | (: paths->peers (-> (Listof String) (Listof Peer))) |
475 | (define (paths->peers paths) | |
476 | (let* ([paths (match paths | |
477 | ['() | |
478 | (let ([peer-refs-file (build-path tt-home-dir "peers")]) | |
479 | (log-debug | |
480 | "No peer ref file paths provided, defaulting to ~v" | |
481 | (path->string peer-refs-file)) | |
482 | (list peer-refs-file))] | |
483 | [paths | |
484 | (log-debug "Peer ref file paths provided: ~v" paths) | |
485 | (map string->path paths)])] | |
486 | [peers (append* (map file->peers paths))]) | |
487 | (log-info "Read-in ~a peers." (length peers)) | |
488 | peers)) | |
489 | ||
56de6228 SK |
490 | (: log-writer-stop (-> Thread Void)) |
491 | (define (log-writer-stop log-writer) | |
492 | (log-message (current-logger) 'fatal 'stop "Exiting." #f) | |
493 | (thread-wait log-writer)) | |
494 | ||
0d3f753c SK |
495 | (: log-writer-start (-> Log-Level Thread)) |
496 | (define (log-writer-start level) | |
56de6228 SK |
497 | (let* ([logger |
498 | (make-logger #f #f level #f)] | |
499 | [log-receiver | |
500 | (make-log-receiver logger level)] | |
501 | [log-writer | |
502 | (thread | |
503 | (λ () | |
504 | (parameterize | |
505 | ([date-display-format 'iso-8601]) | |
506 | (let loop () | |
507 | (match-define (vector level msg _ topic) (sync log-receiver)) | |
508 | (unless (equal? topic 'stop) | |
509 | (eprintf "~a [~a] ~a~n" (date->string (current-date) #t) level msg) | |
510 | (loop))))))]) | |
511 | (current-logger logger) | |
512 | log-writer)) | |
01e4c499 | 513 | |
24c6a76b | 514 | (module+ main |
24f1f64b | 515 | (let ([log-level 'info]) |
c562bea3 | 516 | (command-line |
24f1f64b SK |
517 | #:program |
518 | "tt" | |
c562bea3 | 519 | #:once-each |
01e4c499 SK |
520 | [("-d" "--debug") |
521 | "Enable debug log level." | |
522 | (set! log-level 'debug)] | |
24f1f64b SK |
523 | #:help-labels |
524 | "" | |
525 | "and <command> is one of" | |
4214c0f3 SK |
526 | "r, read i : Read the timeline." |
527 | "d, download : Download the timeline." | |
edadb804 | 528 | ; TODO Add path dynamically |
3a4b2233 | 529 | "u, upload : Upload your twtxt file (alias to execute ~/.tt/upload)." |
24f1f64b SK |
530 | "" |
531 | #:args (command . args) | |
0d3f753c | 532 | (define log-writer (log-writer-start log-level)) |
4214c0f3 | 533 | (current-command-line-arguments (list->vector args)) |
24f1f64b | 534 | (match command |
4214c0f3 | 535 | [(or "d" "download") |
2db1b40f | 536 | ; Initially, 15 was fastest out of the tried: 1, 5, 10, 20. Then I |
edadb804 | 537 | ; started noticing significant slowdowns. Reducing to 5 seems to help. |
2db1b40f | 538 | (let ([num-workers 5]) |
24f1f64b SK |
539 | (command-line |
540 | #:program | |
4214c0f3 | 541 | "tt download" |
24f1f64b SK |
542 | #:once-each |
543 | [("-j" "--jobs") | |
544 | njobs "Number of concurrent jobs." | |
78142acb | 545 | (set! num-workers (string->number njobs))] |
d0a0e073 | 546 | #:args file-paths |
32dcc0b0 | 547 | (define-values (_res _cpu real-ms _gc) |
d0a0e073 | 548 | (time-apply timeline-download (list num-workers (paths->peers file-paths)))) |
0d3f753c | 549 | (log-info "Timeline downloaded in ~a seconds." (/ real-ms 1000.0))))] |
3a4b2233 SK |
550 | [(or "u" "upload") |
551 | (command-line | |
3877a0c4 SK |
552 | #:program |
553 | "tt upload" | |
554 | #:args () | |
edadb804 | 555 | (if (system (path->string (build-path tt-home-dir "upload"))) |
3a4b2233 SK |
556 | (exit 0) |
557 | (exit 1)))] | |
4214c0f3 | 558 | [(or "r" "read") |
a4899240 SK |
559 | (let ([out-format 'multi-line] |
560 | [order 'old->new]) | |
4214c0f3 SK |
561 | (command-line |
562 | #:program | |
563 | "tt read" | |
a4899240 SK |
564 | #:once-each |
565 | [("-r" "--rev") | |
566 | "Reverse displayed timeline order." | |
567 | (set! order 'new->old)] | |
24f1f64b SK |
568 | #:once-any |
569 | [("-s" "--short") | |
570 | "Short output format" | |
571 | (set! out-format 'single-line)] | |
572 | [("-l" "--long") | |
573 | "Long output format" | |
574 | (set! out-format 'multi-line)] | |
d0a0e073 SK |
575 | #:args file-paths |
576 | (timeline-print out-format (timeline-read order (paths->peers file-paths)))))]) | |
0d3f753c | 577 | (log-writer-stop log-writer)))) |