When we use any of SUBSCRIBE commands, we need to restrict the
commands we can use after. Only other SUBSCRIBE, or UNSUBSCRIBE commands
are valid.
We added a "mode", is_subscriber, that default to 0 and will be true after
we subscribe something. When is_subscriber is active, invalid commands
will result in an exception.
Signed-off-by: Pedro Melo <melo@simplicidade.org>
Proto => 'tcp',
) || confess("Could not connect to Redis server at $self->{server}: $!");
$self->{rbuf} = '';
Proto => 'tcp',
) || confess("Could not connect to Redis server at $self->{server}: $!");
$self->{rbuf} = '';
+
+ $self->{is_subscriber} = 0;
return bless($self, $class);
}
return bless($self, $class);
}
my $command = $AUTOLOAD;
$command =~ s/.*://;
my $command = $AUTOLOAD;
$command =~ s/.*://;
+ $self->__is_valid_command($command);
$self->__send_command($command, @_);
$self->__send_command($command, @_);
sub info {
my ($self) = @_;
sub info {
my ($self) = @_;
+ $self->__is_valid_command('INFO');
$self->__send_command('INFO');
$self->__send_command('INFO');
sub keys {
my $self = shift;
sub keys {
my $self = shift;
+ $self->__is_valid_command('KEYS');
$self->__send_command('KEYS', @_);
$self->__send_command('KEYS', @_);
+### Mode validation
+sub __is_valid_command {
+ my ($self, $cmd) = @_;
+
+ return unless $self->{is_subscriber};
+ return if $cmd =~ /^P?(UN)?SUBSCRIBE$/;
+ confess("Cannot use command '$cmd' while in SUBSCRIBE mode, ");
+}
+
+
### Socket operations
sub __send_command {
my $self = shift;
### Socket operations
sub __send_command {
my $self = shift;