--- googlecode.com/svn/trunk/Meteor/Controller.pm 2006/11/20 17:59:30 3 +++ googlecode.com/svn/trunk/Meteor/Controller.pm 2008/11/27 00:33:21 62 @@ -38,10 +38,25 @@ use Meteor::Connection; use Meteor::Channel; + use Meteor::Subscriber; @Meteor::Controller::ISA=qw(Meteor::Connection); ############################################################################### +# Factory methods +############################################################################### +sub newFromServer { + my $class=shift; + + my $self=$class->SUPER::newFromServer(shift); + + $::Statistics->{'current_controllers'}++; + $::Statistics->{'controller_connections_accepted'}++; + + $self; +} + +############################################################################### # Instance methods ############################################################################### sub processLine { @@ -55,7 +70,7 @@ # COUNTSUBSCRIBERS channel1 # < OK 344 - unless($line=~s/^(ADDMESSAGE|COUNTSUBSCRIBERS|LISTCHANNELS|QUIT)//) + unless($line=~s/^(ADDMESSAGE|COUNTSUBSCRIBERS|LISTCHANNELS|LISTSUBSCRIBERS|LISTCONNECTIONS|DESCRIBE|SHOWSTATS|QUIT)//) { $self->write("ERR Invalid command syntax$::CRLF"); @@ -69,21 +84,21 @@ unless($line=~s/^\s+(\S+)\s//) { $self->write("ERR Invalid command syntax$::CRLF"); - + return; } my $channelName=$1; my $channel=Meteor::Channel->channelWithName($channelName); - $channel->addMessage($line); - $self->write("OK$::CRLF"); + my $msg=$channel->addMessage($line); + my $msgID=$msg->id(); + $self->write("OK\t$msgID$::CRLF"); } elsif($cmd eq 'COUNTSUBSCRIBERS') { unless($line=~s/^\s+(\S+)$//) { $self->write("ERR Invalid command syntax$::CRLF"); - return; } @@ -99,7 +114,6 @@ unless($line eq '') { $self->write("ERR Invalid command syntax$::CRLF"); - return; } @@ -107,12 +121,93 @@ $self->write($txt); } - elsif($cmd eq 'QUIT') + elsif($cmd eq 'LISTSUBSCRIBERS') + { + unless($line eq '') + { + $self->write("ERR Invalid command syntax$::CRLF"); + return; + } + + my $txt="OK$::CRLF".Meteor::Subscriber->listSubscribers()."--EOT--$::CRLF"; + + $self->write($txt); + } + elsif($cmd eq 'LISTCONNECTIONS') { unless($line eq '') { $self->write("ERR Invalid command syntax$::CRLF"); + return; + } + + my $txt="OK$::CRLF".Meteor::Connection->listConnections()."--EOT--$::CRLF"; + + $self->write($txt); + } + elsif($cmd eq 'DESCRIBE') + { + unless($line=~s/^\s+(\S+)$//) + { + $self->write("ERR Invalid command syntax$::CRLF"); + return; + } + + my $filenum=$1; + my $condesc=Meteor::Connection->describeConnWithFileNum($filenum); + if ($condesc != -1) { + $self->write("OK$::CRLF"); + $self->write($condesc); + $self->write("--EOT--$::CRLF"); + + } else { + $self->write("ERR Unknown client$::CRLF"); + } + } + elsif($cmd eq 'SHOWSTATS') + { + # uptime + my $uptime=time-$::STARTUP_TIME; + my $txt="OK$::CRLF"."uptime: $uptime$::CRLF"; + + # channel_count + my $numChannels=Meteor::Channel->numChannels(); + $txt.="channel_count: $numChannels$::CRLF"; + + # connection_count + my $numConnections=Meteor::Connection->connectionCount(); + $txt.="connection_count: $numConnections$::CRLF"; + + # subscriber count = current_subscribers + number of pollers in last minute + my $now = time; my $numpoll = 0; + foreach my $key (keys %{$::Pollers}) { + if($::Pollers->{$key} < ($now-60) || Meteor::Subscriber->subscriberExists($key)) { + delete $::Pollers->{$key}; + } else { + $numpoll++; + } + } + my $numsub = 0; + if (exists($::Statistics->{'current_subscribers'})) { + $numsub = $::Statistics->{'current_subscribers'}; + } + $txt.="real_subscribers: ".($numpoll+$numsub)."$::CRLF"; + foreach my $key (keys %{$::Statistics}) + { + $txt.=$key.': '.$::Statistics->{$key}.$::CRLF; + } + + $txt.="--EOT--$::CRLF"; + + $self->write($txt); + } + elsif($cmd eq 'QUIT') + { + unless($line eq '') + { + $self->write("ERR Invalid command syntax$::CRLF"); + return; } @@ -142,5 +237,10 @@ $self->SUPER::close(); } +sub didClose { + + $::Statistics->{'current_controllers'}--; +} + 1; ############################################################################EOF \ No newline at end of file