cvs: pear /Net_Ping Ping.php package.xml
| From: | Jan Lehnardt | Date: | Sun, 26 May 2002 23:06:43 +0000 |
| Subject: | cvs: pear /Net_Ping Ping.php package.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4116@lists.php.net to get a copy of this message | ||
jan Sun May 26 19:06:43 2002 EDT
Modified files:
/pear/Net_Ping Ping.php package.xml
Log:
- rewrite of Net_Ping (Version 2.0)
#please test it on Linux and Windows; I don't have access to those
#OSes. I will bump up the <state> to stable, if no complains
#arrive me in the next two weeks.
Index: pear/Net_Ping/Ping.php diff -u pear/Net_Ping/Ping.php:1.10 pear/Net_Ping/Ping.php:1.11 --- pear/Net_Ping/Ping.php:1.10 Sat May 25 09:37:29 2002 +++ pear/Net_Ping/Ping.php Sun May 26 19:06:43 2002 @@ -15,13 +15,23 @@ // +----------------------------------------------------------------------+ // | Authors: Martin Jansen <mj@php.net> | // | Tomas V.V.Cox <cox@idecnet.com> | +// / Jan Lehnardt <jan@php.net> / // | | // +----------------------------------------------------------------------+ // -// $Id: Ping.php,v 1.10 2002/05/25 13:37:29 jan Exp $ +// $Id: Ping.php,v 1.11 2002/05/26 23:06:43 jan Exp $ + +require_once "PEAR.php"; +require_once "OS/Guess.php"; define('PING_FAILED', 'execution of ping failed'); define('PING_HOST_NOT_FOUND', 'unknown host'); +define('PING_INVALID_ARGUMENTS', 'invalid argument array'); + +/**************************TODO*******************************************/ +/* +return an object with ping results, make raw, ping data optional +*/ /** * Wrapper class for ping calls @@ -31,11 +41,16 @@ * <?php * require_once "Net_Ping/Ping.php"; * $ping = new Net_Ping; -* $ping->ping("yourhosthere"); + $ping->setArgs(array("count" => 5), + "size" => 32), + "ttl" => 512) + ) + ); +* var_dump($ping->ping("example.com")); * ?> * * @author Martin Jansen <mj@php.net> -* @version $Revision: 1.10 $ +* @version $Revision: 1.11 $ * @package Net * @access public */ @@ -44,45 +59,174 @@ /** * Location where the ping program is stored + * * @var string + * @access private */ - var $ping_path = "ping"; + var $ping_path = ""; /** * Array with the result from the ping execution + * * @var array + * @access private */ var $result = array(); + + /** + * OS_Guess instance + * + * @var object + * @access private + */ + var $OS_Guess = ""; + + /** + * OS_Guess->getSysname result + * + * @var string + * @access private + */ + var $sysname = ""; + + /** + * Ping command arguments + * + * @var array + * @access private + */ + var $args = array(); + + /** + * Indicates if an empty array was given to setArgs (not used yet) + * + * @var boolean + * @access private + */ + var $noArgs = true; /** - * Execute ping + * Contains the argument->option relation + * + * @var array + * @access private + */ + var $argRelation = array(); + + + /** + * Constructor for the Class * - * @param string $host hostname - * @param int $count count of the pings - * @param int $packet_size ping packet size - * @param bool $quiet to set or not the "-q" ping param - * @param int $maxwait seconds to wait for a response - * @return mixed String on error or array with the result + * @access private + */ + function Net_Ping() + { + $this->OS_Guess = new OS_Guess; + $this->sysname = $this->OS_Guess->getSysname(); + + $this->setPingPath(); + $this->initArgRelation(); + } + + /** + * Set the arguments array + * + * @param array $args Hash with options + * @return mixed true or PEAR_error * @access public */ - function ping($host, $counts = 5, $packet_size = 32, $quiet = false, $maxwait = 10) + function setArgs($args) { - $q = $quiet ? '-q' : ''; - switch(exec("uname")) { + if (!is_array($args)) { + return PEAR::raiseError(PING_INVALID_ARGUMENTS); + } - case "FreeBSD": - $cmd = $this->ping_path." $q -c $counts -t $maxwait $host"; - break; - - case "HP-UX": - $cmd = $this->ping_path." $host $packet_size $counts"; - break; - - default: - $cmd = $this->ping_path." $q -c $counts -s $packet_size -w $maxwait $host"; - break; + /* accept empty arrays, but set flag*/ + if (0 == count($args)) { + $this->noArgs = true; + } else { + $this->noArgs = false; + } + + $this->args = $args; + + return true; + } + + /** + * Sets the system's path to the ping binary + * + * @access private + */ + function setPingPath() + { + if ("windows" == $this->sysname) { + $this->ping_path = "ping"; + } else { + $this->ping_path = exec("which ping"); /* FIXME: windows */ } + + } + + /** + * Creates the argument list according to platform differences + * + * @return string Argument line + * @access private + */ + function createArgList() + { + $ret = ""; + + $timeout = ""; + $iface = ""; + $ttl = ""; + $count = ""; + $quiet = ""; + $size = ""; + + foreach($this->args AS $option => $value) { + if(!empty($option) && NULL != $this->argRelation[$this->sysname][$option]) { + ${$option} = $this->argRelation[$this->sysname][$option]." ".$value." "; + } + } + + switch($this->sysname) { + case "freebsd": + return $quiet.$count.$ttl.$timeout; + break; + + case "netbsd": + return $quiet.$count.$iface.$size.$ttl.$timeout; + break; + + case "linux": + return $quiet.$count.$ttl.$size.$timeout; + break; + + case "windows": + return $count.$ttl.$timeout; + break; + + default: + return ""; + break; + } + } + + /** + * Execute ping + * + * @param string $host hostname + * @return mixed String on error or array with the result + * @access public + */ + function ping($host) + { + + $argList = $this->createArgList(); + $cmd = $this->ping_path." ".$argList." ".$host; exec($cmd, $result); if (!is_array($result)) { @@ -132,6 +276,75 @@ } } return true; + } + + /** + * Creates the argument list according to platform differences + * + * @return string Argument line + * @access private + */ + function initArgRelation() + { + $this->argRelation = array( + "freebsd" => array ( + "timeout" => "-t", + "ttl" => "-m", + "count" => "-c", + "quiet" => "-q", + "size" => NULL, + "iface" => NULL + ), + + "netbsd" => array ( + "timeout" => "-w", + "iface" => "-I", + "ttl" => "-T", + "count" => "-c", + "quiet" => "-q", + "size" => "-s" + ), + + "openbsd" => array ( + "timeout" => "-w", + "iface" => "-I", + "ttl" => "-t", + "count" => "-c", + "quiet" => "-q", + "size" => "-s" + ), + + /* we don't konw yet whats darwin's signature + + "darwin" => array ( + "timeout" => "-w", + "iface" => "-I", + "ttl" => "-t", + "count" => "-c", + "quiet" => "-q", + "size" => NULL + ), +*/ + "linux" => array ( + "timeout" => "-t", + "iface" => NULL, + "ttl" => "-m", + "count" => "-c", + "quiet" => "-q", + "size" => NULL + ), + "windows" => array ( + "timeout" => "-w", + "iface" => NULL, + "ttl" => "-i", + "count" => "-n", + "quiet" => NULL, + "size" => NULL + ) + + ); + + } } ?> Index: pear/Net_Ping/package.xml diff -u pear/Net_Ping/package.xml:1.9 pear/Net_Ping/package.xml:1.10 --- pear/Net_Ping/package.xml:1.9 Tue Apr 9 05:06:35 2002 +++ pear/Net_Ping/package.xml Sun May 26 19:06:43 2002 @@ -18,12 +18,19 @@ <email>cox@idecnet.com</email> <role>developer</role> </maintainer> + <maintainer> + <user>jan</user> + <name>Jan Lehnardt</name> + <email>jan@php.net</email> + <role>lead</role> + </maintainer> </maintainers> <release> - <version>1.0</version> - <date>2002-03-30</date> - <state>stable</state> + <version>2.0</version> + <date>2002-05-27</date> + <state>beta</state> <notes> + * Rewrite to handle platform differences * Added the checkHost() method * Added parameter to specify maximum wait time (-w) </notes>
Index: pear/Net_Ping/Ping.php diff -u pear/Net_Ping/Ping.php:1.10 pear/Net_Ping/Ping.php:1.11 --- pear/Net_Ping/Ping.php:1.10 Sat May 25 09:37:29 2002 +++ pear/Net_Ping/Ping.php Sun May 26 19:06:43 2002 @@ -15,13 +15,23 @@ // +----------------------------------------------------------------------+ // | Authors: Martin Jansen <mj@php.net> | // | Tomas V.V.Cox <cox@idecnet.com> | +// / Jan Lehnardt <jan@php.net> / // | | // +----------------------------------------------------------------------+ // -// $Id: Ping.php,v 1.10 2002/05/25 13:37:29 jan Exp $ +// $Id: Ping.php,v 1.11 2002/05/26 23:06:43 jan Exp $ + +require_once "PEAR.php"; +require_once "OS/Guess.php"; define('PING_FAILED', 'execution of ping failed'); define('PING_HOST_NOT_FOUND', 'unknown host'); +define('PING_INVALID_ARGUMENTS', 'invalid argument array'); + +/**************************TODO*******************************************/ +/* +return an object with ping results, make raw, ping data optional +*/ /** * Wrapper class for ping calls @@ -31,11 +41,16 @@ * <?php * require_once "Net_Ping/Ping.php"; * $ping = new Net_Ping; -* $ping->ping("yourhosthere"); + $ping->setArgs(array("count" => 5), + "size" => 32), + "ttl" => 512) + ) + ); +* var_dump($ping->ping("example.com")); * ?> * * @author Martin Jansen <mj@php.net> -* @version $Revision: 1.10 $ +* @version $Revision: 1.11 $ * @package Net * @access public */ @@ -44,45 +59,174 @@ /** * Location where the ping program is stored + * * @var string + * @access private */ - var $ping_path = "ping"; + var $ping_path = ""; /** * Array with the result from the ping execution + * * @var array + * @access private */ var $result = array(); + + /** + * OS_Guess instance + * + * @var object + * @access private + */ + var $OS_Guess = ""; + + /** + * OS_Guess->getSysname result + * + * @var string + * @access private + */ + var $sysname = ""; + + /** + * Ping command arguments + * + * @var array + * @access private + */ + var $args = array(); + + /** + * Indicates if an empty array was given to setArgs (not used yet) + * + * @var boolean + * @access private + */ + var $noArgs = true; /** - * Execute ping + * Contains the argument->option relation + * + * @var array + * @access private + */ + var $argRelation = array(); + + + /** + * Constructor for the Class * - * @param string $host hostname - * @param int $count count of the pings - * @param int $packet_size ping packet size - * @param bool $quiet to set or not the "-q" ping param - * @param int $maxwait seconds to wait for a response - * @return mixed String on error or array with the result + * @access private + */ + function Net_Ping() + { + $this->OS_Guess = new OS_Guess; + $this->sysname = $this->OS_Guess->getSysname(); + + $this->setPingPath(); + $this->initArgRelation(); + } + + /** + * Set the arguments array + * + * @param array $args Hash with options + * @return mixed true or PEAR_error * @access public */ - function ping($host, $counts = 5, $packet_size = 32, $quiet = false, $maxwait = 10) + function setArgs($args) { - $q = $quiet ? '-q' : ''; - switch(exec("uname")) { + if (!is_array($args)) { + return PEAR::raiseError(PING_INVALID_ARGUMENTS); + } - case "FreeBSD": - $cmd = $this->ping_path." $q -c $counts -t $maxwait $host"; - break; - - case "HP-UX": - $cmd = $this->ping_path." $host $packet_size $counts"; - break; - - default: - $cmd = $this->ping_path." $q -c $counts -s $packet_size -w $maxwait $host"; - break; + /* accept empty arrays, but set flag*/ + if (0 == count($args)) { + $this->noArgs = true; + } else { + $this->noArgs = false; + } + + $this->args = $args; + + return true; + } + + /** + * Sets the system's path to the ping binary + * + * @access private + */ + function setPingPath() + { + if ("windows" == $this->sysname) { + $this->ping_path = "ping"; + } else { + $this->ping_path = exec("which ping"); /* FIXME: windows */ } + + } + + /** + * Creates the argument list according to platform differences + * + * @return string Argument line + * @access private + */ + function createArgList() + { + $ret = ""; + + $timeout = ""; + $iface = ""; + $ttl = ""; + $count = ""; + $quiet = ""; + $size = ""; + + foreach($this->args AS $option => $value) { + if(!empty($option) && NULL != $this->argRelation[$this->sysname][$option]) { + ${$option} = $this->argRelation[$this->sysname][$option]." ".$value." "; + } + } + + switch($this->sysname) { + case "freebsd": + return $quiet.$count.$ttl.$timeout; + break; + + case "netbsd": + return $quiet.$count.$iface.$size.$ttl.$timeout; + break; + + case "linux": + return $quiet.$count.$ttl.$size.$timeout; + break; + + case "windows": + return $count.$ttl.$timeout; + break; + + default: + return ""; + break; + } + } + + /** + * Execute ping + * + * @param string $host hostname + * @return mixed String on error or array with the result + * @access public + */ + function ping($host) + { + + $argList = $this->createArgList(); + $cmd = $this->ping_path." ".$argList." ".$host; exec($cmd, $result); if (!is_array($result)) { @@ -132,6 +276,75 @@ } } return true; + } + + /** + * Creates the argument list according to platform differences + * + * @return string Argument line + * @access private + */ + function initArgRelation() + { + $this->argRelation = array( + "freebsd" => array ( + "timeout" => "-t", + "ttl" => "-m", + "count" => "-c", + "quiet" => "-q", + "size" => NULL, + "iface" => NULL + ), + + "netbsd" => array ( + "timeout" => "-w", + "iface" => "-I", + "ttl" => "-T", + "count" => "-c", + "quiet" => "-q", + "size" => "-s" + ), + + "openbsd" => array ( + "timeout" => "-w", + "iface" => "-I", + "ttl" => "-t", + "count" => "-c", + "quiet" => "-q", + "size" => "-s" + ), + + /* we don't konw yet whats darwin's signature + + "darwin" => array ( + "timeout" => "-w", + "iface" => "-I", + "ttl" => "-t", + "count" => "-c", + "quiet" => "-q", + "size" => NULL + ), +*/ + "linux" => array ( + "timeout" => "-t", + "iface" => NULL, + "ttl" => "-m", + "count" => "-c", + "quiet" => "-q", + "size" => NULL + ), + "windows" => array ( + "timeout" => "-w", + "iface" => NULL, + "ttl" => "-i", + "count" => "-n", + "quiet" => NULL, + "size" => NULL + ) + + ); + + } } ?> Index: pear/Net_Ping/package.xml diff -u pear/Net_Ping/package.xml:1.9 pear/Net_Ping/package.xml:1.10 --- pear/Net_Ping/package.xml:1.9 Tue Apr 9 05:06:35 2002 +++ pear/Net_Ping/package.xml Sun May 26 19:06:43 2002 @@ -18,12 +18,19 @@ <email>cox@idecnet.com</email> <role>developer</role> </maintainer> + <maintainer> + <user>jan</user> + <name>Jan Lehnardt</name> + <email>jan@php.net</email> + <role>lead</role> + </maintainer> </maintainers> <release> - <version>1.0</version> - <date>2002-03-30</date> - <state>stable</state> + <version>2.0</version> + <date>2002-05-27</date> + <state>beta</state> <notes> + * Rewrite to handle platform differences * Added the checkHost() method * Added parameter to specify maximum wait time (-w) </notes>