Skip to content

Commit 5ee9c8b

Browse files
committed
Added docs for the new timeouts;
Changed Util::add() to ignore non-array arguments.
1 parent c4e1bb6 commit 5ee9c8b

File tree

6 files changed

+67
-27
lines changed

6 files changed

+67
-27
lines changed

RELEASE-1.0.0b4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Brand new way of manipulating data, and listen...
66
- Executing scripts (with the ability to pass typed parameters ala SQL prepared statements)
77
- Putting and getting files out of RouterOS.
88
* Client::loop() and Client::completeRequest() no longer fail if there's no reply within "default_socket_timeout" seconds. This means you can now use the "listen" command without also setting up something else to keep the connection busy.
9-
* Client::loop() now accepts timeouts modeled after stream_select()'s, as opposed to a single float value. As before, the default is "no time limit", but is now specified with NULL instead of 0.
9+
* Client::loop() now accepts timeouts modeled after stream_select()'s, as opposed to a single float value. As before, the default is "no time limit", but is now specified with NULL instead of 0. Analogous arguments have been added to Response's constructor.
1010
* Chnaged the PHAR stub to not fail when reading the hash fails.
1111
* Doc and CS fixes.

src/PEAR2/Net/RouterOS/Client.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ public function __construct(
123123
$username . '/' . $password,
124124
$context
125125
);
126-
if (null == $timeout) {
127-
$timeout = ini_get('default_socket_timeout');
128-
}
126+
$timeout = null == $timeout
127+
? ini_get('default_socket_timeout')
128+
: (int) $timeout;
129129
//Login the user if necessary
130130
if ((!$persist
131131
|| !($old = $this->com->getTransmitter()->lock(S::DIRECTION_ALL)))
@@ -183,6 +183,8 @@ public function __invoke($arg = null)
183183
* @param Communicator $com The communicator to attempt to login to.
184184
* @param string $username The RouterOS username.
185185
* @param string $password The RouterOS password.
186+
* @param int|null $timeout The time to wait for each response. NULL
187+
* waits indefinetly.
186188
*
187189
* @return bool TRUE on success, FALSE on failure.
188190
*/
@@ -234,6 +236,8 @@ public static function login(
234236
* @param string $username The RouterOS username.
235237
* @param string $password The RouterOS password. Potentially parsed
236238
* already by iconv.
239+
* @param int|null $timeout The time to wait for each response. NULL
240+
* waits indefinetly.
237241
*
238242
* @return bool TRUE on success, FALSE on failure.
239243
*/

src/PEAR2/Net/RouterOS/Response.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ class Response extends Message
7171
/**
7272
* Extracts a new response from a communicator.
7373
*
74-
* @param Communicator $com The communicator from which to extract the
75-
* new response.
76-
* @param bool $asStream Whether to populate the argument values
74+
* @param Communicator $com The communicator from which to extract
75+
* the new response.
76+
* @param bool $asStream Whether to populate the argument values
7777
* with streams instead of strings.
78+
* @param int $timeout_s If a response is not immediatly
79+
* available, wait this many seconds. If NULL, wait indefinetly.
80+
* @param int $timeout_us Microseconds to add to the waiting time.
7881
* @param Registry $reg An optional registry to sync the
7982
* response with.
8083
*
@@ -92,8 +95,13 @@ public function __construct(
9295
if ($com->getTransmitter()->isPersistent()) {
9396
$old = $com->getTransmitter()
9497
->lock(T\Stream::DIRECTION_RECEIVE);
95-
$this->_receive($com, $asStream, $timeout_s, $timeout_us);
96-
$com->getTransmitter()->lock($old, true);
98+
try {
99+
$this->_receive($com, $asStream, $timeout_s, $timeout_us);
100+
$com->getTransmitter()->lock($old, true);
101+
} catch (SocketException $e) {
102+
$com->getTransmitter()->lock($old, true);
103+
throw $e;
104+
}
97105
} else {
98106
$this->_receive($com, $asStream, $timeout_s, $timeout_us);
99107
}
@@ -133,10 +141,13 @@ public function __construct(
133141
* This is the function that performs the actual receiving, while the
134142
* constructor is also involved in locks and registry sync.
135143
*
136-
* @param Communicator $com The communicator from which to extract the
137-
* new response.
138-
* @param bool $asStream Whether to populate the argument values
144+
* @param Communicator $com The communicator from which to extract
145+
* the new response.
146+
* @param bool $asStream Whether to populate the argument values
139147
* with streams instead of strings.
148+
* @param int $timeout_s If a response is not immediatly
149+
* available, wait this many seconds. If NULL, wait indefinetly.
150+
* @param int $timeout_us Microseconds to add to the waiting time.
140151
*
141152
* @return void
142153
*/

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public static function escapeValue($value)
139139
public static function escapeString($value)
140140
{
141141
return preg_replace_callback(
142-
'/[^\\_A-Za-z0-9]/S',
143-
array(__CLASS__, '_escapeCharacter'),
142+
'/[^\\_A-Za-z0-9]+/S',
143+
array(__CLASS__, '_escapeCharacters'),
144144
$value
145145
);
146146
}
@@ -155,14 +155,18 @@ public static function escapeString($value)
155155
*
156156
* @return string The escaped character.
157157
*/
158-
private static function _escapeCharacter($char)
158+
private static function _escapeCharacters($chars)
159159
{
160-
return '\\' . str_pad(
161-
strtoupper(dechex(ord($char[0]))),
162-
2,
163-
'0',
164-
STR_PAD_LEFT
165-
);
160+
$result = '';
161+
for ($i = 0, $l = strlen($chars[0]); $i < $l; ++$i) {
162+
$result .= '\\' . str_pad(
163+
strtoupper(dechex(ord($chars[0][$i]))),
164+
2,
165+
'0',
166+
STR_PAD_LEFT
167+
);
168+
}
169+
return $result;
166170
}
167171

168172
/**
@@ -524,6 +528,9 @@ public function add(array $values)
524528
$addRequest = new Request($this->menu . '/add');
525529
$idList = '';
526530
foreach (func_get_args() as $values) {
531+
if (!is_array($values)) {
532+
continue;
533+
}
527534
foreach ($values as $name => $value) {
528535
$addRequest->setArgument($name, $value);
529536
}
@@ -572,7 +579,7 @@ public function move($numbers, $destination)
572579
* Puts a file on RouterOS's file system.
573580
*
574581
* Puts a file on RouterOS's file system, regardless of the current menu.
575-
* Note that this is a VERY VERY VERY time consuming method - it takes a
582+
* Note that this is a **VERY VERY VERY** time consuming method - it takes a
576583
* minimum of a little over 4 seconds, most of which are in sleep. It waits
577584
* 2 seconds after a file is first created (required to actually start
578585
* writing to the file), and another 2 seconds after its contents is written

tests/UtilStateAlteringFeaturesTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class UtilStateAlteringFeaturesTest extends \PHPUnit_Framework_TestCase
88
{
99
const REGEX_ID = '\*[A-F0-9]+';
10-
const REGEX_IDLIST = '/^(\*[A-F0-9]+\,)*(\*[A-F0-9]+)?$/';
10+
const REGEX_IDLIST = '/^(\*[A-F0-9]+\,)*(\*[A-F0-9]+)$/';
1111

1212
/**
1313
* @var Util
@@ -167,13 +167,31 @@ public function testRemove()
167167
public function testAddMultiple()
168168
{
169169
$printRequest = new Request('/queue/simple/print');
170-
$beforeCount = count($this->client->sendSync($printRequest));
171170
$this->util->changeMenu('/queue/simple');
171+
172+
$beforeCount = count($this->client->sendSync($printRequest));
173+
$this->assertRegExp(
174+
self::REGEX_IDLIST,
175+
$idList = $this->util->add(
176+
array('name' => TEST_QUEUE_NAME, 'target' => '0.0.0.0/0'),
177+
array('name' => TEST_QUEUE_NAME1, 'target' => '0.0.0.0/0')
178+
)
179+
);
180+
$afterCount = count($this->client->sendSync($printRequest));
181+
$this->assertSame(2 + $beforeCount, $afterCount);
182+
183+
$this->util->remove($idList);
184+
185+
$postCount = count($this->client->sendSync($printRequest));
186+
$this->assertSame($beforeCount, $postCount);
187+
188+
$beforeCount = count($this->client->sendSync($printRequest));
172189
$this->assertRegExp(
173190
self::REGEX_IDLIST,
174191
$idList = $this->util->add(
175-
array('name' => TEST_QUEUE_NAME),
176-
array('name' => TEST_QUEUE_NAME1)
192+
array('name' => TEST_QUEUE_NAME, 'target' => '0.0.0.0/0'),
193+
null,
194+
array('name' => TEST_QUEUE_NAME1, 'target' => '0.0.0.0/0')
177195
)
178196
);
179197
$afterCount = count($this->client->sendSync($printRequest));

tests/phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999

100100
<!--
101101
A name for a test script. It must be a name not currently in the list of
102-
scripts. Also avoid any function names.
102+
scripts. Also avoid any PHP function names.
103103
-->
104104
<const name="TEST_SCRIPT_NAME" value="API_TEST" />
105105

0 commit comments

Comments
 (0)