sub blocking_request {
my ($self, @payload) = @_;
- local our $Request = $self->router->send_request($self, $self->name, @payload);
- local our $Done;
- local our @Result;
- $self->router->run_until($Done);
- return @Result;
-}
-
-sub send_response {
- my ($self, $tag, @result) = @_;
- {
- our $Request;
- die "Out of order response ${\$Request->tag}, expecting ${tag}"
- if $Request->tag ne $tag;
- }
- our @Result = @result;
- our $Done = 1;
+ my ($done, @result);
+ my $cb = sub { my ($self, $tag, @r) = @_; @result = @r; $done = 1; };
+ $self->router->send_request($self, $cb, $self->name, @payload);
+ $self->router->run_until($done);
+ return @result;
}
1;
has respond_to => (is => 'ro', required => 1, weak_ref => 1);
+has respond_with => (is => 'ro', required => 1);
+
sub respond {
my $self = shift;
- $self->respond_to->send_response($self->tag => @_);
+ $self->respond_to->${\$self->respond_with}($self->tag => @_);
}
1;
my $request
= $self->requests_received->{$tag}
= Tak::Request->new(
- tag => $tag, respond_to => $self
+ tag => $tag, respond_to => $self, respond_with => 'send_response',
);
$handler->start_request($request => @payload);
}
}
sub send_request {
- my ($self, $respond_to, @payload) = @_;
+ my ($self, $respond_to, $respond_with, @payload) = @_;
my $tag = $self->next_serial;
my $request
= $self->requests_sent->{$tag}
= Tak::Request->new(
tag => $tag,
- respond_to => $respond_to
+ respond_to => $respond_to,
+ respond_with => $respond_with,
);
$self->channel->send(REQUEST => $tag => @payload);
return $request;