| 1 | #!/usr/local/bin/perl | ||
| 2 | # -*- perl -*- | ||
| 3 | |||
| 4 | # Copyright (c) 2010 AdCopy | ||
| 5 | # Author: Jeff Weisberg | ||
| 6 | # Created: 2010-Jan-07 17:20 (EST) | ||
| 7 | # Function: | ||
| 8 | # | ||
| 9 | # $Id: Xfer.pm,v 1.1 2010/11/01 18:41:53 jaw Exp $ | ||
| 10 | |||
| 11 | package AC::MrGamoo::API::Xfer; | ||
| 12 | use AC::MrGamoo::Debug 'api_xfer'; | ||
| 13 | use AC::MrGamoo::Config; | ||
| 14 | use AC::MrGamoo::Protocol; | ||
| 15 | use AC::MrGamoo::PeerList; | ||
| 16 | |||
| 17 | use AC::MrGamoo::API::Simple; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | my $MSGID = $$; | ||
| 22 | |||
| 23 | sub handler { | ||
| 24 | my $class = shift; | ||
| 25 | my $io = shift; | ||
| 26 | my $proto = shift; | ||
| 27 | my $req = shift; | ||
| 28 | my $content = shift; | ||
| 29 | |||
| 30 | # validate filename | ||
| 31 | if( $req->{filename} =~ m%/\.|^\.% ){ | ||
| 32 | reply( 500, 'Error', $io, $proto, $req ); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | # new retry | ||
| 37 | debug("starting file xfer $req->{copyid} => $req->{filename}"); | ||
| 38 | |||
| 39 | # start working on the copy | ||
| 40 | my $x = AC::MrGamoo::Retry->new( | ||
| 41 | newobj => \&_mk_xfer, | ||
| 42 | newargs => [ $req ], | ||
| 43 | tryeach => $req->{location}, | ||
| 44 | ); | ||
| 45 | |||
| 46 | # reply now | ||
| 47 | if( $x ){ | ||
| 48 | reply( 200, 'OK', $io, $proto, $req ); | ||
| 49 | }else{ | ||
| 50 | debug("sending error, xfer/retrier failed, $io->{info}"); | ||
| 51 | reply( 501, 'Error', $io, $proto, $req ); | ||
| 52 | } | ||
| 53 | |||
| 54 | # send status when finished | ||
| 55 | $x->set_callback('on_success', \&_yippee, $proto, $req); | ||
| 56 | $x->set_callback('on_failure', \&_boohoo, $proto, $req); | ||
| 57 | |||
| 58 | # start | ||
| 59 | $x->start(); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _mk_xfer { | ||
| 63 | my $loc = shift; | ||
| 64 | my $req = shift; | ||
| 65 | |||
| 66 | my $x = AC::MrGamoo::Xfer->new( | ||
| 67 | $req->{filename}, ($req->{dstname} || $req->{filename}), $loc, $req, | ||
| 68 | ); | ||
| 69 | |||
| 70 | return $x; | ||
| 71 | } | ||
| 72 | |||
| 73 | ################################################################ | ||
| 74 | |||
| 75 | sub _yippee { | ||
| 76 | my $x = shift; | ||
| 77 | my $e = shift; | ||
| 78 | my $proto = shift; | ||
| 79 | my $req = shift; | ||
| 80 | |||
| 81 | tell_master( $req, 200, 'OK' ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _boohoo { | ||
| 85 | my $x = shift; | ||
| 86 | my $e = shift; | ||
| 87 | my $proto = shift; | ||
| 88 | my $req = shift; | ||
| 89 | |||
| 90 | debug("boohoo - xfer failed $req->{copyid}"); | ||
| 91 | tell_master( $req, 500, 'Failed' ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub tell_master { | ||
| 95 | my $req = shift; | ||
| 96 | my $code = shift; | ||
| 97 | my $msg = shift; | ||
| 98 | |||
| 99 | my($addr, $port) = get_peer_addr_from_id( $req->{master} ); | ||
| 100 | debug("sending xfer status update for $req->{copyid} => $code => $req->{master}"); | ||
| 101 | debug("cannot find addr") unless $addr; | ||
| 102 | return unless $addr; | ||
| 103 | |||
| 104 | my $x = AC::MrGamoo::API::Client->new( $addr, $port, "xfer $req->{copyid}", { | ||
| 105 | type => 'mrgamoo_xferstatus', | ||
| 106 | msgidno => $MSGID++, | ||
| 107 | want_reply => 1, | ||
| 108 | }, { | ||
| 109 | jobid => $req->{jobid}, | ||
| 110 | copyid => $req->{copyid}, | ||
| 111 | status_code => $code, | ||
| 112 | status_message => $msg, | ||
| 113 | } ); | ||
| 114 | |||
| 115 | debug("cannot create client") unless $x; | ||
| 116 | return unless $x; | ||
| 117 | $x->start(); | ||
| 118 | |||
| 119 | # we don't need any reply or reply callbacks. just send + forget | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | ################################################################ | ||
| 124 | |||
| 125 | 1; |