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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations)
Sun May 20 19:40:53 2007 UTC (16 years, 10 months ago) by knops.gerd
File size: 6796 byte(s)
• Add simple statistics, available via new SHOWSTATS controller command

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     # 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 deleteChannel {
81     my $class=shift;
82     my $channelName=shift;
83    
84     delete($Channels{$channelName});
85     }
86    
87     sub trimMessageStoresByTimestamp {
88     my $class=shift;
89     my $minTimeStamp=shift;
90    
91     return unless($minTimeStamp);
92    
93     map { $_->trimMessageStoreByTimestamp($minTimeStamp) } (values %Channels);
94     }
95    
96     sub clearAllBuffers {
97     my $class=shift;
98    
99     map { $_->clearBuffer() } (values %Channels);
100     }
101    
102 knops.gerd 25 sub numChannels {
103    
104     return scalar(keys %Channels);
105     }
106    
107 knops.gerd 11 ###############################################################################
108     # Factory methods
109     ###############################################################################
110     sub new {
111     #
112     # Create a new empty instance
113     #
114     my $class=shift;
115    
116     my $obj={};
117    
118     bless($obj,$class);
119     }
120    
121     sub newChannel {
122     #
123     # new instance from new server connection
124     #
125     my $self=shift->new();
126    
127     my $name=shift;
128     $self->{'name'}=$name;
129    
130     $self->{'subscribers'}=[];
131     $self->{'messages'}=[];
132    
133     $self;
134     }
135    
136     sub DESTROY {
137     my $self=shift;
138    
139     my @subscribers=@{$self->{'subscribers'}};
140 knops.gerd 13 map { $_->closeChannel($self->{'name'}) } @subscribers;
141 knops.gerd 11 }
142    
143     ###############################################################################
144     # Instance methods
145     ###############################################################################
146     sub name {
147     shift->{'name'};
148     }
149    
150     sub addSubscriber {
151     my $self=shift;
152     my $subscriber=shift;
153     my $startId=shift;
154     my $persist=shift;
155    
156     # Note: negative $startId means go back that many messages
157    
158     push(@{$self->{'subscribers'}},$subscriber) if($persist);
159    
160     my $startIndex=$self->indexForMessageID($startId);
161     return unless(defined($startIndex));
162    
163     my $msgCount=scalar(@{$self->{'messages'}});
164     my $txt='';
165    
166     $startIndex=0 if($startIndex<0);
167    
168 knops.gerd 25 my $numMsgToSend=0;
169 knops.gerd 11 while($startIndex<$msgCount)
170     {
171     my $message=$self->{'messages'}->[$startIndex++];
172    
173     $txt.=$message->message();
174 knops.gerd 25 $numMsgToSend++;
175 knops.gerd 11 }
176    
177 knops.gerd 25 $subscriber->sendMessage($txt,$numMsgToSend);
178 knops.gerd 11 }
179    
180     sub removeSubscriber {
181     my $self=shift;
182     my $subscriber=shift;
183    
184     my $idx=undef;
185     for(my $i=0;$i<scalar(@{$self->{'subscribers'}});$i++)
186     {
187     if($self->{'subscribers'}->[$i]==$subscriber)
188     {
189     $idx=$i;
190     last;
191     }
192     }
193    
194     if(defined($idx))
195     {
196     splice(@{$self->{'subscribers'}},$idx,1);
197     }
198    
199     $self->checkExpiration();
200     }
201    
202     sub subscriberCount {
203     my $self=shift;
204    
205     scalar(@{$self->{'subscribers'}});
206     }
207    
208     sub addMessage {
209     my $self=shift;
210     my $messageText=shift;
211    
212     my $message=Meteor::Message->newWithID($MessageID++);
213 knops.gerd 16 $message->setText($messageText);
214     $message->setChannelName($self->{'name'});
215 knops.gerd 11 push(@{$self->{'messages'}},$message);
216    
217     $self->trimMessageStoreBySize();
218    
219     my $text=$message->message();
220     map { $_->sendMessage($text) } @{$self->{'subscribers'}};
221     }
222    
223     sub messageCount {
224     my $self=shift;
225    
226     scalar(@{$self->{'messages'}});
227     }
228    
229     sub trimMessageStoreBySize {
230     my $self=shift;
231    
232     my $numMessages=scalar(@{$self->{'messages'}});
233    
234     if($numMessages>$::CONF{'MaxMessagesPerChannel'})
235     {
236     splice(@{$self->{'messages'}},0,-$::CONF{'MaxMessagesPerChannel'});
237     }
238     }
239    
240     sub trimMessageStoreByTimestamp {
241     my $self=shift;
242     my $ts=shift;
243    
244     while(scalar(@{$self->{'messages'}})>0 && $self->{'messages'}->[0]->timestamp()<$ts)
245     {
246     my $msg=shift(@{$self->{'messages'}});
247     }
248    
249     $self->checkExpiration();
250     }
251    
252     sub clearBuffer {
253     my $self=shift;
254    
255     $self->{'messages'}=[];
256    
257     $self->checkExpiration();
258     }
259    
260     sub checkExpiration {
261     my $self=shift;
262    
263     if($self->messageCount()==0 && $self->subscriberCount()==0)
264     {
265     my $name=$self->name();
266     &::syslog('debug',"Channel expired: $name");
267     $self->deleteChannel($name);
268     }
269     }
270    
271     sub indexForMessageID {
272     my $self=shift;
273     my $id=shift;
274    
275     # the messages is always sorted by ID, so we can
276     # use a binary search to find the message.
277     # return undef if there are no messages or the
278     # ID is that of the last message.
279     # Otherwise return the ID of the found message
280     # of if no message with that ID exists the one
281     # with the next higher ID
282     #
283     return undef unless(defined($id));
284    
285     my $numMessages=scalar(scalar(@{$self->{'messages'}}));
286    
287     return undef unless($numMessages);
288     return -1 unless($id ne '');
289    
290     # Note: negative $id means go back that many messages
291     return $numMessages+$id if($id<0);
292    
293     my $low=0;
294     my $high=$numMessages-1;
295     my $mid;
296     my $cond;
297     while($low<=$high)
298     {
299     $mid=($low+$high)>>1;
300     $cond=$id <=> $self->{'messages'}->[$mid]->id();
301     if($cond<0)
302     {
303     $high=$mid-1;
304     }
305     elsif($cond>0)
306     {
307     $low=$mid+1;
308     }
309     else
310     {
311     return $mid;
312     }
313     }
314    
315     return undef if($low>=$numMessages);
316    
317     return $low;
318     }
319    
320     1;
321 andrew.betts 3 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26