| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Mar-30 13:22 (EDT) | ||
| 6 | # Function: read protocol data | ||
| 7 | # | ||
| 8 | # $Id: Protocol.pm,v 1.1 2010/11/01 18:41:43 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::Protocol; | ||
| 11 | use AC::MrGamoo::Debug 'protocol'; | ||
| 12 | use AC::DC::Protocol; | ||
| 13 | use AC::Import; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | our @ISA = 'AC::DC::Protocol'; | ||
| 17 | our @EXPORT = qw(read_protocol read_protocol_no_content); | ||
| 18 | my $HDRSIZE = __PACKAGE__->header_size(); | ||
| 19 | |||
| 20 | my %MSGTYPE = | ||
| 21 | ( | ||
| 22 | status => { num => 0, reqc => '', resc => 'ACPStdReply' }, | ||
| 23 | heartbeat => { num => 1, reqc => '', resc => '' }, | ||
| 24 | heartbeat_request => { num => 2, reqc => '', resc => 'ACPHeartBeat' }, | ||
| 25 | |||
| 26 | scribl_put => { num => 11, reqc => 'ACPScriblRequest', resc => 'ACPScriblReply' }, | ||
| 27 | scribl_get => { num => 12, reqc => 'ACPScriblRequest', resc => 'ACPScriblReply' }, | ||
| 28 | scribl_del => { num => 13, reqc => 'ACPScriblRequest', resc => 'ACPScriblReply' }, | ||
| 29 | scribl_stat => { num => 14, reqc => 'ACPScriblRequest', resc => 'ACPScriblReply' }, | ||
| 30 | |||
| 31 | mrgamoo_jobcreate => { num => 15, reqc => 'ACPMRMJobCreate', resc => 'ACPStdReply' }, | ||
| 32 | mrgamoo_taskcreate => { num => 16, reqc => 'ACPMRMTaskCreate', resc => 'ACPStdReply' }, | ||
| 33 | mrgamoo_jobabort => { num => 17, reqc => 'ACPMRMJobAbort', resc => 'ACPStdReply' }, | ||
| 34 | mrgamoo_taskabort => { num => 18, reqc => 'ACPMRMTaskAbort', resc => 'ACPStdReply' }, | ||
| 35 | mrgamoo_taskstatus => { num => 19, reqc => 'ACPMRMTaskStatus', resc => 'ACPStdReply' }, | ||
| 36 | mrgamoo_filexfer => { num => 20, reqc => 'ACPMRMFileXfer', resc => 'ACPStdReply' }, | ||
| 37 | mrgamoo_filedel => { num => 21, reqc => 'ACPMRMFileDel', resc => 'ACPStdReply' }, | ||
| 38 | mrgamoo_diagmsg => { num => 22, reqc => 'ACPMRMDiagMsg', resc => 'ACPStdReply' }, | ||
| 39 | mrgamoo_xferstatus => { num => 23, reqc => 'ACPMRMXferStatus', resc => 'ACPStdReply' }, | ||
| 40 | mrgamoo_status => { num => 24, reqc => 'ACPMRMStatusRequest', resc => 'ACPMRMStatusReply' }, | ||
| 41 | |||
| 42 | ); | ||
| 43 | |||
| 44 | |||
| 45 | for my $name (keys %MSGTYPE){ | ||
| 46 | my $r = $MSGTYPE{$name}; | ||
| 47 | __PACKAGE__->add_msg( $name, $r->{num}, $r->{reqc}, $r->{resc}); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | sub read_protocol { | ||
| 53 | my $io = shift; | ||
| 54 | my $evt = shift; | ||
| 55 | |||
| 56 | $io->{rbuffer} .= $evt->{data}; | ||
| 57 | |||
| 58 | return read_http($io, $evt) if $io->{rbuffer} =~ /^GET/; | ||
| 59 | |||
| 60 | my $p = _check_protocol( $io, $evt ); | ||
| 61 | return unless $p; # read more | ||
| 62 | |||
| 63 | # do we have everything? | ||
| 64 | return unless length($io->{rbuffer}) >= ($p->{data_length} + $p->{content_length} + $HDRSIZE); | ||
| 65 | |||
| 66 | my $data = substr($io->{rbuffer}, $HDRSIZE, $p->{data_length}); | ||
| 67 | my $content = substr($io->{rbuffer}, $HDRSIZE + $p->{data_length}, $p->{content_length}); | ||
| 68 | |||
| 69 | # content is passed as reference | ||
| 70 | return ($p, $data, ($content ? \$content : undef)); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub read_protocol_no_content { | ||
| 74 | my $io = shift; | ||
| 75 | my $evt = shift; | ||
| 76 | |||
| 77 | $io->{rbuffer} .= $evt->{data}; | ||
| 78 | |||
| 79 | return _read_http($io, $evt) if $io->{rbuffer} =~ /^GET/; | ||
| 80 | |||
| 81 | my $p = _check_protocol( $io, $evt ); | ||
| 82 | return unless $p; # read more | ||
| 83 | |||
| 84 | # do we have everything? | ||
| 85 | return unless length($io->{rbuffer}) >= ($p->{data_length} + $HDRSIZE); | ||
| 86 | |||
| 87 | my $data = substr($io->{rbuffer}, $HDRSIZE, $p->{data_length}); | ||
| 88 | my $content = substr($io->{rbuffer}, $HDRSIZE + $p->{data_length}, $p->{content_length}); | ||
| 89 | |||
| 90 | return ($p, $data, $content); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _check_protocol { | ||
| 94 | my $io = shift; | ||
| 95 | my $evt = shift; | ||
| 96 | |||
| 97 | if( length($io->{rbuffer}) >= $HDRSIZE && !$io->{proto_header} ){ | ||
| 98 | # decode header | ||
| 99 | eval { | ||
| 100 | $io->{proto_header} = __PACKAGE__->decode_header( $io->{rbuffer} ); | ||
| 101 | }; | ||
| 102 | if(my $e=$@){ | ||
| 103 | verbose("cannot decode protocol header: $e"); | ||
| 104 | $io->run_callback('error', { | ||
| 105 | cause => 'read', | ||
| 106 | error => "cannot decode protocol: $e", | ||
| 107 | }); | ||
| 108 | $io->shut(); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | return $io->{proto_header}; | ||
| 114 | } | ||
| 115 | |||
| 116 | # for simple status queries, argus, debugging | ||
| 117 | # this is not an RFC compliant http server | ||
| 118 | sub _read_http { | ||
| 119 | my $io = shift; | ||
| 120 | my $evt = shift; | ||
| 121 | |||
| 122 | return unless $io->{rbuffer} =~ /\r?\n\r?\n/s; | ||
| 123 | my($get, $url, $http) = $io->{rbuffer} =~ /^(\S+)\s+(\S+)\s+(\S+)/; | ||
| 124 | |||
| 125 | return ( { type => 'http' }, $url ); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | 1; |