Client.php
- Author
- Vasil Rangelov
- Category
- Net
- Copyright
- 2011 Vasil Rangelov
- License
- LGPL License 2.1
- Link
- http://pear2.php.net/PEAR2_Net_RouterOS
- Package
- PEAR2_Net_RouterOS
- Version
- 1.0.0b4
\PEAR2\Net\RouterOS\Client
- Author
- Vasil Rangelov
- Category
- Net
- License
- LGPL License 2.1
- Link
- http://pear2.php.net/PEAR2_Net_RouterOS
Constants

FILTER_CALLBACK
= 1

FILTER_BUFFER
= 2
Properties


bool $_streamingResponses = false
Whether to stream future responses.
false
Details- Type
- bool


array $callbacks = array()
An array of callbacks to be executed as responses come. Key is the tag of the request, and the value is the callback for it.
array()
Details- Type
- array


\PEAR2\Net\RouterOS\Registry $registry = null
A registry for the operations. Particularly helpful at persistent connections.
null
DetailsMethods


__construct(string $host, string $username, string $password = '', int | null $port = 8728, bool $persist = false, float $timeout = null, string $crypto = \PEAR2\Net\Transmitter\NetworkStream::CRYPTO_OFF, resource $context = null) : void
Creates a new instance of a RouterOS API client.
Name | Type | Description |
---|---|---|
$host | string | Hostname (IP or domain) of the RouterOS server. |
$username | string | The RouterOS username. |
$password | string | The RouterOS password. |
$port | int | null | The port on which the RouterOS server provides the API service. You can also specify NULL, in which case the port will automatically be chosen between 8728 and 8729, depending on the value of $crypto. |
$persist | bool | Whether or not the connection should be a persistent one. |
$timeout | float | The timeout for the connection. |
$crypto | string | The encryption for this connection. Must be one of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* constants. Off by default. RouterOS currently supports only TLS, but the setting is provided in this fashion for forward compatibility's sake. And for the sake of simplicity, if you specify an encryption, don't specify a context and your default context uses the value "DEFAULT" for ciphers, "ADH" will be automatically added to the list of ciphers. |
$context | resource | A context for the socket. |


__invoke(mixed $arg = null) : mixed
A shorthand gateway.
Name | Type | Description |
---|---|---|
$arg | mixed | Value can be either a {@link Request} to send, which would be sent asynchoniously if it has a tag, and synchroniously if not, a number to loop with or NULL to complete all pending requests. Any other value is converted to string and treated as the tag of a request to complete. |
Type | Description |
---|---|
mixed | Whatever the long form function would have returned. |


_login(\PEAR2\Net\RouterOS\Communicator $com, string $username, string $password = '', int | null $timeout = null) : bool
Login to a RouterOS connection.
Name | Type | Description |
---|---|---|
$com | \PEAR2\Net\RouterOS\Communicator | The communicator to attempt to login to. |
$username | string | The RouterOS username. |
$password | string | The RouterOS password. Potentially parsed already by iconv. |
$timeout | int | null | The time to wait for each response. NULL waits indefinetly. |
Type | Description |
---|---|
bool | TRUE on success, FALSE on failure. |


cancelRequest(string $tag = null) : \PEAR2\Net\RouterOS\Client | \PEAR2\Net\RouterOS\Client
Cancels a request.
Name | Type | Description |
---|---|---|
$tag | string | Tag of the request to cancel. Setting NULL will cancel all requests. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\Client | \PEAR2\Net\RouterOS\Client | The client object. |


close() : bool
Closes the opened connection, even if it is a persistent one.
Type | Description |
---|---|
bool | TRUE on success, FALSE on failure. |


completeRequest(string $tag = null) : \PEAR2\Net\RouterOS\ResponseCollection
Completes a specified request.
Name | Type | Description |
---|---|---|
$tag | string | The tag of the request to complete. Setting NULL completes all requests. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\ResponseCollection | A collection of {@link Response} objects that haven't been passed to a callback function or previously extracted with {@link extractNewResponses()}. Returns an empty collection when $tag is set to NULL (responses can still be extracted). |


dispatchNextResponse(int $timeout_s = 0, int $timeout_us = 0) : \PEAR2\Net\RouterOS\Response
Dispatches the next response in queue.
Name | Type | Description |
---|---|---|
$timeout_s | int | If a response is not immediatly available, wait this many seconds. If NULL, wait indefinetly. |
$timeout_us | int | Microseconds to add to the waiting time. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\Response | The dispatched response. |
Exception | Description |
---|---|
\PEAR2\Net\RouterOS\SocketException | When there's no response within the time limit. |


