Skip to content

Commit

Permalink
[lib/ocaml] use ocaml Http_client from netclient lib to download files
Browse files Browse the repository at this point in the history
* used e.g. to dowload tiles
* raises Blocked if we get a 403 and raises Not_Found on a 404,
  only if we get a Not_Found we try the next zoom level
* in case we get blocked, it still tries the next tile...
  maybe we should stop trying to get tiles at all in that case
  • Loading branch information
flixr committed Sep 12, 2012
1 parent 45ec0f1 commit 4cc1192
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 37 deletions.
8 changes: 4 additions & 4 deletions sw/ground_segment/cockpit/Makefile
Expand Up @@ -36,9 +36,9 @@ FPIC=-fPIC
OCAMLC=ocamlc
OCAMLOPT=ocamlopt
OCAMLOPTFLAGS=-thread
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring)
OCAMLNETCMA=$(shell ocamlfind query -r -a-format -predicates byte netstring)
INCLUDES= $(shell ocamlfind query -r -i-format lablgtk2) -I ../../lib/ocaml $(shell ocamlfind query -r -i-format xml-light) $(OCAMLNETINCLUDES)
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring) $(shell ocamlfind query -r -i-format netclient)
OCAMLNETCMA=$(shell ocamlfind query -r -a-format -predicates byte netstring) $(shell ocamlfind query -r -a-format -predicates byte netclient)
INCLUDES= $(shell ocamlfind query -r -i-format lablgtk2) -I ../../lib/ocaml $(shell ocamlfind query -r -i-format xml-light) $(shell ocamlfind query -r -i-format pcre) $(OCAMLNETINCLUDES)
LIBS=$(OCAMLNETCMA) glibivy-ocaml.cma lablgtk.cma lablglade.cma lib-pprz.cma lablgnomecanvas.cma xlib-pprz.cma
CMXA=$(LIBS:.cma=.cmxa)

Expand All @@ -53,7 +53,7 @@ opt : $(MAIN).opt

$(MAIN) : $(CMO) ../../lib/ocaml/xlib-pprz.cma ../../lib/ocaml/lib-pprz.cma
@echo OL $@
$(Q)$(OCAMLC) $(OCAMLCFLAGS) -custom $(INCLUDES) unix.cma str.cma xml-light.cma $(LIBS) threads.cma gtkThread.cmo myGtkInit.cmo $(CMO) -o $@
$(Q)$(OCAMLC) $(OCAMLCFLAGS) -custom $(INCLUDES) $(OCAMLNETINCLUDES) unix.cma str.cma netstring.cma netclient.cma xml-light.cma $(LIBS) threads.cma gtkThread.cmo myGtkInit.cmo $(CMO) -o $@

$(MAIN).opt : $(CMX)
@echo OOL $@
Expand Down
13 changes: 6 additions & 7 deletions sw/ground_segment/tmtc/Makefile
@@ -1,4 +1,4 @@
#
#
# $Id$
# Copyright (C) 2003-2006 Pascal Brisset, Antoine Drouin
#
Expand All @@ -17,15 +17,15 @@
# You should have received a copy of the GNU General Public License
# along with paparazzi; see the file COPYING. If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Boston, MA 02111-1307, USA.
#

# Quiet
Q=@

LBITS := $(shell getconf LONG_BIT)
ifeq ($(LBITS),64)
FPIC = -fPIC
FPIC = -fPIC
else
FPIC =
endif
Expand All @@ -43,7 +43,7 @@ clean:
OCAMLC = ocamlc
OCAMLOPT = ocamlopt
OCAMLLIB = ../../lib/ocaml
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring)
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring) $(shell ocamlfind query -r -i-format netclient)
OCAMLNETCMA=$(shell ocamlfind query -r -a-format -predicates byte netstring)
INCLUDES= -I $(OCAMLLIB) -I ../multimon $(shell ocamlfind query -r -i-format lablgtk2) $(shell ocamlfind query -r -i-format xml-light) $(OCAMLNETINCLUDES)
LIBPPRZCMA=$(OCAMLLIB)/lib-pprz.cma
Expand Down Expand Up @@ -169,7 +169,6 @@ ivy_serial_bridge: ivy_serial_bridge.c
.depend: Makefile
ocamldep -I ../../lib/ocaml *.ml* > .depend

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),clean)
-include .depend
endif

2 changes: 1 addition & 1 deletion sw/lib/ocaml/Makefile
Expand Up @@ -30,7 +30,7 @@ else
FPIC =
endif

