Permalink
Browse files

Made the query sending and login persistence aware.

  • Loading branch information...
1 parent 77cf445 commit 12ea824e499cb54388d57bd995aca9da78ae5ecd @boenrobot boenrobot committed Mar 14, 2012
Showing with 43 additions and 2 deletions.
  1. +32 −1 src/PEAR2/Net/RouterOS/Client.php
  2. +11 −1 src/PEAR2/Net/RouterOS/Query.php
View
33 src/PEAR2/Net/RouterOS/Client.php
@@ -26,6 +26,11 @@
use PEAR2\Net\Transmitter as T;
/**
+ * Uses shared memory to keep responses in when using persistent connections.
+ */
+use PEAR2\Cache\SHM;
+
+/**
* A RouterOS client.
*
* Provides functionality for easily communicating with a RouterOS host.
@@ -75,6 +80,12 @@ class Client
* Key is the tag of the request, and the value is the callback for it.
*/
protected $callbacks = array();
+
+ /**
+ * @var SHM Handler for shared response buffer. Remains NULL when the
+ * connection is not a persistent one.
+ */
+ protected $shmHandler = null;
/**
* @var bool Whether to stream future responses.
@@ -115,6 +126,13 @@ public function __construct($host, $username, $password = '', $port = 8728,
);
}
}
+
+ if ($persist) {
+ $this->shmHandler = new SHM(
+ 'PEAR2\Net\RouterOS\Client tcp://' .
+ "{$host}:{$port}/{$username}"
+ );
+ }
}
/**
@@ -185,7 +203,20 @@ public static function login(Communicator $com, $username, $password = '')
) : $e;
}
}
-
+
+ /**
+ * Login to a RouterOS connection.
+ *
+ * This is the actual login procedure, applied regardless of persistence and
+ * charset settings.
+ *
+ * @param Communicator $com The communicator to attempt to login to.
+ * @param string $username The RouterOS username.
+ * @param string $password The RouterOS password. Potentially parsed
+ * already by iconv.
+ *
+ * @return bool TRUE on success, FALSE on failure.
+ */
private static function _performLogin(
Communicator $com, $username, $password
) {
View
12 src/PEAR2/Net/RouterOS/Query.php
@@ -182,7 +182,17 @@ public function send(Communicator $com)
}
return $this->_send($com);
}
-
+
+ /**
+ * Sends the query over a communicator.
+ *
+ * The only difference with the non private equivalent is that this one does
+ * not do locking.
+ *
+ * @param Communicator $com The communicator to send the query over.
+ *
+ * @return int The number of bytes sent.
+ */
private function _send(Communicator $com)
{
if (!$com->getTransmitter()->isAcceptingData()) {

0 comments on commit 12ea824

Please sign in to comment.