blob: e277700e8a0ad538db02b3662deb56238814587e [file] [log] [blame]
Roger Meier21c0a852012-09-05 19:47:14 +00001<?php
2
3namespace Thrift\Server;
4
Roger Meier21c0a852012-09-05 19:47:14 +00005use Thrift\Exception\TTransportException;
6
7/**
8 * Simple implemtation of a Thrift server.
9 *
10 * @package thrift.server
11 */
Roger Thomas6fb59232014-11-04 10:09:23 +000012class TSimpleServer extends TServer
13{
Roger Meier21c0a852012-09-05 19:47:14 +000014 /**
15 * Flag for the main serving loop
16 *
17 * @var bool
18 */
19 private $stop_ = false;
20
21 /**
22 * Listens for new client using the supplied
23 * transport. It handles TTransportExceptions
24 * to avoid timeouts etc killing it
25 *
26 * @return void
27 */
Roger Thomas6fb59232014-11-04 10:09:23 +000028 public function serve()
29 {
Roger Meier21c0a852012-09-05 19:47:14 +000030 $this->transport_->listen();
31
32 while (!$this->stop_) {
33 try {
34 $transport = $this->transport_->accept();
35
36 if ($transport != null) {
37 $inputTransport = $this->inputTransportFactory_->getTransport($transport);
38 $outputTransport = $this->outputTransportFactory_->getTransport($transport);
39 $inputProtocol = $this->inputProtocolFactory_->getProtocol($inputTransport);
40 $outputProtocol = $this->outputProtocolFactory_->getProtocol($outputTransport);
41 while ($this->processor_->process($inputProtocol, $outputProtocol)) { }
42 }
Roger Thomas6fb59232014-11-04 10:09:23 +000043 } catch (TTransportException $e) { }
Roger Meier21c0a852012-09-05 19:47:14 +000044 }
45 }
46
47 /**
48 * Stops the server running. Kills the transport
49 * and then stops the main serving loop
50 *
51 * @return void
52 */
Roger Thomas6fb59232014-11-04 10:09:23 +000053 public function stop()
54 {
Roger Meier21c0a852012-09-05 19:47:14 +000055 $this->transport_->close();
56 $this->stop_ = true;
57 }
58}