2 * Copyright (c) 2009 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
25 Amanda::IPC::Binary - binary-framed message-based communication
33 This package is an interface to the C-level protocol library declared in
34 C<common-src/ipc-binary.h>. It enables two-way message-based communication,
35 using a binary framing that permits direct inclusion of non-string data.
37 Unlike the line protocol (see L<Amanda::IPC::LineProtocol>), this package does
38 not yet support asynchronous operation.
40 =head2 DEFINING A PROTOCOL
42 There are two parts to any use of this package. First, define the protocol by
43 creating a subclass and populating it. This subclass represents the protocol,
44 composed of a set of commands or messages and arguments that are attached to
47 Begin with the subclass:
50 use base "Amanda::IPC::Binary";
51 use Amanda::IPC::Binary;
53 Then define the constants for each command. Note that the C<constant> pragma did
54 not support the hash syntax in Perl-5.6, so this must be written as individual
55 invocations of the constant:
57 use constant CMD1 => 1;
58 use constant CMD2 => 2;
60 Then the constants for each argument:
62 use constant USERNAME => 1;
63 use constant PASSWORD => 2;
64 use constant RESOURCE => 3;
65 use constant USE_OVERDRIVE => 4;
67 Next, give the magic value for the protocol:
71 Then begin defining each command, along with its arguments:
75 USERNAME, $IPC_BINARY_STRING,
76 PASSWORD, $IPC_BINARY_STRING|$IPC_BINARY_OPTIONAL);
78 The first argument to C<command> specifies the command ID. The remaining
79 arguments are taken in pairs, and specify the argument and a bitfield of
80 flags. The available flags are:
82 $IPC_BINARY_STRING argument is a printable string
83 $IPC_BINARY_OPTIONAL argument is not required
85 If $IPC_BINARY_STRING is not specified, the argument can contain any sequence of
86 bytes (including nuls). In either case, a perl string is used to represent it.
88 =head2 USING A PROTOCOL
90 Once a protocol is defined, it forms a class which can be used to run the
91 protocol. Multiple instances of this class can be created to handle
92 simultaneous uses of the protocol over different channels.
94 The constructor takes no parameters, but establishs a new channel, complete with
95 buffers for partially-read commands:
97 my $chan = TestProtocol->new();
99 To write a message, call the C<write_message> method, passing a filehandle, a
100 command id, and argument/value pairs:
102 if (!$chan->write_message($fh, TestProtocol::CMD1,
103 TestProtocol::RESOURCE => $res,
104 TestProtocol::USERNAME => "dustin")) {
108 It is not valid to omit an argument value, and all values must be perl strings
109 -- C<undef> is not alloewd, even for optional arguments. If C<write_message>
110 fails, it returns false and C<$!> is set appropriately. The function does not
111 return until the message has been written to the file.
113 To read a message, call C<read_message>, again passing a filehandle:
115 my $msg = $chan->read_message($fh);
117 Note that this will block until a full message has been read. The resulting
118 message object has a C<cmd_id> key that identifies the command and an C<args>
119 key that references a list of argument values, keyed by their argument ID:
121 if ($msg->{'cmd_id'} == TestProtocol::CMD2) { ... }
122 print $msg->{'args'}[TestProtocol::USERNAME], "\n";
124 The C<close> method will flush any open buffers and close a channel. In the
125 synchronous case, this is essentially a no-op since all output buffers are
126 flushed at each call to C<write_message>.