gnunet/examples/download.in

74 lines
2.5 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!@GUILE@ \
-e (@\ (gnunet-download)\ main) -L . -s
!#
;;;; Copyright © 2015 Rémi Delrue <asgeir@free.fr>
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(define-module (gnunet-download)
#:use-module (ice-9 match)
#:use-module (system foreign)
#:use-module (gnu gnunet common)
#:use-module (gnu gnunet container metadata)
#:use-module (gnu gnunet fs)
#:use-module (gnu gnunet fs uri)
#:use-module (gnu gnunet fs progress-info)
#:use-module (gnu gnunet configuration)
#:use-module (gnu gnunet scheduler)
#:export (main))
(define *config-file* "~/.gnunet/gnunet.conf")
(define *config* (load-configuration *config-file*))
(define *fs-handle* #f)
(define *dl-handle* #f)
(define *kill-task* #f)
(define (main args)
"Entry point: simply run FIRST-TASK within GNUnets scheduler."
(call-with-scheduler *config* (first-task args)))
(define (first-task args)
(lambda (_)
"Parse the arguments, connect to the filesharing system and launch
the download."
(match args
((binary-name output-filename uri-string)
(let ((uri (parse-uri uri-string)))
(set! *fs-handle* (open-filesharing-service *config* binary-name
progress-cb))
(set! *dl-handle* (start-download *fs-handle* uri output-filename))
;; add a timeout in 5 seconds
(set! *kill-task*
(add-task! (lambda (_)
(stop-download *dl-handle*))
#:delay (time-rel #:seconds 5))))))))
(define (progress-cb info status)
(let ((filename (pinfo-download-filename info)))
(cond ((equal? status '(#:download #:start))
(simple-format #t "Starting download `~a'.\n" filename))
((equal? status '(#:download #:completed))
(simple-format #t "Downloaded `~a'.\n" filename)
;; the download is complete, we want to execute the kill-task now
(schedule-shutdown!))
((equal? status '(#:download #:stopped))
(add-task! (lambda (_)
(close-filesharing-service! *fs-handle*)))))))
;; Local Variables:
;; mode: scheme
;; End: