Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /branches/2.3.1-dev/web/www/routino/router.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 635 - (hide annotations) (download) (as text)
Fri Feb 11 18:43:15 2011 UTC (14 years, 1 month ago) by amb
Original Path: trunk/web/www/routino/router.pl
File MIME type: text/x-perl
File size: 4519 byte(s)
Move the Javascript and Perl profiles into separate files.

1 amb 569 #
2     # Routino generic router Perl script
3     #
4     # Part of the Routino routing software.
5     #
6 amb 622 # This file Copyright 2008-2011 Andrew M. Bishop
7 amb 569 #
8     # This program is free software: you can redistribute it and/or modify
9     # it under the terms of the GNU Affero General Public License as published by
10     # the Free Software Foundation, either version 3 of the License, or
11     # (at your option) any later version.
12     #
13     # This program is distributed in the hope that it will be useful,
14     # but WITHOUT ANY WARRANTY; without even the implied warranty of
15     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     # GNU Affero General Public License for more details.
17     #
18     # You should have received a copy of the GNU Affero General Public License
19     # along with this program. If not, see <http://www.gnu.org/licenses/>.
20     #
21    
22     # Use the directory paths script
23     require "paths.pl";
24    
25 amb 635 # Load the profiles variables
26     require "profiles.pl";
27    
28 amb 569 # Use the perl Time::HiRes module
29     use Time::HiRes qw(gettimeofday tv_interval);
30    
31     $t0 = [gettimeofday];
32    
33     # Filename prefix
34    
35     $data_prefix="";
36    
37 amb 577 #
38     # Fill in the default parameters using the ones above (don't use executable compiled in defaults)
39     #
40    
41     sub FillInDefaults
42     {
43     my(%params)=@_;
44    
45     $params{transport}=$routino->{transport} if(!defined $params{transport});
46    
47     my($transport)=$params{transport};
48    
49     foreach $highway (keys %{$routino->{highways}})
50     {
51     $key="highway-$highway";
52     $value=$routino->{profile_highway}->{$highway}->{$transport};
53     $params{$key}=$value if(!defined $params{$key});
54    
55     $key="speed-$highway";
56     $value=$routino->{profile_speed}->{$highway}->{$transport};
57     $params{$key}=$value if(!defined $params{$key});
58     }
59    
60     foreach $property (keys %{$routino->{properties}})
61     {
62     $key="property-$property";
63     $value=$routino->{profile_property}->{$property}->{$transport};
64     $params{$key}=$value if(!defined $params{$key});
65     }
66    
67     $params{oneway} =~ s/(true|on)/1/;
68     $params{oneway} =~ s/(false|off)/0/;
69    
70 amb 622 $params{turns} =~ s/(true|on)/1/;
71     $params{turns} =~ s/(false|off)/0/;
72    
73 amb 577 foreach $restriction (keys %{$routino->{restrictions}})
74     {
75     $key="$restriction";
76     $value=$routino->{profile_restrictions}->{$restriction}->{$transport};
77     $params{$key}=$value if(!defined $params{$key});
78     }
79    
80     return %params;
81     }
82    
83    
84     #
85 amb 569 # Run the router
86 amb 577 #
87 amb 569
88     sub RunRouter
89     {
90 amb 577 my($optimise,%params)=@_;
91 amb 569
92     # Combine all of the parameters together
93    
94 amb 577 my($params)="--$optimise";
95 amb 569
96     foreach $key (keys %params)
97     {
98     $params.=" --$key=$params{$key}";
99     }
100    
101     # Change directory
102    
103     mkdir $results_dir,0755 if(! -d $results_dir);
104     chdir $results_dir;
105    
106     # Create a unique output directory
107    
108     chomp($uuid=`echo '$params' $$ | md5sum | cut -f1 '-d '`);
109    
110 amb 572 mkdir $uuid;
111     chmod 0775, $uuid;
112 amb 569 chdir $uuid;
113    
114     # Run the router
115    
116     $params.=" --dir=$data_dir" if($data_dir);
117     $params.=" --prefix=$data_prefix" if($data_prefix);
118 amb 629 $params.=" --loggable";
119 amb 569
120 amb 629 system "$bin_dir/$router_exe $params > router.log 2>&1";
121 amb 569
122     (undef,undef,$cuser,$csystem) = times;
123     $time=sprintf "time: %.3f CPU / %.3f elapsed",$cuser+$csystem,tv_interval($t0);
124    
125 amb 629 $message="";
126    
127     if($? != 0)
128     {
129     $message=`tail -1 router.log`;
130     }
131    
132     $result="";
133    
134 amb 569 if(-f "$optimise.txt")
135     {
136     $result=`tail -1 $optimise.txt`;
137     @result=split(/\t/,$result);
138     $result = $result[4]." , ".$result[5];
139     }
140    
141     # Return the results
142    
143     return($uuid,$time,$result,$message);
144     }
145    
146 amb 577
147     #
148     # Return the output file
149     #
150    
151     # Possible file formats
152    
153     %suffixes=(
154     "html" => ".html",
155     "gpx-route" => "-route.gpx",
156     "gpx-track" => "-track.gpx",
157     "text" => ".txt",
158 amb 629 "text-all" => "-all.txt",
159     "log" => ".log"
160 amb 577 );
161    
162     # Possible MIME types
163    
164     %mimetypes=(
165     "html" => "text/html",
166     "gpx-route" => "text/xml",
167     "gpx-track" => "text/xml",
168     "text" => "text/plain",
169 amb 629 "text-all" => "text/plain",
170     "log" => "text/plain"
171 amb 577 );
172    
173     sub ReturnOutput
174     {
175     my($uuid,$type,$format)=@_;
176    
177 amb 629 if($type eq "router") { $format="log" }
178    
179 amb 577 $suffix=$suffixes{$format};
180     $mime =$mimetypes{$format};
181    
182     $file="$results_dir/$uuid/$type$suffix";
183    
184     # Return the output
185    
186     if(!$type || !$uuid || !$format || ! -f $file)
187     {
188     print header('text/plain','404 Not found');
189     print "Not Found!\n";
190     }
191     else
192     {
193     print header($mime);
194    
195     system "cat $file";
196     }
197     }
198    
199 amb 569 1;