1 | <?php |
||
12 | class Connection extends ClientConnection { |
||
|
|||
13 | |||
14 | /** |
||
15 | * Magic code for request |
||
16 | */ |
||
17 | const MAGIC_REQUEST = "\0REQ"; |
||
18 | |||
19 | /** |
||
20 | * Magic code for response |
||
21 | */ |
||
22 | const MAGIC_RESPONSE = "\0RES"; |
||
23 | |||
24 | /* |
||
25 | * Byte length of header |
||
26 | */ |
||
27 | const HEADER_LENGTH = 12; |
||
28 | |||
29 | /** |
||
30 | * Header binary format |
||
31 | */ |
||
32 | const HEADER_WRITE_FORMAT = "a4NN"; |
||
33 | |||
34 | /** |
||
35 | * Header read format |
||
36 | */ |
||
37 | const HEADER_READ_FORMAT = "a4magic/Ntype/Nsize"; |
||
38 | |||
39 | /** |
||
40 | * Delimeter for function arguments |
||
41 | */ |
||
42 | const ARGS_DELIMITER = "\0"; |
||
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * Request codes |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected static $requestCommandList = [ |
||
52 | 'CAN_DO' => 1, |
||
53 | 'CANT_DO' => 2, |
||
54 | 'RESET_ABILITIES' => 3, |
||
55 | 'PRE_SLEEP' => 4, |
||
56 | 'SUBMIT_JOB' => 7, |
||
57 | 'GRAB_JOB' => 9, |
||
58 | 'WORK_STATUS' => 12, |
||
59 | 'WORK_COMPLETE' => 13, |
||
60 | 'WORK_FAIL' => 14, |
||
61 | 'GET_STATUS' => 15, |
||
62 | 'ECHO_REQ' => 16, |
||
63 | 'SUBMIT_JOB_BG' => 18, |
||
64 | 'SUBMIT_JOB_HIGH' => 21, |
||
65 | 'SET_CLIENT_ID' => 22, |
||
66 | 'CAN_DO_TIMEOUT' => 23, |
||
67 | 'ALL_YOURS' => 24, |
||
68 | 'WORK_EXCEPTION' => 25, |
||
69 | 'OPTION_REQ' => 26, |
||
70 | 'OPTION_RES' => 27, |
||
71 | 'WORK_DATA' => 28, |
||
72 | 'WORK_WARNING' => 29, |
||
73 | 'GRAB_JOB_UNIQ' => 30, |
||
74 | 'SUBMIT_JOB_HIGH_BG' => 32, |
||
75 | 'SUBMIT_JOB_LOW' => 33, |
||
76 | 'SUBMIT_JOB_LOW_BG' => 34, |
||
77 | 'SUBMIT_JOB_SCHED' => 35, |
||
78 | 'SUBMIT_JOB_EPOCH' => 36, |
||
79 | ]; |
||
80 | protected static $requestCommandListFlipped; |
||
81 | |||
82 | /** |
||
83 | * Response codes |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected static $responseCommandList = [ |
||
88 | 'NOOP' => 6, |
||
89 | 'JOB_CREATED' => 8, |
||
90 | 'NO_JOB' => 10, |
||
91 | 'JOB_ASSIGN' => 11, |
||
92 | 'WORK_STATUS' => 12, |
||
93 | 'WORK_COMPLETE' => 13, |
||
94 | 'WORK_FAIL' => 14, |
||
95 | 'ECHO_RES' => 17, |
||
96 | 'ERROR' => 19, |
||
97 | 'STATUS_RES' => 20, |
||
98 | 'WORK_EXCEPTION' => 25, |
||
99 | 'OPTION_RES' => 27, |
||
100 | 'WORK_WARNING' => 29, |
||
101 | 'JOB_ASSIGN_UNIQ' => 31, |
||
102 | ]; |
||
103 | |||
104 | protected static $responseCommandListFlipped; |
||
105 | |||
106 | /** |
||
107 | * @var mixed |
||
108 | */ |
||
109 | public $response; |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | */ |
||
114 | public $responseType; |
||
115 | |||
116 | /** |
||
117 | * @var string |
||
118 | */ |
||
119 | public $responseCommand; |
||
120 | |||
121 | /** |
||
122 | * Called when new data received |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function onRead() |
||
155 | |||
156 | /** |
||
157 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
158 | * @return void |
||
159 | */ |
||
160 | public function onReady() |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Function send ECHO |
||
174 | * |
||
175 | * @param $payload |
||
176 | * @param callable|null $cb |
||
177 | */ |
||
178 | public function sendEcho ($payload, $cb = null) |
||
182 | |||
183 | /** |
||
184 | * Function run task and wait result in callback |
||
185 | * |
||
186 | * @param $params |
||
187 | * @param callable $cb = null |
||
188 | * @param boolean $unique |
||
189 | */ |
||
190 | public function submitJob($params, $cb = null) |
||
210 | |||
211 | /** |
||
212 | * Get job status |
||
213 | * |
||
214 | * @param mixed $jobHandle Job handle that was given in JOB_CREATED packet. |
||
215 | * @param callable $cb = null |
||
216 | * |
||
217 | */ |
||
218 | public function getStatus($jobHandle, $cb = null) { |
||
221 | |||
222 | /** |
||
223 | * Function set settings for current connection |
||
224 | * Available settings |
||
225 | * 'exceptions' - Forward WORK_EXCEPTION packets to the client. |
||
226 | * |
||
227 | * @url http://gearman.org/protocol/ |
||
228 | * |
||
229 | * |
||
230 | * @param int $optionName |
||
231 | * @param callable $cb = null |
||
232 | */ |
||
233 | public function setConnectionOption($optionName, $cb = null) { |
||
236 | |||
237 | /** |
||
238 | * Send a command |
||
239 | * |
||
240 | * @param $commandName |
||
241 | * @param $payload |
||
242 | * @param callable $cb = null |
||
243 | */ |
||
244 | public function sendCommand($commandName, $payload, $cb = null) { |
||
256 | } |