blob: 3783360abd58edc606c813e6c38bdd8a2c6b29a5 [file] [log] [blame]
Volodymyr Panivko68139d12024-03-19 23:14:07 +01001<?php
2
3error_reporting(E_ALL);
4
5require_once __DIR__ . '/../../vendor/autoload.php';
6
Volodymyr Panivko76113eb2026-03-26 21:57:29 +01007class TBinaryProtocolAcceleratedFactory implements \Thrift\Factory\TProtocolFactory
8{
9 public function getProtocol($trans)
10 {
11 return new \Thrift\Protocol\TBinaryProtocolAccelerated($trans, false, true);
12 }
13}
14
Volodymyr Panivko68139d12024-03-19 23:14:07 +010015$opts = getopt(
16 'h::',
17 [
18 'port::',
19 'domain-socket::',
20 'pipe::',
21 'server-type::',
22 'transport::',
23 'protocol::',
24 'multiplex::',
25 'abstract-namespace::',
26 'ssl::',
27 'zlib::',
28 'processor-events::',
29 'workers::',
30 ]
31);
32if (isset($opts['h'])) {
33 echo <<<HELP
34 -h | --help produce help message
35 --port=arg (9090) Port number to listen
36 --domain-socket=arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)
37 --pipe=arg Windows Named Pipe (e.g. MyThriftPipe)
38 --server-type=arg (simple) type of server, "simple", "thread-pool",
39 "threaded", or "nonblocking"
40 --transport=arg (buffered) transport: buffered, framed, http, anonpipe, zlib
Volodymyr Panivko76113eb2026-03-26 21:57:29 +010041 --protocol=arg (binary) protocol: binary, compact, json, accel
Volodymyr Panivko68139d12024-03-19 23:14:07 +010042 --multiplex Add TMultiplexedProtocol service name "ThriftTest"
43 --abstract-namespace Create the domain socket in the Abstract Namespace
44 (no connection with filesystem pathnames)
45 --ssl Encrypted Transport using SSL
46 --zlib Wrapped Transport using Zlib
47 --processor-events processor-events
48 -n=arg | --workers=arg (=4) Number of thread pools workers. Only valid for
49 thread-pool server type
50HELP;
51 exit(0);
52}
53
54$port = $opts['port'] ?? 9090;
55$transport = $opts['transport'] ?? 'buffered';
Volodymyr Panivko76113eb2026-03-26 21:57:29 +010056$protocol = $opts['protocol'] ?? 'binary';
Volodymyr Panivko68139d12024-03-19 23:14:07 +010057
58
59$loader = new Thrift\ClassLoader\ThriftClassLoader();
Volodymyr Panivko76113eb2026-03-26 21:57:29 +010060$loader->registerDefinition('ThriftTest', __DIR__ . '/gen-php-classmap');
Volodymyr Panivko68139d12024-03-19 23:14:07 +010061$loader->register();
62
63$sslOptions = \stream_context_create(
64 [
65 'ssl' => [
66 'verify_peer' => false,
67 'verify_peer_name' => false,
68 ],
69 ]
70);
71
72require_once __DIR__ . '/Handler.php';
73
74switch ($transport) {
75 case 'framed':
76 $serverTransportFactory = new \Thrift\Factory\TFramedTransportFactory();
77 break;
78 default:
79 $serverTransportFactory = new \Thrift\Factory\TTransportFactory();
80}
81
Volodymyr Panivko76113eb2026-03-26 21:57:29 +010082switch ($protocol) {
83 case 'binary':
84 $protocolFactory = new \Thrift\Factory\TBinaryProtocolFactory(false, true);
85 break;
86 case 'accel':
87 if (!function_exists('thrift_protocol_write_binary')) {
88 fwrite(STDERR, "Acceleration extension is not loaded\n");
89 exit(1);
90 }
91 $protocolFactory = new TBinaryProtocolAcceleratedFactory();
92 break;
93 case 'compact':
94 $protocolFactory = new \Thrift\Factory\TCompactProtocolFactory();
95 break;
96 case 'json':
97 $protocolFactory = new \Thrift\Factory\TJSONProtocolFactory();
98 break;
99 default:
100 fwrite(STDERR, "--protocol must be one of {binary|compact|json|accel}\n");
101 exit(1);
102}
103
Volodymyr Panivko381db972026-04-02 10:22:48 +0200104// `localhost` may resolve to an IPv6-only listener in newer PHP/runtime combinations,
105// while some cross-test clients still connect via 127.0.0.1. Bind explicitly to IPv4.
106$serverTransport = new \Thrift\Server\TServerSocket('127.0.0.1', $port);
Volodymyr Panivko68139d12024-03-19 23:14:07 +0100107$handler = new Handler();
108$processor = new ThriftTest\ThriftTestProcessor($handler);
109
110$server = new \Thrift\Server\TSimpleServer(
111 $processor,
112 $serverTransport,
113 $serverTransportFactory,
114 $serverTransportFactory,
Volodymyr Panivko76113eb2026-03-26 21:57:29 +0100115 $protocolFactory,
116 $protocolFactory
Volodymyr Panivko68139d12024-03-19 23:14:07 +0100117);
118
119echo "Starting the Test server...\n";
120$server->serve();