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

Public Member Functions

 __construct ($host, $port=8728, $persist=false, $timeout=null, $key= '', $crypto=T\NetworkStream::CRYPTO_OFF, $context=null)
 Creates a new connection with the specified options. More...
 
 __invoke ($string=null)
 A shorthand gateway. More...
 
 setCharset ($charset, $charsetType=self::CHARSET_ALL)
 Sets the charset(s) for this connection. More...
 
 getCharset ($charsetType)
 Gets the charset(s) for this connection. More...
 
 getTransmitter ()
 Gets the transmitter for this connection. More...
 
 sendWord ($word)
 Sends a word. More...
 
 sendWordFromStream ($prefix, $stream)
 Sends a word based on a stream. More...
 
 getNextWord ()
 Get the next word in queue as a string. More...
 
 getNextWordAsStream ()
 Get the next word in queue as a stream. More...
 
 close ()
 Closes the opened connection, even if it is a persistent one. More...
 

Static Public Member Functions

static isSeekableStream ($var)
 Checks whether a variable is a seekable stream resource. More...
 
static iconvStream ($inCharset, $outCharset, $stream)
 Uses iconv to convert a stream from one charset to another. More...
 
static setDefaultCharset ($charset, $charsetType=self::CHARSET_ALL)
 Sets the default charset(s) for new connections. More...
 
static getDefaultCharset ($charsetType)
 Gets the default charset(s). More...
 
static seekableStreamLength ($stream)
 Gets the length of a seekable stream. More...
 
static encodeLength ($length)
 Encodes the length as requred by the RouterOS API. More...
 
static decodeLength (T\Stream $trans)
 Decodes the lenght of the incoming message. More...
 

Data Fields

const CHARSET_ALL = -1
 Used when getting/setting all (default) charsets. More...
 
const CHARSET_REMOTE = 0
 Used when getting/setting the (default) remote charset. More...
 
const CHARSET_LOCAL = 1
 Used when getting/setting the (default) local charset. More...
 

Static Protected Member Functions

static verifyLengthSupport ($length)
 Verifies that the length is supported. More...
 

Protected Attributes

 $charsets = array()
 
 $trans
 

Static Protected Attributes

static $defaultCharsets
 

Detailed Description

Definition at line 44 of file Communicator.php.

Constructor & Destructor Documentation

__construct (   $host,
  $port = 8728,
  $persist = false,
  $timeout = null,
  $key = '',
  $crypto = T\NetworkStream::CRYPTO_OFF,
  $context = null 
)

Creates a new connection with the specified options.

Parameters
string$hostHostname (IP or domain) of the RouterOS server.
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$keyA string that uniquely identifies 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
sendWord()

Definition at line 113 of file Communicator.php.

Member Function Documentation

