1 | <?php |
||
17 | class ZmqServer implements ServerInterface, LoggerAwareInterface |
||
18 | { |
||
19 | /** |
||
20 | * Cruft flake generator. |
||
21 | * |
||
22 | * @var Generator |
||
23 | */ |
||
24 | private $generator; |
||
25 | |||
26 | /** |
||
27 | * DSN |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $dsn; |
||
32 | |||
33 | /** |
||
34 | * Logger. |
||
35 | * |
||
36 | * @var LoggerInterface |
||
37 | */ |
||
38 | private $logger; |
||
39 | |||
40 | private $debugMode = false; |
||
41 | |||
42 | /** |
||
43 | * Constructor. |
||
44 | * |
||
45 | * @param @inject Generator $generator |
||
46 | * @param string $dsn Where socket should be bound. Default 'tcp://*:5599' |
||
47 | * @param bool $debugMode Debug mode. If set to true, server will only listen for one command, before exiting. |
||
48 | */ |
||
49 | 4 | public function __construct(Generator $generator, $dsn = 'tcp://*:5599', $debugMode = false) |
|
50 | { |
||
51 | 4 | $this->generator = $generator; |
|
52 | 4 | $this->dsn = $dsn; |
|
53 | 4 | $this->logger = new NullLogger(); |
|
54 | 4 | $this->debugMode = $debugMode; |
|
55 | 4 | } |
|
56 | |||
57 | /** |
||
58 | * Run ZMQ interface for generator. |
||
59 | * |
||
60 | * Req-rep pattern; msgs are commands: |
||
61 | * |
||
62 | * GEN = Generate ID |
||
63 | * STATUS = Get status string |
||
64 | */ |
||
65 | 4 | public function run() |
|
89 | |||
90 | /** |
||
91 | * Create generate command response. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | 2 | private function commandGenerate() |
|
106 | |||
107 | /** |
||
108 | * Create status command response. |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | 1 | private function commandStatus() |
|
116 | |||
117 | /** |
||
118 | * Prepare response. |
||
119 | * |
||
120 | * @param mixed $message Return message. Anything, which is JSON serializable. |
||
121 | * @param int $code Response code. |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | 4 | private function createResponse($message, $code = 200) |
|
132 | |||
133 | /** |
||
134 | * Get ZMQ socket. |
||
135 | * |
||
136 | * @param string $dsn DSN, on which ZMQ connection should listen. |
||
137 | * |
||
138 | * @return \ZMQSocket |
||
139 | */ |
||
140 | protected function getZmqSocket($dsn) |
||
149 | |||
150 | public function setLogger(LoggerInterface $logger) |
||
154 | } |
||
155 |