XUL/SessionManager.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« XUL/EventManager.pm ↑ Summary XUL/ChangeManager.pm »
1 package POE::XUL::SessionManager;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use POE::XUL::Session;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use base 'Exporter';
10  
11 our @EXPORT = qw(BOOT_REQUEST_TYPE EVENT_REQUEST_TYPE);
12  
13 use constant {
14 BOOT_REQUEST_TYPE => 'boot',
15 EVENT_REQUEST_TYPE => 'event',
16 };
17  
18 # public ----------------------------------------------------------------------
19  
20 sub new {
21 my $class = shift;
22 my $self = bless({
23 sessions => {},
24 @_,
25 }, $class);
26 return $self;
27 }
28  
29 sub handle_request {
30 my ($self, $request) = @_;
31 my $type = delete $request->{type} || croak "no request type";
32 my $method = 'handle_request_'.(
33 $type eq BOOT_REQUEST_TYPE ? 'boot' :
34 $type eq EVENT_REQUEST_TYPE? 'event':
35 croak "unknown request type: $type"
36 );
37 return $self->$method($request);
38 }
39  
40 sub timeout_session {
41 my ($self, $session_id) = @_;
42 my $session = $self->get_session($session_id) ||
43 croak "session not found: [$session_id]";
44 $session->destroy;
45 delete $self->sessions->{$session_id};
46 }
47  
48 # request handling ------------------------------------------------------------
49  
50 sub handle_request_boot {
51 my ($self, $request) = @_;
52 my $session = POE::XUL::Session->new(apps => $self->{apps});
53 my $session_id = $self->make_session_id;
54 $request->{session} = $session_id;
55 # we 1st boot, so in case of error session is not registered
56 my $response = "$session_id\n". $session->handle_boot($request);
57 # now we can register session
58 $self->sessions->{$session_id} = $session;
59 return $response;
60 }
61  
62 sub handle_request_event {
63 my ($self, $request) = @_;
64 my $session_id = $request->{session} || croak "no session ID";
65 my $session = $self->get_session($session_id) ||
66 croak "session not found: [$session_id]";
67 return $session->handle_event($request,$self);
68 }
69  
70 # private ---------------------------------------------------------------------
71  
72 # stolen from CGI::Session::ID::MD5
73 sub make_session_id {
74 my $self = shift;
75 my $id = md5_base64($$, time, rand(9999));
76 return $id;
77 }
78  
79 sub sessions { shift->{sessions} }
80 sub get_session { shift->sessions->{pop()} }
81  
82 1;
83  
« XUL/EventManager.pm ↑ Summary XUL/ChangeManager.pm »