1 (* Copyright (C) 2002, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
29 let debug_print s = if debug then prerr_endline s;;
31 let daemon_name = "Draw Graph";;
32 let default_port = 48083;;
33 let default_dir = "/projects/helm/graphs/tools";;
34 let port_env_var = "DRAW_GRAPH_PORT";;
35 let dir_env_var = "DRAW_GRAPH_DIR";;
38 prerr_endline (sprintf "DEBUG: wgetting url '%s'" url);
39 let oc = open_out fname in
40 Http_client.http_get_iter (output_string oc) url;
46 int_of_string (Sys.getenv port_env_var)
48 | Not_found -> default_port
49 | Failure "int_of_string" ->
50 prerr_endline "Warning: invalid port, reverting to default";
53 let dir = try Sys.getenv dir_env_var with Not_found -> default_dir in
58 <title>Graph: error</title>
61 <h1>Error occurred while drawing graph!<br />Please report the occured problem</h1>
66 let string_of_exit_status = function
67 | Unix.WEXITED n -> sprintf "Process exited with code %d" n
68 | Unix.WSIGNALED n -> sprintf "Process killed by signal %d" n
69 | Unix.WSTOPPED n -> sprintf "Process stopped by signal %d" n
71 let callback (req: Http_types.request) outchan =
75 let url = req#param "url" in
76 let pid = Unix.getpid () in
77 wget (sprintf "%s¶m.PID=%d" url pid) (sprintf "prova0.%d.dot" pid);
78 (match Unix.system (sprintf "make tmp PID=%d > log.%d" pid pid) with
80 debug_print "HTML successfully generated";
81 Http_daemon.respond_file (sprintf "prova.%d.html" pid) outchan
83 debug_print "Failure, sending error message";
85 new Http_response.response
87 (errmsg ("Exit status: " ^ (string_of_exit_status status)))
90 Http_daemon.respond_with res outchan)
92 let pid = req#param "pid" in
93 Http_daemon.respond_file (sprintf "prova.%s.gif" pid) outchan;
95 sprintf "make PID=%s clean_tmp; rm -f prova0.%s.dot" pid pid))
97 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
99 | Http_types.Param_not_found attr_name ->
100 Http_daemon.respond_error
101 ~status:(`Client_error `Bad_request)
102 ~body:(sprintf "Parameter '%s' is missing" attr_name)
106 printf "%s started and listening on port %d\n" daemon_name port;
107 printf "current directory is %s\n" (Sys.getcwd ());
109 Http_daemon.start' ~port callback;
110 printf "%s is terminating, bye!\n" daemon_name