diff options
Diffstat (limited to 'vendor/packaged/thrift/src/Server')
-rw-r--r-- | vendor/packaged/thrift/src/Server/TForkingServer.php | 125 | ||||
-rw-r--r-- | vendor/packaged/thrift/src/Server/TSSLServerSocket.php | 97 | ||||
-rw-r--r-- | vendor/packaged/thrift/src/Server/TServer.php | 102 | ||||
-rw-r--r-- | vendor/packaged/thrift/src/Server/TServerSocket.php | 124 | ||||
-rw-r--r-- | vendor/packaged/thrift/src/Server/TServerTransport.php | 56 | ||||
-rw-r--r-- | vendor/packaged/thrift/src/Server/TSimpleServer.php | 60 |
6 files changed, 564 insertions, 0 deletions
diff --git a/vendor/packaged/thrift/src/Server/TForkingServer.php b/vendor/packaged/thrift/src/Server/TForkingServer.php new file mode 100644 index 000000000..0bb6e9192 --- /dev/null +++ b/vendor/packaged/thrift/src/Server/TForkingServer.php @@ -0,0 +1,125 @@ +<?php + +namespace Thrift\Server; + +use Thrift\Transport\TTransport; +use Thrift\Exception\TException; +use Thrift\Exception\TTransportException; + +/** + * A forking implementation of a Thrift server. + * + * @package thrift.server + */ +class TForkingServer extends TServer +{ + /** + * Flag for the main serving loop + * + * @var bool + */ + private $stop_ = false; + + /** + * List of children. + * + * @var array + */ + protected $children_ = array(); + + /** + * Listens for new client using the supplied + * transport. We fork when a new connection + * arrives. + * + * @return void + */ + public function serve() + { + $this->transport_->listen(); + + while (!$this->stop_) { + try { + $transport = $this->transport_->accept(); + + if ($transport != null) { + $pid = pcntl_fork(); + + if ($pid > 0) { + $this->handleParent($transport, $pid); + } elseif ($pid === 0) { + $this->handleChild($transport); + } else { + throw new TException('Failed to fork'); + } + } + } catch (TTransportException $e) { + } + + $this->collectChildren(); + } + } + + /** + * Code run by the parent + * + * @param TTransport $transport + * @param int $pid + * @return void + */ + private function handleParent(TTransport $transport, $pid) + { + $this->children_[$pid] = $transport; + } + + /** + * Code run by the child. + * + * @param TTransport $transport + * @return void + */ + private function handleChild(TTransport $transport) + { + try { + $inputTransport = $this->inputTransportFactory_->getTransport($transport); + $outputTransport = $this->outputTransportFactory_->getTransport($transport); + $inputProtocol = $this->inputProtocolFactory_->getProtocol($inputTransport); + $outputProtocol = $this->outputProtocolFactory_->getProtocol($outputTransport); + while ($this->processor_->process($inputProtocol, $outputProtocol)) { + } + @$transport->close(); + } catch (TTransportException $e) { + } + + exit(0); + } + + /** + * Collects any children we may have + * + * @return void + */ + private function collectChildren() + { + foreach ($this->children_ as $pid => $transport) { + if (pcntl_waitpid($pid, $status, WNOHANG) > 0) { + unset($this->children_[$pid]); + if ($transport) { + @$transport->close(); + } + } + } + } + + /** + * Stops the server running. Kills the transport + * and then stops the main serving loop + * + * @return void + */ + public function stop() + { + $this->transport_->close(); + $this->stop_ = true; + } +} diff --git a/vendor/packaged/thrift/src/Server/TSSLServerSocket.php b/vendor/packaged/thrift/src/Server/TSSLServerSocket.php new file mode 100644 index 000000000..ac589b76b --- /dev/null +++ b/vendor/packaged/thrift/src/Server/TSSLServerSocket.php @@ -0,0 +1,97 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +namespace Thrift\Server; + +use Thrift\Transport\TSSLSocket; + +/** + * Socket implementation of a server agent. + * + * @package thrift.transport + */ +class TSSLServerSocket extends TServerSocket +{ + /** + * Remote port + * + * @var resource + */ + protected $context_ = null; + + /** + * ServerSocket constructor + * + * @param string $host Host to listen on + * @param int $port Port to listen on + * @param resource $context Stream context + * @return void + */ + public function __construct($host = 'localhost', $port = 9090, $context = null) + { + $ssl_host = $this->getSSLHost($host); + parent::__construct($ssl_host, $port); + $this->context_ = $context; + } + + public function getSSLHost($host) + { + $transport_protocol_loc = strpos($host, "://"); + if ($transport_protocol_loc === false) { + $host = 'ssl://' . $host; + } + return $host; + } + + /** + * Opens a new socket server handle + * + * @return void + */ + public function listen() + { + $this->listener_ = @stream_socket_server( + $this->host_ . ':' . $this->port_, + $errno, + $errstr, + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, + $this->context_ + ); + } + + /** + * Implementation of accept. If not client is accepted in the given time + * + * @return TSocket + */ + protected function acceptImpl() + { + $handle = @stream_socket_accept($this->listener_, $this->acceptTimeout_ / 1000.0); + if (!$handle) { + return null; + } + + $socket = new TSSLSocket(); + $socket->setHandle($handle); + + return $socket; + } +} diff --git a/vendor/packaged/thrift/src/Server/TServer.php b/vendor/packaged/thrift/src/Server/TServer.php new file mode 100644 index 000000000..268c37820 --- /dev/null +++ b/vendor/packaged/thrift/src/Server/TServer.php @@ -0,0 +1,102 @@ +<?php + +namespace Thrift\Server; + +use Thrift\Factory\TTransportFactory; +use Thrift\Factory\TProtocolFactory; + +/** + * Generic class for a Thrift server. + * + * @package thrift.server + */ +abstract class TServer +{ + /** + * Processor to handle new clients + * + * @var TProcessor + */ + protected $processor_; + + /** + * Server transport to be used for listening + * and accepting new clients + * + * @var TServerTransport + */ + protected $transport_; + + /** + * Input transport factory + * + * @var TTransportFactory + */ + protected $inputTransportFactory_; + + /** + * Output transport factory + * + * @var TTransportFactory + */ + protected $outputTransportFactory_; + + /** + * Input protocol factory + * + * @var TProtocolFactory + */ + protected $inputProtocolFactory_; + + /** + * Output protocol factory + * + * @var TProtocolFactory + */ + protected $outputProtocolFactory_; + + /** + * Sets up all the factories, etc + * + * @param object $processor + * @param TServerTransport $transport + * @param TTransportFactory $inputTransportFactory + * @param TTransportFactory $outputTransportFactory + * @param TProtocolFactory $inputProtocolFactory + * @param TProtocolFactory $outputProtocolFactory + * @return void + */ + public function __construct( + $processor, + TServerTransport $transport, + TTransportFactory $inputTransportFactory, + TTransportFactory $outputTransportFactory, + TProtocolFactory $inputProtocolFactory, + TProtocolFactory $outputProtocolFactory + ) { + $this->processor_ = $processor; + $this->transport_ = $transport; + $this->inputTransportFactory_ = $inputTransportFactory; + $this->outputTransportFactory_ = $outputTransportFactory; + $this->inputProtocolFactory_ = $inputProtocolFactory; + $this->outputProtocolFactory_ = $outputProtocolFactory; + } + + /** + * Serves the server. This should never return + * unless a problem permits it to do so or it + * is interrupted intentionally + * + * @abstract + * @return void + */ + abstract public function serve(); + + /** + * Stops the server serving + * + * @abstract + * @return void + */ + abstract public function stop(); +} diff --git a/vendor/packaged/thrift/src/Server/TServerSocket.php b/vendor/packaged/thrift/src/Server/TServerSocket.php new file mode 100644 index 000000000..8f38fb23f --- /dev/null +++ b/vendor/packaged/thrift/src/Server/TServerSocket.php @@ -0,0 +1,124 @@ +<?php +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * @package thrift.transport + */ + +namespace Thrift\Server; + +use Thrift\Transport\TSocket; + +/** + * Socket implementation of a server agent. + * + * @package thrift.transport + */ +class TServerSocket extends TServerTransport +{ + /** + * Handle for the listener socket + * + * @var resource + */ + protected $listener_; + + /** + * Port for the listener to listen on + * + * @var int + */ + protected $port_; + + /** + * Timeout when listening for a new client + * + * @var int + */ + protected $acceptTimeout_ = 30000; + + /** + * Host to listen on + * + * @var string + */ + protected $host_; + + /** + * ServerSocket constructor + * + * @param string $host Host to listen on + * @param int $port Port to listen on + * @return void + */ + public function __construct($host = 'localhost', $port = 9090) + { + $this->host_ = $host; + $this->port_ = $port; + } + + /** + * Sets the accept timeout + * + * @param int $acceptTimeout + * @return void + */ + public function setAcceptTimeout($acceptTimeout) + { + $this->acceptTimeout_ = $acceptTimeout; + } + + /** + * Opens a new socket server handle + * + * @return void + */ + public function listen() + { + $this->listener_ = stream_socket_server('tcp://' . $this->host_ . ':' . $this->port_); + } + + /** + * Closes the socket server handle + * + * @return void + */ + public function close() + { + @fclose($this->listener_); + $this->listener_ = null; + } + + /** + * Implementation of accept. If not client is accepted in the given time + * + * @return TSocket + */ + protected function acceptImpl() + { + $handle = @stream_socket_accept($this->listener_, $this->acceptTimeout_ / 1000.0); + if (!$handle) { + return null; + } + + $socket = new TSocket(); + $socket->setHandle($handle); + + return $socket; + } +} diff --git a/vendor/packaged/thrift/src/Server/TServerTransport.php b/vendor/packaged/thrift/src/Server/TServerTransport.php new file mode 100644 index 000000000..15a27afa8 --- /dev/null +++ b/vendor/packaged/thrift/src/Server/TServerTransport.php @@ -0,0 +1,56 @@ +<?php + +namespace Thrift\Server; + +use Thrift\Exception\TTransportException; + +/** + * Generic class for Server agent. + * + * @package thrift.transport + */ +abstract class TServerTransport +{ + /** + * List for new clients + * + * @abstract + * @return void + */ + abstract public function listen(); + + /** + * Close the server + * + * @abstract + * @return void + */ + abstract public function close(); + + /** + * Subclasses should use this to implement + * accept. + * + * @abstract + * @return TTransport + */ + abstract protected function acceptImpl(); + + /** + * Uses the accept implemtation. If null is returned, an + * exception is thrown. + * + * @throws TTransportException + * @return TTransport + */ + public function accept() + { + $transport = $this->acceptImpl(); + + if ($transport == null) { + throw new TTransportException("accept() may not return NULL"); + } + + return $transport; + } +} diff --git a/vendor/packaged/thrift/src/Server/TSimpleServer.php b/vendor/packaged/thrift/src/Server/TSimpleServer.php new file mode 100644 index 000000000..4c1dda5a5 --- /dev/null +++ b/vendor/packaged/thrift/src/Server/TSimpleServer.php @@ -0,0 +1,60 @@ +<?php + +namespace Thrift\Server; + +use Thrift\Exception\TTransportException; + +/** + * Simple implemtation of a Thrift server. + * + * @package thrift.server + */ +class TSimpleServer extends TServer +{ + /** + * Flag for the main serving loop + * + * @var bool + */ + private $stop_ = false; + + /** + * Listens for new client using the supplied + * transport. It handles TTransportExceptions + * to avoid timeouts etc killing it + * + * @return void + */ + public function serve() + { + $this->transport_->listen(); + + while (!$this->stop_) { + try { + $transport = $this->transport_->accept(); + + if ($transport != null) { + $inputTransport = $this->inputTransportFactory_->getTransport($transport); + $outputTransport = $this->outputTransportFactory_->getTransport($transport); + $inputProtocol = $this->inputProtocolFactory_->getProtocol($inputTransport); + $outputProtocol = $this->outputProtocolFactory_->getProtocol($outputTransport); + while ($this->processor_->process($inputProtocol, $outputProtocol)) { + } + } + } catch (TTransportException $e) { + } + } + } + + /** + * Stops the server running. Kills the transport + * and then stops the main serving loop + * + * @return void + */ + public function stop() + { + $this->transport_->close(); + $this->stop_ = true; + } +} |