| 1 | package RPC::Async::Util; | ||
| 2 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.05'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | RPC::Async::Util - util module of the asynchronous RPC framework | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | 3 | ▶ | use base "Exporter"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Class::ISA; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Storable qw(nfreeze thaw); | ||
| 16 | |||
| 17 | our @EXPORT_OK = qw(append_data read_packet make_packet expand); | ||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =over | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | =item expand($ref, $in) | ||
| 26 | |||
| 27 | Expands and normalizes the def_* input and output definitions to a unified | ||
| 28 | order and naming convention. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub expand { | ||
| 33 | my ($ref, $in) = @_; | ||
| 34 | |||
| 35 | treewalk($ref, | ||
| 36 | sub { | ||
| 37 | if($in and ${$_[0]} =~ s/^(.+?)_(\d+)$/$2$1/) { | ||
| 38 | return ${$_[0]}; | ||
| 39 | } elsif($in) { | ||
| 40 | my $count = 0; | ||
| 41 | return map { sprintf "%02d$_", $count++ } split /\|/, ${$_[0]}; | ||
| 42 | } else { | ||
| 43 | return split /\|/, ${$_[0]}; | ||
| 44 | } | ||
| 45 | }, | ||
| 46 | sub{ | ||
| 47 | no warnings 'uninitialized'; # So we can use $1 even when we got no match | ||
| 48 | ${$_[0]} =~ s/^(?:latin1)/latin1string/; | ||
| 49 | ${$_[0]} =~ s/^(?:str(?:ing)?)|(:?utf8)/utf8string/; | ||
| 50 | ${$_[0]} =~ s/^(?:(u)?int(?:eger)?(?:8))|(?:byte)|(?:char)/$1integer8/; | ||
| 51 | ${$_[0]} =~ s/^(?:(u)?int(?:eger)?(?:16))|(?:short)/$1integer16/; | ||
| 52 | ${$_[0]} =~ s/^(?:(u)?int(?:eger)?(?:(:)|(?:32)|$))|(?:long)/$1integer32$2/; # Default to 32bit | ||
| 53 | ${$_[0]} =~ s/^(?:(u)?int(?:eger)?(?:64))|(?:longlong)/$1integer64/; | ||
| 54 | ${$_[0]} =~ s/^(?:float)?(?:(:)|(?:32)|$)/float32$1/; # Default to 32bit | ||
| 55 | ${$_[0]} =~ s/^(?:float64)|(?:double)/float64$1/; | ||
| 56 | ${$_[0]} =~ s/^(?:bin)|(?:data)/binary/; | ||
| 57 | ${$_[0]} =~ s/^(?:bool)/boolean/; | ||
| 58 | } | ||
| 59 | ); | ||
| 60 | |||
| 61 | return $ref; | ||
| 62 | } | ||
| 63 | |||
| 64 | =item append_data($buf, $data) | ||
| 65 | |||
| 66 | Function for buffering data. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub append_data { | ||
| 71 | my ($buf, $data) = @_; | ||
| 72 | |||
| 73 | if (not defined $$buf) { | ||
| 74 | $$buf = $data; | ||
| 75 | } else { | ||
| 76 | $$buf .= $data; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | =item read_packet($buf) | ||
| 81 | |||
| 82 | Reads the next packet and thaws it if enough data has been received. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub read_packet { | ||
| 87 | my ($buf) = @_; | ||
| 88 | |||
| 89 | return if not defined $$buf or length $$buf < 4; | ||
| 90 | |||
| 91 | my $length = unpack("N", $$buf); | ||
| 92 | die if $length < 4; # TODO: catch this | ||
| 93 | return if length $$buf < $length; | ||
| 94 | |||
| 95 | my $frozen = substr $$buf, 4, $length - 4; | ||
| 96 | if (length $$buf == $length) { | ||
| 97 | $$buf = undef; | ||
| 98 | } else { | ||
| 99 | $$buf = substr $$buf, $length; | ||
| 100 | } | ||
| 101 | |||
| 102 | return thaw $frozen; | ||
| 103 | } | ||
| 104 | |||
| 105 | =item make_packet($ref) | ||
| 106 | |||
| 107 | Generate a packet for sending. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub make_packet { | ||
| 112 | my ($ref) = @_; | ||
| 113 | |||
| 114 | my $frozen = nfreeze($ref); | ||
| 115 | return pack("N", 4 + length $frozen) . $frozen; | ||
| 116 | } | ||
| 117 | |||
| 118 | #use Misc::Common qw(treewalk); # FIXME: Put in own module instead | ||
| 119 | |||
| 120 | =item treewalk($tree, $replace_key, $replace_value) | ||
| 121 | |||
| 122 | Sub used to walk a tree structure. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub treewalk { | ||
| 127 | my ($tree, $replace_key, $replace_value) = @_; | ||
| 128 | $replace_key = $replace_key?$replace_key:sub{}; | ||
| 129 | $replace_value = $replace_value?$replace_value:sub{}; | ||
| 130 | |||
| 131 | my @walk = ($tree); | ||
| 132 | while(my $branch = shift @walk) { | ||
| 133 | if(ref($branch) eq 'HASH') { | ||
| 134 | foreach my $key (keys %{$branch}) { | ||
| 135 | my $newkey = $key; | ||
| 136 | if(my @results = grep { $_ } $replace_key->(\$newkey)) { | ||
| 137 | if(@results > 1) { | ||
| 138 | if (ref($branch->{$newkey}) eq '') { | ||
| 139 | $replace_value->(\$branch->{$key}); | ||
| 140 | foreach my $result (@results) { | ||
| 141 | $branch->{$result} = $branch->{$key}; | ||
| 142 | } | ||
| 143 | } else { | ||
| 144 | foreach my $result (@results) { | ||
| 145 | $branch->{$result} = $branch->{$key}; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | $newkey = $results[0]; | ||
| 149 | delete($branch->{$key}); | ||
| 150 | |||
| 151 | } elsif($newkey ne $key) { | ||
| 152 | $branch->{$newkey} = $branch->{$key}; | ||
| 153 | delete($branch->{$key}); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | if (ref($branch->{$newkey}) eq 'HASH') { | ||
| 158 | push(@walk, $branch->{$newkey}); | ||
| 159 | } elsif (ref($branch->{$newkey}) eq 'ARRAY') { | ||
| 160 | push(@walk, $branch->{$newkey}); | ||
| 161 | } else { | ||
| 162 | $replace_value->(\$branch->{$newkey}); | ||
| 163 | } | ||
| 164 | |||
| 165 | } | ||
| 166 | } elsif(ref($branch) eq 'ARRAY') { | ||
| 167 | for(my $i=0;$i<int(@{$branch});$i++) { | ||
| 168 | if(ref($branch->[$i]) eq 'ARRAY') { | ||
| 169 | push(@walk, $branch->[$i]); | ||
| 170 | } elsif(ref($branch->[$i]) eq 'HASH') { | ||
| 171 | push(@walk, $branch->[$i]); | ||
| 172 | } else { | ||
| 173 | $replace_value->(\$branch->[$i]); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | =back | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Jonas Jensen <jbj@knef.dk>, Troels Liebe Bentsen <tlb@rapanden.dk> | ||
| 185 | |||
| 186 | =head1 COPYRIGHT | ||
| 187 | |||
| 188 | Copyright(C) 2005-2007 Troels Liebe Bentsen | ||
| 189 | |||
| 190 | Copyright(C) 2005-2007 Jonas Jensen | ||
| 191 | |||
| 192 | This library is free software; you can redistribute it and/or modify | ||
| 193 | it under the same terms as Perl itself. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | 1; | ||
| 198 | # vim: et sw=4 sts=4 tw=80 | ||