/[meteor]/googlecode.com/svn/trunk/Meteor/Config.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /googlecode.com/svn/trunk/Meteor/Config.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 57 - (show annotations)
Sun Mar 2 22:18:59 2008 UTC (11 years, 9 months ago) by andrew.betts
File size: 10253 byte(s)
Updated some config defaults
Release


1 #!/usr/bin/perl -w
2 ###############################################################################
3 # Meteor
4 # An HTTP server for the 2.0 web
5 # Copyright (c) 2006 contributing authors
6 #
7 # Subscriber.pm
8 #
9 # Description:
10 # Meteor Configuration handling.
11 #
12 # Main program should call Meteor::Config::setCommandLineParameters(@ARGV),.
13 # Afterwards anybody can access $::CONF{<parameterName>}, where
14 # <parameterName> is any valid parameter (except 'Help') listed in the
15 # @DEFAULTS array below.
16 #
17 ###############################################################################
18 #
19 # This program is free software; you can redistribute it and/or modify it
20 # under the terms of the GNU General Public License as published by the Free
21 # Software Foundation; either version 2 of the License, or (at your option)
22 # any later version.
23 #
24 # This program is distributed in the hope that it will be useful, but WITHOUT
25 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
27 # more details.
28 #
29 # You should have received a copy of the GNU General Public License along
30 # with this program; if not, write to the Free Software Foundation, Inc.,
31 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #
33 # For more information visit www.meteorserver.org
34 #
35 ###############################################################################
36
37 package Meteor::Config;
38 ###############################################################################
39 # Configuration
40 ###############################################################################
41
42 use strict;
43
44 our @DEFAULTS=(
45 'Template for each line in channelinfo',
46 ChannelInfoTemplate => '<script>ch("~name~", ~lastMsgID~);</script>\r\n',
47
48 'Configuration file location on disk (if any)',
49 ConfigFileLocation => '/etc/meteord.conf',
50
51 'IP address for controller server (leave empty for all local addresses)',
52 ControllerIP => '',
53
54 'Port number for controller connections',
55 ControllerPort => 4671,
56
57 'Controller Shutdown message, sent when the controller server shuts down (leave empty for no message)',
58 ControllerShutdownMsg => '',
59
60 'Debug Flag, when set daemon will run in foreground and emit debug messages',
61 Debug => 0,
62
63 'Name of index file to serve when a directory is requested from the static file web server',
64 DirectoryIndex => 'index.html',
65
66 'Header template, ~server~, ~servertime~ and ~status~ will be replaced by the appropriate values.',
67 HeaderTemplate => 'HTTP/1.1 ~status~\r\nServer: ~server~\r\nContent-Type: text/html; charset=utf-8\r\nPragma: no-cache\r\nCache-Control: no-cache, no-store, must-revalidate\r\nExpires: Thu, 1 Jan 1970 00:00:00 GMT\r\n\r\n',
68
69 'Print out this help message',
70 Help => '',
71
72 'Format to use for timestamps in syslog: unix or human',
73 LogTimeFormat => 'human',
74
75 'Maximum age of a message in seconds',
76 MaxMessageAge => 7200,
77
78 'Maximum number of messages to send to a subscriber before forcing their connection to close. Use 0 to disable',
79 MaxMessages => 0,
80
81 'Maximum number of stored messages per channel',
82 MaxMessagesPerChannel => 250,
83
84 'Maximum duration in seconds for a subscriber connection to exist before forcing a it to close. Note that the server checks for expired connections in 60 second intervals, so small changes to this value will not have much of an effect. Use 0 to disable',
85 MaxTime => 0,
86
87 'Message template, ~text~, ~id~, ~channel~ and ~timestamp~ will be replaced by the appropriate values',
88 MessageTemplate => '<script>p(~id~,"~channel~","~text~");</script>\r\n',
89
90 'Interval at which PingMessage is sent to all persistent subscriber connections. Must be at least 3 if set higher than zero. Set to zero to disable.',
91 PingInterval => 5,
92
93 'Persistence of a connection.',
94 Persist => 0,
95
96 'Message to be sent to all persistent subscriber connections (see above) every PingInterval seconds',
97 PingMessage => '<script>p(-1,"");</script>\r\n',
98
99 'IP address for subscriber server (leave empty for all local addresses)',
100 SubscriberIP => '',
101
102 'Port number for subscriber connections',
103 SubscriberPort => 4670,
104
105 'Subscriber Shutdown message, sent when the subscriber server shuts down (leave empty for no message)',
106 SubscriberShutdownMsg => '<script>eof();</script>\r\n',
107
108 'An absolute filesystem path, to be used as the document root for Meteor\'s static file web server. If left empty, no documents will be served.',
109 SubscriberDocumentRoot => '/usr/local/meteor/public_html',
110
111 'Since Meteor is capable of serving static pages from a document root as well as streaming events to subscribers, this parameter is used to specify the URI at which the event server can be reached. If set to the root, Meteor will lose the ability to serve static pages.',
112 SubscriberDynamicPageAddress => '/push',
113
114 'The syslog facility to use',
115 SyslogFacility => 'daemon',
116
117 'IP address for udp server (leave empty for all local addresses)',
118 UDPIP => '',
119
120 'Port number for udp connections, set to 0 to disable',
121 UDPPort => 0,
122
123 );
124
125 our %ConfigFileData=();
126 our %CommandLine=();
127 our %Defaults=();
128 our %Modes=();
129
130 for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
131 {
132 $Defaults{$DEFAULTS[$i+1]}=$DEFAULTS[$i+2];
133 }
134
135 ###############################################################################
136 # Class methods
137 ###############################################################################
138 sub updateConfig {
139 my $class=shift;
140
141 %::CONF=();
142
143 my $debug=$class->valueForKey('Debug');
144
145 print STDERR '-'x79 ."\nMeteor server v$::VERSION (release date: $::RELEASE_DATE)\r\nLicensed under the terms of the GNU General Public Licence (2.0)\n".'-'x79 ."\n" if($debug);
146
147 my @keys=();
148
149 for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
150 {
151 next if($DEFAULTS[$i+1] eq 'Help');
152 push(@keys,$DEFAULTS[$i+1]);
153 }
154
155 foreach my $mode ('',keys %Modes)
156 {
157 print STDERR ($mode) ? "\r\n$mode:\r\n" : "\r\nDefaults:\r\n" if($debug);
158 foreach my $baseKey (@keys)
159 {
160 my $foundValue=0;
161 my $key=$baseKey.$mode;
162
163 if(exists($CommandLine{$key}))
164 {
165 print STDERR "CmdLine" if($debug);
166 $::CONF{$key}=$CommandLine{$key};
167 $foundValue=1;
168 }
169 elsif(exists($ConfigFileData{$key}))
170 {
171 print STDERR "CnfFile" if($debug);
172 $::CONF{$key}=$ConfigFileData{$key};
173 $foundValue=1;
174 }
175 elsif(exists($Defaults{$key}))
176 {
177 print STDERR "Default" if($debug);
178 $::CONF{$key}=$Defaults{$key};
179 $foundValue=1;
180 }
181
182 next unless($foundValue);
183
184 print STDERR "\t$baseKey\t$::CONF{$key}\n" if($debug);
185
186 # Take care of escapes
187 $::CONF{$key}=~s/\\(.)/
188 if($1 eq 'r') {
189 "\r";
190 } elsif($1 eq 'n') {
191 "\n";
192 } elsif($1 eq 's') {
193 ' ';
194 } elsif($1 eq 't') {
195 "\t";
196 } elsif($1 eq '0') {
197 "\0";
198 } else {
199 $1;
200 }
201 /gex;
202 }
203 }
204 print STDERR '-'x79 ."\n" if($debug);
205 }
206
207 sub valueForKey {
208 my $class=shift;
209 my $key=shift;
210
211 return $CommandLine{$key} if(exists($CommandLine{$key}));
212 return $ConfigFileData{$key} if(exists($ConfigFileData{$key}));
213
214 $Defaults{$key};
215 }
216
217 sub setCommandLineParameters {
218 my $class=shift;
219
220 #
221 # Quick check if we should show the version, if so ignore everything else
222 # Accept -v, -version, and everything in between
223 #
224 foreach my $p (@_)
225 {
226 if(index($p,'-v')==0 && index('-version',$p)==0)
227 {
228 print "$::PGM $::VERSION\n";
229 exit(0);
230 }
231 }
232
233 while(my $cnt=scalar(@_))
234 {
235 my $k=shift(@_);
236 &usage("'$k' invalid") unless($k=~s/^\-(?=.+)//);
237
238 $k='Debug' if($k eq 'd');
239
240 my $mode='';
241
242 if($k=~s/(\.(.+))$//)
243 {
244 $mode=$2;
245 $Modes{$mode}=1;
246 }
247
248 my $key=undef;
249 my $kl=length($k);
250 my $kOrig=$k;
251 $k=lc($k);
252
253 for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
254 {
255 my $p=$DEFAULTS[$i+1];
256 my $pl=length($p);
257
258 next if($kl>$pl);
259
260 #print "$kl $pl $k $p\n";
261
262 if($kl==$pl && $k eq lc($p))
263 {
264 $key=$p;
265 last;
266 }
267
268 my $ps=lc(substr($p,0,$kl));
269
270 if($k eq $ps)
271 {
272 if(defined($key))
273 {
274 &usage("Ambigous parameter name '$kOrig'");
275 }
276 $key=$p;
277 }
278 }
279
280 &usage("Unknown parameter name '$kOrig'") unless(defined($key));
281
282 &usage() if($key eq 'Help');
283
284 #print "$kOrig: $key\n";
285
286 $CommandLine{"$key$mode"}=1;
287
288 if($cnt>1 && $_[0]!~/^\-(?!\-)/)
289 {
290 my $param=shift;
291 $param=~s/^\-\-/\-/;
292 $CommandLine{"$key$mode"}=$param;
293 }
294 }
295
296 $class->readConfig();
297
298 $class->updateConfig();
299 }
300
301 sub readConfig {
302 my $class=shift;
303
304 %ConfigFileData=();
305
306 my $path=$class->valueForKey('ConfigFileLocation');
307 return unless(defined($path) && -f $path);
308
309 my $mode='';
310
311 open(CONFIG,"$path") or &usage("Config file '$path' for read: $!\n");
312 while(<CONFIG>)
313 {
314 next if(/^\s*#/);
315 next if(/^\s*$/);
316
317 s/[\r\n]*$//;
318
319 if(/^\s*\[\s*([^\]\s]+)\s*\]\s*$/)
320 {
321 $Modes{$1}=1;
322 $mode = $1;
323 next;
324 }
325
326 unless(/^(\S+)\s*(.*)/)
327 {
328 &usage("Invalid configuration file parameter line '$_'");
329 }
330
331 my $key=$1;
332 my $val=$2;
333 $val='' unless(defined($val));
334
335 unless(exists($Defaults{$key}))
336 {
337 &usage("Unknown configuration file parameter name '$key$mode'");
338 }
339 if($key eq 'ConfigFileLocation')
340 {
341 &usage("'ConfigFileLocation' parameter not allowed in configuration file!");
342 }
343
344 $val=~s/^--/-/;
345
346 $ConfigFileData{"$key$mode"}=$val;
347 }
348 close(CONFIG);
349 }
350
351 sub usage {
352 my $msg=shift || '';
353
354 if($msg) {
355 print STDERR <<"EOT";
356 $msg;
357 For further help type $::PGM -help
358 or consult docs at http://meteorserver.org/
359 EOT
360
361 } else {
362
363
364 print STDERR <<"EOT";
365
366 Meteor server v$::VERSION (release date: $::RELEASE_DATE)
367 Licensed under the terms of the GNU General Public Licence (2.0)
368
369 Usage:
370
371 $::PGM [-parameter [value] [-parameter [value]...]]
372
373 Accepted command-line parameters:
374
375 EOT
376
377 for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
378 {
379 print STDERR "-$DEFAULTS[$i+1]\n$DEFAULTS[$i].\n\n";
380 }
381
382 print STDERR <<"EOT";
383
384 Any of the parameters listed above can also be configured in the
385 configuration file. The default location for this file is:
386
387 $Defaults{'ConfigFileLocation'}
388
389 For more information and complete documentation, see the Meteor
390 website at http://meteorserver.org/
391 EOT
392
393 }
394 exit(1);
395 }
396
397 1;
398 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26