PEAR2_Net_RouterOS  1.0.0b5
A MikroTik RouterOS client
 All Data Structures Namespaces Files Functions Variables Pages
Client Class Reference

Public Member Functions

 __construct ($host, $username, $password= '', $port=8728, $persist=false, $timeout=null, $crypto=N::CRYPTO_OFF, $context=null)
 Creates a new instance of a RouterOS API client. More...
 
 __invoke ($arg=null)
 A shorthand gateway. More...
 
 setCharset ($charset, $charsetType=Communicator::CHARSET_ALL)
 Sets the charset(s) for this connection. More...
 
 getCharset ($charsetType)
 Gets the charset(s) for this connection. More...
 
 sendAsync (Request $request, $callback=null)
 Sends a request and waits for responses. More...
 
 isRequestActive ($tag, $filter=self::FILTER_ALL)
 Checks if a request is active. More...
 
 sendSync (Request $request)
 Sends a request and gets the full response. More...
 
 completeRequest ($tag=null)
 Completes a specified request. More...
 
 extractNewResponses ($tag=null)
 Extracts responses for a request. More...
 
 loop ($sTimeout=null, $usTimeout=0)
 Starts an event loop for the RouterOS callbacks. More...
 
 getPendingRequestsCount ()
 Gets the number of pending requests. More...
 
 cancelRequest ($tag=null)
 Cancels a request. More...
 
 setStreamingResponses ($streamingResponses)
 Sets response streaming setting. More...
 
 isStreamingResponses ()
 Gets response streaming setting. More...
 
 close ()
 Closes the opened connection, even if it is a persistent one. More...
 
 __destruct ()
 Closes the connection, unless it's a persistent one. More...
 

Static Public Member Functions

static login (Communicator $com, $username, $password= '', $timeout=null)
 Login to a RouterOS connection. More...
 

Data Fields

const FILTER_CALLBACK = 1
 Used in static::isRequestActive() to limit search only to requests that have a callback. More...
 
const FILTER_BUFFER = 2
 Used in static::isRequestActive() to limit search only to requests that use the buffer. More...
 
const FILTER_ALL = 3
 Used in static::isRequestActive() to indicate no limit in search. More...
 

Protected Member Functions

 send (Request $request)
 Sends a request to RouterOS. More...
 
 dispatchNextResponse ($sTimeout=0, $usTimeout=0)
 Dispatches the next response in queue. More...
 

Protected Attributes

 $com
 
 $pendingRequestsCount = 0
 
 $responseBuffer = array()
 
 $callbacks = array()
 
 $registry = null
 

Detailed Description

Definition at line 49 of file Client.php.

Constructor & Destructor Documentation

__construct (   $host,
  $username,
  $password = '',
  $port = 8728,
  $persist = false,
  $timeout = null,
  $crypto = N::CRYPTO_OFF,
  $context = null 
)

Creates a new instance of a RouterOS API client.

Creates a new instance of a RouterOS API client with the specified settings.

Parameters
string$hostHostname (IP or domain) of the RouterOS server.
string$usernameThe RouterOS username.
string$passwordThe RouterOS password.
int | null$portThe 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.
bool$persistWhether or not the connection should be a persistent one.
float$timeoutThe timeout for the connection.
string$cryptoThe encryption for this connection. Must be one of the PEAR2::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.
resource$contextA context for the socket.
See also
sendSync()
sendAsync()

Definition at line 128 of file Client.php.

__destruct ( )

Closes the connection, unless it's a persistent one.

Definition at line 745 of file Client.php.

Member Function Documentation

__invoke (   $arg = null)

A shorthand gateway.

This is a magic PHP method that allows you to call the object as a function. Depending on the argument given, one of the other functions in the class is invoked and its returned value is returned by this function.

Parameters
mixed$argValue can be either a 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.
Returns
mixed Whatever the long form function would have returned.

Definition at line 188 of file Client.php.

cancelRequest (   $tag = null)

Cancels a request.

Cancels an active request. Using this function in favor of a plain call to the "/cancel" command is highly reccomended, as it also updates the counter of pending requests properly. Note that canceling a request also removes any responses for it that were not previously extracted with static::extractNewResponses().

Parameters
string$tagTag of the request to cancel. Setting NULL will cancel all requests.
Returns
$this The client object.
See also
sendAsync()
close()

Definition at line 608 of file Client.php.

close ( )

Closes the opened connection, even if it is a persistent one.

Closes the opened connection, even if it is a persistent one. Note that static::extractNewResponses() can still be used to extract responses collected prior to the closing.

Returns
bool TRUE on success, FALSE on failure.

Definition at line 706 of file Client.php.

completeRequest (   $tag = null)

Completes a specified request.

Starts an event loop for the RouterOS callbacks and finishes when a specified request is completed.

Parameters
string$tagThe tag of the request to complete. Setting NULL completes all requests.
Returns
ResponseCollection A collection of Response objects that haven't been passed to a callback function or previously extracted with static::extractNewResponses(). Returns an empty collection when $tag is set to NULL (responses can still be extracted).

Definition at line 450 of file Client.php.

dispatchNextResponse (   $sTimeout = 0,
  $usTimeout = 0 
)
protected

Dispatches the next response in queue.

Dispatches the next response in queue, i.e. it executes the associated callback if there is one, or places the response in the response buffer.

Parameters
int$sTimeoutIf a response is not immediatly available, wait this many seconds. If NULL, wait indefinetly.
int$usTimeoutMicroseconds to add to the waiting time.
Exceptions
SocketExceptionWhen there's no response within the time limit.
Returns
Response The dispatched response.

Definition at line 785 of file Client.php.

extractNewResponses (   $tag = null)

