-
Notifications
You must be signed in to change notification settings - Fork 0
/
timed-poll-2.php
246 lines (202 loc) · 4.5 KB
/
timed-poll-2.php
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
<?php
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\EventLoop\Timer\Timer;
require "vendor/autoload.php";
class Context
{
/**
* @var Deferred
*/
private $deferred;
private $needs = [];
private $unsettled = [];
private $data = [];
private $requests = [];
/**
* @var Timer
*/
private $cancel_timer;
/**
* @var LoopInterface
*/
private $loop;
private $then_callback;
private $response;
function __construct ($client , $loop, $response)
{
$this->loop = $loop;
$this->response = $response;
$this->client = $client ;
}
public function must ($url, $alias = '')
{
if (empty ($alias))
{
$alias = $url;
}
$this->needs [$alias] = $url;
$this->unsettled [$alias] = 1;
$this->should ($url, $alias);
return $this;
}
public function should ($url, $alias = '')
{
if (empty ($alias))
{
$alias = $url;
}
$request = $this->client->request ('GET', $url);
$this->requests [] = $request;
$request->on ('response', function ($response) use ($alias)
{
$response->on ('data', function ($data) use ($alias)
{
if (empty($data))
{
return;
}
$this->deferred->notify ([
'part' => $alias,
'data' => $data
]);
});
});
return $this;
}
public function then (callable $callback)
{
$this->then_callback = $callback;
return $this;
}
public function run ()
{
$this->deferred = new Deferred();
$this->deferred->promise ()->progress (function ($event)
{
$this->data [$event ['part']] = $event ['data'];
unset ($this->unsettled [$event ['part']]);
if ($this->isEverythingHasBeenReceived ())
{
$this->deferred->resolve ();
}
})->then (function ()
{
if (isset ($this->cancel_timer))
{
$this->loop->cancelTimer ($this->cancel_timer);
unset ($this->cancel_timer);
}
})
->done (function ()
{
$response = call_user_func ($this->then_callback, $this->data);
$headers = ['Content-Type' => 'text/plain'];
$this->response->writeHead (200, $headers);
$this->response->end ($response);
}, function ()
{
$headers = ['Content-Type' => 'text/plain'];
$this->response->writeHead (404, $headers);
$this->response->end ("Failed");
});
if (empty ($this->requests))
{
$this->deferred->resolve ();
}
else
{
$this->registerCancelTimer ();
foreach ($this->requests as $request)
{
$request->end ();
}
}
return $this;
}
protected function isAllNeedsProvided ()
{
return empty ($this->unsettled);
}
protected function isEverythingHasBeenReceived ()
{
return count ($this->requests) == count ($this->data);
}
protected function registerCancelTimer ()
{
$this->cancel_timer = $this->loop->addTimer (.5, function ()
{
if (false == $this->isAllNeedsProvided ())
{
$this->deferred->reject ();
}
echo "resolving by timer\n";
$this->deferred->resolve ($this->response);
});
}
}
/**
* @param $loop
*/
function registerSignalHandlers ($loop)
{
$pcntl = new MKraemer\ReactPCNTL\PCNTL($loop);
$pcntl->on (SIGTERM, function ()
{
echo 'Bye' . PHP_EOL;
die();
});
$pcntl->on (SIGINT, function ()
{
echo 'Terminated by console' . PHP_EOL;
die();
});
}
$loop = React\EventLoop\Factory::create ();
registerSignalHandlers ($loop);
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);
$loader = new Twig_Loader_Array([
'index' => '
a = {{ a }}
b = {{ b }}
c = {{ c }}
{% if d %}
d = {{ d }}
{% endif %}
',
]);
$twig = new Twig_Environment($loader);
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached ('127.0.0.1', $loop);
$factory = new React\HttpClient\Factory();
$client = $factory->create ($loop, $dnsResolver);
$http->on ('request', function ($request, $response) use ($loop, $twig, $client)
{
$url = "http://monster.akond.dev/tmp.test.php";
$context = new Context($client , $loop, $response);
$context->then (function () {
return date ('r');
})->run ();
return;
$context->must ($url . '?a', 'a')
->must ($url . '?b', 'b')
->must ($url . '?c', 'c')
->must ($url . '?d', 'd')
->must ($url . '?e', 'e')
->must ($url . '?f', 'f')
->must ($url . '?g', 'g')
->must ($url . '?h', 'h')
->must ($url . '?i', 'i')
->then (function ($data) use ($twig)
{
return var_export($data, true);
return 1;
return $twig->render ('index', $data);
})->run ();
});
$interface = '127.0.0.1';
$port = 1337;
$socket->listen ($port, $interface);
echo "Start listening on $interface:$port\n";
$loop->run ();