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);
260 * XferElement functions
262 * Some of these methods are not intended to be used from Perl; they are annotated
266 void xfer_element_unref(XferElement *elt); /* (wrap the macro, above) */
267 /* xfer_element_link_to -- private */
268 char *xfer_element_repr(XferElement *elt);
269 /* xfer_element_start -- private */
270 /* xfer_element_cancel -- private */
273 static gboolean same_elements(
281 /* subclass constructors */
283 /* N.B. When adding new classes, ensure that the class_init function
284 * sets perl_class to the appropriate value. */
286 %newobject xfer_source_random;
287 XferElement *xfer_source_random(
291 guint32 xfer_source_random_get_seed(
294 %typemap(in) (void * pattern, size_t pattern_length) {
298 pat = SvPV($input, len);
299 $1 = g_memdup(pat, len);
303 %typemap(in) (gchar **argv) {
308 /* check that it's an arrayref */
309 if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
310 SWIG_exception(SWIG_TypeError, "Expected a non-empty arrayref");
312 av = (AV *)SvRV($input);
314 /* allocate memory for $1 */
315 len = av_len(av)+1; /* av_len(av) is like $#av */
317 SWIG_exception(SWIG_TypeError, "Expected a non-empty arrayref");
319 $1 = g_new0(gchar *, len+1);
321 for (i = 0; i < len; i++) {
322 SV **sv = av_fetch(av, i, 0);
323 g_assert(sv != NULL);
324 $1[i] = g_strdup(SvPV_nolen(*sv));
327 /* final element is already NULL due to g_new0; xfer_filter_process takes
328 * care of freeing this array, so we don't have to */
331 %newobject xfer_source_pattern;
332 XferElement *xfer_source_pattern(
335 size_t pattern_length);
337 %newobject xfer_source_fd;
338 XferElement *xfer_source_fd(
341 %newobject xfer_source_directtcp_listen;
342 XferElement *xfer_source_directtcp_listen(void);
345 static DirectTCPAddr *
346 xfer_source_directtcp_listen_get_addrs(XferElement *elt) {
347 return elt->input_listen_addrs;
351 %newobject xfer_source_directtcp_connect;
352 XferElement *xfer_source_directtcp_connect(DirectTCPAddr *addrs);
354 %newobject xfer_filter_xor;
355 XferElement *xfer_filter_xor(
356 unsigned char xor_key);
358 %newobject xfer_filter_process;
359 XferElement *xfer_filter_process(
363 %newobject xfer_dest_null;
364 XferElement *xfer_dest_null(
367 %newobject xfer_dest_buffer;
368 XferElement *xfer_dest_buffer(
371 %cstring_output_allocate_size(gpointer *buf, gsize *size, );
372 void xfer_dest_buffer_get(
377 %newobject xfer_dest_fd;
378 XferElement *xfer_dest_fd(
381 %newobject xfer_dest_directtcp_listen;
382 XferElement *xfer_dest_directtcp_listen(void);
385 static DirectTCPAddr *
386 xfer_dest_directtcp_listen_get_addrs(XferElement *elt) {
387 return elt->output_listen_addrs;
391 %newobject xfer_dest_directtcp_connect;
392 XferElement *xfer_dest_directtcp_connect(DirectTCPAddr *addrs);
398 %types(amglue_Source *);
401 xmsgsource_perl_callback(
407 amglue_Source *src = (amglue_Source *)data;
412 /* keep the source around long enough for the call to finish */
413 amglue_source_ref(src);
414 g_assert(src->callback_sv != NULL);
419 /* create a new SV pointing to 'src', and increase its refcount
421 amglue_source_ref(src);
422 src_sv = SWIG_NewPointerObj(src, SWIGTYPE_p_amglue_Source,
423 SWIG_OWNER | SWIG_SHADOW);
424 SvREFCNT_inc(src_sv);
426 msg_sv = new_sv_for_xmsg(msg);
427 xfer_sv = new_sv_for_xfer(xfer);
430 XPUSHs(sv_2mortal(src_sv));
431 XPUSHs(sv_2mortal(msg_sv));
432 XPUSHs(sv_2mortal(xfer_sv));
435 call_sv(src->callback_sv, G_EVAL|G_DISCARD);
440 /* we no longer need the src */
441 amglue_source_unref(src);
444 /* these may be gone, so NULL them out */
449 /* check for an uncaught 'die'. If we don't do this, then Perl will longjmp()
450 * over the GMainLoop mechanics, leaving GMainLoop in an inconsistent (locked)
453 /* We handle this just the way the default 'die' handler in Amanda::Debug
454 * does, but since Amanda's debug support may not yet be running, we back
455 * it up with an exit() */
456 g_critical("%s", SvPV_nolen(ERRSV));
464 %newobject xfer_get_amglue_source;
467 xfer_get_amglue_source(
470 return amglue_source_get(xfer_get_source(xfer),
471 (GSourceFunc)xmsgsource_perl_callback);
476 * XMsg and XMsgSource handling
483 /* First, a few macros to generate decent Perl */
491 %define XFER_ELEMENT_SUBCLASS_OF(PARENT)
498 %define XFER_ELEMENT_SUBCLASS()
499 XFER_ELEMENT_SUBCLASS_OF(Amanda::Xfer::Element)
502 %define DECLARE_CONSTRUCTOR(C_CONSTRUCTOR)
506 # The C function adds the proper blessing -- this function
507 # just gets $pkg out of the way.
513 %define OVERLOAD_REPR()
515 use overload '""' => sub { $_[0]->repr(); };
516 # overload comparison, so users can ask if one obj == another
517 use overload '==' => sub { Amanda::Xfer::same_elements($_[0], $_[1]); };
518 use overload '!=' => sub { not Amanda::Xfer::same_elements($_[0], $_[1]); };
522 %define DECLARE_METHOD(METHOD_NAME, C_FUNCTION)
523 %perlcode {*METHOD_NAME = *C_FUNCTION;
527 /* And now define the required perl classes */
529 PACKAGE(Amanda::Xfer::Xfer)
530 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_new);
531 DECLARE_METHOD(DESTROY, Amanda::Xfer::xfer_unref);
533 DECLARE_METHOD(repr, Amanda::Xfer::xfer_repr);
534 DECLARE_METHOD(get_status, Amanda::Xfer::xfer_get_status);
535 DECLARE_METHOD(get_source, Amanda::Xfer::xfer_get_amglue_source);
536 DECLARE_METHOD(start, Amanda::Xfer::xfer_start_with_callback);
537 DECLARE_METHOD(cancel, Amanda::Xfer::xfer_cancel);
541 PACKAGE(Amanda::Xfer::Element)
542 DECLARE_METHOD(DESTROY, Amanda::Xfer::xfer_element_unref);
544 DECLARE_METHOD(repr, Amanda::Xfer::xfer_element_repr);
548 PACKAGE(Amanda::Xfer::Element::Glue)
549 XFER_ELEMENT_SUBCLASS()
550 /* no constructor -- internal use only */
554 PACKAGE(Amanda::Xfer::Source::Fd)
555 XFER_ELEMENT_SUBCLASS()
556 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_fd)
560 PACKAGE(Amanda::Xfer::Source::Random)
561 XFER_ELEMENT_SUBCLASS()
562 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_random)
563 DECLARE_METHOD(get_seed, Amanda::Xfer::xfer_source_random_get_seed)
567 PACKAGE(Amanda::Xfer::Source::DirectTCPListen)
568 XFER_ELEMENT_SUBCLASS()
569 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_directtcp_listen)
570 DECLARE_METHOD(get_addrs, Amanda::Xfer::xfer_source_directtcp_listen_get_addrs)
574 PACKAGE(Amanda::Xfer::Source::DirectTCPConnect)
575 XFER_ELEMENT_SUBCLASS()
576 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_directtcp_connect)
580 PACKAGE(Amanda::Xfer::Source::Pattern)
581 XFER_ELEMENT_SUBCLASS()
582 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_source_pattern)
586 PACKAGE(Amanda::Xfer::Filter::Xor)
587 XFER_ELEMENT_SUBCLASS()
588 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_filter_xor)
592 PACKAGE(Amanda::Xfer::Filter::Process)
593 XFER_ELEMENT_SUBCLASS()
594 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_filter_process)
598 PACKAGE(Amanda::Xfer::Dest::Fd)
599 XFER_ELEMENT_SUBCLASS()
600 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_fd)
604 PACKAGE(Amanda::Xfer::Dest::Null)
605 XFER_ELEMENT_SUBCLASS()
606 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_null)
610 PACKAGE(Amanda::Xfer::Dest::Buffer)
611 XFER_ELEMENT_SUBCLASS()
612 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_buffer)
613 DECLARE_METHOD(get, Amanda::Xfer::xfer_dest_buffer_get)
617 PACKAGE(Amanda::Xfer::Dest::DirectTCPListen)
618 XFER_ELEMENT_SUBCLASS()
619 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_directtcp_listen)
620 DECLARE_METHOD(get_addrs, Amanda::Xfer::xfer_dest_directtcp_listen_get_addrs)
624 PACKAGE(Amanda::Xfer::Dest::DirectTCPConnect)
625 XFER_ELEMENT_SUBCLASS()
626 DECLARE_CONSTRUCTOR(Amanda::Xfer::xfer_dest_directtcp_connect)
630 PACKAGE(Amanda::Xfer::Msg)
633 use overload '""' => sub { $_[0]->repr(); };
637 local $Data::Dumper::Indent = 0;
638 local $Data::Dumper::Terse = 1;
639 local $Data::Dumper::Useqq = 1;
641 my $typestr = Amanda::Xfer::xmsg_type_to_string($self->{'type'});
642 my $str = "{ type => \$$typestr, elt => $self->{'elt'}, version => $self->{'version'},";
644 my %skip = ( "type" => 1, "elt" => 1, "version" => 1 );
645 for my $k (keys %$self) {
647 $str .= " $k => " . Dumper($self->{$k}) . ",";
650 # strip the trailing comma and add a closing brace
659 PACKAGE(Amanda::Xfer)
661 # make Amanda::Xfer->new equivalent to Amanda::Xfer::Xfer->new (don't
662 # worry, the blessings work out just fine)
663 *new = *Amanda::Xfer::Xfer::new;
665 # try to load Amanda::XferServer, which is server-only. If it's not found, then
666 # its classes just remain undefined.
668 eval "use Amanda::XferServer;";