lib/Jubatus/Common/Client.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Jubatus/Clustering/Client.pm ↑ Summary lib/Jubatus/Graph/Client.pm »
1 package Jubatus::Common::Client;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶use Try::Lite;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use AnyEvent::MPRPC;
10  
11 use Jubatus::Common::Types;
12  
13 sub new {
14 my ($class, $host, $port, $name, $timeout) = @_;
15 $timeout = 10 unless ((defined $timeout) && ($timeout >= 0));
16 my $hash = {
17 "host" => $host, # hostname of jubatus server
18 "port" => $port, # port number of jubatus server
19 "name" => $name, # name of jubatus application
20 "client" => AnyEvent::MPRPC::Client->new( # MPRPC client
21 'host' => $host,
22 'port' => $port,
23 "on_error" => sub { # to wrap an error handle
24 my ($hdl, $fatal, $msg) = @_;
25 $hdl->destroy;
26 _error_handler($msg);
27 },
28 "timeout" => $timeout, # default time out = 10 sec
29 ),
30 };
31 bless $hash, $class;
32 }
33  
34 # Replace the naive error message with readable error message
35 sub _error_handler {
36 my ($e) = @_;
37 if ($e == 1) {
38 Jubatus::Common::Exception->show("Unknown method exception : $e");
39 } elsif ($e == 2) {
40 Jubatus::Common::Exception->show("API mismatch exception : $e");
41 } else {
42 Jubatus::Common::Exception->show("Something RPC exception : $e");
43 }
44 return;
45 }
46  
47 # Wrap AnyEvent::MPRPC::Client->call() to reduce $name from argument values
48 sub _call {
49 my ($self, $method, $ret_type, $args, $arg_types) = @_;
50 my $res;
51 # Chek matching of argument types and the types of argument value
52 if (Jubatus::Common::Types::compare_element_num($args, $arg_types, "Array")) {
53 my $name = $self->{name} || "";
54 my $values = [$name];
55 for (my $i = 0; $i <= $#$args; $i++) { # zip()
56 my $arg = $args->[$i];
57 my $arg_type = $arg_types->[$i];
58 push @{$values}, $arg_type->to_msgpack($arg); # to_msgpackがtype checkする
59 }
60 eval {
61 try {
62 # {client}->handler->**で諸々設定できる。
63 my $retval = $self->{client}->call($method, $values)->recv;
64 if ((defined $retval) && (defined $ret_type)) {
65 $res = $ret_type->from_msgpack($retval); # from_msgpackがtype checkする
66 }
67 } (
68 "*" => sub { Jubatus::Common::Exception->show($@); },
69 );
70 }; if ($@) { Jubatus::Common::Exception->show($@); }
71 }
72 return $res;
73 }
74  
75 # Get AnyEvent::MPRPC::Client instance
76 sub get_client {
77 my ($self) = @_;
78 return $self->{client};
79 }
80  
81 # Get JSON configure data from Jubatus server
82 sub get_config {
83 my ($self) = @_;
84 my $retval = $self->_call("get_config",
85 Jubatus::Common::TString->new(),
86 [],
87 [],);
88 return $retval;
89 }
90  
91 # Get JSON status data from Jubatus server
92 sub get_status {
93 my ($self) = @_;
94 my $retval = $self->_call("get_status",
95 Jubatus::Common::TMap->new(
96 Jubatus::Common::TString->new(),
97 Jubatus::Common::TMap->new(
98 Jubatus::Common::TString->new(),
99 Jubatus::Common::TString->new(),
100 ),
101 ),
102 [],
103 [],);
104 return $retval;
105 }
106  
107 # Dump the model data from current Jubatus server process
108 sub save {
109 my ($self, $id) = @_;
110 my $retval = $self->_call("save",
111 Jubatus::Common::TBool->new(),
112 [$id],
113 [Jubatus::Common::TString->new()]);
114 return $retval;
115 }
116  
117 # Load the model data to current Jubatus server process
118 sub load {
119 my ($self, $id) = @_;
120 my $retval = $self->_call("load",
121 Jubatus::Common::TBool->new(),
122 [$id],
123 [Jubatus::Common::TString->new()]);
124 return $retval;
125 }
126  
127 1;
128  
129 __END__
« lib/Jubatus/Clustering/Client.pm ↑ Summary lib/Jubatus/Graph/Client.pm »