| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2010 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2010-Jan-12 18:15 (EST) | ||
| 6 | # Function: common to most API handlers | ||
| 7 | # | ||
| 8 | # $Id: Simple.pm,v 1.1 2010/11/01 18:41:52 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::API::Simple; | ||
| 11 | use AC::MrGamoo::Protocol; | ||
| 12 | use AC::MrGamoo::Debug 'api'; | ||
| 13 | use AC::Import; | ||
| 14 | use POSIX; | ||
| 15 | |||
| 16 | require 'AC/protobuf/std_reply.pl'; | ||
| 17 | use strict; | ||
| 18 | |||
| 19 | our @EXPORT = qw(reply on_success on_failure in_background nbfd_reply); | ||
| 20 | |||
| 21 | sub reply { | ||
| 22 | my $code = shift; | ||
| 23 | my $msg = shift; | ||
| 24 | my $io = shift; | ||
| 25 | my $proto = shift; | ||
| 26 | my $req = shift; | ||
| 27 | |||
| 28 | unless( $proto->{want_reply} ){ | ||
| 29 | $io->shut(); | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | my $response = AC::MrGamoo::Protocol->encode_reply( { | ||
| 34 | type => $proto->{type}, | ||
| 35 | msgid => $proto->{msgid}, | ||
| 36 | is_reply => 1, | ||
| 37 | }, { | ||
| 38 | status_code => $code, | ||
| 39 | status_message => $msg, | ||
| 40 | } ); | ||
| 41 | |||
| 42 | debug("sending $code reply for $proto->{type} on $io->{info}"); | ||
| 43 | $io->write_and_shut( $response ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub nbfd_reply { | ||
| 47 | my $code = shift; | ||
| 48 | my $msg = shift; | ||
| 49 | my $fd = shift; | ||
| 50 | my $proto = shift; | ||
| 51 | my $req = shift; | ||
| 52 | |||
| 53 | return unless $proto->{want_reply}; | ||
| 54 | |||
| 55 | my $response = AC::MrGamoo::Protocol->encode_reply( { | ||
| 56 | type => $proto->{type}, | ||
| 57 | msgid => $proto->{msgid}, | ||
| 58 | is_reply => 1, | ||
| 59 | }, { | ||
| 60 | status_code => $code, | ||
| 61 | status_message => $msg, | ||
| 62 | } ); | ||
| 63 | |||
| 64 | debug("sending $code reply for $proto->{type} (from bkg)"); | ||
| 65 | syswrite( $fd, $response ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub on_success { | ||
| 69 | my $x = shift; | ||
| 70 | my $e = shift; | ||
| 71 | my $io = shift; | ||
| 72 | my $proto = shift; | ||
| 73 | my $req = shift; | ||
| 74 | |||
| 75 | reply( 200, 'OK', $io, $proto, $req ); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub on_failure { | ||
| 79 | my $x = shift; | ||
| 80 | my $e = shift; | ||
| 81 | my $io = shift; | ||
| 82 | my $proto = shift; | ||
| 83 | my $req = shift; | ||
| 84 | |||
| 85 | reply( 500, 'Error', $io, $proto, $req ); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub in_background { | ||
| 90 | my $func = shift; | ||
| 91 | my $io = shift; | ||
| 92 | my $proto = shift; | ||
| 93 | my $req = shift; | ||
| 94 | |||
| 95 | my $pid = fork(); | ||
| 96 | |||
| 97 | if( !defined($pid) ){ | ||
| 98 | problem("cannot fork: $!"); | ||
| 99 | reply( 500, 'Error', $io, $proto, $req ); | ||
| 100 | return ; | ||
| 101 | }elsif( $pid ){ | ||
| 102 | # parent | ||
| 103 | $io->shut(); | ||
| 104 | waitpid $pid, 0; | ||
| 105 | return; | ||
| 106 | }else{ | ||
| 107 | # child | ||
| 108 | my $gpid = fork(); | ||
| 109 | |||
| 110 | if( $gpid ){ | ||
| 111 | # parent | ||
| 112 | _exit(0); | ||
| 113 | }else{ | ||
| 114 | # orphaned child | ||
| 115 | eval { | ||
| 116 | $func->($io, $proto, $req, @_); | ||
| 117 | }; | ||
| 118 | if(my $e = $@){ | ||
| 119 | chomp $e; | ||
| 120 | verbose("child error: $e"); | ||
| 121 | _exit(1); | ||
| 122 | } | ||
| 123 | _exit(0); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; |