App-Context

 view release on metacpan or  search on metacpan

lib/App/Context/POE/Server.pm  view on Meta::CPAN

    my ($self, $event, $callback_event) = @_;
    $self->profile_start("send_async_event_now") if $self->{poe_profile};

    if ($event->{destination} eq "in_process") {
        my $event_token = $self->send_async_event_in_process($event, $callback_event);
    }
    else {
        ### TODO: potentially use POE child processes instead
        my $pid = $self->fork();
        if (!$pid) {   # running in child
            my $exitval = 0;
            my (@results);
            eval {
                @results = $self->send_event($event);
            };
            if ($@) {
                @results = ($@);
            }
            if ($#results > -1 && defined $results[0] && $results[0] ne "") {
                my $ipc_file = $self->{options}{prefix} . "/data/app/Context/$$";
                ### Use Storable as IPC
                if ($self->{options}{poe_storable_ipc}) {
                    my $results = (@results == 1) ? $results[0] : \@results;
                    my $success = lock_store($results, $ipc_file);
                }
                ### Use a string value as IPC
                else {
                    if (open(FILE, "> $ipc_file")) {
                            print App::Context::POE::Server::FILE @results;
                            close(App::Context::POE::Server::FILE);
                    }
                    else {
                        $exitval = 1;
                    }
                }
            }
            $self->shutdown();
            $self->exit($exitval);
        }
        my $destination = $event->{destination} || "local";
        $self->{num_async_events}++;
        $self->{node}{$destination}{num_async_events}++;
        my $runtime_event_token = $pid;
        $self->{running_async_event}{$runtime_event_token} = [ $event, $callback_event ];
    }
    $self->profile_stop("send_async_event_now") if $self->{poe_profile};
    &App::sub_exit() if ($App::trace);
}
=head2 wait_for_event()

    * Signature: $self->wait_for_event($event_token)
    * Param:     $event_token     string
    * Return:    void
    * Throws:    App::Exception
    * Since:     0.01

    Sample Usage: 

    $self->wait_for_event($event_token);

The wait_for_event() method is called when an asynchronous event has been
sent and no more processing can be completed before it is done.

=cut

sub wait_for_event {
    &App::sub_entry if ($App::trace);
    my ($self, $event_token) = @_;
    &App::sub_exit() if ($App::trace);
}

sub fork {
    &App::sub_entry if ($App::trace);
    my ($self) = @_;
    my $pid = $self->SUPER::fork();
    if ($pid) {  # the parent process has a new child process
        $self->{num_procs}++;
        $self->{proc}{$pid} = {};
    }
    else {  # the new child process has no sub-processes
        $self->{num_procs} = 0;
        $self->{proc} = {};
        $SIG{INT}  = sub { $self->log({level=>3},"fork: Caught Signal: @_ (quitting)\n"); $self->exit(102); };   # SIG  2
        $SIG{QUIT} = sub { $self->log({level=>3},"fork: Caught Signal: @_ (quitting)\n"); $self->exit(103); };   # SIG  3
        $SIG{TERM} = sub { $self->log({level=>3},"fork: Caught Signal: @_ (quitting)\n"); $self->shutdown(); $self->exit(115); };   # SIG 15
    }
    &App::sub_exit($pid) if ($App::trace);
    return($pid);
}

sub finish_pid {
    &App::sub_entry if ($App::trace);
    my ($self, $pid, $exitval, $sig) = @_;

    $self->{num_procs}--;
    delete $self->{proc}{$pid};

    my $runtime_event_token = $pid;
    my $async_event = $self->{running_async_event}{$runtime_event_token};
    if ($async_event) {
        my ($event, $callback_event) = @$async_event;
        my $returnval = "";
        my $ipc_file = $self->{options}{prefix} . "/data/app/Context/$pid";
        ### Use Storable as IPC
        if ($self->{options}{poe_storable_ipc}) {
            $returnval = lock_retrieve($ipc_file);
            unlink($ipc_file);
        }
        ### Use a string value as IPC
        else {
            if (open(FILE, $ipc_file)) {
                if ($callback_event) {
                    $returnval = join("",<App::Context::POE::Server::FILE>);
                }
                close(App::Context::POE::Server::FILE);
                unlink($ipc_file);
            }
        }

        my $destination = $event->{destination} || "local";
        $self->{num_async_events}--;



( run in 0.709 second using v1.01-cache-2.11-cpan-5837b0d9d2c )