source: main/trunk/greenstone2/perllib/cpan/Mojo/Reactor/EV.pm@ 32205

Last change on this file since 32205 was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 4.6 KB
Line 
1package Mojo::Reactor::EV;
2use Mojo::Base 'Mojo::Reactor::Poll';
3
4use Carp 'croak';
5use EV 4.0;
6
7my $EV;
8
9sub DESTROY { undef $EV }
10
11sub again {
12 croak 'Timer not active' unless my $timer = shift->{timers}{shift()};
13 $timer->{watcher}->again;
14}
15
16sub is_running { !!EV::depth }
17
18# We have to fall back to Mojo::Reactor::Poll, since EV is unique
19sub new { $EV++ ? Mojo::Reactor::Poll->new : shift->SUPER::new }
20
21sub one_tick { EV::run(EV::RUN_ONCE) }
22
23sub recurring { shift->_timer(1, @_) }
24
25sub start {EV::run}
26
27sub stop { EV::break(EV::BREAK_ALL) }
28
29sub timer { shift->_timer(0, @_) }
30
31sub watch {
32 my ($self, $handle, $read, $write) = @_;
33
34 my $fd = fileno $handle;
35 croak 'I/O watcher not active' unless my $io = $self->{io}{$fd};
36
37 my $mode = 0;
38 $mode |= EV::READ if $read;
39 $mode |= EV::WRITE if $write;
40
41 if ($mode == 0) { delete $io->{watcher} }
42 elsif (my $w = $io->{watcher}) { $w->events($mode) }
43 else {
44 my $cb = sub {
45 my ($w, $revents) = @_;
46 $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 0)
47 if EV::READ & $revents;
48 $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 1)
49 if EV::WRITE & $revents && $self->{io}{$fd};
50 };
51 $io->{watcher} = EV::io($fd, $mode, $cb);
52 }
53
54 return $self;
55}
56
57sub _timer {
58 my ($self, $recurring, $after, $cb) = @_;
59 $after ||= 0.0001 if $recurring;
60
61 my $id = $self->_id;
62 my $wrapper = sub {
63 delete $self->{timers}{$id} unless $recurring;
64 $self->_try('Timer', $cb);
65 };
66 EV::now_update() if $after > 0;
67 $self->{timers}{$id}{watcher} = EV::timer($after, $after, $wrapper);
68
69 return $id;
70}
71
721;
73
74=encoding utf8
75
76=head1 NAME
77
78Mojo::Reactor::EV - Low-level event reactor with libev support
79
80=head1 SYNOPSIS
81
82 use Mojo::Reactor::EV;
83
84 # Watch if handle becomes readable or writable
85 my $reactor = Mojo::Reactor::EV->new;
86 $reactor->io($first => sub {
87 my ($reactor, $writable) = @_;
88 say $writable ? 'First handle is writable' : 'First handle is readable';
89 });
90
91 # Change to watching only if handle becomes writable
92 $reactor->watch($first, 0, 1);
93
94 # Turn file descriptor into handle and watch if it becomes readable
95 my $second = IO::Handle->new_from_fd($fd, 'r');
96 $reactor->io($second => sub {
97 my ($reactor, $writable) = @_;
98 say $writable ? 'Second handle is writable' : 'Second handle is readable';
99 })->watch($second, 1, 0);
100
101 # Add a timer
102 $reactor->timer(15 => sub {
103 my $reactor = shift;
104 $reactor->remove($first);
105 $reactor->remove($second);
106 say 'Timeout!';
107 });
108
109 # Start reactor if necessary
110 $reactor->start unless $reactor->is_running;
111
112=head1 DESCRIPTION
113
114L<Mojo::Reactor::EV> is a low-level event reactor based on L<EV> (4.0+).
115
116=head1 EVENTS
117
118L<Mojo::Reactor::EV> inherits all events from L<Mojo::Reactor::Poll>.
119
120=head1 METHODS
121
122L<Mojo::Reactor::EV> inherits all methods from L<Mojo::Reactor::Poll> and
123implements the following new ones.
124
125=head2 again
126
127 $reactor->again($id);
128
129Restart timer. Note that this method requires an active timer.
130
131=head2 is_running
132
133 my $bool = $reactor->is_running;
134
135Check if reactor is running.
136
137=head2 new
138
139 my $reactor = Mojo::Reactor::EV->new;
140
141Construct a new L<Mojo::Reactor::EV> object.
142
143=head2 one_tick
144
145 $reactor->one_tick;
146
147Run reactor until an event occurs or no events are being watched anymore.
148
149 # Don't block longer than 0.5 seconds
150 my $id = $reactor->timer(0.5 => sub {});
151 $reactor->one_tick;
152 $reactor->remove($id);
153
154=head2 recurring
155
156 my $id = $reactor->recurring(0.25 => sub {...});
157
158Create a new recurring timer, invoking the callback repeatedly after a given
159amount of time in seconds.
160
161=head2 start
162
163 $reactor->start;
164
165Start watching for I/O and timer events, this will block until L</"stop"> is
166called or no events are being watched anymore.
167
168 # Start reactor only if it is not running already
169 $reactor->start unless $reactor->is_running;
170
171=head2 stop
172
173 $reactor->stop;
174
175Stop watching for I/O and timer events.
176
177=head2 timer
178
179 my $id = $reactor->timer(0.5 => sub {...});
180
181Create a new timer, invoking the callback after a given amount of time in
182seconds.
183
184=head2 watch
185
186 $reactor = $reactor->watch($handle, $readable, $writable);
187
188Change I/O events to watch handle for with true and false values. Note that
189this method requires an active I/O watcher.
190
191 # Watch only for readable events
192 $reactor->watch($handle, 1, 0);
193
194 # Watch only for writable events
195 $reactor->watch($handle, 0, 1);
196
197 # Watch for readable and writable events
198 $reactor->watch($handle, 1, 1);
199
200 # Pause watching for events
201 $reactor->watch($handle, 0, 0);
202
203=head1 SEE ALSO
204
205L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
206
207=cut
Note: See TracBrowser for help on using the repository browser.