| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Win32::ProcFarm::TkPool - Tk based child process pool that allows for async | ||
| 4 | # callbacks under a Tk event loop | ||
| 5 | # | ||
| 6 | # Author: Toby Everett | ||
| 7 | # Revision: 2.15 | ||
| 8 | # Last Change: Modified in response to rearchitecture of Win32::ProcFarm::Pool | ||
| 9 | ############################################################################# | ||
| 10 | # Copyright 1999, 2000, 2001 Toby Everett. All rights reserved. | ||
| 11 | # | ||
| 12 | # This file is distributed under the Artistic License. See | ||
| 13 | # http://www.ActiveState.com/corporate/artistic_license.htm or | ||
| 14 | # the license that comes with your perl distribution. | ||
| 15 | # | ||
| 16 | # For comments, questions, bugs or general interest, feel free to | ||
| 17 | # contact Toby Everett at teverett@alascom.att.com | ||
| 18 | ############################################################################# | ||
| 19 | |||
| 20 | use Win32::ProcFarm::Port; | ||
| 21 | use Win32::ProcFarm::Pool; | ||
| 22 | use Tk; | ||
| 23 | |||
| 24 | BEGIN { | ||
| 25 | use Win32::ProcFarm::Parent; | ||
| 26 | |||
| 27 | $Win32::ProcFarm::Parent::ref2oldconnect = \&Win32::ProcFarm::Parent::connect; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub Win32::ProcFarm::Parent::connect { | ||
| 31 | my $self = shift; | ||
| 32 | $Win32::ProcFarm::Parent::ref2oldconnect->($self, @_); | ||
| 33 | if (ref($Win32::ProcFarm::TkPool::connect_callbacks{$self->{port_obj}->get_port_num}) eq 'CODE') { | ||
| 34 | $Win32::ProcFarm::TkPool::connect_callbacks{$self->{port_obj}->get_port_num}->(); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | package Win32::ProcFarm::TkPool; | ||
| 40 | |||
| 41 | use strict; | ||
| 42 | use vars qw($VERSION @ISA); | ||
| 43 | |||
| 44 | $VERSION = '2.15'; | ||
| 45 | |||
| 46 | @ISA = qw(Win32::ProcFarm::Pool); | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $class = shift; | ||
| 50 | my($num_threads, $port_num, $perlscript, $curdir, %options) = @_; | ||
| 51 | |||
| 52 | if (exists($options{connect_callback})) { | ||
| 53 | $Win32::ProcFarm::TkPool::connect_callbacks{$port_num} = $options{connect_callback}; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $self = $class->SUPER::new($num_threads, $port_num, $perlscript, $curdir, %options); | ||
| 57 | |||
| 58 | foreach my $i (qw(cnd_callback)) { | ||
| 59 | exists $options{$i} and $self->{$i} = $options{$i}; | ||
| 60 | } | ||
| 61 | |||
| 62 | $options{mw}->repeat($options{sleep} || 100, sub {$self->cleanse_and_dispatch()}); | ||
| 63 | |||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub add_waiting_job { | ||
| 68 | my $self = shift; | ||
| 69 | my(%params) = @_; | ||
| 70 | |||
| 71 | push(@{$self->{waiting_pool}}, {%params}); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub cleanse_thread { | ||
| 75 | my $self = shift; | ||
| 76 | my($thread) = @_; | ||
| 77 | |||
| 78 | $thread->{Parent}->get_state eq 'fin' or return 0; | ||
| 79 | |||
| 80 | my @temp = $thread->{Parent}->get_retval; | ||
| 81 | if (ref($thread->{return_callback}) eq 'CODE') { | ||
| 82 | $thread->{return_callback}->(@temp); | ||
| 83 | } | ||
| 84 | $thread->{return_callback} = undef; | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub dispatch_job { | ||
| 89 | my $self = shift; | ||
| 90 | my($thread) = @_; | ||
| 91 | |||
| 92 | $thread->{Parent}->get_state eq 'idle' or return 0; | ||
| 93 | my $job = $self->get_next_job() or return 0; | ||
| 94 | |||
| 95 | $thread->{Parent}->execute($job->{command}, @{$job->{params}}); | ||
| 96 | $thread->{return_callback} = $job->{return_callback}; | ||
| 97 | if (ref($job->{start_callback}) eq 'CODE') { | ||
| 98 | $job->{start_callback}->(); | ||
| 99 | } | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub cleanse_and_dispatch { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | $self->SUPER::cleanse_and_dispatch(); | ||
| 107 | |||
| 108 | if (ref($self->{cnd_callback}) eq 'CODE') { | ||
| 109 | $self->{cnd_callback}->($self); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; |