-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathexamples.xml
436 lines (384 loc) · 11.5 KB
/
examples.xml
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 8d666e819852f6b0561b40fcf8689b747568865c Maintainer: takagi Status: ready -->
<chapter xml:id="ev.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<example>
<title>タイマーのサンプル</title>
<programlisting role="php">
<![CDATA[
<?php
// タイマーを作り、2 秒後に起動します
$w1 = new EvTimer(2, 0, function () {
echo "2 seconds elapsed\n";
});
// タイマーを作って 2 秒後に起動させ、その後は
// 手で止めるまで 1 秒おきに繰り返します
$w2 = new EvTimer(2, 1, function ($w) {
echo "is called every second, is launched after 2 seconds\n";
echo "iteration = ", Ev::iteration(), PHP_EOL;
// 5 回繰り返したあとでウォッチャーを止めます
Ev::iteration() == 5 and $w->stop();
// Stop the watcher if further calls cause more than 10 iterations
Ev::iteration() >= 10 and $w->stop();
});
// 停止状態のタイマーを作ります。開始させるまでは非アクティブになります。
$w_stopped = EvTimer::createStopped(10, 5, function($w) {
echo "Callback of a timer created as stopped\n";
// 2 回繰り返したあとでウォッチャーを止めます
Ev::iteration() >= 2 and $w->stop();
});
// Ev::stop() が呼ばれるか、すべてのウォッチャーが止まるまでループします
Ev::run();
// 開始させ、うまく動いているかどうかを確認します
$w_stopped->start();
echo "Run single iteration\n";
Ev::run(Ev::RUN_ONCE);
echo "Restart the second watcher and try to handle the same events, but don't block\n";
$w2->again();
Ev::run(Ev::RUN_NOWAIT);
$w = new EvTimer(10, 0, function() {});
echo "Running a blocking loop\n";
Ev::run();
echo "END\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
2 seconds elapsed
is called every second, is launched after 2 seconds
iteration = 1
is called every second, is launched after 2 seconds
iteration = 2
is called every second, is launched after 2 seconds
iteration = 3
is called every second, is launched after 2 seconds
iteration = 4
is called every second, is launched after 2 seconds
iteration = 5
Run single iteration
Callback of a timer created as stopped
Restart the second watcher and try to handle the same events, but don't block
Running a blocking loop
is called every second, is launched after 2 seconds
iteration = 8
is called every second, is launched after 2 seconds
iteration = 9
is called every second, is launched after 2 seconds
iteration = 10
END
]]>
</screen>
</example>
<example>
<title>10.5 秒おきに繰り返すタイマー</title>
<programlisting role="php">
<![CDATA[
<?php
$w = new EvPeriodic(0., 10.5, NULL, function ($w, $revents) {
echo time(), PHP_EOL;
});
Ev::run();
?>
]]>
</programlisting>
</example>
<example>
<title>再スケジュールコールバックを使う定期タイマー</title>
<programlisting role="php">
<![CDATA[
<?php
// 10.5 秒おきに繰り返します
function reschedule_cb ($watcher, $now) {
return $now + (10.5. - fmod($now, 10.5));
}
$w = new EvPeriodic(0., 0., "reschedule_cb", function ($w, $revents) {
echo time(), PHP_EOL;
});
Ev::run();
?>
]]>
</programlisting>
</example>
<example>
<title>すぐに開始し、10.5 秒おきに繰り返すタイマー</title>
<programlisting role="php">
<![CDATA[
<?php
// すぐに開始し、10.5 秒おきに繰り返します
$w = new EvPeriodic(fmod(Ev::now(), 10.5), 10.5, NULL, function ($w, $revents) {
echo time(), PHP_EOL;
});
Ev::run();
?>
]]>
</programlisting>
</example>
<example>
<title>STDIN が読み込み可能になるまで待つ例</title>
<programlisting role="php">
<![CDATA[
<?php
// STDIN が読み込み可能になるまで待ちます
$w = new EvIo(STDIN, Ev::READ, function ($watcher, $revents) {
echo "STDIN is readable\n";
});
Ev::run(Ev::RUN_ONCE);
?>
]]>
</programlisting>
</example>
<example>
<title>非同期 I/O を使ったソケットへのアクセス</title>
<programlisting role="php">
<![CDATA[
<?php
/* 非同期 I/O を使ってソケットにアクセスします */
// `sockets' 拡張モジュールは、
// EINPROGRESS や EAGAIN/EWOULDBLOCK などに対して warning を発光します
error_reporting(E_ERROR);
$e_nonblocking = array (/*EAGAIN or EWOULDBLOCK*/11, /*EINPROGRESS*/115);
// WWW サービス用のポートを取得します
$service_port = getservbyname('www', 'tcp');
// 対象ホストの IP アドレスを取得します
$address = gethostbyname('google.co.uk');
// TCP/IP ソケットを作ります
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === FALSE) {
echo "socket_create() failed: reason: "
.socket_strerror(socket_last_error()) . "\n";
}
// O_NONBLOCK フラグを立てます
socket_set_nonblock($socket);
// タイムアウト時に終了させます
$timeout_watcher = new EvTimer(10.0, 0., function () use ($socket) {
socket_close($socket);
Ev::stop(Ev::BREAK_ALL);
});
// ソケットが書き込み可能になったときに HEAD リクエストを作ります
$write_watcher = new EvIo($socket, Ev::WRITE, function ($w)
use ($socket, $timeout_watcher, $e_nonblocking)
{
// タイムアウトのウォッチャーを止めます
$timeout_watcher->stop();
// 書き込みのウォッチャーを止めます
$w->stop();
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: google.co.uk\r\n";
$in .= "Connection: Close\r\n\r\n";
if (!socket_write($socket, $in, strlen($in))) {
trigger_error("Failed writing $in to socket", E_USER_ERROR);
}
$read_watcher = new EvIo($socket, Ev::READ, function ($w, $re)
use ($socket, $e_nonblocking)
{
// ソケットが読み込み可能になりました。非ブロックモードで 20 バイト recv() します
$ret = socket_recv($socket, $out, 20, MSG_DONTWAIT);
if ($ret) {
echo $out;
} elseif ($ret === 0) {
// すべて読み終えました
$w->stop();
socket_close($socket);
return;
}
// EINPROGRESS、EAGAIN あるいは EWOULDBLOCK を捕捉した場合
if (in_array(socket_last_error(), $e_nonblocking)) {
return;
}
$w->stop();
socket_close($socket);
});
Ev::run();
});
$result = socket_connect($socket, $address, $service_port);
Ev::run();
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
HTTP/1.1 301 Moved Permanently
Location: http://www.google.co.uk/
Content-Type: text/html; charset=UTF-8
Date: Sun, 23 Dec 2012 16:08:27 GMT
Expires: Tue, 22 Jan 2013 16:08:27 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Connection: close
]]>
</screen>
</example>
<example>
<title>ループの中に別のループを埋め込む例</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* 埋め込み可能なイベントループをデフォルトのイベントループに組み込みます。
* できなかった場合はデフォルトのループを使います。
* デフォルトのループは $loop_hi に、そして埋め込み可能なループは $loop_lo
* に格納されます (埋め込み可能なループが使えなかった場合は $loop_hi
* を使います)。
*
* このサンプルを PHP に移植したものです。
* http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
*/
$loop_hi = EvLoop::defaultLoop();
$loop_lo = NULL;
$embed = NULL;
/*
* 使えるループを取得できるかどうかを調べます
* (フラグの値が 0 の場合は自動検出を意味します)
*/
$loop_lo = Ev::embeddableBackends() & Ev::recommendedBackends()
? new EvLoop(Ev::embeddableBackends() & Ev::recommendedBackends())
: 0;
if ($loop_lo) {
$embed = new EvEmbed($loop_lo, function () {});
} else {
$loop_lo = $loop_hi;
}
?>
]]>
</programlisting>
</example>
<example>
<title>kqueue バックエンドで作ったループをデフォルトのループに埋め込む例</title>
<programlisting role="php">
<![CDATA[
<?php
/*
* kqueue が使えるかどうかを調べ、ソケットで使う kqueue バックエンドを作ります
* (通常は、どんな kqueue 実装でも動きます)。
* kqueue/socket-only イベントループを loop_socket に格納します
* (EVFLAG_NOENV も使えます)。
*
* この例を流用しました
* http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
*/
$loop = EvLoop::defaultLoop();
$socket_loop = NULL;
$embed = NULL;
if (Ev::supportedBackends() & ~Ev::recommendedBackends() & Ev::BACKEND_KQUEUE) {
if (($socket_loop = new EvLoop(Ev::BACKEND_KQUEUE))) {
$embed = new EvEmbed($loop);
}
}
if (!$socket_loop) {
$socket_loop = $loop;
}
// これで、すべてのソケットに対して $socket_loop を使い、それ以外については $loop を使うようになりました
?>
]]>
</programlisting>
</example>
<example>
<title>SIGTERM の処理</title>
<programlisting role="php">
<![CDATA[
<?php
$w = new EvSignal(SIGTERM, function ($watcher) {
echo "SIGTERM received\n";
$watcher->stop();
});
Ev::run();
?>
]]>
</programlisting>
</example>
<example>
<title>/var/log/messages の変更の監視</title>
<programlisting role="php">
<![CDATA[
<?php
// 更新間隔を 10 秒にします
$w = new EvStat("/var/log/messages", 8, function ($w) {
echo "/var/log/messages changed\n";
$attr = $w->attr();
if ($attr['nlink']) {
printf("Current size: %ld\n", $attr['size']);
printf("Current atime: %ld\n", $attr['atime']);
printf("Current mtime: %ld\n", $attr['mtime']);
} else {
fprintf(STDERR, "`messages` file is not there!");
$w->stop();
}
});
Ev::run();
?>
]]>
</programlisting>
</example>
<example>
<title>/var/log/messages の変更の監視 (1 秒の遅延を使って、更新を見落とさないようにする)</title>
<programlisting role="php">
<![CDATA[
<?php
$timer = EvTimer::createStopped(0., 1.02, function ($w) {
$w->stop();
$stat = $w->data;
// ファイルへの直近の変更から 1 秒後
printf("Current size: %ld\n", $stat->attr()['size']);
});
$stat = new EvStat("/var/log/messages", 0., function () use ($timer) {
// タイマーウォッチャーをリセットします
$timer->again();
});
$timer->data = $stat;
Ev::run();
?>
]]>
</programlisting>
</example>
<example>
<title>ステータスの変更の処理</title>
<programlisting role="php">
<![CDATA[
<?php
$pid = pcntl_fork();
if ($pid == -1) {
fprintf(STDERR, "pcntl_fork failed\n");
} elseif ($pid) {
$w = new EvChild($pid, FALSE, function ($w, $revents) {
$w->stop();
printf("Process %d exited with status %d\n", $w->rpid, $w->rstatus);
});
Ev::run();
// ゾンビ対策
pcntl_wait($status);
} else {
// フォークした子プロセス
exit(2);
}
?>
]]>
</programlisting>
</example>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->