line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package M3::ServerView; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
139812
|
use 5.006; |
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
189
|
|
4
|
4
|
|
|
4
|
|
24
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
174
|
|
5
|
4
|
|
|
4
|
|
24
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
329
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
23
|
use Carp qw(croak carp); |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
432
|
|
8
|
4
|
|
|
4
|
|
5411
|
use HTTP::Request; |
|
4
|
|
|
|
|
209262
|
|
|
4
|
|
|
|
|
2814
|
|
9
|
4
|
|
|
4
|
|
6422
|
use LWP::UserAgent; |
|
4
|
|
|
|
|
161209
|
|
|
4
|
|
|
|
|
189
|
|
10
|
4
|
|
|
4
|
|
181
|
use Scalar::Util qw(refaddr blessed); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
713
|
|
11
|
4
|
|
|
4
|
|
4858
|
use Time::HiRes qw(time); |
|
4
|
|
|
|
|
8860
|
|
|
4
|
|
|
|
|
21
|
|
12
|
4
|
|
|
4
|
|
770
|
use URI; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
109
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Load views |
15
|
4
|
|
|
4
|
|
3271
|
use M3::ServerView::View; |
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
143
|
|
16
|
4
|
|
|
4
|
|
2677
|
use M3::ServerView::RootView; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
107
|
|
17
|
4
|
|
|
4
|
|
2446
|
use M3::ServerView::ServerView; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
152
|
|
18
|
4
|
|
|
4
|
|
2701
|
use M3::ServerView::FindJobView; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
5569
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Module version |
21
|
|
|
|
|
|
|
our $VERSION = "0.04"; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Inside-out objects |
24
|
|
|
|
|
|
|
my %Base_uri; |
25
|
|
|
|
|
|
|
my %Password; |
26
|
|
|
|
|
|
|
my %User; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub connect_to { |
29
|
0
|
|
|
0
|
1
|
0
|
my ($pkg, $base_uri, %args) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
0
|
my $self = bless \do { my $v; }, $pkg; |
|
0
|
|
|
|
|
0
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Transform to URI object if necessary |
34
|
0
|
0
|
|
|
|
0
|
if (blessed $base_uri) { |
35
|
0
|
0
|
|
|
|
0
|
croak "URL is not an URI-instance" unless $base_uri->isa("URI"); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
0
|
|
|
|
|
0
|
$base_uri = URI->new($base_uri); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Path must end with / because we append to it |
42
|
0
|
0
|
|
|
|
0
|
$base_uri->path("/") if $base_uri->path eq ""; |
43
|
0
|
0
|
|
|
|
0
|
croak "Invalid URL '$base_uri' - must end with /" unless $base_uri->path =~ m|/$|; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Store object attributes |
46
|
0
|
|
|
|
|
0
|
$Base_uri{refaddr $self} = $base_uri; |
47
|
0
|
|
|
|
|
0
|
$User{refaddr $self} = $args{user}; |
48
|
0
|
|
|
|
|
0
|
$Password{refaddr $self} = $args{password}; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
0
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub root { |
54
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
55
|
0
|
|
|
|
|
0
|
my $view = $self->_load_view(""); |
56
|
0
|
|
|
|
|
0
|
return $view; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub find_jobs { |
60
|
0
|
|
|
0
|
1
|
0
|
my ($self, $in_query) = @_; |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
0
|
croak "Missing query" unless ref $in_query eq "HASH"; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
my %out_query = ( |
65
|
|
|
|
|
|
|
name => undef, |
66
|
|
|
|
|
|
|
owner => undef, |
67
|
|
|
|
|
|
|
type => undef, |
68
|
|
|
|
|
|
|
bjno => undef, |
69
|
|
|
|
|
|
|
find => "Find", |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
0
|
if (exists $in_query->{name}) { |
73
|
0
|
|
|
|
|
0
|
$out_query{name} = $in_query->{name}; |
74
|
|
|
|
|
|
|
} |
75
|
0
|
0
|
|
|
|
0
|
if (exists $in_query->{user}) { |
76
|
0
|
|
|
|
|
0
|
$out_query{owner} = $in_query->{user}; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
0
|
|
|
|
0
|
if (exists $in_query->{type}) { |
79
|
0
|
|
|
|
|
0
|
$out_query{type} = $in_query->{type}; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
0
|
|
|
|
0
|
if (exists $in_query->{batch_job_number}) { |
82
|
0
|
|
|
|
|
0
|
$out_query{bjno} = $in_query->{batch_job_number}; |
83
|
|
|
|
|
|
|
} |
84
|
0
|
0
|
|
|
|
0
|
if ($in_query->{queued}) { |
85
|
0
|
|
|
|
|
0
|
$out_query{queued} = "on"; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
0
|
return $self->_load_view("/findjob", \%out_query); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Loads the contents of an URL and measures the time it takes |
92
|
|
|
|
|
|
|
sub _get_page_contents { |
93
|
3
|
|
|
3
|
|
8
|
my ($self, $uri) = @_; |
94
|
|
|
|
|
|
|
|
95
|
3
|
|
|
|
|
40
|
my $ua = LWP::UserAgent->new; |
96
|
3
|
|
|
|
|
46238
|
my $req = HTTP::Request->new(GET => $uri); |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
43894
|
my $user = $self->user; |
99
|
3
|
|
|
|
|
20
|
my $password = $self->password; |
100
|
|
|
|
|
|
|
|
101
|
3
|
50
|
33
|
|
|
17
|
if (defined $user && defined $password) { |
102
|
0
|
|
|
|
|
0
|
$req->authorization_basic($user, $password); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
3
|
|
|
|
|
20
|
my $t = time; |
106
|
|
|
|
|
|
|
|
107
|
3
|
|
|
|
|
29
|
my $res = $ua->request($req); |
108
|
3
|
50
|
|
|
|
94593
|
unless ($res->is_success) { |
109
|
0
|
|
|
|
|
0
|
croak "Failed to get '$uri' because server returned: ", $res->status_line; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
3
|
50
|
|
|
|
73
|
return wantarray ? ($res->content, time - $t) : $res->content; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Clean up inside-out attriutes |
116
|
|
|
|
|
|
|
sub DESTROY { |
117
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
118
|
0
|
|
|
|
|
0
|
my $id = refaddr $self; |
119
|
0
|
|
|
|
|
0
|
delete $Base_uri{$id}; |
120
|
0
|
|
|
|
|
0
|
delete $User{$id}; |
121
|
0
|
|
|
|
|
0
|
delete $Password{$id}; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub base_uri { |
125
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
126
|
0
|
|
|
|
|
0
|
return $Base_uri{refaddr $self}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub user { |
130
|
3
|
|
|
3
|
1
|
17
|
my ($self) = @_; |
131
|
3
|
50
|
|
|
|
35
|
return undef unless ref $self; |
132
|
0
|
|
|
|
|
0
|
return $User{refaddr $self}; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub password { |
136
|
3
|
|
|
3
|
1
|
12
|
my ($self) = @_; |
137
|
3
|
50
|
|
|
|
15
|
return undef unless ref $self; |
138
|
0
|
|
|
|
|
|
return $Password{refaddr $self}; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
{ |
143
|
|
|
|
|
|
|
# This table keeps the mapping between path and view class |
144
|
|
|
|
|
|
|
my %View_class = ( |
145
|
|
|
|
|
|
|
"/" => "M3::ServerView::RootView", |
146
|
|
|
|
|
|
|
"/server" => "M3::ServerView::ServerView", |
147
|
|
|
|
|
|
|
"/findjob" => "M3::ServerView::FindJobView", |
148
|
|
|
|
|
|
|
); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub _view_class_for_target { |
151
|
0
|
|
0
|
0
|
|
|
my $target = shift || "/"; |
152
|
0
|
|
0
|
|
|
|
return $View_class{$target} || ""; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _load_view { |
157
|
0
|
|
|
0
|
|
|
my ($self, $path, $query) = @_; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
0
|
|
|
|
my $target = $path || "/"; |
160
|
0
|
|
|
|
|
|
my $view_class = _view_class_for_target($target); |
161
|
0
|
0
|
|
|
|
|
croak "Can't determinte view class for '${path}'" unless $view_class; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
my $uri = $self->base_uri->clone; |
164
|
0
|
|
|
|
|
|
$uri->path($path); |
165
|
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
|
if (ref $query) { |
167
|
0
|
|
|
|
|
|
$uri->query_form($query); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
else { |
170
|
0
|
|
|
|
|
|
$uri->query($query); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
my $view = $view_class->new($self, $uri); |
174
|
0
|
|
|
|
|
|
return $view; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |
178
|
|
|
|
|
|
|
__END__ |