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   751 use vars qw($VERSION);
  175         246  
  175         9132  
7             $VERSION = '1.365'; # NOTE - Should be #.### (three decimal places)
8              
9             # These methods are folded into POE::Kernel;
10             package POE::Kernel;
11              
12 175     175   880 use strict;
  175         221  
  175         90154  
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   609 $poe_kernel->[KR_ALIASES] = \%kr_aliases;
31             }
32              
33             sub _data_alias_relocate_kernel_id {
34 4     4   29 my ($self, $old_id, $new_id) = @_;
35 4 50       32 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 190     190   726 my $finalized_ok = 1;
44 190         868 while (my ($alias, $ses) = each(%kr_aliases)) {
45 0         0 _warn "!!! Leaked alias: $alias = $ses\n";
46 0         0 $finalized_ok = 0;
47             }
48 190         753 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 190         375 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   1974 my ($self, $session, $alias) = @_;
70             # _warn( "Session ", $session->ID, " is alias $alias\n" );
71 150         382 $self->_data_ses_refcount_inc($session->ID);
72 150         15079 $kr_aliases{$alias} = $session;
73 150         541 $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 131     131   555 my ($self, $session, $alias) = @_;
83             # _warn( "Session ", $session->ID, " was alias $alias\n" );
84 131         238 delete $kr_aliases{$alias};
85 131         397 delete $kr_ses_to_alias{$session->ID}->{$alias};
86 131         651 $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 776     776   1187 my ($self, $sid) = @_;
93 776 100       2294 return unless exists $kr_ses_to_alias{$sid}; # avoid autoviv
94 126         159 while (my ($alias, $ses_ref) = each %{$kr_ses_to_alias{$sid}}) {
  197         833  
95 71         201 $self->_data_alias_remove($ses_ref, $alias);
96             }
97 126         392 delete $kr_ses_to_alias{$sid};
98             }
99              
100             ### Resolve an alias. Just an alias.
101              
102             sub _data_alias_resolve {
103 381     381   559 my ($self, $alias) = @_;
104 381 100       1162 return undef unless exists $kr_aliases{$alias};
105 224         563 return $kr_aliases{$alias};
106             }
107              
108             ### Return a list of aliases for a session.
109              
110             sub _data_alias_list {
111 8495     8495   8215 my ($self, $sid) = @_;
112 8495 100       14087 return () unless exists $kr_ses_to_alias{$sid};
113 8494         7313 return sort keys %{$kr_ses_to_alias{$sid}};
  8494         84584  
114             }
115              
116             ### Return the number of aliases for a session.
117              
118             sub _data_alias_count_ses {
119 4500     4500   5668 my ($self, $sid) = @_;
120 4500 100       16751 return 0 unless exists $kr_ses_to_alias{$sid};
121 1483         1466 return scalar keys %{$kr_ses_to_alias{$sid}};
  1483         4788  
122             }
123              
124             ### Return a session's ID in a form suitable for logging.
125              
126             sub _data_alias_loggable {
127 35703     35703   41694 my ($self, $sid) = @_;
128 35703 100       235587 "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__