]> matita.cs.unibo.it Git - helm.git/blob - helm/graphs/tools/drawGraph.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / graphs / tools / drawGraph.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 open Printf;;
27
28 let debug = true;;
29 let debug_print s = if debug then prerr_endline s;;
30
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";;
36
37 let wget url fname =
38   prerr_endline (sprintf "DEBUG: wgetting url '%s'" url);
39   let data = Http_client.Convenience.http_get url in
40   let oc = open_out fname in
41   output_string oc data;
42   close_out oc
43 ;;
44
45 let port =
46   try
47     int_of_string (Sys.getenv port_env_var)
48   with
49   | Not_found -> default_port
50   | Failure "int_of_string" ->
51       prerr_endline "Warning: invalid port, reverting to default";
52       default_port
53 in
54 let dir = try Sys.getenv dir_env_var with Not_found -> default_dir in
55 let errmsg =
56   sprintf
57 "<html>
58  <head>
59   <title>Graph: error</title>
60  </head>
61  <body>
62   <h1>Error occured while drawing graph!<br />Please report the occured problem</h1>
63   <h2>%s</h2>
64  </body>
65 </html>"
66 in
67 let string_of_exit_status = function
68   | Unix.WEXITED n -> sprintf "Process exited with code %d" n
69   | Unix.WSIGNALED n -> sprintf "Process killed by signal %d" n
70   | Unix.WSTOPPED n -> sprintf "Process stopped by signal %d" n
71 in
72 let callback (req: Http_types.request) outchan =
73   try
74     (match req#path with
75     | "/draw" ->
76         let url = req#param "url" in
77         let pid = Unix.getpid () in
78         wget (sprintf "%s&param.PID=%d" url pid) (sprintf "prova0.%d.dot" pid);
79         (match Unix.system (sprintf "make PID=%d > log.%d" pid pid) with
80         | Unix.WEXITED 0 ->
81             debug_print "HTML successfully generated";
82             Http_daemon.respond_file (sprintf "prova.%d.html" pid) outchan
83         | status ->
84             debug_print "Failure, sending error message";
85             let res =
86               new Http_response.response
87                 ~body:
88                   (errmsg ("Exit status: " ^ (string_of_exit_status status)))
89                 ()
90             in
91             Http_daemon.respond_with res outchan)
92     | "/get_gif" ->
93         let pid = req#param "pid" in
94         Http_daemon.respond_file (sprintf "prova.%s.gif" pid) outchan;
95         ignore (Unix.system (
96           sprintf "make PID=%s clean; rm -f prova0.%s.dot" pid pid))
97     | invalid_request ->
98         Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
99   with
100   | Http_types.Param_not_found attr_name ->
101       Http_daemon.respond_error
102         ~status:(`Client_error `Bad_request)
103         ~body:(sprintf "Parameter '%s' is missing" attr_name)
104         outchan
105 in
106 Sys.chdir dir;
107 printf "%s started and listening on port %d\n" daemon_name port;
108 printf "current directory is %s\n" (Sys.getcwd ());
109 flush stdout;
110 Http_daemon.start' ~port callback;
111 printf "%s is terminating, bye!\n" daemon_name
112