2 * Copyright (c) 2008, 2009, 2010 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
21 %module "Amanda::Xfer"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
25 %import "Amanda/MainLoop.swg"
30 #include "glib-util.h"
34 /* The SWIGging of the transfer architecture.
36 * The C layer of the transfer architecture exposes some structs, which are
37 * arranged through GObject magic into a class hierarchy. It also exposes
38 * regular C functions which are intended to act as methods on these structs.
39 * Furthermore, it exposes Perl callbacks (via Amanda::MainLoop) with
40 * parameters involving objects of these classes.
42 * SWIG doesn't support callbacks very well, and makes it particularly
43 * difficult to represent a GObject class hierarchy. Rather than try to "make
44 * it fit" into SWIG, this module uses custom typemaps and perl/C conversions
45 * to get all of this stuff right in the first place.
47 * For Xfer objects, we define two functions, new_sv_for_xfer and xfer_from_sv,
48 * which create a new SV for an Xfer object, and subsequently extract a pointer
49 * to the object from the SV. The SV is both blessed and tied to the
50 * Amanda::Xfer::Xfer class, in which all of the method calls are defined, and
51 * which defines a DESTROY method that calls xfer_unref.
53 * XferElements are similar, but we have the added challenge of representing
54 * subclasses with appropriate perl subclasses. The solution is to tag each C
55 * class with a perl class name, and use that name when blessing a new SV.
57 * Finally, XMsgs are reflected entirely into perl hashrefs, in the interest of
66 /* We need GType and GThread initialized to use xfers */
74 amglue_add_enum_tag_fns(xfer_status);
75 amglue_add_constant(XFER_INIT, xfer_status);
76 amglue_add_constant(XFER_START, xfer_status);
77 amglue_add_constant(XFER_RUNNING, xfer_status);
78 amglue_add_constant(XFER_DONE, xfer_status);
79 amglue_copy_to_tag(xfer_status, constants);
81 amglue_add_enum_tag_fns(xmsg_type);
82 amglue_add_constant(XMSG_INFO, xmsg_type);
83 amglue_add_constant(XMSG_ERROR, xmsg_type);
84 amglue_add_constant(XMSG_DONE, xmsg_type);
85 amglue_add_constant(XMSG_CANCEL, xmsg_type);
86 amglue_add_constant(XMSG_PART_DONE, xmsg_type);
87 amglue_add_constant(XMSG_READY, xmsg_type);
88 amglue_copy_to_tag(xmsg_type, constants);
95 /* Given an XMsg, return a hashref representing the message as a pure-perl
96 * object. The object is new, has refcount 1, and is totally independent of
97 * the underlying XMsg.
99 * Reflecting the XMsg directly into Perl avoids the need to reference-count
100 * the XMsg objects themselves, which can simply be freed after a callback
101 * completes. The overhead of creating a hash is likely equivalent to or
102 * less than the overhead that would be consumed with SWIG's swig_$field_get
103 * accessors, assuming that perl code examines most of the fields in a message.
105 * @param msg: the message to represent
106 * @returns: a perl SV
112 static HV *amanda_xfer_msg_stash = NULL;
114 SV *rv = newRV_noinc((SV *)hash);
116 /* bless the rv as an Amanda::Xfer::Msg object */
117 if (!amanda_xfer_msg_stash) {
118 amanda_xfer_msg_stash = gv_stashpv("Amanda::Xfer::Msg", GV_ADD);
120 sv_bless(rv, amanda_xfer_msg_stash);
122 /* TODO: consider optimizing by precomputing the hash values of
126 hv_store(hash, "elt", 3, new_sv_for_xfer_element(msg->elt), 0);
129 hv_store(hash, "type", 4, newSViv(msg->type), 0);
132 hv_store(hash, "version", 7, newSViv(msg->version), 0);
136 hv_store(hash, "message", 7, newSVpv(msg->message, 0), 0);
139 hv_store(hash, "successful", 10, newSViv(msg->successful), 0);
142 hv_store(hash, "eom", 3, newSViv(msg->eom), 0);
145 hv_store(hash, "eof", 3, newSViv(msg->eof), 0);
148 hv_store(hash, "size", 4, amglue_newSVu64(msg->size), 0);
151 hv_store(hash, "duration", 8, newSVnv(msg->duration), 0);
154 hv_store(hash, "partnum", 7, amglue_newSVu64(msg->partnum), 0);
157 hv_store(hash, "fileno", 6, amglue_newSVu64(msg->fileno), 0);
163 %typemap(in) Xfer * {
164 $1 = xfer_from_sv($input);
167 %typemap(in) XferElement * {
168 $1 = xfer_element_from_sv($input);
171 %typemap(out) Xfer * {
172 $result = sv_2mortal(new_sv_for_xfer($1));
176 %typemap(out) XferElement * {
177 $result = sv_2mortal(new_sv_for_xfer_element($1));
181 %typemap(newfree) Xfer * {
185 %typemap(newfree) XferElement * {
186 xfer_element_unref($1);
193 /* A typemap for the input to the Xfer constructor, a.k.a. xfer_new */
194 %typemap(in,numinputs=1) (XferElement **elementlist, unsigned int nelements) {
198 /* check that it's an arrayref */
199 if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
200 SWIG_exception(SWIG_TypeError, "Expected an arrayref");
202 av = (AV *)SvRV($input);
204 /* allocate memory for $1 */
205 $2 = av_len(av)+1; /* av_len(av) is like $#av */
206 $1 = g_new(XferElement *, $2);
208 /* extract the underlying XferElement objects and add pointers to
209 * them, "borrowing" the caller's references for the moment. */
210 for (i = 0; i < $2; i++) {
211 SV **sv = av_fetch(av, i, 0);
212 XferElement *elt = sv? xfer_element_from_sv(*sv):NULL;
215 SWIG_exception(SWIG_TypeError, "Expected an arrayref of Amanda::Xfer::Element objects");
221 %typemap(freearg) (XferElement **elementlist, unsigned int nelements) {
222 /* free the element vector allocated in the (in) typemap */
227 Xfer *xfer_new(XferElement **elementlist, unsigned int nelements);
228 void xfer_unref(Xfer *);
229 xfer_status xfer_get_status(Xfer *xfer);
230 char *xfer_repr(Xfer *xfer);
231 void xfer_start(Xfer *xfer);
232 void xfer_cancel(Xfer *xfer);
233 /* xfer_get_source is implemented below */
236 /* SWIG wants to treat this as a function */
237 #define xfer_get_status(xfer) ((xfer)->status)
240 /* upgrade the start method to optionally take a callback, which is
241 * passed to the GSource's set_callback */
243 sub xfer_start_with_callback {
244 my ($xfer, $cb) = @_;
246 my $releasing_cb = sub {
247 my ($src, $msg, $xfer) = @_;
248 my $done = $msg->{'type'} == $XMSG_DONE;
249 $src->remove() if $done;
251 $cb = undef if $done; # break potential reference loop
253 $xfer->get_source()->set_callback($releasing_cb);
259 /* Change the callback */
261 sub xfer_set_callback {
262 my ($xfer, $cb) = @_;
264 my $releasing_cb = sub {
265 my ($src, $msg, $xfer) = @_;
266 my $done = $msg->{'type'} == $XMSG_DONE;
267 $src->remove() if $done;
269 $cb = undef if $done; # break potential reference loop
271 $xfer->get_source()->set_callback($releasing_cb);
273 $xfer->get_source()->set_callback(undef);
279 * XferElement functions
281 * Some of these methods are not intended to be used from Perl; they are annotated
285 void xfer_element_unref(XferElement *elt); /* (wrap the macro, above) */
286 /* xfer_element_link_to -- private */
287 char *xfer_element_repr(XferElement *elt);
288 /* xfer_element_start -- private */
289 /* xfer_element_cancel -- private */
292 static gboolean same_elements(
300 /* subclass constructors */
302 /* N.B. When adding new classes, ensure that the class_init function
303 * sets perl_class to the appropriate value. */
305 %newobject xfer_source_random;
306 XferElement *xfer_source_random(
310 guint32 xfer_source_random_get_seed(
313 %typemap(in) (void * pattern, size_t pattern_length) {
317 pat = SvPV($input, len);
318 $1 = g_memdup(pat, len);
322 %typemap(in) (gchar **argv) {
327 /* check that it's an arrayref */
328 if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
329 SWIG_exception(SWIG_TypeError, "Expected a non-empty arrayref");
331 av = (AV *)SvRV($input);
333 /* allocate memory for $1 */
334 len = av_len(av)+1; /* av_len(av) is like $#av */
336 SWIG_exception(SWIG_TypeError, "Expected a non-empty arrayref");
338 $1 = g_new0(gchar *, len+1);
340 for (i = 0; i < len; i++) {
341 SV **sv = av_fetch(av, i, 0);
342 g_assert(sv != NULL);
343 $1[i] = g_strdup(SvPV_nolen(*sv));
346 /* final element is already NULL due to g_new0; xfer_filter_process takes
347 * care of freeing this array, so we don't have to */
350 %newobject xfer_source_pattern;
351 XferElement *xfer_source_pattern(
354 size_t pattern_length);
356 %newobject xfer_source_fd;
357 XferElement *xfer_source_fd(
360 %newobject xfer_source_directtcp_listen;
361 XferElement *xfer_source_directtcp_listen(void);
364 static DirectTCPAddr *
365 xfer_source_directtcp_listen_get_addrs(XferElement *elt) {
366 return elt->input_listen_addrs;
370 %newobject xfer_source_directtcp_connect;
371 XferElement *xfer_source_directtcp_connect(DirectTCPAddr *addrs);
373 %newobject xfer_filter_xor;
374 XferElement *xfer_filter_xor(
375 unsigned char xor_key);
377 %newobject xfer_filter_process;
378 XferElement *xfer_filter_process(
381 gboolean log_stderr);
383 %newobject xfer_dest_null;
384 XferElement *xfer_dest_null(
387 %newobject xfer_dest_buffer;
388 XferElement *xfer_dest_buffer(
391 %cstring_output_allocate_size(gpointer *buf, gsize *size, );
392 void xfer_dest_buffer_get(
397 %newobject xfer_dest_fd;
398 XferElement *xfer_dest_fd(
401 %newobject xfer_dest_directtcp_listen;
402 XferElement *xfer_dest_directtcp_listen(void);
405 static DirectTCPAddr *
406 xfer_dest_directtcp_listen_get_addrs(XferElement *elt) {
407 return elt->output_listen_addrs;
411 %newobject xfer_dest_directtcp_connect;
412 XferElement *xfer_dest_directtcp_connect(DirectTCPAddr *addrs);
418 %types(amglue_Source *);
421 xmsgsource_perl_callback(
427 amglue_Source *src = (amglue_Source *)data;
432 /* keep the source around long enough for the call to finish */
433 amglue_source_ref(src);
434 g_assert(src->callback_sv != NULL);
439 /* create a new SV pointing to 'src', and increase its refcount
441 amglue_source_ref(src);
442 src_sv = SWIG_NewPointerObj(src, SWIGTYPE_p_amglue_Source,
443 SWIG_OWNER | SWIG_SHADOW);
444 SvREFCNT_inc(src_sv);
446 msg_sv = new_sv_for_xmsg(msg);
447 xfer_sv = new_sv_for_xfer(xfer);
450 XPUSHs(sv_2mortal(src_sv));
451 XPUSHs(sv_2mortal(msg_sv));
452 XPUSHs(sv_2mortal(xfer_sv));
455 call_sv(src->callback_sv, G_EVAL|G_DISCARD);
460 /* we no longer need the src */
461 amglue_source_unref(src);
464 /* these may be gone, so NULL them out */
469 /* check for an uncaught 'die'. If we don't do this, then Perl will longjmp()
470 * over the GMainLoop mechanics, leaving GMainLoop in an inconsistent (locked)
473 /* We handle this just the way the default 'die' handler in Amanda::Debug
474 * does, but since Amanda's debug support may not yet be running, we back
475 * it up with an exit() */
476 g_critical("%s", SvPV_nolen(ERRSV));
484 %newobject xfer_get_amglue_source;
487 xfer_get_amglue_source(
490 return amglue_source_get(xfer_get_source(xfer),
491 (GSourceFunc)xmsgsource_perl_callback);
496 * XMsg and XMsgSource handling
503 /* First, a few macros to generate decent Perl */
511 %define XFER_ELEMENT_SUBCLASS_OF(PARENT)
518 %define XFER_ELEMENT_SUBCLASS()
519 XFER_ELEMENT_SUBCLASS_OF(Amanda::Xfer::Element)
522 %define DECLARE_CONSTRUCTOR(C_CONSTRUCTOR)
526 # The C function adds the proper blessing -- this function
527 # just gets $pkg out of the way.
533 %define OVERLOAD_REPR()
535 use overload '""' => sub { $_[0]->repr(); };
536 # overload comparison, so users can ask if one obj == another
537 use overload '==' => sub { Amanda::Xfer::same_elements($_[0], $_[1]); };
538 use overload '!=' => sub { not Amanda::Xfer::same_elements($_[0], $_[1]); };
542 %define DECLARE_METHOD(METHOD_NAME, C_FUNCTION)
543 %perlcode {*METHOD_NAME = *C_FUNCTION;
547 /* And now define the required perl classes */
549 PACKAGE(Amanda::Xfer::Xfer)
550 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_new);
551 DECLARE_METHOD(DESTROY, Amanda::Xfer::xfer_unref);
553 DECLARE_METHOD(repr, Amanda::Xfer::xfer_repr);
554 DECLARE_METHOD(get_status, Amanda::Xfer::xfer_get_status);
555 DECLARE_METHOD(get_source, Amanda::Xfer::xfer_get_amglue_source);
556 DECLARE_METHOD(start, Amanda::Xfer::xfer_start_with_callback);
557 DECLARE_METHOD(set_callback, Amanda::Xfer::xfer_set_callback);
558 DECLARE_METHOD(cancel, Amanda::Xfer::xfer_cancel);
562 PACKAGE(Amanda::Xfer::Element)
563 DECLARE_METHOD(DESTROY, Amanda::Xfer::xfer_element_unref);
565 DECLARE_METHOD(repr, Amanda::Xfer::xfer_element_repr);
569 PACKAGE(Amanda::Xfer::Element::Glue)
570 XFER_ELEMENT_SUBCLASS()
571 /* no constructor -- internal use only */
575 PACKAGE(Amanda::Xfer::Source::Fd)
576 XFER_ELEMENT_SUBCLASS()
577 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_fd)
581 PACKAGE(Amanda::Xfer::Source::Random)
582 XFER_ELEMENT_SUBCLASS()
583 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_random)
584 DECLARE_METHOD(get_seed, Amanda::Xfer::xfer_source_random_get_seed)
588 PACKAGE(Amanda::Xfer::Source::DirectTCPListen)
589 XFER_ELEMENT_SUBCLASS()
590 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_directtcp_listen)
591 DECLARE_METHOD(get_addrs, Amanda::Xfer::xfer_source_directtcp_listen_get_addrs)
595 PACKAGE(Amanda::Xfer::Source::DirectTCPConnect)
596 XFER_ELEMENT_SUBCLASS()
597 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_directtcp_connect)
601 PACKAGE(Amanda::Xfer::Source::Pattern)
602 XFER_ELEMENT_SUBCLASS()
603 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_pattern)
607 PACKAGE(Amanda::Xfer::Filter::Xor)
608 XFER_ELEMENT_SUBCLASS()
609 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_filter_xor)
613 PACKAGE(Amanda::Xfer::Filter::Process)
614 XFER_ELEMENT_SUBCLASS()
615 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_filter_process)
619 PACKAGE(Amanda::Xfer::Dest::Fd)
620 XFER_ELEMENT_SUBCLASS()
621 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_fd)
625 PACKAGE(Amanda::Xfer::Dest::Null)
626 XFER_ELEMENT_SUBCLASS()
627 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_null)
631 PACKAGE(Amanda::Xfer::Dest::Buffer)
632 XFER_ELEMENT_SUBCLASS()
633 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_buffer)
634 DECLARE_METHOD(get, Amanda::Xfer::xfer_dest_buffer_get)
638 PACKAGE(Amanda::Xfer::Dest::DirectTCPListen)
639 XFER_ELEMENT_SUBCLASS()
640 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_directtcp_listen)
641 DECLARE_METHOD(get_addrs, Amanda::Xfer::xfer_dest_directtcp_listen_get_addrs)
645 PACKAGE(Amanda::Xfer::Dest::DirectTCPConnect)
646 XFER_ELEMENT_SUBCLASS()
647 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_directtcp_connect)
651 PACKAGE(Amanda::Xfer::Msg)
654 use overload '""' => sub { $_[0]->repr(); };
658 local $Data::Dumper::Indent = 0;
659 local $Data::Dumper::Terse = 1;
660 local $Data::Dumper::Useqq = 1;
662 my $typestr = Amanda::Xfer::xmsg_type_to_string($self->{'type'});
663 my $str = "{ type => \$$typestr, elt => $self->{'elt'}, version => $self->{'version'},";
665 my %skip = ( "type" => 1, "elt" => 1, "version" => 1 );
666 for my $k (keys %$self) {
668 $str .= " $k => " . Dumper($self->{$k}) . ",";
671 # strip the trailing comma and add a closing brace
680 PACKAGE(Amanda::Xfer)
682 # make Amanda::Xfer->new equivalent to Amanda::Xfer::Xfer->new (don't
683 # worry, the blessings work out just fine)
684 *new = *Amanda::Xfer::Xfer::new;
686 # try to load Amanda::XferServer, which is server-only. If it's not found, then
687 # its classes just remain undefined.
690 if (Amanda::Util::built_with_component("server")) {
691 eval "use Amanda::XferServer;";