|
101 | 101 | 'SENT' => '',
|
102 | 102 | 'RECV' => '',
|
103 | 103 | 'ERR' => '',
|
| 104 | + 'NOTE' => '', |
104 | 105 | '' => ''
|
105 | 106 | );
|
106 | 107 | if ($cmd->options['colors']) {
|
|
118 | 119 | Color\Fonts::WHITE,
|
119 | 120 | Color\Backgrounds::RED
|
120 | 121 | );
|
| 122 | + $c_colors['NOTE'] = new Color( |
| 123 | + Color\Fonts::BLUE, |
| 124 | + Color\Backgrounds::YELLOW |
| 125 | + ); |
121 | 126 | $c_colors[''] = new Color();
|
122 |
| - |
123 |
| - //fwrite(STDOUT, $c_colors['']); |
124 | 127 | }
|
125 | 128 |
|
126 | 129 | $cmd->options['size'] = $cmd->options['size'] ?: 80;
|
|
201 | 204 | }
|
202 | 205 |
|
203 | 206 | if ($cmd->options['verbose']) {
|
| 207 | + $c_sep = ' | '; |
204 | 208 | $c_columns = array(
|
205 | 209 | 'mode' => 4,
|
206 | 210 | 'length' => 10,
|
207 | 211 | 'encodedLength' => 12
|
208 | 212 | );
|
209 | 213 | $c_columns['contents'] = $cmd->options['size'] - 1//row length
|
210 | 214 | - array_sum($c_columns)
|
211 |
| - - (3/*strlen(' | ')*/ * count($c_columns)); |
| 215 | + - (3/*strlen($c_sep)*/ * count($c_columns)); |
212 | 216 | fwrite(
|
213 | 217 | STDOUT,
|
214 | 218 | implode(
|
215 | 219 | "\n",
|
216 | 220 | array(
|
217 | 221 | implode(
|
218 |
| - ' | ', |
| 222 | + $c_sep, |
219 | 223 | array(
|
220 | 224 | str_pad(
|
221 | 225 | 'MODE',
|
|
239 | 243 | )
|
240 | 244 | ),
|
241 | 245 | implode(
|
242 |
| - ' | ', |
| 246 | + $c_sep, |
243 | 247 | array(
|
244 | 248 | str_repeat(' ', $c_columns['mode']),
|
245 | 249 | str_pad(
|
|
279 | 283 | $word,
|
280 | 284 | $msg = ''
|
281 | 285 | ) use (
|
| 286 | + $c_sep, |
282 | 287 | $c_columns,
|
283 | 288 | $c_regexWrap,
|
284 | 289 | $c_colors
|
285 |
| - ) { |
| 290 | + ) { |
286 | 291 | $wordFragments = preg_split(
|
287 | 292 | $c_regexWrap,
|
288 | 293 | $word,
|
|
293 | 298 | unset($wordFragments[$i]);
|
294 | 299 | }
|
295 | 300 |
|
296 |
| - if ('ERR' === $mode) { |
| 301 | + $isAbnormal = 'ERR' === $mode || 'NOTE' === $mode; |
| 302 | + if ($isAbnormal) { |
297 | 303 | $details = str_pad(
|
298 | 304 | $msg,
|
299 | 305 | $c_columns['length'] + $c_columns['encodedLength'] + 3,
|
|
319 | 325 | ' ',
|
320 | 326 | STR_PAD_LEFT
|
321 | 327 | ) .
|
322 |
| - ' | ' . |
| 328 | + $c_sep . |
323 | 329 | str_pad(
|
324 | 330 | '0x' . strtoupper($encodedLength),
|
325 | 331 | $c_columns['encodedLength'],
|
|
329 | 335 | }
|
330 | 336 | fwrite(
|
331 | 337 | STDOUT,
|
| 338 | + $c_colors[$mode] . |
332 | 339 | str_pad($mode, $c_columns['mode'], ' ', STR_PAD_RIGHT) .
|
333 |
| - " | {$details} | {$c_colors[$mode]}" . |
| 340 | + $c_colors[''] . |
| 341 | + "{$c_sep}{$details}{$c_sep}{$c_colors[$mode]}" . |
334 | 342 | implode(
|
335 | 343 | "\n{$c_colors['']}" .
|
336 | 344 | str_repeat(' ', $c_columns['mode']) .
|
337 |
| - ' | ' . |
| 345 | + $c_sep . |
338 | 346 | implode(
|
339 |
| - ('ERR' === $mode ? ' ' : ' | '), |
| 347 | + ($isAbnormal ? ' ' : $c_sep), |
340 | 348 | array(
|
341 | 349 | str_repeat(' ', $c_columns['length']),
|
342 | 350 | str_repeat(' ', $c_columns['encodedLength'])
|
343 | 351 | )
|
344 |
| - ) . ' | ' . $c_colors[$mode], |
| 352 | + ) . $c_sep . $c_colors[$mode], |
345 | 353 | $wordFragments
|
346 | 354 | ) . "\n{$c_colors['']}"
|
347 | 355 | );
|
348 | 356 | }
|
349 | 357 | : function ($mode, $word, $msg = '') use ($c_colors) {
|
350 |
| - if ('ERR' === $mode) { |
351 |
| - fwrite(STDERR, "{$c_colors[$mode]}{$msg}: {$word}{$c_colors['']}"); |
| 358 | + if ('ERR' === $mode || 'NOTE' === $mode) { |
| 359 | + fwrite(STDERR, "{$c_colors[$mode]}{$msg}"); |
| 360 | + if ('' !== $word) { |
| 361 | + fwrite(STDERR, ": {$word}"); |
| 362 | + } |
| 363 | + fwrite(STDERR, "{$c_colors['']}\n"); |
352 | 364 | } elseif ('SENT' !== $mode) {
|
353 | 365 | fwrite(STDOUT, "{$c_colors[$mode]}{$word}{$c_colors['']}\n");
|
354 | 366 | }
|
|
361 | 373 | $word = '';
|
362 | 374 | $words = array();
|
363 | 375 |
|
| 376 | + |
| 377 | + if (!$com->getTransmitter()->isAvailable()) { |
| 378 | + $printWord('NOTE', '', 'Connection terminated'); |
| 379 | + break; |
| 380 | + } |
| 381 | + |
364 | 382 | //Input cycle
|
365 | 383 | while (true) {
|
366 | 384 | if ($cmd->options['verbose']) {
|
367 | 385 | fwrite(
|
368 | 386 | STDOUT,
|
369 | 387 | implode(
|
370 |
| - ' | ', |
| 388 | + $c_sep, |
371 | 389 | array(
|
372 | 390 | str_pad('SEND', $c_columns['mode'], ' ', STR_PAD_RIGHT),
|
373 | 391 | str_pad(
|
|
404 | 422 | STDOUT,
|
405 | 423 | "\n{$c_colors['']}" .
|
406 | 424 | implode(
|
407 |
| - ' | ', |
| 425 | + $c_sep, |
408 | 426 | array(
|
409 | 427 | str_repeat(' ', $c_columns['mode']),
|
410 | 428 | str_repeat(' ', $c_columns['length']),
|
|
455 | 473 | $com->sendWord($word);
|
456 | 474 | $printWord('SENT', $word);
|
457 | 475 | } catch (SE $e) {
|
458 |
| - $printWord('ERR', $word, 'Failed to send word'); |
| 476 | + if (0 === $e->getFragment()) { |
| 477 | + $printWord('ERR', '', 'Failed to send word'); |
| 478 | + } else { |
| 479 | + $printWord( |
| 480 | + 'ERR', |
| 481 | + substr($word, 0, $e->getFragment()), |
| 482 | + "Partial word sent" |
| 483 | + ); |
| 484 | + } |
459 | 485 | }
|
460 | 486 | }
|
461 | 487 |
|
462 | 488 | //Output cycle
|
463 | 489 | while (true) {
|
| 490 | + if (!$com->getTransmitter()->isAvailable()) { |
| 491 | + break; |
| 492 | + } |
| 493 | + |
464 | 494 | if (!$com->getTransmitter()->isDataAwaiting($cmd->options['time'])) {
|
| 495 | + $printWord('NOTE', '', 'Receiving timed out'); |
465 | 496 | break;
|
466 | 497 | }
|
467 | 498 |
|
|
475 | 506 | break;
|
476 | 507 | }
|
477 | 508 | } catch (SE $e) {
|
478 |
| - $printWord('ERR', $e->getFragment(), 'Incomplete word'); |
| 509 | + if ('' === $e->getFragment()) { |
| 510 | + $printWord('ERR', '', 'Failed to receive word'); |
| 511 | + } else { |
| 512 | + $printWord('ERR', $e->getFragment(), 'Partial word received'); |
| 513 | + } |
479 | 514 | break;
|
480 | 515 | } catch (RouterOS\NotSupportedException $e) {
|
481 | 516 | $printWord('ERR', $e->getValue(), 'Unsupported control byte');
|
|
485 | 520 | break;
|
486 | 521 | }
|
487 | 522 | }
|
488 |
| - |
489 |
| - if (!$com->getTransmitter()->isAvailable()) { |
490 |
| - break; |
491 |
| - } |
492 | 523 | }
|
0 commit comments