| 1 | # $Id: Command.pm,v 0.16 2003/08/07 00:01:59 lawrence Exp $ | ||
| 2 | |||
| 3 | package Net::Msmgr::Command; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Net::Msmgr qw(:debug); | ||
| 7 | use Net::Msmgr::Object; | ||
| 8 | our @ISA = qw (Net::Msmgr::Object); | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Net::Msmgr::Command | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Net::Msmgr::Command; | ||
| 21 | |||
| 22 | my $connection = new Net::Msmgr::Connection( .... ); | ||
| 23 | |||
| 24 | my $command = new Net::Msmgr::Command; | ||
| 25 | |||
| 26 | $command->type(Net::Msmgr::Command::Async); # there are other types | ||
| 27 | $command->cmd('XYZ'); # one of the MSNP messages | ||
| 28 | $command->params( [ param1, param2, ... ] ); | ||
| 29 | |||
| 30 | $command->send($connection); | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Net::Msmgr::Command is the encapsulation for an MSNP command. They come in four types, and this library provides manifest constants for each type. | ||
| 35 | |||
| 36 | =over | ||
| 37 | |||
| 38 | =item Net::Msmgr::Command::Normal | ||
| 39 | |||
| 40 | This is a normal command. It will have a TRID and no payload. | ||
| 41 | |||
| 42 | =item Net::Msmgr::Command::Async | ||
| 43 | |||
| 44 | This is used to instantiate async commands, which are commands that have no TRID. | ||
| 45 | |||
| 46 | =item Net::Msmgr::Command::Payload | ||
| 47 | |||
| 48 | These are commands with payload data (e.g. MSG), and a TRID. | ||
| 49 | |||
| 50 | =item Net::Msmgr::Command::Pseudo | ||
| 51 | |||
| 52 | These are unused in the current version of the library, but are | ||
| 53 | placeholders to associate with user handlers. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | use constant Normal => 1; | ||
| 60 | use constant Payload => 2; | ||
| 61 | use constant Async => 3; | ||
| 62 | use constant Pseudo => 4; | ||
| 63 | |||
| 64 | =pod | ||
| 65 | |||
| 66 | =head1 CONSTRUCTOR | ||
| 67 | |||
| 68 | |||
| 69 | my $command = new Net::Msmgr::Command ( type => ... ); | ||
| 70 | |||
| 71 | or | ||
| 72 | |||
| 73 | my $command = Net::Msmgr::Command->new( type => ...); | ||
| 74 | |||
| 75 | Constructor parameters are: | ||
| 76 | |||
| 77 | =over | ||
| 78 | |||
| 79 | =item type (mandatory) | ||
| 80 | |||
| 81 | One of Net::Msmgr::Command::Async, Payload, Normal, or Pseudo | ||
| 82 | |||
| 83 | =item cmd (mandatory) | ||
| 84 | |||
| 85 | A MSNP Command (e.g. MSG, USR, XFR) | ||
| 86 | |||
| 87 | =item params (optional) | ||
| 88 | |||
| 89 | A listref of optional parameters. Each command type has a fixed list of parameters. | ||
| 90 | |||
| 91 | =item body (optional) | ||
| 92 | |||
| 93 | The payload data for Net::Msmgr::Command::Payload messages. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =head1 INSTANCE METHODS | ||
| 98 | |||
| 99 | =over | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub _fields | ||
| 104 | { | ||
| 105 | return shift->SUPER::_fields, | ||
| 106 | ( type => undef, | ||
| 107 | cmd => undef, | ||
| 108 | trid => undef, | ||
| 109 | params => [], | ||
| 110 | connection => undef, | ||
| 111 | body => undef); | ||
| 112 | } | ||
| 113 | |||
| 114 | =pod | ||
| 115 | |||
| 116 | =item $command->as_text; | ||
| 117 | |||
| 118 | Human readable representation of only the command and parameters | ||
| 119 | (excluding payload data) for debugging. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub as_text | ||
| 124 | { | ||
| 125 | my $self = shift; | ||
| 126 | my $message = $self->cmd . ' ' . join(' ', @{$self->params}) . "\n"; | ||
| 127 | return $message; | ||
| 128 | } | ||
| 129 | |||
| 130 | =pod | ||
| 131 | |||
| 132 | =item $command->send( $connection ); | ||
| 133 | |||
| 134 | Associate a command with a Net::Msmgr::Connection stream and transmit it. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub send($$) | ||
| 139 | { | ||
| 140 | my $self = shift; | ||
| 141 | my $connection = shift; | ||
| 142 | if ($self->{'type'} eq Normal) | ||
| 143 | { | ||
| 144 | my $trid = Net::Msmgr::TRID; | ||
| 145 | $self->trid($trid); | ||
| 146 | my $message = $self->cmd . | ||
| 147 | " $trid " . | ||
| 148 | join(' ', @{$self->{params}}) . | ||
| 149 | "\r\n" ; | ||
| 150 | $connection->send($message); | ||
| 151 | print STDERR "--> $message" if $connection->debug & DEBUG_COMMAND_SEND; | ||
| 152 | } | ||
| 153 | elsif ($self->{'type'} eq Payload) | ||
| 154 | { | ||
| 155 | my $length = length($self->{body}); | ||
| 156 | my $trid = Net::Msmgr::TRID; | ||
| 157 | $self->trid($trid); | ||
| 158 | my $message = | ||
| 159 | $self->cmd . | ||
| 160 | " $trid " . | ||
| 161 | join(' ', @{$self->{params}}, $length) . | ||
| 162 | "\r\n" . | ||
| 163 | $self->{body} ; | ||
| 164 | |||
| 165 | $connection->send($message); | ||
| 166 | } | ||
| 167 | elsif ($self->{'type'} eq Async) | ||
| 168 | { | ||
| 169 | my $message = $self->cmd . | ||
| 170 | join(' ', @{$self->{params}}) . | ||
| 171 | "\r\n" ; | ||
| 172 | $connection->send($message); | ||
| 173 | print STDERR "--> $message" if $connection->debug & DEBUG_COMMAND_SEND; | ||
| 174 | } | ||
| 175 | elsif ($self-{'type'} eq Pseudo) | ||
| 176 | { # can't be sent | ||
| 177 | } | ||
| 178 | else | ||
| 179 | { | ||
| 180 | carp "unknown message type. Cannot send"; | ||
| 181 | } | ||
| 182 | return $self; | ||
| 183 | } | ||
| 184 | |||
| 185 | =pod | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =head1 ACCESSOR METHODS | ||
| 190 | |||
| 191 | =over | ||
| 192 | |||
| 193 | =item my $type = $command->type; | ||
| 194 | |||
| 195 | =item $command->type($newtype); | ||
| 196 | |||
| 197 | Read or set the type. | ||
| 198 | |||
| 199 | =item my $cmd = $command->cmd; | ||
| 200 | =item $command->cmd($newcmd); | ||
| 201 | |||
| 202 | Read or set the cmd. | ||
| 203 | |||
| 204 | =item my @params = @{$command->params}; | ||
| 205 | |||
| 206 | =item $command->params( [ $p0, $p1, $p2 ] ); | ||
| 207 | |||
| 208 | Read or set the parameter list. | ||
| 209 | |||
| 210 | =item my $connection = $command->connection; | ||
| 211 | |||
| 212 | =item $command->connection($new_connection); | ||
| 213 | |||
| 214 | Set or change the connection stream associated with this command. | ||
| 215 | Probably not a good idea. | ||
| 216 | |||
| 217 | |||
| 218 | =item my $body = $command->body; | ||
| 219 | |||
| 220 | =item $command->body($new_body); | ||
| 221 | |||
| 222 | Read or set the body. | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | 1; | ||
| 229 | |||
| 230 | # | ||
| 231 | # $Log: Command.pm,v $ | ||
| 232 | # Revision 0.16 2003/08/07 00:01:59 lawrence | ||
| 233 | # Initial Release | ||
| 234 | # | ||
| 235 | # |