/[meteor]/googlecode.com/svn/trunk/Meteor/Channel.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/Channel.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations)
Mon Feb 4 19:02:49 2008 UTC (11 years, 10 months ago) by knops.gerd
File size: 7364 byte(s)
• Mode-specific configurations
• New ~channelinfo~ header keyword
• New ChannelInfoTemplate parameter

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 # A Meteor Channel
11 #
12 ###############################################################################
13 #
14 # This program is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by the Free
16 # Software Foundation; either version 2 of the License, or (at your option)
17 # any later version.
18 #
19 # This program is distributed in the hope that it will be useful, but WITHOUT
20 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 # more details.
23 #
24 # You should have received a copy of the GNU General Public License along
25 # with this program; if not, write to the Free Software Foundation, Inc.,
26 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #
28 # For more information visit www.meteorserver.org
29 #
30 ###############################################################################
31
32 package Meteor::Channel;
33 ###############################################################################
34 # Configuration
35 ###############################################################################
36
37 use strict;
38
39 use Meteor::Message;
40
41 our %Channels=();
42 our $MessageID=0;
43
44 ###############################################################################
45 # Class methods
46 ###############################################################################
47 sub channelWithName {
48 my $class=shift;
49 my $channelName=shift;
50 my $avoidCreation=shift;
51
52 unless(exists($Channels{$channelName}))
53 {
54 return undef if($avoidCreation);
55 #
56 # Create new channel
57 #
58 $Channels{$channelName}=$class->newChannel($channelName);
59
60 &::syslog('debug',"New channel $channelName");
61 }
62
63 return $Channels{$channelName};
64 }
65
66 sub listChannels {
67 my $class=shift;
68
69 my $list='';
70 foreach my $channelName (sort keys %Channels)
71 {
72 my $channel=$Channels{$channelName};
73
74 $list.=$channelName.'('.$channel->messageCount().'/'.$channel->subscriberCount().")$::CRLF";
75 }
76
77 $list;
78 }
79
80 sub listChannelsUsingTemplate {
81 my $class=shift;
82 my $template=shift;
83
84 return '' unless(defined($template) && $template ne '');
85
86 my $list='';
87 foreach my $channelName (sort keys %Channels)
88 {
89 my $channel=$Channels{$channelName};
90
91 $list.=$channel->descriptionWithTemplate($template);
92 }
93
94 $list;
95 }
96
97 sub deleteChannel {
98 my $class=shift;
99 my $channelName=shift;
100
101 delete($Channels{$channelName});
102 }
103
104 sub trimMessageStoresByTimestamp {
105 my $class=shift;
106 my $minTimeStamp=shift;
107
108 return unless($minTimeStamp);
109
110 map { $_->trimMessageStoreByTimestamp($minTimeStamp) } (values %Channels);
111 }
112
113 sub clearAllBuffers {
114 my $class=shift;
115
116 map { $_->clearBuffer() } (values %Channels);
117 }
118
119 sub numChannels {
120
121 return scalar(keys %Channels);
122 }
123
124 ###############################################################################
125 # Factory methods
126 ###############################################################################
127 sub new {
128 #
129 # Create a new empty instance
130 #
131 my $class=shift;
132
133 my $obj={};
134
135 bless($obj,$class);
136 }
137
138 sub newChannel {
139 #
140 # new instance from new server connection
141 #
142 my $self=shift->new();
143
144 my $name=shift;
145 $self->{'name'}=$name;
146
147 $self->{'subscribers'}=[];
148 $self->{'messages'}=[];
149
150 $self;
151 }
152
153 sub DESTROY {
154 my $self=shift;
155
156 my @subscribers=@{$self->{'subscribers'}};
157 map { $_->closeChannel($self->{'name'}) } @subscribers;
158 }
159
160 ###############################################################################
161 # Instance methods
162 ###############################################################################
163 sub name {
164 shift->{'name'};
165 }
166
167 sub addSubscriber {
168 my $self=shift;
169 my $subscriber=shift;
170 my $startId=shift;
171 my $persist=shift;
172
173 # Note: negative $startId means go back that many messages
174
175 push(@{$self->{'subscribers'}},$subscriber) if($persist);
176
177 my $startIndex=$self->indexForMessageID($startId);
178 return unless(defined($startIndex));
179
180 my $msgCount=scalar(@{$self->{'messages'}});
181 my $txt='';
182
183 $startIndex=0 if($startIndex<0);
184
185 if($startIndex<$msgCount)
186 {
187 $subscriber->sendMessages(@{$self->{'messages'}}[$startIndex,$msgCount-1]);
188 }
189 }
190
191 sub removeSubscriber {
192 my $self=shift;
193 my $subscriber=shift;
194
195 my $idx=undef;
196 for(my $i=0;$i<scalar(@{$self->{'subscribers'}});$i++)
197 {
198 if($self->{'subscribers'}->[$i]==$subscriber)
199 {
200 $idx=$i;
201 last;
202 }
203 }
204
205 if(defined($idx))
206 {
207 splice(@{$self->{'subscribers'}},$idx,1);
208 }
209
210 $self->checkExpiration();
211 }
212
213 sub subscriberCount {
214 my $self=shift;
215
216 scalar(@{$self->{'subscribers'}});
217 }
218
219 sub addMessage {
220 my $self=shift;
221 my $messageText=shift;
222
223 my $message=Meteor::Message->newWithID($MessageID++);
224 $message->setText($messageText);
225 $message->setChannelName($self->{'name'});
226 push(@{$self->{'messages'}},$message);
227
228 $self->trimMessageStoreBySize();
229
230 map { $_->sendMessages($message) } @{$self->{'subscribers'}};
231 }
232
233 sub messageCount {
234 my $self=shift;
235
236 scalar(@{$self->{'messages'}});
237 }
238
239 sub trimMessageStoreBySize {
240 my $self=shift;
241
242 my $numMessages=scalar(@{$self->{'messages'}});
243
244 if($numMessages>$::CONF{'MaxMessagesPerChannel'})
245 {
246 splice(@{$self->{'messages'}},0,-$::CONF{'MaxMessagesPerChannel'});
247 }
248 }
249
250 sub trimMessageStoreByTimestamp {
251 my $self=shift;
252 my $ts=shift;
253
254 while(scalar(@{$self->{'messages'}})>0 && $self->{'messages'}->[0]->timestamp()<$ts)
255 {
256 my $msg=shift(@{$self->{'messages'}});
257 }
258
259 $self->checkExpiration();
260 }
261
262 sub clearBuffer {
263 my $self=shift;
264
265 $self->{'messages'}=[];
266
267 $self->checkExpiration();
268 }
269
270 sub checkExpiration {
271 my $self=shift;
272
273 if($self->messageCount()==0 && $self->subscriberCount()==0)
274 {
275 my $name=$self->name();
276 &::syslog('debug',"Channel expired: $name");
277 $self->deleteChannel($name);
278 }
279 }
280
281 sub indexForMessageID {
282 my $self=shift;
283 my $id=shift;
284
285 # the messages is always sorted by ID, so we can
286 # use a binary search to find the message.
287 # return undef if there are no messages or the
288 # ID is that of the last message.
289 # Otherwise return the ID of the found message
290 # of if no message with that ID exists the one
291 # with the next higher ID
292 #
293 return undef unless(defined($id));
294
295 my $numMessages=scalar(scalar(@{$self->{'messages'}}));
296
297 return undef unless($numMessages);
298 return -1 unless($id ne '');
299
300 # Note: negative $id means go back that many messages
301 return $numMessages+$id if($id<0);
302
303 my $low=0;
304 my $high=$numMessages-1;
305 my $mid;
306 my $cond;
307 while($low<=$high)
308 {
309 $mid=($low+$high)>>1;
310 $cond=$id <=> $self->{'messages'}->[$mid]->id();
311 if($cond<0)
312 {
313 $high=$mid-1;
314 }
315 elsif($cond>0)
316 {
317 $low=$mid+1;
318 }
319 else
320 {
321 return $mid;
322 }
323 }
324
325 return undef if($low>=$numMessages);
326
327 return $low;
328 }
329
330 sub descriptionWithTemplate {
331 my $self=shift;
332 my $template=shift;
333
334 $template=~s/~([a-zA-Z0-9_]*)~/
335 if(!defined($1) || $1 eq '')
336 {
337 '~';
338 }
339 elsif($1 eq 'messageCount')
340 {
341 $self->messageCount();
342 }
343 elsif($1 eq 'subscriberCount')
344 {
345 $self->subscriberCount();
346 }
347 elsif(exists($self->{$1}))
348 {
349 $self->{$1};
350 }
351 else
352 {
353 '';
354 }
355 /gex;
356
357 $template;
358 }
359
360 1;
361 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26