-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteord
394 lines (315 loc) · 9.06 KB
/
meteord
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/perl -w
###############################################################################
# Meteor
# An HTTP server for the 2.0 web
# Copyright (c) 2006-2010 contributing authors
#
# The Meteor daemon
#
# Main program should call Meteor::Config::setCommandLineParameters(@ARGV),.
# Afterwards anybody can access $::CONF{<parameterName>}, where
# <parameterName> is any valid parameter (except 'Help') listed in the
# @DEFAULTS array below.
#
###############################################################################
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# For more information visit www.meteorserver.org
#
###############################################################################
###############################################################################
# meteord version
################################################################################
$::VERSION='1.06.07';
$::RELEASE_DATE='not yet released';
###############################################################################
# Configuration
###############################################################################
use strict;
use Socket;
use Meteor::Syslog;
use Meteor::Socket;
use Meteor::Connection;
use Meteor::Controller;
use Meteor::Subscriber;
use Meteor::Channel;
use Meteor::Document;
use Meteor::Config;
$::CRLF="\r\n"; # Line separator to be used throughout all modules
our $CONTROL_QUEUE_SIZE=5;
our $SUBSCRIBER_QUEUE_SIZE=20;
our $MAIN_LOOP_TIMEOUT=60;
our $AGE_CHECK_INTERVALL=60;
our $MAX_EXIT_DELAY=120;
our $UDP_MAX_MESSAGE_SIZE=8192;
###############################################################################
# Main
###############################################################################
#
# Record startup time
#
$::STARTUP_TIME=time;
$::STARTUP_TIME+=0; # avoid warning
#
# Program name
#
$::PGM=$0;
$::PGM=~s/^.*\///;
#
# Handle command line options and config file
#
Meteor::Config->setCommandLineParameters(@ARGV);
#
# Do something about warn and die
#
unless($::CONF{'Debug'})
{
$SIG{'__WARN__'}=\&Meteor::Syslog::myWarn;
$SIG{'__DIE__'}=\&Meteor::Syslog::myDie;
}
&::syslog('info',"$::PGM launched!");
#
# Daemonize
#
{
$0="$::PGM daemon";
my $facility=$::CONF{'SyslogFacility'} || $Meteor::Syslog::DEFAULT_FACILITY;
unless($::CONF{'Debug'} || $facility eq 'none')
{
# close standard file descriptors
close(STDIN);
close(STDOUT);
close(STDERR);
chdir("/");
umask(0);
# fork and exit parent
print STDERR "Exit: Fork\n";
exit if fork;
setpgrp(0, $$) if defined $SIG{TTOU};
$SIG{TTOU}='ignore' if defined $SIG{TTOU};
# Avoid 'stdin reopened for output' warning with newer perls
open(NULL,'/dev/null');
<NULL> if(0);
open(OUT,">/var/run/$::PGM.pid");
print OUT "$$\n";
close(OUT);
}
else
{
&::syslog('info',"PID\t%s",$$);
open(OUT,">/var/run/$::PGM.pid");
print OUT "$$\n";
close(OUT);
}
}
#
# Signal handlers
#
$::HUP=$::TERM=$::USR1=$::USR2=0;
$SIG{'HUP'}=sub{$::HUP=1};
$SIG{'TERM'}=sub{$::TERM=1};
$SIG{'USR1'}=sub{$::USR1=1};
$SIG{'USR2'}=sub{$::USR2=1};
$SIG{'PIPE'}=sub{&::syslog('info',"Signal PIPE received and ignored\n");};
#
# Run server
#
my $con_counter=0;
my $con;
my $controlServer=Meteor::Socket->newServer(
$::CONF{'ControllerPort'},
$CONTROL_QUEUE_SIZE,
$::CONF{'ControllerIP'}
);
my $controlServerFN=$controlServer->fileno();
my $subscriberServer=Meteor::Socket->newServer(
$::CONF{'SubscriberPort'},
$SUBSCRIBER_QUEUE_SIZE,
$::CONF{'SubscriberIP'}
);
my $subscriberServerFN=$subscriberServer->fileno();
my $udpServer=undef;
my $udpPort=$::CONF{'UDPPort'};
my $udpServerFN=undef;
if($udpPort && $udpPort>0)
{
$udpServer=Meteor::Socket->newUDPServer(
$udpPort,
$::CONF{'UDPIP'}
);
$udpServerFN=$udpServer->fileno();
}
my $serverVector='';
vec($serverVector,$controlServerFN,1)=1;
vec($serverVector,$subscriberServerFN,1)=1;
vec($serverVector,$udpServerFN,1)=1 if(defined($udpServerFN));
my $lastAgeCheck=time;
my $nextPing=undef;
if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2)
{
$nextPing=$::CONF{'PingInterval'}+$lastAgeCheck;
}
while(!$::TERM)
{
eval
{
while(!$::TERM)
{
my $rVec=$serverVector;
my $wVec='';
my $eVec='';
my $rout;
my $wout;
my $eout;
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);
my $timeout=$MAIN_LOOP_TIMEOUT;
if(defined($nextPing))
{
$timeout=$nextPing-time;
}
my $result=0;
if($timeout>0)
{
$result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout);
}
if($result>0)
{
if(vec($rout,$controlServerFN,1))
{
Meteor::Controller->newFromServer($controlServer);
}
if(vec($rout,$subscriberServerFN,1))
{
Meteor::Subscriber->newFromServer($subscriberServer);
}
if(defined($udpServerFN) && vec($rout,$udpServerFN,1))
{
&handleUDP($udpServer);
}
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);
}
elsif($result<0)
{
&::syslog('crit',"Select failed: $!");
sleep(30);
}
if($::HUP)
{
$::HUP=0;
&::syslog('info',"Received SIGHUP, re-reading config, reopening logs and clearing document cache!");
Meteor::Syslog->cycleLogFileHandle();
Meteor::Config->readConfig();
Meteor::Config->updateConfig();
Meteor::Document->clearDocuments();
}
if($::USR1)
{
$::USR1=0;
&::syslog('info',"Received SIGUSR1, clearing channel buffers!");
Meteor::Channel->clearAllBuffers();
}
if($::USR2)
{
$::USR2=0;
&::syslog('info',"Received SIGUSR2, clearing document cache!");
Meteor::Document->clearDocuments()
}
my $t=time;
if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL)
{
my $minTimeStap=time-$::CONF{'MaxMessageAge'};
Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap);
$lastAgeCheck=time;
$t=$lastAgeCheck;
Meteor::Subscriber->checkPersistentConnectionsForMaxTime();
Meteor::Connection->destroyBadRequests();
}
if(defined($nextPing) && $nextPing<=$t)
{
$nextPing=undef;
Meteor::Subscriber->pingPersistentConnections();
if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2)
{
$nextPing=$::CONF{'PingInterval'}+time;
}
}
}
};
unless($::TERM)
{
&::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@");
sleep(2);
}
}
#
# Proper shutdown
#
if($::TERM)
{
&::syslog('info',"Received SIGTERM, begin shutdown!");
$subscriberServer->close();
$controlServer->close();
unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'});
Meteor::Connection->closeAllConnections();
my $timoutAt=time+$MAX_EXIT_DELAY;
while(Meteor::Connection->connectionCount() && time<$timoutAt)
{
my $rVec='';
my $wVec='';
my $eVec='';
my $rout;
my $wout;
my $eout;
Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);
my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time);
if($result>0)
{
Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);
}
}
if(my $cnt=Meteor::Connection->connectionCount())
{
&::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway");
print STDERR "Exit: TERM Shutdown (unresponsive clients)\n";
exit(1);
}
&::syslog('info',"shutdown succeeded");
print STDERR "Exit: TERM Shutdown (clean)\n";
exit(0);
}
&::syslog('emerg',"$::PGM loop exited");
###############################################################################
# Subroutines
###############################################################################
sub handleUDP {
$udpServer=shift;
my $line;
my $hispaddr=recv($udpServer->{'handle'},$line,$::UDP_MAX_MESSAGE_SIZE,0);
&::syslog('debug',"udp message received: %s",$line);
return unless($line=~s/^(\S+)\s//);
my $cmd=$1;
if($cmd eq 'ADDMESSAGE')
{
return unless($line=~s/^(\S+)\s//);
my $channelName=$1;
my $channel=Meteor::Channel->channelWithName($channelName);
my $msg=$channel->addMessage($line);
my $msgID=$msg->id();
&::syslog('debug',"udp: new message added, ID %s",$msgID);
}
}
1;
############################################################################EOF