/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 49 - (hide annotations)
Mon Feb 4 22:30:35 2008 UTC (11 years, 10 months ago) by knops.gerd
File size: 11127 byte(s)
• Removed support for numbered header templates and hard coded persist values, use new mode support instead

1 knops.gerd 11 #!/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     'Configuration file location on disk (if any)',
46     ConfigFileLocation => '/etc/meteord.conf',
47    
48     'IP address for controller server (leave empty for all local addresses)',
49     ControllerIP => '',
50    
51     'Port number for controller connections',
52     ControllerPort => 4671,
53    
54     'Controller Shutdown message, sent when the controller server shuts down (leave empty for no message)',
55     ControllerShutdownMsg => '',
56    
57     'Debug Flag, when set daemon will run in foreground and emit debug messages',
58     Debug => 0,
59    
60     'Name of index file to serve when a directory is requested from the static file web server',
61     DirectoryIndex => 'index.html',
62    
63     'Header template, ~server~, ~servertime~ and ~status~ will be replaced by the appropriate values. **NOTE**: It is possible to define more than one HeaderTemplate by appending a number at the end, for example *HeaderTemplate42*. Clients can request a specific header to be used by adding the parameter template=<number> to their GET request. If *HeaderTemplate<number>* is not found, the system will use the default HeaderTemplate (no number)',
64 andrew.betts 26 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<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">\r\n<meta http-equiv="Cache-Control" content="no-store">\r\n<meta http-equiv="Cache-Control" content="no-cache">\r\n<meta http-equiv="Pragma" content="no-cache">\r\n<meta http-equiv="Expires" content="Thu, 1 Jan 1970 00:00:00 GMT">\r\n<script type="text/javascript">\r\nwindow.onError = null;\r\nvar domainparts = document.domain.split(".");\r\ndocument.domain = domainparts[domainparts.length-2]+"."+domainparts[domainparts.length-1];\r\nparent.Meteor.register(this);\r\n</script>\r\n</head>\r\n<body onload="try { parent.Meteor.reset(this) } catch (e) {}">\r\n',
65 knops.gerd 11
66 knops.gerd 45 'Template for each line in channelinfo',
67    
68 knops.gerd 46 ChannelInfoTemplate => '~name~ ~messageCount~ ~lastMsgID~\r\n',
69 knops.gerd 45
70 knops.gerd 11 'Print out this help message',
71     Help => '',
72    
73     'Maximum age of a message in seconds',
74     MaxMessageAge => 7200,
75    
76     'Maximum number of messages to send to a subscriber before forcing their connection to close. Use 0 to disable',
77     MaxMessages => 0,
78    
79     'Maximum number of stored messages per channel',
80     MaxMessagesPerChannel => 250,
81    
82     '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',
83     MaxTime => 0,
84    
85 andrew.betts 20 'Message template, ~text~, ~id~, ~channel~ and ~timestamp~ will be replaced by the appropriate values',
86     MessageTemplate => '<script>p(~id~,"~channel~","~text~");</script>\r\n',
87 knops.gerd 11
88     'Interval at which PingMessage is sent to all persistent and identified subscriber connections (ie those including id=someuniqueidentifier in their request, and not specifying persist=0). Must be at least 3 if set higher than zero. Set to zero to disable.',
89     PingInterval => 5,
90    
91 knops.gerd 45 'Persistence of a connection. Note: some modes have this hardcoded!',
92    
93     Persist => 0,
94    
95 knops.gerd 11 'Message to be sent to all persistent and identified subscriber connections (see above) every PingInterval seconds',
96     PingMessage => '<script>p(-1,"");</script>\r\n',
97    
98     'IP address for subscriber server (leave empty for all local addresses)',
99     SubscriberIP => '',
100    
101     'Port number for subscriber connections',
102     SubscriberPort => 4670,
103    
104     'Subscriber Shutdown message, sent when the subscriber server shuts down (leave empty for no message)',
105     SubscriberShutdownMsg => '<script>eof();</script>\r\n',
106    
107     '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.',
108     SubscriberDocumentRoot => '/usr/local/meteor/public_html',
109    
110     'Since Meteor is capable of serving static pages from a document root as well as streaming events to subscribers, this paramter 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.',
111     SubscriberDynamicPageAddress => '/push',
112    
113     'The syslog facility to use',
114     SyslogFacility => 'daemon',
115 knops.gerd 48
116     'IP address for udp server (leave empty for all local addresses)',
117     UDPIP => '',
118    
119     'Port number for udp connections, set to 0 to disable',
120     UDPPort => 0,
121    
122 knops.gerd 11 );
123    
124     our %ConfigFileData=();
125     our %CommandLine=();
126     our %Defaults=();
127 knops.gerd 45 our %Modes=();
128 knops.gerd 11
129     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
130     {
131     $Defaults{$DEFAULTS[$i+1]}=$DEFAULTS[$i+2];
132     }
133    
134     ###############################################################################
135     # Class methods
136     ###############################################################################
137     sub updateConfig {
138     my $class=shift;
139    
140     %::CONF=();
141    
142     my $debug=$class->valueForKey('Debug');
143    
144     print STDERR '-'x79 ."\nParamters:\nSource \tName and Value\n".'-'x79 ."\n" if($debug);
145    
146     my @keys=();
147    
148     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
149     {
150     next if($DEFAULTS[$i+1] eq 'Help');
151     push(@keys,$DEFAULTS[$i+1]);
152     }
153    
154 knops.gerd 45 foreach my $mode ('',keys %Modes)
155     {
156     foreach my $baseKey (@keys)
157 knops.gerd 11 {
158 knops.gerd 45 my $foundValue=0;
159     my $key=$baseKey.$mode;
160    
161     if(exists($CommandLine{$key}))
162 knops.gerd 11 {
163 knops.gerd 45 print STDERR "CmdLine" if($debug);
164     $::CONF{$key}=$CommandLine{$key};
165     $foundValue=1;
166 knops.gerd 11 }
167 knops.gerd 45 elsif(exists($ConfigFileData{$key}))
168 knops.gerd 11 {
169 knops.gerd 45 print STDERR "CnfFile" if($debug);
170     $::CONF{$key}=$ConfigFileData{$key};
171     $foundValue=1;
172 knops.gerd 11 }
173 knops.gerd 45 elsif(exists($Defaults{$key}))
174 knops.gerd 11 {
175 knops.gerd 45 print STDERR "Default" if($debug);
176     $::CONF{$key}=$Defaults{$key};
177     $foundValue=1;
178 knops.gerd 11 }
179 knops.gerd 45
180     next unless($foundValue);
181    
182     print STDERR "\t$key\t$::CONF{$key}\n" if($debug);
183    
184     # Take care of escapes
185     $::CONF{$key}=~s/\\(.)/
186     if($1 eq 'r')
187     {
188     "\r";
189     }
190     elsif($1 eq 'n')
191     {
192     "\n";
193     }
194     elsif($1 eq 's')
195     {
196     ' ';
197     }
198     elsif($1 eq 't')
199     {
200     "\t";
201     }
202     else
203     {
204     $1;
205     }
206     /gex;
207     }
208 knops.gerd 11 }
209     print STDERR '-'x79 ."\n" if($debug);
210     }
211    
212     sub valueForKey {
213     my $class=shift;
214     my $key=shift;
215    
216     return $CommandLine{$key} if(exists($CommandLine{$key}));
217     return $ConfigFileData{$key} if(exists($ConfigFileData{$key}));
218    
219     $Defaults{$key};
220     }
221    
222     sub setCommandLineParameters {
223     my $class=shift;
224    
225 knops.gerd 42 #
226     # Quick check if we should show the version, if so ignore everything else
227     # Accept -v, -version, and everything in between
228     #
229     foreach my $p (@_)
230     {
231     if(index($p,'-v')==0 && index('-version',$p)==0)
232     {
233     print "$::PGM $::VERSION\n";
234     exit(0);
235     }
236     }
237    
238 knops.gerd 11 while(my $cnt=scalar(@_))
239     {
240     my $k=shift(@_);
241     &usage("'$k' invalid") unless($k=~s/^\-(?=.+)//);
242    
243     $k='Debug' if($k eq 'd');
244    
245 knops.gerd 45 my $mode='';
246    
247     if($k=~s/(\..+)$//)
248     {
249     $mode=$1;
250     $Modes{$mode}=1;
251     }
252    
253 knops.gerd 11 my $key=undef;
254     my $kl=length($k);
255     my $kOrig=$k;
256     $k=lc($k);
257    
258     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
259     {
260     my $p=$DEFAULTS[$i+1];
261     my $pl=length($p);
262    
263     next if($kl>$pl);
264    
265     #print "$kl $pl $k $p\n";
266    
267     if($kl==$pl && $k eq lc($p))
268     {
269     $key=$p;
270     last;
271     }
272    
273     my $ps=lc(substr($p,0,$kl));
274    
275     if($k eq $ps)
276     {
277     if(defined($key))
278     {
279     &usage("Ambigous parameter name '$kOrig'");
280     }
281     $key=$p;
282     }
283     }
284    
285     &usage("Unknown parameter name '$kOrig'") unless(defined($key));
286    
287     &usage() if($key eq 'Help');
288    
289     #print "$kOrig: $key\n";
290    
291 knops.gerd 45 $CommandLine{"$key$mode"}=1;
292 knops.gerd 11
293     if($cnt>1 && $_[0]!~/^\-(?!\-)/)
294     {
295     my $param=shift;
296     $param=~s/^\-\-/\-/;
297 knops.gerd 45 $CommandLine{"$key$mode"}=$param;
298 knops.gerd 11 }
299     }
300    
301     $class->readConfig();
302    
303     $class->updateConfig();
304     }
305    
306     sub readConfig {
307     my $class=shift;
308    
309     %ConfigFileData=();
310    
311     my $path=$class->valueForKey('ConfigFileLocation');
312     return unless(defined($path) && -f $path);
313    
314 knops.gerd 45 my $mode='';
315    
316 knops.gerd 11 open(CONFIG,"$path") or &usage("Config file '$path' for read: $!\n");
317     while(<CONFIG>)
318     {
319     next if(/^\s*#/);
320     next if(/^\s*$/);
321    
322     s/[\r\n]*$//;
323    
324 knops.gerd 45 if(/^\s*\[\s*([^\]\s]+)\s*\]\s*$/)
325     {
326     $Modes{".$1"}=1;
327     next;
328     }
329    
330 knops.gerd 11 unless(/^(\S+)\s*(.*)/)
331     {
332     &usage("Invalid configuration file parameter line '$_'");
333     }
334    
335     my $key=$1;
336     my $val=$2;
337     $val='' unless(defined($val));
338    
339 knops.gerd 49 unless(exists($Defaults{$key}))
340 knops.gerd 11 {
341 knops.gerd 49 &usage("Unknown configuration file parameter name '$key$mode'");
342 knops.gerd 11 }
343 knops.gerd 49 if($key eq 'ConfigFileLocation')
344 knops.gerd 11 {
345 knops.gerd 49 &usage("'ConfigFileLocation' parameter not allowed in configuration file!");
346 knops.gerd 11 }
347    
348     $val=~s/^--/-/;
349    
350 knops.gerd 45 $ConfigFileData{"$key$mode"}=$val;
351 knops.gerd 11 }
352     close(CONFIG);
353     }
354    
355     sub usage {
356     my $msg=shift || '';
357    
358     if($msg) {
359     print STDERR <<"EOT";
360     $msg;
361     For further help type $::PGM -help
362     or consult docs at http://www.meteorserver.org/
363     EOT
364    
365     } else {
366    
367    
368     print STDERR <<"EOT";
369    
370 knops.gerd 45 Meteor server v$::VERSION (release date: $::RELEASE_DATE)
371 knops.gerd 11 Licensed under the terms of the GNU General Public Licence (2.0)
372    
373     Usage:
374    
375     $::PGM [-parameter [value] [-parameter [value]...]]
376    
377     Accepted command-line parameters:
378    
379     EOT
380    
381     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
382     {
383     print STDERR "-$DEFAULTS[$i+1]\n$DEFAULTS[$i].\n\n";
384     }
385    
386     print STDERR <<"EOT";
387    
388     Any of the parameters listed above can also be configured in the
389     configuration file. The default location for this file is:
390    
391     $Defaults{'ConfigFileLocation'}
392    
393     For more information and complete documentation, see the Meteor
394     website at http://www.meteorserver.org/
395     EOT
396    
397     }
398     exit(1);
399     }
400    
401     1;
402 andrew.betts 3 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26