package Node::Channel::Cache;

use strict;
use vars qw/@ISA/;
@ISA = qw/Node::Channel/;

# fixed triggers - is this bad?  Maybe allow to specify trigger names
# when sending a message which will elicit a response ...

sub query {
    my ($self, $handler, $type, $id, $requester) = @_;
    $self->write("query $type $id $requester\n.\n");

    push @{$self->{handlers}}, $handler;
}

sub store {
    my ($self, $key, $until, $data);
    $self->write("store $key $until\n$data\n.\n");
}

sub receive {
    my ($self) = @_;
    my $i = index $self->{read_buf}, "\n.\n";
    return 0 unless $i;

    my $message = substr $self->{read_buf}, 0, $i;
    substr $self->{read_buf}, 0, $i+3, '';
    my ($status, $data) = split /\n/, $message, 2;

    my $handler = shift @{$self->{handlers}};

    if ($status eq '000 OK') {
	$handler->cache_response($data);
    } else {
	log("cache error: $status");
	$handler->cache_error($status, $data);
    }

    return 1;
}

sub closed {}

1
