| 1 | # | ||
| 2 | # This file is part of App-CPAN2Pkg | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2009 by Jerome Quelin. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | use 5.010; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | package App::CPAN2Pkg::Controller; | ||
| 14 | # ABSTRACT: controller for cpan2pkg interface | ||
| 15 | $App::CPAN2Pkg::Controller::VERSION = '3.004'; | ||
| 16 | use Moose; | ||
| 17 | use MooseX::Has::Sugar; | ||
| 18 | use MooseX::POE; | ||
| 19 | use MooseX::SemiAffordanceAccessor; | ||
| 20 | use Readonly; | ||
| 21 | |||
| 22 | use App::CPAN2Pkg; | ||
| 23 | use App::CPAN2Pkg::Module; | ||
| 24 | use App::CPAN2Pkg::Utils qw{ $WORKER_TYPE }; | ||
| 25 | |||
| 26 | Readonly my $K => $poe_kernel; | ||
| 27 | |||
| 28 | |||
| 29 | # -- public attributes | ||
| 30 | |||
| 31 | |||
| 32 | has queue => ( ro, auto_deref, isa =>'ArrayRef[Str]' ); | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | # -- initialization | ||
| 37 | |||
| 38 | # | ||
| 39 | # START() | ||
| 40 | # | ||
| 41 | # called as poe session initialization. | ||
| 42 | # | ||
| 43 | sub START { | ||
| 44 | my $self = shift; | ||
| 45 | $K->alias_set('controller'); | ||
| 46 | $self->yield( new_module_wanted => $_ ) for $self->queue; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | # -- events | ||
| 51 | |||
| 52 | |||
| 53 | event new_module_wanted => sub { | ||
| 54 | my ($self, $modname) = @_[OBJECT, ARG0]; | ||
| 55 | |||
| 56 | my $app = App::CPAN2Pkg->instance; | ||
| 57 | if ( $app->seen_module( $modname ) ) { | ||
| 58 | my $module = $app->module( $modname ); | ||
| 59 | my $sender = $_[SENDER]; | ||
| 60 | $K->post( $sender => local_prereqs_available => $modname ) | ||
| 61 | if $module->local->status eq "available"; | ||
| 62 | $K->post( $sender => upstream_prereqs_available => $modname ) | ||
| 63 | if $module->upstream->status eq "available"; | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $module = App::CPAN2Pkg::Module->new( name => $modname ); | ||
| 68 | $app->register_module( $modname => $module ); | ||
| 69 | $WORKER_TYPE->new( module => $module ); | ||
| 70 | }; | ||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | event module_ready_locally => sub { | ||
| 75 | my ($self, $modname) = @_[OBJECT, ARG0]; | ||
| 76 | my $app = App::CPAN2Pkg->instance; | ||
| 77 | $K->post( $_ => local_prereqs_available => $modname ) | ||
| 78 | for $app->all_modules; | ||
| 79 | }; | ||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | event module_ready_upstream => sub { | ||
| 84 | my ($self, $modname) = @_[OBJECT, ARG0]; | ||
| 85 | my $app = App::CPAN2Pkg->instance; | ||
| 86 | $K->post( $_ => upstream_prereqs_available => $modname ) | ||
| 87 | for $app->all_modules; | ||
| 88 | }; | ||
| 89 | |||
| 90 | no Moose; | ||
| 91 | __PACKAGE__->meta->make_immutable; | ||
| 92 | 1; | ||
| 93 | |||
| 94 | __END__ |