-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaemonServer.php
107 lines (86 loc) · 3.07 KB
/
DaemonServer.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
<?php
class DaemonServer {
protected $pids = [];
protected $server = null;
protected $pool = [];
protected $workerPipes = [];
function __construct()
{
unlink('logs.txt');
file_put_contents('server.pid', getmypid());
//create socket listener
//create childs list
var_dump('creating server daemon '.getmypid());
// $this->tcpServer = (new \OVM\Unicast\SocketServer(80))->init();
// $this->pool[0] = $this->tcpServer->getStream();
}
/**
* Register child in workers scope
* @param $pid
*/
// public function registerChild($id){
// $this->pids[] = new DaemonWorkerStatus($id);
// }
//
// public function killChild($id){
//
// }
public function serve($childrens){
$request = "GET /whitelist?var1=val1 HTTP/1.1
Host: www.nowhere123.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
<html><body><h1>It works!</h1></body></html>
";
var_dump('REFRESHING PIPES #'.getmypid());
$this->refreshPipes($childrens);
$pid = $childrens[array_rand($childrens)];
var_dump('>>>>>>>>>>Sending data from server to children #'.$pid);
// $this->sendToPipe($pid,'WOOOOHAAAA!!!!! {}{}{} 8====3 {}{}{}');
$this->sendToPipe($pid,$request);
var_dump('Serving as server '.getmypid().' with childrens : '.json_encode($childrens));
// usleep(750000);
usleep(750000);
pcntl_signal_dispatch();
}
function __destruct()
{
unlink('server.pid');
foreach($this->pool as $pid=>$pipe){
$this->closePipe($pid);
}
// TODO: Implement __destruct() method.
}
private function refreshPipes($childrens){
$died = [];
foreach($childrens as $pid){
if(isset($this->pool[$pid])){continue;}//already exist
$this->workerPipes[$pid] = fopen('run/'.$pid.'.pipein', 'w+');
$this->pool[$pid] = fopen('run/'.$pid.'.pipeout', 'r+');
stream_set_blocking($this->pool[$pid], false);
stream_set_blocking($this->workerPipes[$pid], false);
}
if(array_diff_key(array_flip($childrens),$this->pool)){
$died = array_diff_key(array_flip($childrens),$this->pool);
$died = array_flip($died);
var_dump('Found died process in server childrens: '.json_encode($died));
// var_dump('Found died process in server childrens: '.json_encode(array_diff_key(array_flip($childrens),$this->pool)));
}
foreach($died as $pid){
$this->closePipe($pid);
}
// stream_set_blocking($pipe, false);
// $pipe = fopen('run/'.$pid.'.pipein', 'w+');
}
private function sendToPipe($pid,$message){
// return;
// stream_set_blocking($pipe, false);
fputs($this->workerPipes[$pid], $message, 4096);
}
private function closePipe($pid){
fclose($this->pool[$pid]);
fclose($this->workerPipes[$pid]);
}
}