-
Notifications
You must be signed in to change notification settings - Fork 26
/
Reference.pm
267 lines (198 loc) · 7.4 KB
/
Reference.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# $Id$
# Filter::Reference partial copyright 1998 Artur Bergman
# <artur@vogon-solutions.com>. Partial copyright 1999 Philip Gwyn.
package POE::Filter::Reference;
use strict;
use Carp;
#------------------------------------------------------------------------------
# Try to require one of the default freeze/thaw packages.
sub _default_freezer {
local $SIG{'__DIE__'} = 'DEFAULT';
my $ret;
foreach my $p (qw(Storable FreezeThaw)) {
eval { require "$p.pm"; import $p (); };
warn $@ if $@;
return $p if $@ eq '';
}
die "Filter::Reference requires Storable or FreezeThaw";
}
#------------------------------------------------------------------------------
# Try to acquire Compress::Zlib at runtime.
my $zlib_status = undef;
sub _include_zlib {
local $SIG{'__DIE__'} = 'DEFAULT';
unless (defined $zlib_status) {
eval { require 'Compress::Zlib';
import Compress::Zlib qw(compress uncompress);
};
if ($@) {
$zlib_status = $@;
eval <<' EOE';
sub compress { @_ }
sub uncompress { @_ }
EOE
}
else {
$zlib_status = '';
}
}
$zlib_status;
}
#------------------------------------------------------------------------------
sub new {
my($type, $freezer, $compression) = @_;
$freezer ||= _default_freezer();
# not a reference... maybe a package?
unless(ref $freezer) {
unless(exists $::{$freezer.'::'}) {
eval {require "$freezer.pm"; import $freezer ();};
croak $@ if $@;
}
}
# Now get the methodes we want
my $freeze=$freezer->can('nfreeze') || $freezer->can('freeze');
carp "$freezer doesn't have a freeze or nfreeze method" unless $freeze;
my $thaw=$freezer->can('thaw');
carp "$freezer doesn't have a thaw method" unless $thaw;
# If it's an object, we use closures to create a $self->method()
my $tf=$freeze;
my $tt=$thaw;
if(ref $freezer) {
$tf=sub {$freeze->($freezer, @_)};
$tt=sub {$thaw->($freezer, @_)};
}
# Compression
$compression ||= 0;
if ($compression) {
my $zlib_status = &_include_zlib();
if ($zlib_status ne '') {
warn "Compress::Zlib load failed with error: $zlib_status\n";
carp "Filter::Reference compression option ignored";
$compression = 0;
}
}
my $self = bless { buffer => '',
expecting => undef,
thaw => $tt,
freeze => $tf,
compress => $compression,
}, $type;
$self;
}
#------------------------------------------------------------------------------
sub get {
my ($self, $stream) = @_;
my @return;
$self->{buffer} .= join('', @$stream);
while ( defined($self->{expecting}) ||
( ($self->{buffer} =~ s/^(\d+)\0//s) &&
($self->{expecting} = $1)
)
) {
last if (length $self->{buffer} < $self->{expecting});
my $chunk = substr($self->{buffer}, 0, $self->{expecting});
substr($self->{buffer}, 0, $self->{expecting}) = '';
undef $self->{expecting};
$chunk = uncompress($chunk) if $self->{compress};
push @return, $self->{thaw}->( $chunk );
}
return \@return;
}
#------------------------------------------------------------------------------
# freeze one or more references, and return a string representing them
sub put {
my ($self, $references) = @_;
my @raw = map {
my $frozen = $self->{freeze}->($_);
$frozen = compress($frozen) if $self->{compress};
length($frozen) . "\0" . $frozen;
} @$references;
\@raw;
}
#------------------------------------------------------------------------------
# We are about to be destroyed! Hand all we have left over to our Wheel
sub get_pending {
my($self)=@_;
return unless $self->{'framing buffer'};
my $ret=[$self->{'framing buffer'}];
$self->{'framing buffer'}='';
return $ret;
}
###############################################################################
1;
__END__
=head1 NAME
POE::Filter::Reference - POE Freeze/Thaw Protocol Abstraction
=head1 SYNOPSIS
$filter = new POE::Filter::Reference();
$arrayref_of_perl_references =
$filter->get($arrayref_of_raw_chunks_from_driver);
$arrayref_of_serialized_perl_references =
$filter->put($arrayref_of_perl_references);
=head1 DESCRIPTION
The "put" half of this filter freezes referenced Perl structures into
serialized versions for sending. The "get" half of this filter thaws
serialized Perl structures back into references. This provides a
handy way to ship data between processes and systems.
Serializers should recognize that POE::Filter::Reference is used to
ship data between systems with different byte orders.
=head1 PUBLIC FILTER METHODS
=over 4
=item *
POE::Filter::Reference::new( ... )
The new() method creates and initializes the reference filter. It
accepts optional parameters to specify a serializer and the use of
compression. The serializer may be a package or an object; the
compression flag is a Perl "boolean" value.
A package serializer must have a thaw() function, and it must have
either a freeze() or nfreeze() function. If it has both freeze() and
nfreeze(), then Filter::Reference will use nfreeze() for portability.
These functions match Storable and FreezeThaw's call signatures.
An object serializer must have a thaw() method. It also must have
either a freeze() or nfreeze() method. If it has both freeze() and
nfreeze(), then Filter::Reference will use nfreeze() for portability.
The thaw() method accepts $self and a scalar; it should return a
reference to the reconstituted data. The freeze() and nfreeze()
methods receive $self and a reference; they should return a scalar
with the reference's serialized representation.
If the serializer parameter is undef, a default one will be used.
This lets programs specify compression without having to worry about
naming a serializer.
For example:
# Use the default filter (either Storable or FreezeThaw).
my $filter = new POE::Filter::Reference();
# Use Storable explicitly, specified by package name.
my $filter = new POE::Filter::Reference('Storable');
# Use an object.
my $filter = new POE::Filter::Reference($object);
# Use an object, with compression.
my $filter = new POE::Filter::Reference($object, 1);
# Use the default serializer, with compression.
my $filter = new POE::Filter::Reference(undef, 1);
The new() method will try to require any packages it needs.
The default behavior is to try Storable first, FreezeThaw second, and
fail if neither is present. This is rapidly becoming moot because of
the PM_PREREQ entry in Makefile.PL, which makes CPAN and ``make'' carp
about requirements even when they aren't required.
=item *
POE::Filter::Reference::get($frozen_data)
The get() method thaws streamed, frozen data into references.
References will be blessed, if necessary. If the reference points to
an object, be sure the receiving end has use'd it before calling its
methods.
=item *
POE::Filter::Reference::put($reference)
The put() method freezes references and returns their serialized,
streamable representations.
=back
=head1 SEE ALSO
POE::Filter.
The SEE ALSO section in L<POE> contains a table of contents covering
the entire POE distribution.
=head1 BUGS
Oh, probably some.
=head1 AUTHORS & COPYRIGHTS
The Reference filter was contributed by Arturn Bergman, with changes
by Philip Gwyn.
Please see L<POE> for more information about authors and contributors.
=cut