Imported Upstream version 3.3.2
[debian/amanda] / perl / Amanda / IPC / Binary.pod
1 /*
2  * Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
3  *
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.
7  *
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
11  * for more details.
12  *
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
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %perlcode %{
22
23 =head1 NAME
24
25 Amanda::IPC::Binary - binary-framed message-based communication
26
27 =head1 SYNOPSIS
28
29 See below.
30
31 =head1 DESCRIPTION
32
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.
36
37 Unlike the line protocol (see L<Amanda::IPC::LineProtocol>), this package does
38 not yet support asynchronous operation.
39
40 =head2 DEFINING A PROTOCOL
41
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
45 those commands.
46
47 Begin with the subclass:
48
49     package TestProtocol;
50     use base "Amanda::IPC::Binary";
51     use Amanda::IPC::Binary;
52
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:
56
57     use constant CMD1 => 1;
58     use constant CMD2 => 2;
59
60 Then the constants for each argument:
61
62     use constant USERNAME => 1;
63     use constant PASSWORD => 2;
64     use constant RESOURCE => 3;
65     use constant USE_OVERDRIVE => 4;
66
67 Next, give the magic value for the protocol:
68
69     magic(0x9812);
70
71 Then begin defining each command, along with its arguments:
72
73     command(CMD1,
74         RESOURCE, 0,
75         USERNAME, $IPC_BINARY_STRING,
76         PASSWORD, $IPC_BINARY_STRING|$IPC_BINARY_OPTIONAL);
77
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:
81
82     $IPC_BINARY_STRING      argument is a printable string
83     $IPC_BINARY_OPTIONAL    argument is not required
84
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.
87
88 =head2 USING A PROTOCOL
89
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.
93
94 The constructor takes no parameters, but establishs a new channel, complete with
95 buffers for partially-read commands:
96
97     my $chan = TestProtocol->new();
98
99 To write a message, call the C<write_message> method, passing a filehandle, a
100 command id, and argument/value pairs:
101
102     if (!$chan->write_message($fh, TestProtocol::CMD1,
103             TestProtocol::RESOURCE => $res,
104             TestProtocol::USERNAME => "dustin")) {
105         # ...
106     }
107
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.
112
113 To read a message, call C<read_message>, again passing a filehandle:
114
115     my $msg = $chan->read_message($fh);
116
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:
120
121     if ($msg->{'cmd_id'} == TestProtocol::CMD2) { ... }
122     print $msg->{'args'}[TestProtocol::USERNAME], "\n";
123
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>.
127
128 =cut
129
130 %}