Extracts responses for a request.

Gets all new responses for a request that haven't been passed to a callback and clears the buffer from them.

Parameters
string$tagThe tag of the request to extract new responses for. Specifying NULL with extract new responses for all requests.
Returns
ResponseCollection A collection of Response objects for the specified request.
See also
loop()

Definition at line 492 of file Client.php.

getCharset (   $charsetType)

Gets the charset(s) for this connection.

Parameters
int$charsetTypeWhich charset to get. Valid values are the Communicator::CHARSET_* constants. Any other value is treated as Communicator::CHARSET_ALL.
Returns
string|array The current charset. If $charsetType is Communicator::CHARSET_ALL, the current values will be returned as an array with the types as keys, and charsets as values.
See also
setCharset()

Definition at line 332 of file Client.php.

getPendingRequestsCount ( )

Gets the number of pending requests.

Returns
int The number of pending requests.
See also
isRequestActive()

Definition at line 587 of file Client.php.

isRequestActive (   $tag,
  $filter = self::FILTER_ALL 
)

Checks if a request is active.

Checks if a request is active. A request is considered active if it's a pending request and/or has responses that are not yet extracted.

Parameters
string$tagThe tag of the request to look for.
int$filterOne of the FILTER_* consntants. Limits the search to the specified places.
Returns
bool TRUE if the request is active, FALSE otherwise.
See also
getPendingRequestsCount()
completeRequest()

Definition at line 403 of file Client.php.

isStreamingResponses ( )

Gets response streaming setting.

Gets whether future responses are streamed.

Returns
bool The value of the setting.
See also
setStreamingResponses()

Definition at line 692 of file Client.php.

static login ( Communicator  $com,
  $username,
  $password = '',
  $timeout = null 
)
static

Login to a RouterOS connection.

Parameters
Communicator$comThe communicator to attempt to login to.
string$usernameThe RouterOS username.
string$passwordThe RouterOS password.
int | null$timeoutThe time to wait for each response. NULL waits indefinetly.
Returns
bool TRUE on success, FALSE on failure.

Definition at line 212 of file Client.php.

loop (   $sTimeout = null,
  $usTimeout = 0 
)

Starts an event loop for the RouterOS callbacks.

Starts an event loop for the RouterOS callbacks and finishes when there are no more pending requests or when a specified timeout has passed (whichever comes first).

Parameters
int$sTimeoutTimeout for the loop. If NULL, there is no time limit.
int$usTimeoutMicroseconds to add to the time limit.
Returns
bool TRUE when there are any more pending requests, FALSE otherwise.
See also
extractNewResponses()
getPendingRequestsCount()

Definition at line 539 of file Client.php.

send ( Request  $request)
protected

Sends a request to RouterOS.

Parameters
Request$requestThe request to send.
Returns
$this The client object.
See also
sendSync()
sendAsync()

Definition at line 765 of file Client.php.

sendAsync ( Request  $request,
  $callback = null 
)

Sends a request and waits for responses.

Parameters
Request$requestThe request to send.
callback$callbackOptional. A function that is to be executed when new responses for this request are available. The callback takes two parameters. The Response object as the first, and the 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.
Returns
$this The client object.
See also
completeRequest()
loop()
cancelRequest()

Definition at line 354 of file Client.php.

sendSync ( Request  $request)

Sends a request and gets the full response.

Parameters
Request$requestThe request to send.
Returns
ResponseCollection The received responses as a collection.
See also
sendAsync()
close()

Definition at line 424 of file Client.php.

setCharset (   $charset,
  $charsetType = Communicator::CHARSET_ALL 
)

Sets the charset(s) for this connection.

Sets the charset(s) for this connection. The specified charset(s) will be used for all future requests and responses. When sending, Communicator::CHARSET_LOCAL is converted to Communicator::CHARSET_REMOTE, and when receiving, Communicator::CHARSET_REMOTE is converted to Communicator::CHARSET_LOCAL. Setting NULL to either charset will disable charset convertion, and data will be both sent and received "as is".

Parameters
mixed$charsetThe charset to set. If $charsetType is 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.
int$charsetTypeWhich charset to set. Valid values are the Communicator::CHARSET_* constants. Any other value is treated as Communicator::CHARSET_ALL.
Returns
string|array The old charset. If $charsetType is Communicator::CHARSET_ALL, the old values will be returned as an array with the types as keys, and charsets as values.
See also
Communicator::setDefaultCharset()

Definition at line 313 of file Client.php.

setStreamingResponses (   $streamingResponses)

Sets response streaming setting.

Sets whether future responses are streamed. If responses are streamed, the argument values are returned as streams instead of strings. This is particularly useful if you expect a response that may contain one or more very large words.

Parameters
bool$streamingResponsesWhether to stream future responses.
Returns
bool The previous value of the setting.
See also
isStreamingResponses()

Definition at line 677 of file Client.php.

Field Documentation

$callbacks = array()
protected

Definition at line 87 of file Client.php.

$com
protected

Definition at line 69 of file Client.php.

$pendingRequestsCount = 0
protected

Definition at line 74 of file Client.php.

$registry = null
protected

Definition at line 93 of file Client.php.

$responseBuffer = array()
protected

Definition at line 81 of file Client.php.

const FILTER_ALL = 3

Used in static::isRequestActive() to indicate no limit in search.

Definition at line 64 of file Client.php.

const FILTER_BUFFER = 2

Used in static::isRequestActive() to limit search only to requests that use the buffer.

Definition at line 60 of file Client.php.

const FILTER_CALLBACK = 1

Used in static::isRequestActive() to limit search only to requests that have a callback.

Definition at line 55 of file Client.php.


The documentation for this class was generated from the following file: