1 | <?php |
||
18 | class ZmqServer implements ServerInterface, LoggerAwareInterface |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Cruft flake generator. |
||
23 | * |
||
24 | * @var Generator |
||
25 | */ |
||
26 | private $generator; |
||
27 | |||
28 | /** |
||
29 | * DSN. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $dsn; |
||
34 | |||
35 | /** |
||
36 | * Logger. |
||
37 | * |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | private $logger; |
||
41 | private $debugMode = false; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param @inject Generator $generator |
||
47 | * @param string $dsn Where socket should be bound. Default 'tcp://*:5599' |
||
48 | * @param bool $debugMode Debug mode. If set to true, server will only listen for one command, before exiting. |
||
49 | */ |
||
50 | 4 | public function __construct(Generator $generator, $dsn = 'tcp://*:5599', $debugMode = false) |
|
57 | |||
58 | /** |
||
59 | * Run ZMQ interface for generator. |
||
60 | * |
||
61 | * Req-rep pattern; msgs are commands: |
||
62 | * |
||
63 | * GEN = Generate ID |
||
64 | * STATUS = Get status string |
||
65 | */ |
||
66 | 4 | public function run() |
|
79 | |||
80 | 4 | private function runCommand($msg) |
|
92 | |||
93 | /** |
||
94 | * Create generate command response. |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | 2 | private function commandGenerate() |
|
109 | |||
110 | /** |
||
111 | * Create status command response. |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | 1 | private function commandStatus() |
|
119 | |||
120 | /** |
||
121 | * Prepare response. |
||
122 | * |
||
123 | * @param mixed $message Return message. Anything, which is JSON serializable. |
||
124 | * @param int $code Response code. |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | 4 | private function createResponse($message, $code = 200) |
|
135 | |||
136 | /** |
||
137 | * Get ZMQ socket. |
||
138 | * |
||
139 | * @param string $dsn DSN, on which ZMQ connection should listen. |
||
140 | * |
||
141 | * @return \ZMQSocket |
||
142 | */ |
||
143 | protected function getZmqSocket($dsn) |
||
152 | |||
153 | public function setLogger(LoggerInterface $logger) |
||
157 | |||
158 | } |
||
159 |