| 1 | # $Id: ServerSession.pm,v 1.6 2003/09/11 19:57:31 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::ServerSession; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Net::BEEP::Lite::ServerSession | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | my $s_session = Net::BEEP::Lite::Session( Socket => $sock | ||
| 29 | AllowMultipleChannels => 1 ); | ||
| 30 | $s_session->add_local_profile($my_profile); | ||
| 31 | |||
| 32 | $s_session->process_messages(); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This is the session subclass that handles the pure "server" case. | ||
| 37 | This models the case of a process of fetching messages from the | ||
| 38 | internal socket, and handing those messages to the profile | ||
| 39 | implementation associated with the channel on which the message was | ||
| 40 | received. That is, it only reacts to messages appearing on the | ||
| 41 | socket. | ||
| 42 | |||
| 43 | Note that at the moment, handling multiple channels is very weak, so | ||
| 44 | is it B<not> recommended that you use that feature. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | use Carp; | ||
| 49 | |||
| 50 | use Net::BEEP::Lite::Session; | ||
| 51 | |||
| 52 | use strict; | ||
| 53 | use warnings; | ||
| 54 | |||
| 55 | our(@ISA); | ||
| 56 | |||
| 57 | @ISA = qw(Net::BEEP::Lite::Session); | ||
| 58 | |||
| 59 | =head1 CONSTRUCTOR | ||
| 60 | |||
| 61 | This is the main constructor. It accepts a named parameter list. In | ||
| 62 | addition to the parameters accepted by the superclass | ||
| 63 | (C<Net::BEEP::Lite::Session>), the same parameters as the C<initialize> | ||
| 64 | method. | ||
| 65 | |||
| 66 | This constructor will attempt to establish a valid BEEP session (i.e., | ||
| 67 | it will send and process the greeting message.) if it is passed an | ||
| 68 | actual socket in the constructor. If not (or if NoGreeting is set to | ||
| 69 | true), it is up to the calling code to send the greeting. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub new { | ||
| 74 | my $this = shift; | ||
| 75 | my $class = ref($this) || $this; | ||
| 76 | |||
| 77 | my $self = {}; | ||
| 78 | bless $self, $class; | ||
| 79 | |||
| 80 | $self->SUPER::initialize(@_); | ||
| 81 | $self->initialize(@_); | ||
| 82 | |||
| 83 | $self->send_greeting() unless $self->{_no_greeting}; | ||
| 84 | |||
| 85 | $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head1 METHODS | ||
| 89 | |||
| 90 | =over 4 | ||
| 91 | |||
| 92 | =item initialize( I<ARGS> ) | ||
| 93 | |||
| 94 | This will initialize the internal state of this object. This is | ||
| 95 | normally called by the constructor. It accepts a named parameter list | ||
| 96 | with the following parameters: | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item | ||
| 101 | |||
| 102 | Profiles -- a list (or scalar) of profile implementations to advertise | ||
| 103 | and support. | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub initialize { | ||
| 110 | my $self = shift; | ||
| 111 | my %args = @_; | ||
| 112 | |||
| 113 | for (keys %args) { | ||
| 114 | my $val = $args{$_}; | ||
| 115 | |||
| 116 | /^Profiles?/io and do { | ||
| 117 | if (ref($val) eq "ARRAY") { | ||
| 118 | for my $p (@$val) { | ||
| 119 | $self->add_local_profile($p); | ||
| 120 | } | ||
| 121 | } elsif (ref($val) eq "SCALAR") { | ||
| 122 | $self->add_local_profile($val); | ||
| 123 | } | ||
| 124 | next; | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | |||
| 128 | croak "At least one profile must be specified" | ||
| 129 | if not $self->get_local_profile_uris(); | ||
| 130 | |||
| 131 | # as a server session, we assume that we are the listener. | ||
| 132 | $self->{channelno_counter} = 2; | ||
| 133 | } | ||
| 134 | |||
| 135 | =item process_message() | ||
| 136 | |||
| 137 | Pull a single message from the internal socket (this will block if | ||
| 138 | there is nothing to read), and hand it off to the apropriate profile | ||
| 139 | implementation. | ||
| 140 | |||
| 141 | Note: this relies on the C<recv_message> method, and thus doesn't | ||
| 142 | handle multiple channels. This method may go away in future versions. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub process_message { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $message = $self->recv_message(); | ||
| 150 | return 0 if not $message; | ||
| 151 | |||
| 152 | # print STDERR "processing a message:", ref($message), "\n"; | ||
| 153 | |||
| 154 | # call appropriate method of profile assoc. with channel. | ||
| 155 | my $channel = $self->channel($message->channel_number()); | ||
| 156 | my $profile = $channel->profile(); | ||
| 157 | |||
| 158 | if (not $profile) { | ||
| 159 | warn "message received on channel without profile\n"; | ||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | # print STDERR "handling a message.\n"; | ||
| 164 | $profile->handle_message($self, $message); | ||
| 165 | } | ||
| 166 | |||
| 167 | =item process_message() | ||
| 168 | |||
| 169 | Process messages until the session closes. This the the main routine | ||
| 170 | for server sessions. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub process_messages { | ||
| 175 | my $self = shift; | ||
| 176 | |||
| 177 | while (1) { | ||
| 178 | my $res = $self->process_message(); | ||
| 179 | last if (not $self->_is_connected()); | ||
| 180 | last if (not $res); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | =item reply_message( I<ARGS> ) | ||
| 185 | |||
| 186 | This is a convenience methods for profiles to create responses to | ||
| 187 | received messages. It takes the same named parameters as the | ||
| 188 | C<Net::BEEP::Lite::Message> constructor, with the addition of "Message", | ||
| 189 | containing a reference to another message object. At minimum, the | ||
| 190 | "Message" and "Payload" parameters should be set. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub reply_message { | ||
| 195 | my $self = shift; | ||
| 196 | my %args = @_; | ||
| 197 | |||
| 198 | my $message; | ||
| 199 | |||
| 200 | for (keys %args) { | ||
| 201 | my $val = $args{$_}; | ||
| 202 | |||
| 203 | /^Message$/i and do { | ||
| 204 | $message = $val; | ||
| 205 | next; | ||
| 206 | }; | ||
| 207 | } | ||
| 208 | |||
| 209 | # some base defaults not based on the source message. | ||
| 210 | $args{Type} = 'RPY' if not $args{Type}; | ||
| 211 | $args{Session} = $self; | ||
| 212 | |||
| 213 | if ($message) { | ||
| 214 | $args{Channel} = $message->channel_number(); | ||
| 215 | $args{Msgno} = $message->msgno(); | ||
| 216 | } | ||
| 217 | |||
| 218 | Net::BEEP::Lite::Message->new(%args); | ||
| 219 | } | ||
| 220 | |||
| 221 | =pod | ||
| 222 | |||
| 223 | =back | ||
| 224 | |||
| 225 | =head1 SEE ALSO | ||
| 226 | |||
| 227 | =over 4 | ||
| 228 | |||
| 229 | =item L<Net::BEEP::Lite::Session> | ||
| 230 | |||
| 231 | =back | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | 1; |