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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.9  
changed lines
  Added in v.64

  ViewVC Help
Powered by ViewVC 1.1.26