Gungho

 view release on metacpan or  search on metacpan

lib/Gungho/Engine/POE.pm  view on Meta::CPAN

                my $caller = (caller(2))[3];

                if ($caller eq 'POE::Component::Client::HTTP::Request::return_response') {
                    $opt{charset} = 'none';
                }
                $self->SUPER::decoded_content(%%opt);
            }
        EOCODE
    }
}

sub setup
{
    my $self = shift;
    $self->alias('MainComp');
    $self->loop_delay( $self->config->{loop_delay} ) if $self->config->{loop_delay};
    $self->next::method(@_);
}

sub run
{
    my ($self, $c) = @_;

    my %config = %{ $self->config || {} };

    my $keepalive_config = delete $config{keepalive} || {};

    {
        my %defaults = (
            keep_alive   => 10,
            max_open     => 200,
            max_per_host => 5,
            timeout      => 10
        );
        while (my($key, $value) = each %defaults) {
            if (! defined $keepalive_config->{$key}) {
                $keepalive_config->{$key} = $value;
            }
        }
    }

    my $keepalive = POE::Component::Client::Keepalive->new(%$keepalive_config);

    my $client_config = delete $config{client} || {};
    foreach my $key (keys %$client_config) {
        if ($key =~ /^[a-z]/) { # ah, need to make this CamelCase
            my $camel = ucfirst($key);
            $camel =~ s/_(\w)/uc($1)/ge;
            $client_config->{$camel} = delete $client_config->{$key};
        }
    }

    # Starting from 0.09002, we accept that there are environments where
    # DNS resolution is NOT necessary. This turns out to be a problem when
    # going through, for example, a misconfigured proxy.
    #
    # Here, we detect if one of the following is true:
    #   1) The user has explicitly disable DNS resolution via dns.disable = 1
    #   2) The user has requested the use of a proxy via engine.client.proxy
    #   3) The user has implicitly requested the use of a proxy via
    #      $ENV{HTTP_PROXY}
    my $dns_config = delete $config{dns} || {};
    unless ($dns_config->{disable} || $client_config->{Proxy} || $client_config->{proxy} || $ENV{HTTP_PROXY}) {
        foreach my $key (keys %$dns_config) {
            if ($key =~ /^[a-z]/) { # ah, need to make this CamelCase
                my $camel = ucfirst($key);
                $camel =~ s/_(\w)/uc($1)/ge;
                $dns_config->{$camel} = delete $dns_config->{$key};
            }
        }
        my $resolver = POE::Component::Client::DNS->spawn(
            %$dns_config,
            Alias => &DnsResolverAlias,
        );
        $self->resolver($resolver);
    }

    # Oh, guess what. We will create as many clients as we were requested,
    # just so that PoCo::Client::HTTP doesn't stall on us (as of 
    # PoCo::Client::HTTP 0.82, PoCo::Client::HTTP tended to get filled up
    # pretty quickcly)
    $self->clients( [] );
    my $spawn = delete $client_config->{Spawn} || 2;
    if ($spawn < 1) { $spawn = 2 }
    for my $i ( 1 .. $spawn ) {
        my $alias = join('-', &UserAgentAlias, $i);
        push @{ $self->clients }, $alias;
        POE::Component::Client::HTTP->spawn(
            FollowRedirects   => 1,
            Agent             => $c->user_agent,
            Timeout           => 60,
            %$client_config,
            Alias             => $alias,
            ConnectionManager => $keepalive,
        );
    }

    POE::Session->create(
        heap => { CONTEXT => $c },
        object_states => [
            $self => {
                _start => '_poe_session_start',
                _stop  => '_poe_session_stop',
                map { ($_ => "_poe_$_") }
                    qw(session_loop start_request handle_response got_dns_response shutdown)
            }
        ]
    );

    POE::Kernel->run() if
        ! exists $config{ kernel_start } || $config{ kernel_start };
}

sub stop
{
    my ($self, $c) = @_;
    POE::Kernel->post($self->alias, 'shutdown');
}

sub _poe_shutdown
{
    my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
    my $clients = $self->clients;



( run in 2.093 seconds using v1.01-cache-2.11-cpan-71847e10f99 )