Source for file remoteConsole.php
Documentation is available at remoteConsole.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Luca Mariano <luca.mariano@email.it> |
// +----------------------------------------------------------------------+
// $Id: remoteConsole.php
* This is a base class for a TCP based console listener; it manages by itself
* the standard console operations:
* - help: generated from the options list given at console creation;
* - quit: exit from the console;
* - shutdown: request for server shutdown; close the listener itself and
* calls (if exists) the class method shutdownActionPerformed().
* In order to use the class you must write your own class that extends remoteConsole;
* into your constructor you must call the superclass constructor passing as args
* at least an array of options that the console will support, the application name
* (a random string if you've no idea...) and socket data (address & port to bind);
* other options are avaible, see API for details.
* Then you must implement into your class a method for each avaible option; this method
* must have a name with the syntax: [choice]actionPerformed
* When a client choose the option [choice], this method will be executed
* (if exists), and return string is sent back to the client
* (for example, if you have a menu like L -> list files in current dir
* and you have defined the method LactionPerformed() this
define('REMOTE_CONSOLE_MAXLINE', 1024 ); // how much to read from a socket at a time
class Net_Server_Driver_Multiprocess_remoteConsole extends PHP_Fork {
* A list of all options avaible to clients; the format is
* array('choice1'=>'description 1','choice2'=>'description 2')...
* The name of the application that uses this console
* for reference only, no spaces
* Max number of cuncurrent conenctions
* A list of IP address that are allowed to connect to the console
* all other IP will be banned.
* Array of client sockets in use
function Net_Server_Driver_Multiprocess_remoteConsole ($options, $applicationName, $address = "localhost", $port = "9999", $listenq = 100 , $fd_set_size = 5 , $acl = array ("127.0.0.1"))
// if (!is_array($options))
// die ("You should specify some options for your console!\n");
$this->_options = $options;
$this->_applicationName = $applicationName;
$this->_address = $address;
$this->_listenq = $listenq;
$this->_fd_set_size = $fd_set_size;
$this->PHP_Fork ($this->_applicationName);
if (!$this->_openSocket ()) {
die ("Can't open listening socket\n");
if (($this->_sock = socket_create (AF_INET , SOCK_STREAM , 0 )) < 0 ) {
echo "socket_create() fallito: motivo: " . socket_strerror ($this->_sock) . "\n";
if (false == (@socket_bind ($this->_sock, $this->_address, $this->_port))) {
if (($retcode = socket_listen ($this->_sock, $this->_listenq)) < 0 ) {
echo "socket_listen() fallito: motivo: " . socket_strerror ($retcode) . "\n";
for ($i = 0; $i < $this->_fd_set_size; $i++ )
$this->_clientFD[$i] = null;
$rfds[0 ] = &$this->_sock;
for ($i = 0; $i < $this->_fd_set_size; $i++ ) {
if ($this->_clientFD[$i] != null )
$rfds[$i + 1 ] = $this->_clientFD[$i];
// block indefinitely until we receive a connection...
// if we have a new connection, stick it in the $client array,
//print "listenfd heard something, setting up new client\n";
for ($i = 0; $i < $this->_fd_set_size; $i++ ) {
if ($this->_clientFD[$i] == null ) {
socket_setopt($this->_clientFD[$i], SOL_SOCKET , SO_REUSEADDR , 1 );
socket_getpeername($this->_clientFD[$i], $this->_remote_host[$i], $this->_remote_port[$i]);
if ($i == ($this->_fd_set_size - 1 )) {
print (" too many clients, refusing {$this->_remote_host[$i]}\n" );
socket_write($this->_clientFD[$i], "Sorry, too many clients" , 23 );
} else if (!in_array($this->_remote_host[$i], $this->_acl)) {
//print ("Refusing {$this->_remote_host[$i]} for ACL rules\n");
socket_write($this->_clientFD[$i], "Permission denied" , 17 );
//print "Accepted {$this->_remote_host[$i]}:{$this->_remote_port[$i]} as client[$i]\n";
$talkback = $this->_help ();
// check the clients for incoming data.
for ($i = 0; $i <= $maxi; $i++ ) {
if ($this->_clientFD[$i] == null )
if (in_array($this->_clientFD[$i], $rfds)) {
// the request string is $n
// build the response and put in $talkback
$talkback = $this->_help ();
// quit the whole application
$this->_stopApplication ();
// Here we manage all user-defined menu entries
// if a method exist with the name [choice]actionPerformed()
// this method will be executed and return string is sent
// (for example, if you have a menu like L -> list files in current dir
// and you have defined the method LactionPerformed() this
// method will be called)
$talkback = "\r\n" . $this->$methodName() . "\r\n" . $this->_prompt;
$talkback = "Sorry, feature not yet implemented.\r\n" . $this->_prompt;
$talkback = "\r\n" . $this->_prompt;
// send out $talkback to client
//print "From {$this->_remote_host[$i]}:{$this->_remote_port[$i]}, client[$i]: $n\n";
if ($this->_clientFD[$i] != null ) {
print (time() . " - You should never read this...\n");
$msg = "\r\n" . $this->_applicationName . " Remote Console\r\n";
$msg .= "Avaible commands follows:\r\n\r\n";
foreach($this->_options as $key => $option) {
$msg .= strtoupper($key) . " -> " . $option . "\r\n";
$msg .= "H -> this help\r\n";
$msg .= "Q -> quit console\r\n";
$msg .= "X -> stop application (CAUTION!!!)\r\n\r\n";
function _closeClient ($i)
//print "closing client[$i] ({$this->_remote_host[$i]}:{$this->_remote_port[$i]})\n";
$this->_clientFD[$i] = null;
unset ($this->_remote_host[$i]);
unset ($this->_remote_port[$i]);
function _stopApplication ()
// call to application shutdown...
$this->shutdownActionPerformed ();
$msg = "Remote console is going down!\n";
for ($i = 0; $i < $this->_fd_set_size; $i++ ) {
if ($this->_clientFD[$i] != null ) {
Documentation generated on Mon, 11 Mar 2019 15:47:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|