/[meteor]/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 /trunk/Meteor/Config.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations)
Sun Feb 3 23:10:24 2008 UTC (11 years, 9 months ago) by knops.gerd
Original Path: googlecode.com/svn/trunk/Meteor/Config.pm
File size: 10539 byte(s)
• meteord will return it's version number and exit if any command line parameter is '-v', '-version' or anything in between

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     'Print out this help message',
67     Help => '',
68    
69     'Maximum age of a message in seconds',
70     MaxMessageAge => 7200,
71    
72     'Maximum number of messages to send to a subscriber before forcing their connection to close. Use 0 to disable',
73     MaxMessages => 0,
74    
75     'Maximum number of stored messages per channel',
76     MaxMessagesPerChannel => 250,
77    
78     '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',
79     MaxTime => 0,
80    
81 andrew.betts 20 'Message template, ~text~, ~id~, ~channel~ and ~timestamp~ will be replaced by the appropriate values',
82     MessageTemplate => '<script>p(~id~,"~channel~","~text~");</script>\r\n',
83 knops.gerd 11
84     '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.',
85     PingInterval => 5,
86    
87     'Message to be sent to all persistent and identified subscriber connections (see above) every PingInterval seconds',
88     PingMessage => '<script>p(-1,"");</script>\r\n',
89    
90     'IP address for subscriber server (leave empty for all local addresses)',
91     SubscriberIP => '',
92    
93     'Port number for subscriber connections',
94     SubscriberPort => 4670,
95    
96     'Subscriber Shutdown message, sent when the subscriber server shuts down (leave empty for no message)',
97     SubscriberShutdownMsg => '<script>eof();</script>\r\n',
98    
99     '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.',
100     SubscriberDocumentRoot => '/usr/local/meteor/public_html',
101    
102     '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.',
103     SubscriberDynamicPageAddress => '/push',
104    
105     'The syslog facility to use',
106     SyslogFacility => 'daemon',
107     );
108    
109     our %ConfigFileData=();
110     our %CommandLine=();
111     our %Defaults=();
112     our %ExtraKeys=();
113    
114     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
115     {
116     $Defaults{$DEFAULTS[$i+1]}=$DEFAULTS[$i+2];
117     }
118    
119     ###############################################################################
120     # Class methods
121     ###############################################################################
122     sub updateConfig {
123     my $class=shift;
124    
125     %::CONF=();
126    
127     my $debug=$class->valueForKey('Debug');
128    
129     print STDERR '-'x79 ."\nParamters:\nSource \tName and Value\n".'-'x79 ."\n" if($debug);
130    
131     my @keys=();
132    
133     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
134     {
135     next if($DEFAULTS[$i+1] eq 'Help');
136     push(@keys,$DEFAULTS[$i+1]);
137     }
138     push(@keys,keys %ExtraKeys);
139    
140     foreach my $key (@keys)
141     {
142     if(exists($CommandLine{$key}))
143     {
144     print STDERR "CmdLine" if($debug);
145     $::CONF{$key}=$CommandLine{$key};
146     }
147     elsif(exists($ConfigFileData{$key}))
148     {
149     print STDERR "CnfFile" if($debug);
150     $::CONF{$key}=$ConfigFileData{$key};
151     }
152     elsif(exists($Defaults{$key}))
153     {
154     print STDERR "Default" if($debug);
155     $::CONF{$key}=$Defaults{$key};
156     }
157    
158     print STDERR "\t$key\t$::CONF{$key}\n" if($debug);
159    
160     # Take care of escapes
161     $::CONF{$key}=~s/\\(.)/
162     if($1 eq 'r')
163     {
164     "\r";
165     }
166     elsif($1 eq 'n')
167     {
168     "\n";
169     }
170     elsif($1 eq 's')
171     {
172     ' ';
173     }
174     elsif($1 eq 't')
175     {
176     "\t";
177     }
178     else
179     {
180     $1;
181     }
182     /gex;
183     }
184    
185     print STDERR '-'x79 ."\n" if($debug);
186     }
187    
188     sub valueForKey {
189     my $class=shift;
190     my $key=shift;
191    
192     return $CommandLine{$key} if(exists($CommandLine{$key}));
193     return $ConfigFileData{$key} if(exists($ConfigFileData{$key}));
194    
195     $Defaults{$key};
196     }
197    
198     sub setCommandLineParameters {
199     my $class=shift;
200    
201 knops.gerd 42 #
202     # Quick check if we should show the version, if so ignore everything else
203     # Accept -v, -version, and everything in between
204     #
205     foreach my $p (@_)
206     {
207     if(index($p,'-v')==0 && index('-version',$p)==0)
208     {
209     print "$::PGM $::VERSION\n";
210     exit(0);
211     }
212     }
213    
214 knops.gerd 11 while(my $cnt=scalar(@_))
215     {
216     my $k=shift(@_);
217     &usage("'$k' invalid") unless($k=~s/^\-(?=.+)//);
218    
219     $k='Debug' if($k eq 'd');
220    
221     my $key=undef;
222     my $kl=length($k);
223     my $kOrig=$k;
224     $k=lc($k);
225    
226     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
227     {
228     my $p=$DEFAULTS[$i+1];
229     my $pl=length($p);
230    
231     next if($kl>$pl);
232    
233     #print "$kl $pl $k $p\n";
234    
235     if($kl==$pl && $k eq lc($p))
236     {
237     $key=$p;
238     last;
239     }
240    
241     my $ps=lc(substr($p,0,$kl));
242    
243     if($k eq $ps)
244     {
245     if(defined($key))
246     {
247     &usage("Ambigous parameter name '$kOrig'");
248     }
249     $key=$p;
250     }
251     }
252    
253     if($k=~/^HeaderTemplate(\d+)$/i)
254     {
255     $key="HeaderTemplate$1";
256     $ExtraKeys{$key}=1;
257     }
258    
259     &usage("Unknown parameter name '$kOrig'") unless(defined($key));
260    
261     &usage() if($key eq 'Help');
262    
263     #print "$kOrig: $key\n";
264    
265     $CommandLine{$key}=1;
266    
267     if($cnt>1 && $_[0]!~/^\-(?!\-)/)
268     {
269     my $param=shift;
270     $param=~s/^\-\-/\-/;
271     $CommandLine{$key}=$param;
272     }
273     }
274    
275     $class->readConfig();
276    
277     $class->updateConfig();
278     }
279    
280     sub readConfig {
281     my $class=shift;
282    
283     %ConfigFileData=();
284    
285     my $path=$class->valueForKey('ConfigFileLocation');
286     return unless(defined($path) && -f $path);
287    
288     open(CONFIG,"$path") or &usage("Config file '$path' for read: $!\n");
289     while(<CONFIG>)
290     {
291     next if(/^\s*#/);
292     next if(/^\s*$/);
293    
294     s/[\r\n]*$//;
295    
296     unless(/^(\S+)\s*(.*)/)
297     {
298     &usage("Invalid configuration file parameter line '$_'");
299     }
300    
301     my $key=$1;
302     my $val=$2;
303     $val='' unless(defined($val));
304    
305     if($key=~/^HeaderTemplate\d+$/)
306     {
307     $ExtraKeys{$key}=1;
308     }
309     else
310     {
311     unless(exists($Defaults{$key}))
312     {
313     &usage("Unknown configuration file parameter name '$key'");
314     }
315     if($key eq 'ConfigFileLocation')
316     {
317     &usage("'ConfigFileLocation' parameter not allowed in configuration file!");
318     }
319     }
320    
321     $val=~s/^--/-/;
322    
323     $ConfigFileData{$key}=$val;
324     }
325     close(CONFIG);
326     }
327    
328     sub usage {
329     my $msg=shift || '';
330    
331     if($msg) {
332     print STDERR <<"EOT";
333     $msg;
334     For further help type $::PGM -help
335     or consult docs at http://www.meteorserver.org/
336     EOT
337    
338     } else {
339    
340    
341     print STDERR <<"EOT";
342    
343     Meteor server v1.0 (release date: 1 Dec 2006)
344     Licensed under the terms of the GNU General Public Licence (2.0)
345    
346     Usage:
347    
348     $::PGM [-parameter [value] [-parameter [value]...]]
349    
350     Accepted command-line parameters:
351    
352     EOT
353    
354     for(my $i=0;$i<scalar(@DEFAULTS);$i+=3)
355     {
356     print STDERR "-$DEFAULTS[$i+1]\n$DEFAULTS[$i].\n\n";
357     }
358    
359     print STDERR <<"EOT";
360    
361     Any of the parameters listed above can also be configured in the
362     configuration file. The default location for this file is:
363    
364     $Defaults{'ConfigFileLocation'}
365    
366     For more information and complete documentation, see the Meteor
367     website at http://www.meteorserver.org/
368     EOT
369    
370     }
371     exit(1);
372     }
373    
374     1;
375 andrew.betts 3 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26