Skip to content

Commit 5611fea

Browse files
committed
Added Util::getall();
Added notifications to console on timeout and connection termination; Doc fixes (notably: replacing the use of the word "entry" with "item", as per the RouterOS manual).
1 parent da26eec commit 5611fea

File tree

3 files changed

+159
-77
lines changed

3 files changed

+159
-77
lines changed

bin/roscon.php

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
'SENT' => '',
102102
'RECV' => '',
103103
'ERR' => '',
104+
'NOTE' => '',
104105
'' => ''
105106
);
106107
if ($cmd->options['colors']) {
@@ -118,9 +119,11 @@
118119
Color\Fonts::WHITE,
119120
Color\Backgrounds::RED
120121
);
122+
$c_colors['NOTE'] = new Color(
123+
Color\Fonts::BLUE,
124+
Color\Backgrounds::YELLOW
125+
);
121126
$c_colors[''] = new Color();
122-
123-
//fwrite(STDOUT, $c_colors['']);
124127
}
125128

126129
$cmd->options['size'] = $cmd->options['size'] ?: 80;
@@ -201,21 +204,22 @@
201204
}
202205

203206
if ($cmd->options['verbose']) {
207+
$c_sep = ' | ';
204208
$c_columns = array(
205209
'mode' => 4,
206210
'length' => 10,
207211
'encodedLength' => 12
208212
);
209213
$c_columns['contents'] = $cmd->options['size'] - 1//row length
210214
- array_sum($c_columns)
211-
- (3/*strlen(' | ')*/ * count($c_columns));
215+
- (3/*strlen($c_sep)*/ * count($c_columns));
212216
fwrite(
213217
STDOUT,
214218
implode(
215219
"\n",
216220
array(
217221
implode(
218-
' | ',
222+
$c_sep,
219223
array(
220224
str_pad(
221225
'MODE',
@@ -239,7 +243,7 @@
239243
)
240244
),
241245
implode(
242-
' | ',
246+
$c_sep,
243247
array(
244248
str_repeat(' ', $c_columns['mode']),
245249
str_pad(
@@ -279,10 +283,11 @@
279283
$word,
280284
$msg = ''
281285
) use (
286+
$c_sep,
282287
$c_columns,
283288
$c_regexWrap,
284289
$c_colors
285-
) {
290+
) {
286291
$wordFragments = preg_split(
287292
$c_regexWrap,
288293
$word,
@@ -293,7 +298,8 @@
293298
unset($wordFragments[$i]);
294299
}
295300

296-
if ('ERR' === $mode) {
301+
$isAbnormal = 'ERR' === $mode || 'NOTE' === $mode;
302+
if ($isAbnormal) {
297303
$details = str_pad(
298304
$msg,
299305
$c_columns['length'] + $c_columns['encodedLength'] + 3,
@@ -319,7 +325,7 @@
319325
' ',
320326
STR_PAD_LEFT
321327
) .
322-
' | ' .
328+
$c_sep .
323329
str_pad(
324330
'0x' . strtoupper($encodedLength),
325331
$c_columns['encodedLength'],
@@ -329,26 +335,32 @@
329335
}
330336
fwrite(
331337
STDOUT,
338+
$c_colors[$mode] .
332339
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]}" .
334342
implode(
335343
"\n{$c_colors['']}" .
336344
str_repeat(' ', $c_columns['mode']) .
337-
' | ' .
345+
$c_sep .
338346
implode(
339-
('ERR' === $mode ? ' ' : ' | '),
347+
($isAbnormal ? ' ' : $c_sep),
340348
array(
341349
str_repeat(' ', $c_columns['length']),
342350
str_repeat(' ', $c_columns['encodedLength'])
343351
)
344-
) . ' | ' . $c_colors[$mode],
352+
) . $c_sep . $c_colors[$mode],
345353
$wordFragments
346354
) . "\n{$c_colors['']}"
347355
);
348356
}
349357
: 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");
352364
} elseif ('SENT' !== $mode) {
353365
fwrite(STDOUT, "{$c_colors[$mode]}{$word}{$c_colors['']}\n");
354366
}
@@ -361,13 +373,19 @@
361373
$word = '';
362374
$words = array();
363375

376+
377+
if (!$com->getTransmitter()->isAvailable()) {
378+
$printWord('NOTE', '', 'Connection terminated');
379+
break;
380+
}
381+
364382
//Input cycle
365383
while (true) {
366384
if ($cmd->options['verbose']) {
367385
fwrite(
368386
STDOUT,
369387
implode(
370-
' | ',
388+
$c_sep,
371389
array(
372390
str_pad('SEND', $c_columns['mode'], ' ', STR_PAD_RIGHT),
373391
str_pad(
@@ -404,7 +422,7 @@
404422
STDOUT,
405423
"\n{$c_colors['']}" .
406424
implode(
407-
' | ',
425+
$c_sep,
408426
array(
409427
str_repeat(' ', $c_columns['mode']),
410428
str_repeat(' ', $c_columns['length']),
@@ -455,13 +473,26 @@
455473
$com->sendWord($word);
456474
$printWord('SENT', $word);
457475
} 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+
}
459485
}
460486
}
461487

462488
//Output cycle
463489
while (true) {
490+
if (!$com->getTransmitter()->isAvailable()) {
491+
break;
492+
}
493+
464494
if (!$com->getTransmitter()->isDataAwaiting($cmd->options['time'])) {
495+
$printWord('NOTE', '', 'Receiving timed out');
465496
break;
466497
}
467498

@@ -475,7 +506,11 @@
475506
break;
476507
}
477508
} 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+
}
479514
break;
480515
} catch (RouterOS\NotSupportedException $e) {
481516
$printWord('ERR', $e->getValue(), 'Unsupported control byte');
@@ -485,8 +520,4 @@
485520
break;
486521
}
487522
}
488-
489-
if (!$com->getTransmitter()->isAvailable()) {
490-
break;
491-
}
492523
}

data/roscon.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Defaults to PHP's default_socket_timeout ini option.</description>
6565
<description>Mode to send commands in. Can be one of:
6666
"w" - send every word as soon as it is entered
6767
"s" - wait for a sentence to be formed, and send all its words then
68-
"e" - wait for an empty sentence, and send all non empty sentences then.
68+
"e" - wait for an empty sentence, and send all of the sentences then.
6969
(Default: "s")
7070
</description>
7171
<action>StoreString</action>

0 commit comments

Comments
 (0)