File Coverage

blib/lib/POE/Resource/Aliases.pm
Criterion Covered Total %
statement 40 46 86.9
branch 11 12 91.6
condition n/a
subroutine 12 12 100.0
pod n/a
total 63 70 90.0


line stmt bran cond sub pod time code
1             # Manage the POE::Kernel data structures necessary to keep track of
2             # session aliases.
3              
4             package POE::Resource::Aliases;
5              
6 175     175   1045 use vars qw($VERSION);
  175         267  
  175         10657  
7             $VERSION = '1.367'; # NOTE - Should be #.### (three decimal places)
8              
9             # These methods are folded into POE::Kernel;
10             package POE::Kernel;
11              
12 175     175   893 use strict;
  175         224  
  175         151298  
13              
14             ### The table of session aliases, and the sessions they refer to.
15              
16             my %kr_aliases;
17             # ( $alias => $session_ref,
18             # ...,
19             # );
20              
21             my %kr_ses_to_alias;
22             # ( $session_id =>
23             # { $alias => $session_ref,
24             # ...,
25             # },
26             # ...,
27             # );
28              
29             sub _data_alias_initialize {
30 173     173   704 $poe_kernel->[KR_ALIASES] = \%kr_aliases;
31             }
32              
33             sub _data_alias_relocate_kernel_id {
34 4     4   18 my ($self, $old_id, $new_id) = @_;
35 4 50       45 return unless exists $kr_ses_to_alias{$old_id};
36 0         0 $kr_ses_to_alias{$new_id} = delete $kr_ses_to_alias{$old_id};
37             }
38              
39             ### End-run leak checking. Returns true if finalization was ok, or
40             ### false if it failed.
41              
42             sub _data_alias_finalize {
43 191     191   867 my $finalized_ok = 1;
44 191         1754 while (my ($alias, $ses) = each(%kr_aliases)) {
45 0         0 _warn "!!! Leaked alias: $alias = $ses\n";
46 0         0 $finalized_ok = 0;
47             }
48 191         975 while (my ($ses_id, $alias_rec) = each(%kr_ses_to_alias)) {
49 0         0 my @aliases = keys(%$alias_rec);
50 0         0 _warn "!!! Leaked alias cross-reference: $ses_id (@aliases)\n";
51 0         0 $finalized_ok = 0;
52             }
53 191         416 return $finalized_ok;
54             }
55              
56             # Add an alias to a session.
57             #
58             # TODO This has a potential problem: setting the same alias twice on a
59             # session will increase the session's reference count twice. Removing
60             # the alias will only decrement it once. That potentially causes
61             # reference counts that never go away. The public interface for this
62             # function, alias_set(), does not allow this to occur. We should add
63             # a test to make sure it never does.
64             #
65             # TODO It is possible to add aliases to sessions that do not exist.
66             # The public alias_set() function prevents this from happening.
67              
68             sub _data_alias_add {
69 150     150   1631 my ($self, $session, $alias) = @_;
70             # _warn( "Session ", $session->ID, " is alias $alias\n" );
71 150         641 $self->_data_ses_refcount_inc($session->ID);
72 150         14844 $kr_aliases{$alias} = $session;
73 150         573 $kr_ses_to_alias{$session->ID}->{$alias} = $session;
74             }
75              
76             # Remove an alias from a session.
77             #
78             # TODO Happily allows the removal of aliases from sessions that don't
79             # exist. This will cause problems with reference counting.
80              
81             sub _data_alias_remove {
82 132     132   833 my ($self, $session, $alias) = @_;
83             # _warn( "Session ", $session->ID, " was alias $alias\n" );
84 132         289 delete $kr_aliases{$alias};
85 132         531 delete $kr_ses_to_alias{$session->ID}->{$alias};
86 132         370 $self->_data_ses_refcount_dec($session->ID);
87             }
88              
89             ### Clear all the aliases from a session.
90              
91             sub _data_alias_clear_session {
92 791     791   1578 my ($self, $sid) = @_;
93 791 100       2805 return unless exists $kr_ses_to_alias{$sid}; # avoid autoviv
94 127         230 while (my ($alias, $ses_ref) = each %{$kr_ses_to_alias{$sid}}) {
  198         977  
95 71         285 $self->_data_alias_remove($ses_ref, $alias);
96             }
97 127         486 delete $kr_ses_to_alias{$sid};
98             }
99              
100             ### Resolve an alias. Just an alias.
101              
102             sub _data_alias_resolve {
103 383     383   810 my ($self, $alias) = @_;
104 383 100       1542 return undef unless exists $kr_aliases{$alias};
105 226         685 return $kr_aliases{$alias};
106             }
107              
108             ### Return a list of aliases for a session.
109              
110             sub _data_alias_list {
111 8555     8555   8886 my ($self, $sid) = @_;
112 8555 100       16254 return () unless exists $kr_ses_to_alias{$sid};
113 8554         7823 return sort keys %{$kr_ses_to_alias{$sid}};
  8554         101698  
114             }
115              
116             ### Return the number of aliases for a session.
117              
118             sub _data_alias_count_ses {
119 4675     4675   6981 my ($self, $sid) = @_;
120 4675 100       20910 return 0 unless exists $kr_ses_to_alias{$sid};
121 1485         1928 return scalar keys %{$kr_ses_to_alias{$sid}};
  1485         6594  
122             }
123              
124             ### Return a session's ID in a form suitable for logging.
125              
126             sub _data_alias_loggable {
127 36919     36919   45997 my ($self, $sid) = @_;
128 36919 100       273343 "session $sid" . (
129             (exists $kr_ses_to_alias{$sid})
130             ? ( " (" . join(", ", $self->_data_alias_list($sid)) . ")" )
131             : ""
132             );
133             }
134              
135             1;
136              
137             __END__