INCLUDES= $(shell ocamlfind query -r -i-format xml-light) $(shell ocamlfind query -r -i-format netstring) $(shell ocamlfind query -r -i-format pcre)
INCLUDES= $(shell ocamlfind query -r -i-format xml-light) $(shell ocamlfind query -r -i-format netstring) $(shell ocamlfind query -r -i-format netclient) $(shell ocamlfind query -r -i-format pcre)
XINCLUDES= $(shell ocamlfind query -r -i-format lablgtk2) $(shell ocamlfind query -r -i-format xml-light)
OCAMLC=ocamlc
OCAMLOPT=ocamlopt
Expand Down
35 changes: 21 additions & 14 deletions sw/lib/ocaml/gm.ml
Expand Up @@ -244,20 +244,27 @@ let get_image = fun key ->
if !policy = NoCache then raise Not_found;
get_from_cache cache_dir key
with
Not_found ->
if !policy = NoHttp then raise Not_available;
let rec loop = fun k ->
if String.length k >= 1 then
let url = url_of_tile_key !maps_source k in
let jpg_file = cache_dir // (k ^ ".jpg") in
try
ignore (Http.file_of_url ~dest:jpg_file url);
tile_of_key k, jpg_file
with
Http.Failure _ -> loop (remove_last_char k)
else
raise Not_available in
loop key
Not_found ->
if !policy = NoHttp then raise Not_available;
let rec loop = fun k ->
if String.length k >= 1 then
let url = url_of_tile_key !maps_source k in
let jpg_file = cache_dir // (k ^ ".jpg") in
try
ignore (Http.file_of_url ~dest:jpg_file url);
tile_of_key k, jpg_file
with
Http.Not_Found _ -> loop (remove_last_char k)
| Http.Blocked _ ->
begin
prerr_endline (Printf.sprintf "Seem to be temporarily blocked, '%s'" url);
flush stderr;
raise Not_available
end
| _ -> raise Not_available
else
raise Not_available in
loop key


let rec get_tile = fun wgs84 zoom ->
Expand Down
46 changes: 38 additions & 8 deletions sw/lib/ocaml/http.ml
@@ -1,5 +1,8 @@

exception Failure of string
exception Not_Found of string
exception Blocked of string

open Http_client

let file_of_url = fun ?dest url ->
if String.sub url 0 7 = "file://" then
Expand All @@ -9,10 +12,37 @@ let file_of_url = fun ?dest url ->
match dest with
Some s -> s
| None -> Filename.temp_file "fp" ".wget" in
let c = Printf.sprintf "wget -nv --cache=off -O %s '%s'" tmp_file url in
if Sys.command c = 0 then
tmp_file
else begin
Sys.remove tmp_file;
raise (Failure url)
end
let call = new Http_client.get url in
call#set_response_body_storage (`File (fun () -> tmp_file));
let pipeline = new Http_client.pipeline in
pipeline#add call;
pipeline#run ();
match call#status with
| `Successful ->
(*
prerr_endline (Printf.sprintf "file sucessfull: %s, '%s'" tmp_file url);
flush stderr;
*)
tmp_file
| `Client_error ->
begin
(*
prerr_endline (Printf.sprintf "getting file '%s', client error: %d" url call#response_status_code);
flush stderr;
*)
Sys.remove tmp_file;
match call#response_status_code with
404 -> raise (Not_Found url)
| 403 ->
begin
(*
prerr_endline (Printf.sprintf "Blocked!!!");
flush stderr;
*)
raise (Blocked url)
end
| _ -> raise (Failure url)
end
| _ ->
Sys.remove tmp_file;
raise (Failure url)
2 changes: 2 additions & 0 deletions sw/lib/ocaml/http.mli
@@ -1,4 +1,6 @@
exception Failure of string
exception Not_Found of string
exception Blocked of string
val file_of_url : ?dest:string -> string -> string
(** [file_of_url ?dest url] Downloads a given document and returns
the place where it is stored. Default [dest] is in [/tmp]. *)
8 changes: 6 additions & 2 deletions sw/tools/Makefile
Expand Up @@ -25,8 +25,8 @@ Q=@
OCAML=ocaml
OCAMLC=ocamlc
INCLUDES=-I ../lib/ocaml $(shell ocamlfind query -r -i-format xml-light) -I .
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring)
OCAMLNETCMA=$(shell ocamlfind query -r -a-format -predicates byte netstring)
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring) $(shell ocamlfind query -r -i-format netclient)
OCAMLNETCMA=$(shell ocamlfind query -r -a-format -predicates byte netstring) $(shell ocamlfind query -r -a-format -predicates byte netclient)
OCAMLLEX=ocamllex
OCAMLYACC=ocamlyacc

Expand All @@ -44,6 +44,10 @@ gen_flight_plan.cmo : fp_proc.cmi

gen_common.cmo : gen_common.cmi

gen_srtm.out : ../lib/ocaml/lib-pprz.cma
@echo OC $@
$(Q)$(OCAMLC) $(INCLUDES) $(OCAMLNETINCLUDES) -custom -o $@ unix.cma str.cma netstring.cma netclient.cma xml-light.cma ivy-ocaml.cma lib-pprz.cma gen_common.cmo $^

mergelogs: mergelogs.c
gcc mergelogs.c -o mergelogs

Expand Down
2 changes: 1 addition & 1 deletion sw/tools/wiki_gen/Makefile
Expand Up @@ -14,7 +14,7 @@ Q=@
OCAML=ocaml
OCAMLC=ocamlc
INCLUDES=-I ../../lib/ocaml $(shell ocamlfind query -r -i-format xml-light)
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring)
OCAMLNETINCLUDES=$(shell ocamlfind query -r -i-format netstring) $(shell ocamlfind query -r -i-format netclient)
OCAMLNETCMA=$(shell ocamlfind query -r -a-format -predicates byte netstring)


Expand Down

0 comments on commit 4cc1192

Please sign in to comment.