| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## Copyright (c) 2013-2022 Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 6 | ## http://cade.noxrun.com | ||
| 7 | ## | ||
| 8 | ## LICENSE: GPLv2 | ||
| 9 | ## https://github.com/cade-vs/perl-web-reactor | ||
| 10 | ## | ||
| 11 | ############################################################################## | ||
| 12 | ## | ||
| 13 | ## base class for all reactor's pawns | ||
| 14 | ## | ||
| 15 | ############################################################################## | ||
| 16 | package Web::Reactor::Base; | ||
| 17 | use strict; | ||
| 18 | use Scalar::Util; | ||
| 19 | use Hash::Util qw( lock_ref_keys ); | ||
| 20 | use Exception::Sink; | ||
| 21 | |||
| 22 | sub new | ||
| 23 | { | ||
| 24 | my $class = shift; | ||
| 25 | my $reo = shift; | ||
| 26 | my $cfg = shift; | ||
| 27 | |||
| 28 | $class = ref( $class ) || $class; | ||
| 29 | my $self = {}; | ||
| 30 | bless $self, $class; | ||
| 31 | |||
| 32 | $self->{ 'CFG' } = $cfg; | ||
| 33 | $self->__set_reo( $reo ); | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub __lock_self_keys | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | for my $key ( @_ ) | ||
| 43 | { | ||
| 44 | next if exists $self->{ $key }; | ||
| 45 | $self->{ $key } = undef; | ||
| 46 | } | ||
| 47 | lock_ref_keys( $self ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub __set_reo | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | my $reo = shift; | ||
| 55 | |||
| 56 | # note: well, should be only reactor subclasses, not pawns but... | ||
| 57 | if( ref( $reo ) =~ /^Web::Reactor(::|$)/ ) | ||
| 58 | { | ||
| 59 | $self->{ 'REO_REACTOR' } = $reo; | ||
| 60 | |||
| 61 | # weaken backlinks to reactor, needed for proper destruction | ||
| 62 | Scalar::Util::weaken( $self->{ 'REO_REACTOR' } ); | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { | ||
| 66 | boom "missing REO reactor object"; | ||
| 67 | } | ||
| 68 | |||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub get_reo | ||
| 73 | { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | return $self->{ 'REO_REACTOR' } or boom "missing REO object reference"; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub get_cfg | ||
| 80 | { | ||
| 81 | my $self = shift; | ||
| 82 | |||
| 83 | return $self->{ 'CFG' } | ||
| 84 | } | ||
| 85 | |||
| 86 | #sub DESTROY | ||
| 87 | #{ | ||
| 88 | # my $self = shift; | ||
| 89 | # | ||
| 90 | # print "DESTROY: Reactor: $self\n"; | ||
| 91 | #} | ||
| 92 | |||
| 93 | ### EOF ###################################################################### | ||
| 94 | 1; |