APNS-Agent

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Perl extension APNS-Agent

0.06 2014-03-17T08:40:31Z
    - add uri for monitor
    - fix bug when parsing payload (macopy++)

0.05 2014-03-02T12:03:24Z
    - fix error handling on send

0.04 2014-03-01T13:08:19Z
    - refactor message queuing way

0.03 2014-03-01T12:47:11Z
    - add `send_interval` option

0.02 2014-01-12T04:26:12Z

    - fix test case

lib/APNS/Agent.pm  view on Meta::CPAN


use Class::Accessor::Lite::Lazy 0.03 (
    new => 1,
    ro => [qw/
        certificate
        private_key
        sandbox
        debug_port
    /],
    ro_lazy => {
        on_error_response   => sub {
            sub {
                my $self = shift;
                my %d = %{$_[0]};
                warnf "identifier:%s\tstate:%s\ttoken:%s", $d{identifier}, $d{state}, $d{token} || '';
            }
        },
        disconnect_interval => sub { 60 },
        send_interval       => sub { 0.01 },
        _sent_cache         => sub { Cache::LRU->new(size => 10000) },
        _queue              => sub { [] },

lib/APNS/Agent.pm  view on Meta::CPAN

    ], [$body]];
}

sub _build_apns {
    my $self = shift;

    AnyEvent::APNS->new(
        certificate => $self->certificate,
        private_key => $self->private_key,
        sandbox     => $self->sandbox,
        on_error    => sub {
            my ($handle, $fatal, $message) = @_;

            my $t; $t = AnyEvent->timer(
                after    => 0,
                interval => 10,
                cb       => sub {
                    undef $t;
                    infof "event:reconnect";
                    $self->_connect_to_apns;
                },
            );
            warnf "event:error\tfatal:$fatal\tmessage:$message";
        },
        on_connect  => sub {
            infof "event:on_connect";
            $self->_disconnect_timer($self->_build_disconnect_timer);

            if (@{$self->_queue}) {
                $self->_sending;
            }
        },
        on_error_response => sub {
            my ($identifier, $state) = @_;
            my $data = $self->_sent_cache->get($identifier) || {};
            $self->on_error_response->($self, {
                identifier => $identifier,
                state      => $state,
                token      => $data->{token},
                payload    => $data->{payload},
            });
        },
        ($self->debug_port ? (debug_port => $self->debug_port) : ()),
    );
}



( run in 0.390 second using v1.01-cache-2.11-cpan-65fba6d93b7 )