]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | <?php |
2 | ||
3 | namespace Thrift\Server; | |
4 | ||
5 | use Thrift\Transport\TTransport; | |
6 | use Thrift\Exception\TException; | |
7 | use Thrift\Exception\TTransportException; | |
8 | ||
9 | /** | |
10 | * A forking implementation of a Thrift server. | |
11 | * | |
12 | * @package thrift.server | |
13 | */ | |
14 | class TForkingServer extends TServer | |
15 | { | |
16 | /** | |
17 | * Flag for the main serving loop | |
18 | * | |
19 | * @var bool | |
20 | */ | |
21 | private $stop_ = false; | |
22 | ||
23 | /** | |
24 | * List of children. | |
25 | * | |
26 | * @var array | |
27 | */ | |
28 | protected $children_ = array(); | |
29 | ||
30 | /** | |
31 | * Listens for new client using the supplied | |
32 | * transport. We fork when a new connection | |
33 | * arrives. | |
34 | * | |
35 | * @return void | |
36 | */ | |
37 | public function serve() | |
38 | { | |
39 | $this->transport_->listen(); | |
40 | ||
41 | while (!$this->stop_) { | |
42 | try { | |
43 | $transport = $this->transport_->accept(); | |
44 | ||
45 | if ($transport != null) { | |
46 | $pid = pcntl_fork(); | |
47 | ||
48 | if ($pid > 0) { | |
49 | $this->handleParent($transport, $pid); | |
50 | } elseif ($pid === 0) { | |
51 | $this->handleChild($transport); | |
52 | } else { | |
53 | throw new TException('Failed to fork'); | |
54 | } | |
55 | } | |
56 | } catch (TTransportException $e) { | |
57 | } | |
58 | ||
59 | $this->collectChildren(); | |
60 | } | |
61 | } | |
62 | ||
63 | /** | |
64 | * Code run by the parent | |
65 | * | |
66 | * @param TTransport $transport | |
67 | * @param int $pid | |
68 | * @return void | |
69 | */ | |
70 | private function handleParent(TTransport $transport, $pid) | |
71 | { | |
72 | $this->children_[$pid] = $transport; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Code run by the child. | |
77 | * | |
78 | * @param TTransport $transport | |
79 | * @return void | |
80 | */ | |
81 | private function handleChild(TTransport $transport) | |
82 | { | |
83 | try { | |
84 | $inputTransport = $this->inputTransportFactory_->getTransport($transport); | |
85 | $outputTransport = $this->outputTransportFactory_->getTransport($transport); | |
86 | $inputProtocol = $this->inputProtocolFactory_->getProtocol($inputTransport); | |
87 | $outputProtocol = $this->outputProtocolFactory_->getProtocol($outputTransport); | |
88 | while ($this->processor_->process($inputProtocol, $outputProtocol)) { | |
89 | } | |
90 | @$transport->close(); | |
91 | } catch (TTransportException $e) { | |
92 | } | |
93 | ||
94 | exit(0); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Collects any children we may have | |
99 | * | |
100 | * @return void | |
101 | */ | |
102 | private function collectChildren() | |
103 | { | |
104 | foreach ($this->children_ as $pid => $transport) { | |
105 | if (pcntl_waitpid($pid, $status, WNOHANG) > 0) { | |
106 | unset($this->children_[$pid]); | |
107 | if ($transport) { | |
108 | @$transport->close(); | |
109 | } | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | /** | |
115 | * Stops the server running. Kills the transport | |
116 | * and then stops the main serving loop | |
117 | * | |
118 | * @return void | |
119 | */ | |
120 | public function stop() | |
121 | { | |
122 | $this->transport_->close(); | |
123 | $this->stop_ = true; | |
124 | } | |
125 | } |