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