extractNewResponses(string $tag = null) : \PEAR2\Net\RouterOS\ResponseCollection
Extracts responses for a request.
Name | Type | Description |
---|---|---|
$tag | string | The tag of the request to extract new responses for. Specifying NULL with extract new responses for all requests. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\ResponseCollection | A collection of {@link Response} objects for the specified request. |


getCharset(int $charsetType) : string | array
Gets the charset(s) for this connection.
Name | Type | Description |
---|---|---|
$charsetType | int | Which charset to get. Valid values are the Communicator::CHARSET_* constants. Any other value is treated as {@link Communicator::CHARSET_ALL}. |
Type | Description |
---|---|
string | array | The current charset. If $charsetType is {@link Communicator::CHARSET_ALL}, the current values will be returned as an array with the types as keys, and charsets as values. |


isRequestActive(string $tag, int $filter = self::FILTER_ALL) : bool
Checks if a request is active.
Name | Type | Description |
---|---|---|
$tag | string | The tag of the request to look for. |
$filter | int | One of the FILTER_* consntants. Limits the search to the specified places. |
Type | Description |
---|---|
bool | TRUE if the request is active, FALSE otherwise. |


login(\PEAR2\Net\RouterOS\Communicator $com, string $username, string $password = '', int | null $timeout = null) : bool
Login to a RouterOS connection.
Name | Type | Description |
---|---|---|
$com | \PEAR2\Net\RouterOS\Communicator | The communicator to attempt to login to. |
$username | string | The RouterOS username. |
$password | string | The RouterOS password. |
$timeout | int | null | The time to wait for each response. NULL waits indefinetly. |
Type | Description |
---|---|
bool | TRUE on success, FALSE on failure. |


loop(int $timeout_s = null, int $timeout_us = 0) : bool
Starts an event loop for the RouterOS callbacks.
Name | Type | Description |
---|---|---|
$timeout_s | int | Timeout for the loop. If NULL, there is no time limit. |
$timeout_us | int | Microseconds to add to the time limit. |
Type | Description |
---|---|
bool | TRUE when there are any more pending requests, FALSE otherwise. |


send(\PEAR2\Net\RouterOS\Request $request) : \PEAR2\Net\RouterOS\Client | \PEAR2\Net\RouterOS\Client
Sends a request to RouterOS.
Name | Type | Description |
---|---|---|
$request | \PEAR2\Net\RouterOS\Request | The request to send. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\Client | \PEAR2\Net\RouterOS\Client | The client object. |


sendAsync(\PEAR2\Net\RouterOS\Request $request, callback $callback = null) : \PEAR2\Net\RouterOS\Client | \PEAR2\Net\RouterOS\Client
Sends a request and waits for responses.
Name | Type | Description |
---|---|---|
$request | \PEAR2\Net\RouterOS\Request | The request to send. |
$callback | callback | Optional. A function that is to be executed when new responses for this request are available. The callback takes two parameters. The {@link Response} object as the first, and the {@link Client} object as the second one. If the function returns TRUE, the request is canceled. Note that the callback may be executed one last time after that with a response that notifies about the canceling. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\Client | \PEAR2\Net\RouterOS\Client | The client object. |


sendSync(\PEAR2\Net\RouterOS\Request $request) : \PEAR2\Net\RouterOS\ResponseCollection
Sends a request and gets the full response.
Name | Type | Description |
---|---|---|
$request | \PEAR2\Net\RouterOS\Request | The request to send. |
Type | Description |
---|---|
\PEAR2\Net\RouterOS\ResponseCollection | The received responses as a collection. |


setCharset(mixed $charset, int $charsetType = \PEAR2\Net\RouterOS\Communicator::CHARSET_ALL) : string | array
Sets the charset(s) for this connection.
Name | Type | Description |
---|---|---|
$charset | mixed | The charset to set. If $charsetType is {@link Communicator::CHARSET_ALL}, you can supply either a string to use for all charsets, or an array with the charset types as keys, and the charsets as values. |
$charsetType | int | Which charset to set. Valid values are the Communicator::CHARSET_* constants. Any other value is treated as {@link Communicator::CHARSET_ALL}. |
Type | Description |
---|---|
string | array | The old charset. If $charsetType is {@link Communicator::CHARSET_ALL}, the old values will be returned as an array with the types as keys, and charsets as values. |


setStreamingResponses(bool $streamingResponses) : bool
Sets response streaming setting.
Name | Type | Description |
---|---|---|
$streamingResponses | bool | Whether to stream future responses. |
Type | Description |
---|---|
bool | The previous value of the setting. |