Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/web/www/routino/router.pl
Parent Directory
|
Revision Log
Revision 1520 -
(show annotations)
(download)
(as text)
Sat Mar 8 19:36:35 2014 UTC (11 years, 1 month ago) by amb
File MIME type: text/x-perl
File size: 5139 byte(s)
Sat Mar 8 19:36:35 2014 UTC (11 years, 1 month ago) by amb
File MIME type: text/x-perl
File size: 5139 byte(s)
Update all Perl scripts to "use strict".
1 | # |
2 | # Routino generic router Perl script |
3 | # |
4 | # Part of the Routino routing software. |
5 | # |
6 | # This file Copyright 2008-2014 Andrew M. Bishop |
7 | # |
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 strict; |
23 | |
24 | # Use the directory paths script |
25 | require "paths.pl"; |
26 | |
27 | # Load the profiles variables |
28 | require "profiles.pl"; |
29 | |
30 | # Use the perl Time::HiRes module |
31 | use Time::HiRes qw(gettimeofday tv_interval); |
32 | |
33 | my $t0 = [gettimeofday]; |
34 | |
35 | |
36 | # |
37 | # Fill in the default parameters using the ones above (don't use executable compiled in defaults) |
38 | # |
39 | |
40 | sub FillInDefaults |
41 | { |
42 | my(%params)=@_; |
43 | |
44 | $params{transport}=$main::routino->{transport} if(!defined $params{transport}); |
45 | |
46 | my $transport=$params{transport}; |
47 | |
48 | foreach my $highway (keys %{$main::routino->{highways}}) |
49 | { |
50 | my $key="highway-$highway"; |
51 | my $value=$main::routino->{profile_highway}->{$highway}->{$transport}; |
52 | $params{$key}=$value if(!defined $params{$key}); |
53 | |
54 | $key="speed-$highway"; |
55 | $value=$main::routino->{profile_speed}->{$highway}->{$transport}; |
56 | $params{$key}=$value if(!defined $params{$key}); |
57 | } |
58 | |
59 | foreach my $property (keys %{$main::routino->{properties}}) |
60 | { |
61 | my $key="property-$property"; |
62 | my $value=$main::routino->{profile_property}->{$property}->{$transport}; |
63 | $params{$key}=$value if(!defined $params{$key}); |
64 | } |
65 | |
66 | $params{oneway} =~ s/(true|on)/1/; |
67 | $params{oneway} =~ s/(false|off)/0/; |
68 | |
69 | $params{turns} =~ s/(true|on)/1/; |
70 | $params{turns} =~ s/(false|off)/0/; |
71 | |
72 | foreach my $restriction (keys %{$main::routino->{restrictions}}) |
73 | { |
74 | my $key="$restriction"; |
75 | my $value=$main::routino->{profile_restrictions}->{$restriction}->{$transport}; |
76 | $params{$key}=$value if(!defined $params{$key}); |
77 | } |
78 | |
79 | return %params; |
80 | } |
81 | |
82 | |
83 | # |
84 | # Run the router |
85 | # |
86 | |
87 | sub RunRouter |
88 | { |
89 | my($optimise,%params)=@_; |
90 | |
91 | # Combine all of the parameters together |
92 | |
93 | my $params="--$optimise"; |
94 | |
95 | foreach my $key (keys %params) |
96 | { |
97 | $params.=" --$key=$params{$key}"; |
98 | } |
99 | |
100 | # Change directory |
101 | |
102 | mkdir $main::results_dir,0755 if(! -d $main::results_dir); |
103 | chdir $main::results_dir; |
104 | |
105 | # Create a unique output directory |
106 | |
107 | my $uuid; |
108 | |
109 | if($^O eq "darwin") |
110 | { |
111 | chomp($uuid=`echo '$params' $$ | md5 | cut -f1 '-d '`); |
112 | } |
113 | else |
114 | { |
115 | chomp($uuid=`echo '$params' $$ | md5sum | cut -f1 '-d '`); |
116 | } |
117 | |
118 | mkdir $uuid; |
119 | chmod 0775, $uuid; |
120 | chdir $uuid; |
121 | |
122 | # Run the router |
123 | |
124 | my $safe_params =""; |
125 | if($main::data_dir) |
126 | { |
127 | my @pathparts=split('/',$main::data_dir); |
128 | $safe_params.=" --dir=".pop(@pathparts); |
129 | } |
130 | # This works in newer Perl versions, but not older ones. |
131 | #$safe_params.=" --dir=".pop([split('/',$main::data_dir)]) if($main::data_dir); |
132 | $safe_params.=" --prefix=$main::data_prefix" if($main::data_prefix); |
133 | |
134 | open(LOG,">router.log"); |
135 | print LOG "$main::router_exe $params$safe_params\n\n"; # Don't put the full pathnames in the logfile. |
136 | close(LOG); |
137 | |
138 | $params.=" --dir=$main::data_dir" if($main::data_dir); |
139 | $params.=" --prefix=$main::data_prefix" if($main::data_prefix); |
140 | $params.=" --loggable"; |
141 | |
142 | system "$main::bin_dir/$main::router_exe $params >> router.log 2>&1"; |
143 | |
144 | my $status="OK"; |
145 | $status="ERROR" if($? != 0); |
146 | |
147 | my(undef,undef,$cuser,$csystem) = times; |
148 | |
149 | open(LOG,">>router.log"); |
150 | printf LOG "\nTime: %.3f CPU / %.3f elapsed\n",$cuser+$csystem,tv_interval($t0); |
151 | close(LOG); |
152 | |
153 | # Return the results |
154 | |
155 | return($uuid,$status); |
156 | } |
157 | |
158 | |
159 | # |
160 | # Return the output file |
161 | # |
162 | |
163 | # Possible file formats |
164 | |
165 | my %suffixes=( |
166 | "html" => ".html", |
167 | "gpx-route" => "-route.gpx", |
168 | "gpx-track" => "-track.gpx", |
169 | "text" => ".txt", |
170 | "text-all" => "-all.txt", |
171 | "log" => ".log" |
172 | ); |
173 | |
174 | # Possible MIME types |
175 | |
176 | my %mimetypes=( |
177 | "html" => "text/html", |
178 | "gpx-route" => "text/xml", |
179 | "gpx-track" => "text/xml", |
180 | "text" => "text/plain", |
181 | "text-all" => "text/plain", |
182 | "log" => "text/plain" |
183 | ); |
184 | |
185 | sub ReturnOutput |
186 | { |
187 | my($uuid,$type,$format)=@_; |
188 | |
189 | if($type eq "router") { $format="log" } |
190 | |
191 | my $suffix=$suffixes{$format}; |
192 | my $mime =$mimetypes{$format}; |
193 | |
194 | my $file="$main::results_dir/$uuid/$type$suffix"; |
195 | |
196 | # Return the output |
197 | |
198 | if(!$type || !$uuid || !$format || ! -f $file) |
199 | { |
200 | print header('text/plain','404 Not found'); |
201 | print "Not Found!\n"; |
202 | } |
203 | else |
204 | { |
205 | print header($mime); |
206 | |
207 | system "cat $file"; |
208 | } |
209 | } |
210 | |
211 | 1; |