… | |
… | |
72 | 127.0.0.1:9481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
72 | 127.0.0.1:9481, or the environment variables C<FREDHOST> and C<FREDPORT>). |
73 | |
73 | |
74 | If no C<name> was specified, then AnyEvent::FCP will generate a |
74 | If no C<name> was specified, then AnyEvent::FCP will generate a |
75 | (hopefully) unique client name for you. |
75 | (hopefully) unique client name for you. |
76 | |
76 | |
77 | =cut |
|
|
78 | |
|
|
79 | #TODO |
|
|
80 | #You can install a progress callback that is being called with the AnyEvent::FCP |
77 | You can install a progress callback that is being called with the AnyEvent::FCP |
81 | #object, a txn object, the type of the transaction and the attributes. Use |
78 | object, the type, a hashref with key-value pairs and a reference to any received data, |
82 | #it like this: |
79 | for all unsolicited messages. |
83 | # |
80 | |
|
|
81 | Example: |
|
|
82 | |
84 | # sub progress_cb { |
83 | sub progress_cb { |
85 | # my ($self, $txn, $type, $attr) = @_; |
84 | my ($self, $type, $kv, $rdata) = @_; |
86 | # |
85 | |
87 | # warn "progress<$txn,$type," . (join ":", %$attr) . ">\n"; |
86 | if ($type eq "simple_progress") { |
|
|
87 | warn "$kv->{identifier} $kv->{succeeded}/$kv->{required}\n"; |
|
|
88 | } |
88 | # } |
89 | } |
|
|
90 | |
|
|
91 | =cut |
89 | |
92 | |
90 | sub new { |
93 | sub new { |
91 | my $class = shift; |
94 | my $class = shift; |
92 | my $self = bless { @_ }, $class; |
95 | my $self = bless { @_ }, $class; |
93 | |
96 | |
94 | $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; |
97 | $self->{host} ||= $ENV{FREDHOST} || "127.0.0.1"; |
95 | $self->{port} ||= $ENV{FREDPORT} || 9481; |
98 | $self->{port} ||= $ENV{FREDPORT} || 9481; |
96 | $self->{name} ||= time.rand.rand.rand; # lame |
99 | $self->{name} ||= time.rand.rand.rand; # lame |
97 | $self->{timeout} ||= 600; |
100 | $self->{timeout} ||= 600; |
|
|
101 | $self->{progress} ||= sub { }; |
98 | |
102 | |
99 | $self->{id} = "a0"; |
103 | $self->{id} = "a0"; |
100 | |
104 | |
101 | { |
105 | { |
102 | Scalar::Util::weaken (my $self = $self); |
106 | Scalar::Util::weaken (my $self = $self); |
… | |
… | |
120 | expected_version => "2.0", |
124 | expected_version => "2.0", |
121 | ); |
125 | ); |
122 | |
126 | |
123 | $self |
127 | $self |
124 | } |
128 | } |
125 | |
|
|
126 | #sub progress { |
|
|
127 | # my ($self, $txn, $type, $attr) = @_; |
|
|
128 | # |
|
|
129 | # $self->{progress}->($self, $txn, $type, $attr) |
|
|
130 | # if $self->{progress}; |
|
|
131 | #} |
|
|
132 | |
129 | |
133 | sub send_msg { |
130 | sub send_msg { |
134 | my ($self, $type, %kv) = @_; |
131 | my ($self, $type, %kv) = @_; |
135 | |
132 | |
136 | my $data = delete $kv{data}; |
133 | my $data = delete $kv{data}; |
… | |
… | |
221 | $self->{node_hello} = $kv; |
218 | $self->{node_hello} = $kv; |
222 | } elsif (exists $self->{id}{$kv->{identifier}}) { |
219 | } elsif (exists $self->{id}{$kv->{identifier}}) { |
223 | $self->{id}{$kv->{identifier}}($self, $type, $kv, $rdata) |
220 | $self->{id}{$kv->{identifier}}($self, $type, $kv, $rdata) |
224 | and delete $self->{id}{$kv->{identifier}}; |
221 | and delete $self->{id}{$kv->{identifier}}; |
225 | } else { |
222 | } else { |
226 | # on_warn |
223 | &{ $self->{progress} }; |
227 | #warn "protocol warning (unexpected $type message)\n"; |
|
|
228 | } |
224 | } |
229 | } |
225 | } |
230 | |
226 | |
231 | sub _txn { |
227 | sub _txn { |
232 | my ($name, $sub) = @_; |
228 | my ($name, $sub) = @_; |