Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Created July 25, 2012 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamasushi/3174805 to your computer and use it in GitHub Desktop.
Save yamasushi/3174805 to your computer and use it in GitHub Desktop.
スレッドを使ってhttp-getからキャラクタジェネレータを作る。そして仮想ポートをつくる。
; スレッドを使ってhttp-getからキャラクタジェネレータを作る。
; そして仮想ポートをつくる。
(use rfc.http)
(use gauche.threads)
(use gauche.generator)
(use util.queue)
(use gauche.vport)
(use gauche.uvector)
(use sxml.ssax)
;(use htmlprag)
;(use pretty-print)
;(use web-helper)
(define (http-hook-receiver proc)
(lambda (code hdrs total retr)
(let loop [ ]
(receive (port size) (retr)
(cond
[ (eqv? size 0) #t]
[ (or (not size) (> size 0) )
(let loop-rb [(data (read-block size port) )
(req-size size) ]
(proc data) ; hook
(let1 nrest (- req-size (string-length data))
(if (= nrest 0)
(loop)
(loop-rb (read-block nrest port) nrest ) ) ) ) ]
) ) ) ) )
(define (call-with-input-http http-param proc :key (queue-size 100))
(define qr (make-mtqueue :max-length queue-size))
(define (make-producer http-param)
;#?= http-param
(^ []
(guard (e [else
(print (standard-error-port) (ref e 'message) )
(enqueue/wait! qr (eof-object)) ] )
(apply http-get (append
http-param
`(:receiver
,(http-hook-receiver
(^(xp)
;#?= xp
(enqueue/wait! qr xp) ) ) ) ) )
(enqueue/wait! qr (eof-object))
) ) )
(define (make-consumer proc)
(^ []
(guard (e [else (print (standard-error-port) (ref e 'message) ) ])
(let* [( g ($ gconcatenate
$ gmap ( $ x->generator $ string->u8vector $)
$ generate (^(yield)
(let loop [(xc (dequeue/wait! qr))]
(if (eof-object? xc)
(yield (eof-object))
(begin
;#?= xc
(yield xc)
(loop (dequeue/wait! qr) ) ) ) ) ) ) )
( vp (make <virtual-input-port> :getb g ) )
]
(proc vp)
) ) ) )
(and-let* [ ( c (make-thread (make-consumer proc) ) )
( p (make-thread (make-producer http-param)))]
(let [(tc (thread-start! c))
(tp (thread-start! p)) ]
(thread-join! tp)
(thread-join! tc) ; <----consumerの値を返す
) ) )
;(define (call-with-input-http-uri uri proc :key (queue-size 100))
; (receive (host path) (uri->param uri)
; (call-with-input-http
; (list host path) proc :queue-size queue-size )))
(define (main args)
(call-with-input-http
(cdr args)
(^p
;(print (port->string p))
($ print $ (cut ssax:xml->sxml <> '()) p)
)
:queue-size 3
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment