Lite/ClientSession.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 100 CC 8 SCAR 17.4
CC
8
Cov
0%
new_message
12.0
new
6.0
start_channel
6.0
CRAP 5.7
1 # $Id: ClientSession.pm,v 1.7 2004/04/22 20:45:13 davidb Exp $
2 #
3 # Copyright (C) 2003 Verisign, Inc.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 # USA
19  
20 package Net::BEEP::Lite::ClientSession;
21  
22 =head1 NAME
23  
24 Net::BEEP::Lite::ClientSession
25  
26 =head1 SYNOPSIS
27  
28 my $c_session = Net::BEEP::Lite::ClientSession->new(Socket => $sock);
29  
30 my $channel_num = $c_session->start_channel('http://some/profile/url',
31 (ServerName => "some.host.org"));
32  
33 my $resp = $c_session->send_and_recv_message(ContentType => "text/plain",
34 Content => "hello!");
35  
36 $c_session->close_channel($channel_num);
37 $c_session->close_channel(0); # close session.
38  
39 =head1 DESCRIPTION
40  
41 This is the session subclass that handles the pure "client" case.
42 This models the case of a process sending individual messages and
43 dealing with individual replies on its own. In general, "clients" do
44 not advertise any profiles, so it is impossible for the peer to open
45 any new channels.
46  
47 =cut
48  
49 use Carp;
50  
51 use Net::BEEP::Lite::Session;
52  
53 use strict;
54 use warnings;
55  
56 our(@ISA);
57  
58 @ISA = qw(Net::BEEP::Lite::Session);
59  
60 =head1 CONSTRUCTOR
61  
62 =over 4
63  
64 =item new( I<ARGS> )
65  
66 This is the main constructor for this class. It takes a named
67 parameter list. It accepts all parameters defined by the
68 C<initialize> method (see below) and its superclass constructor (see
69 C<Net::BEEP::Lite::Session>).
70  
71 This constructor will attempt to establish a valid BEEP session (i.e.,
72 it will send and process the greeting message.) if it is passed an
73 actual socket in the constructor. If not (or if NoGreeting is set to
74 true), it is up to the calling code to send the greeting.
75  
76 =back
77  
78 =cut
79  
80 sub new {
81 my $this = shift;
82 my $class = ref($this) || $this;
83  
84 my $self = {};
85 bless $self, $class;
86  
87 $self->SUPER::initialize(@_);
88 $self->initialize(@_);
89  
90 $self->send_greeting() unless $self->{_no_greeting};
91 $self;
92 }
93  
94 =head1 METHODS
95  
96 =over 4
97  
98 =item initialize( I<ARGS> )
99  
100 This will initialize the object. It is generally called from the
101 constructor. At the moment it does not define any new named
102 parameters in addition to those supported by the superclass.
103  
104 =cut
105  
106 sub initialize {
107 my $self = shift;
108 my %args = @_;
109  
110 # for (keys %args) {
111 # my $val = $args{$_};
112 # }
113  
114 # as a client session, we assume that we are the initiator.
115 $self->{channelno_counter} = 1;
116  
117 # we keep track of the "current" channel for the client. This is
118 # the last channel successfully opened.
119 $self->{current_channel} = 0;
120 }
121  
122 =item start_channel( I<ARGS> )
123  
124 Sends a start channel request to the peer. The named arguments are:
125  
126 =over 4
127  
128 =item URI
129  
130 The URI of the BEEP profile to use this channel. This is mandatory.
131  
132 =item ServerName
133  
134 The "server name" to present to the server. Essentially this is the
135 name the client thinks the server is. It is optional.
136  
137 =item StartData
138  
139 Data to piggyback with the start channel request. This is optional.
140  
141 =item Encoding
142  
143 Set this to true of the StartData value is base64 encoded.
144  
145 =back
146  
147 This returns the channel number started, or dies on failure.
148  
149 See the C<start_channel_message> method in
150 C<Net::BEEP::Lite::MgmtProfile>.
151  
152 =cut
153  
154 sub start_channel {
155 my $self = shift;
156 my %args = @_;
157  
158 my ($chno, $start_msg)
159 = $self->{mgmt_profile}->send_start_channel_message($self, %args);
160 my $resp = $self->_recv_message();
161  
162 # FIXME: should we die here? probably not.
163 croak "unable to start channel: ", $resp->content(), "\n"
164 if $resp->type() eq 'ERR';
165  
166 $self->{mgmt_profile}->handle_message($self, $resp);
167  
168 $self->current_channel($chno);
169  
170 $chno;
171 }
172  
173 =item close_channel($channel_number, [$code, $content, $lang])
174  
175 Sends a "close" message for the given channel. See the
176 C<close_channel_message> in C<Net::BEEP::Lite:MgmtProfile> for more
177 information.
178  
179 =cut
180  
181 sub close_channel {
182 my $self = shift;
183 my $chno = shift;
184 my $code = shift;
185 my $content = shift;
186 my $lang = shift;
187  
188 my $msg = $self->{mgmt_profile}->send_close_channel_message($self,
189 $chno,
190 $code,
191 $content,
192 $lang);
193 my $resp = $self->_recv_message();
194  
195 # FIXME: should we die here? probably not.
196 croak "unable to close channel: ", $resp->content(), "\n"
197 if $resp->type() eq 'ERR';
198  
199 $self->{mgmt_profile}->handle_message($self, $resp);
200 }
201  
202 =item new_message( I<ARGS> )
203  
204 This is a convenience method for creating new messages. It accepts
205 the following named parameters:
206  
207 =over 4
208  
209 =item Type
210  
211 One of "MSG", "RPY", etc. Defaults to "MSG".
212  
213 =item Channel
214  
215 The channel number to use. This defaults to the "current channel",
216 which is the last successfully opened channel.
217  
218 =item Content
219  
220 The message content.
221  
222 =item Content-Type (or ContentType, or contenttype)
223  
224 The MIME content type. This defaults to 'application/octet-stream'.
225  
226 =back
227  
228 =cut
229  
230 sub new_message {
231 my $self = shift;
232 my %args = @_;
233  
234 $args{Type} = "MSG" if not $args{Type};
235 $args{Channel} = $self->current_channel() if not $args{Channel};
236  
237 Net::BEEP::Lite::Message->new(%args);
238 }
239  
240 =item send_and_recv_message( I<ARGS> )
241  
242 This is a convenience method for send and message and receiving the
243 response to it all in one step. This takes the same named parameters
244 as the C<new_message> method.
245  
246 Note: currently this will have trouble with multiple channels.
247  
248 =cut
249  
250 sub send_and_recv_message {
251 my $self = shift;
252  
253 my $message = $self->new_message(@_);
254  
255 $self->send_message($message);
256 my $resp = $self->recv_message();
257  
258 $resp;
259 }
260  
261 =item selected_profile([$uri])
262  
263 Returns the profile selected from the last successful start channel.
264  
265 =cut
266  
267 sub selected_profile {
268 my $self = shift;
269 my $uri = shift;
270  
271 $self->{selected_profile} = $uri if $uri;
272  
273 $self->{selected_profile};
274 }
275  
276 =item current_channel([$channel_number])
277  
278 Returns the current channel number.
279  
280 =cut
281  
282 sub current_channel {
283 my $self = shift;
284 my $chno = shift;
285  
286 $self->{current_channel} = $chno if $chno;
287  
288 $self->{current_channel};
289 }
290  
291 =pod
292  
293 =back
294  
295 =head1 SEE ALSO
296  
297 =over 4
298  
299 =item L<Net::BEEP::Lite::Session>
300  
301 =back
302  
303 =cut
304  
305 1;