__invoke (   $string = 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
string$stringA string of the word to send, or NULL to get the next word as a string.
Returns
int|string If a string is provided, returns the number of bytes sent, otherwise retuns the next word as a string.

Definition at line 175 of file Communicator.php.

close ( )

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

Returns
bool TRUE on success, FALSE on failure.

Definition at line 667 of file Communicator.php.

static decodeLength ( T\Stream  $trans)
static

Decodes the lenght of the incoming message.

Decodes the lenght of the incoming message, as specified by the RouterOS API.

Parameters
T\Stream$transThe transmitter from which to decode the length of the incoming message.
Returns
int The decoded length.

Definition at line 609 of file Communicator.php.

static encodeLength (   $length)
static

Encodes the length as requred by the RouterOS API.

Parameters
int$lengthThe length to encode.
Returns
string The encoded length.

Definition at line 487 of file Communicator.php.

getCharset (   $charsetType)

Gets the charset(s) for this connection.

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

Definition at line 359 of file Communicator.php.

static getDefaultCharset (   $charsetType)
static

Gets the default charset(s).

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

Definition at line 279 of file Communicator.php.

getNextWord ( )

Get the next word in queue as a string.

Get the next word in queue as a string, after automatically decoding its length.

Returns
string The word.
See also
close()

Definition at line 529 of file Communicator.php.

getNextWordAsStream ( )

Get the next word in queue as a stream.

Get the next word in queue as a stream, after automatically decoding its length.

Returns
resource The word, as a stream.
See also
close()

Definition at line 567 of file Communicator.php.

getTransmitter ( )

Gets the transmitter for this connection.

Returns
T The transmitter for this connection.

Definition at line 370 of file Communicator.php.

static iconvStream (   $inCharset,
  $outCharset,
  $stream 
)
static

Uses iconv to convert a stream from one charset to another.

Parameters
string$inCharsetThe charset of the stream.
string$outCharsetThe desired resulting charset.
resource$streamThe stream to convert. The stream is assumed to be seekable, and is read from its current position to its end, after which, it is seeked back to its starting position.
Returns
resource A new stream that uses the $out_charset. The stream is a subset from the original stream, from its current position to its end, seeked at its start.

Definition at line 210 of file Communicator.php.

static isSeekableStream (   $var)
static

Checks whether a variable is a seekable stream resource.

Parameters
mixed$varThe value to check.
Returns
bool TRUE if $var is a seekable stream, FALSE otherwise.

Definition at line 188 of file Communicator.php.

static seekableStreamLength (   $stream)
static

Gets the length of a seekable stream.

Gets the length of a seekable stream.

Parameters
resource$streamThe stream to check. The stream is assumed to be seekable.
Returns
double The number of bytes in the stream between its current position and its end.

Definition at line 296 of file Communicator.php.

sendWord (   $word)

Sends a word.

Sends a word and automatically encodes its length when doing so.

Parameters
string$wordThe word to send.
Returns
int The number of bytes sent.
See also
sendWordFromStream()
getNextWord()

Definition at line 386 of file Communicator.php.

sendWordFromStream (   $prefix,
  $stream 
)

Sends a word based on a stream.

Sends a word based on a stream and automatically encodes its length when doing so. The stream is read from its current position to its end, and then returned to its current position. Because of those operations, the supplied stream must be seekable.

Parameters
string$prefixA string to prepend before the stream contents.
resource$streamThe seekable stream to send.
Returns
int The number of bytes sent.
See also
sendWord()

Definition at line 422 of file Communicator.php.

setCharset (   $charset,
  $charsetType = self::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 words. When sending, self::CHARSET_LOCAL is converted to self::CHARSET_REMOTE, and when receiving, self::CHARSET_REMOTE is converted to self::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 self::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 CHARSET_* constants. Any other value is treated as self::CHARSET_ALL.
Returns
string|array The old charset. If $charsetType is self::CHARSET_ALL, the old values will be returned as an array with the types as keys, and charsets as values.
See also
setDefaultCharset()

Definition at line 329 of file Communicator.php.

static setDefaultCharset (   $charset,
  $charsetType = self::CHARSET_ALL 
)
static

Sets the default charset(s) for new connections.

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

Definition at line 248 of file Communicator.php.

static verifyLengthSupport (   $length)
staticprotected

Verifies that the length is supported.

Verifies if the specified length is supported by the API. Throws a LengthException if that's not the case. Currently, RouterOS supports words up to 0xFFFFFFFF in length, so that's the only check performed.

Parameters
int$lengthThe length to verify.
Returns
void

Definition at line 468 of file Communicator.php.

Field Documentation

$charsets = array()
protected

Definition at line 82 of file Communicator.php.

$defaultCharsets
staticprotected
Initial value:
= array(
self::CHARSET_REMOTE => null,
self::CHARSET_LOCAL => null
)

Definition at line 73 of file Communicator.php.

$trans
protected

Definition at line 87 of file Communicator.php.

const CHARSET_ALL = -1

Used when getting/setting all (default) charsets.

Definition at line 49 of file Communicator.php.

const CHARSET_LOCAL = 1

Used when getting/setting the (default) local charset.

The local charset is the charset in which the data from RouterOS will be returned as. This charset should match the charset of the place the data will eventually be written to.

Definition at line 67 of file Communicator.php.

const CHARSET_REMOTE = 0

Used when getting/setting the (default) remote charset.

The remote charset is the charset in which RouterOS stores its data. If you want to keep compatibility with your Winbox, this charset should match the default charset from your Windows' regional settings.

Definition at line 58 of file Communicator.php.


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