codetruck wrote:I do have the script permanently using a while loop etc.
So the script is running as a command line script!
codetruck wrote:Since my script is running, the data members of the class should be "somewhere" isn't it? I have defined the data member/property that i need to access as "public". Any ideas?
You're going to need to create a method to communicate between the pages, probably using sockets or similar.
How competent a coder are you? Because socket programming isn't for the faint-hearted.
You'll need the firstpage.php script to Initialise a "listener" for telnet connections or external requests,
Code: Select all
// Initialise the Listener for telnet connections or external requests for information about the
// controller status
$this->_listenerHost = '127.0.0.1';
$this->_listenerPort = 81;
$this->_maxConnections = 5;
if ($this->_controlSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) {
if (socket_set_option($this->_controlSocket, SOL_SOCKET,SO_REUSEADDR, 1)) {
// Bind it to our external controller port
if (socket_bind($this->_controlSocket, $this->_listenerHost, $this->_listenerPort)) {
// Start listening, though we won't actually accept any messages until
// we get into the main processing loop ($this->go())
if ($result = socket_listen($this->_controlSocket, $this->_maxConnections)) {
// Store details of the listener socket in our socket arrays for future reference
$this->_activeSockets = array($this->_controlSocket);
$this->_clientInputBuffer = array('');
$this->_clientInputBufferFull = array(False);
$this->_clientLastCommunication = array(time());
}
}
}
}
Then you need your while loop to wait for any request for data from secondpage.php (or any other page scripts). I'm not going to list the code for that here, as it's quite large, especially as you need a decent chunk of error trapping as well; but when the script receives a 'requestData' query, it should return the data that it is holding for you.
Finally, secondpage.php needs modifying to make a call against firstpage.php
Code: Select all
$IPConnection = new IPC($hostIPAddress,$serverListenerPort);
if (!$IPConnection->isErrorSet()) {
$IPConnection->sendMessage('requestData');
$data = $IPConnection->waitForResponse();
$IPConnection->closeConnection();
}
I have an IPC (inter-process communication) class that I use for this.
Code: Select all
class IPC extends baseClass
{
// --- ATTRIBUTES ---
/**
* @access private
* @var string
*/
private $_loggerName = 'IPCInterface';
/**
* @access private
* @var string
*/
private $_os = NULL;
/**
* @access private
* @var resource
*/
private $_socket = NULL;
/**
* @access private
* @var boolean
*/
private $_socketConnected = False;
// --- OPERATIONS ---
public function sendMessage($message)
{
socket_write($this->_socket,$message."\n");
} // function sendMessage()
public function waitForResponse()
{
$clientInputBuffer = '';
do {
$clientInputBuffer = $this->_processResponse();
} while ($clientInputBuffer == '');
return $clientInputBuffer;
} // function waitForResponse()
private function _processResponse()
{
$clientInputBufferFull = False;
$clientInputBuffer = '';
while (!$clientInputBufferFull) {
$changedSockets = array($this->_socket);
$num_changed_sockets = socket_select($changedSockets, $write = NULL, $except = NULL, NULL);
$socketErrorCode = 0;
@socket_clear_error($this->_socket);
foreach($changedSockets as $changedSocket) {
// Read the incoming message from the socket
if ($this->_os == 'win32') {
// If we're running on a windows platform we build up the message a character at a time until we get a terminator
// (Carriage Return or Line Feed are the two possible terminators)
$char = socket_read($changedSocket, 1, PHP_BINARY_READ);
$socketErrorCode = socket_last_error($changedSocket);
if ($socketErrorCode == 0) {
if ($char == "\r" || $char == "\n") {
$clientInputBufferFull = True;
} else {
$clientInputBuffer .= $char;
}
}
} else {
// If we're running on a *nix platform we receive the message in a single block with an arbitrary size limit of
// 1024 characters, with Carriage Return or Line Feed as the two possible terminators
$clientInputBuffer = socket_read($changedSocket, 1024, PHP_NORMAL_READ);
$socketErrorCode = socket_last_error($changedSocket);
if ($socketErrorCode == 0) {
$clientInputBufferFull = True;
}
}
// Socket error handler
if ($socketErrorCode <> 0) {
$clientInputBufferFull = False;
}
}
}
return $clientInputBuffer;
} // function _processResponse()
/**
*
* @access constructor
* @param string $serverHostIP IP Address of the server to connect to
* @param integer $serverPort IP Port number to use for the connection
* @param string $connectionName name to use for this connection
* @return void
*
*/
function __construct( $serverHostIP, $serverPort, $connectionName = NULL )
{
$this->_logger = &LoggerManager::getLogger($this->_loggerName);
$this->_logger->info(__CLASS__.'::constructor');
// OS Specific settings
$this->_os = ((strpos(strtolower(PHP_OS), 'win') === 0) || (strpos(strtolower(PHP_OS), 'cygwin') !== false)) ? 'win32' : 'unix';
$this->_logger->info('OS = '.$this->_os);
// Create a connection socket
if (($this->_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === False) {
$this->_logger->info('Failed to create socket');
$this->_setError(socketError,'socket_create()',socket_strerror(socket_last_error()));
return;
}
$this->_logger->info('Created socket');
// Connect socket to our connection server and port
$this->_socketConnected = False;
// Use non-blocking connection so that the connect is immediately set
socket_set_nonblock($this->_socket);
@socket_connect($this->_socket, $serverHostIP, $serverPort);
// Reset to non-blocking so that we can work with it
socket_set_block($this->_socket);
// Use socket_select to trap for errors on the socket_connect, including a timeout trap (5 seconds)
switch(socket_select($r=array($this->_socket), $w=array($this->_socket), $f=array($this->_socket), 5)) {
case 2: $this->_logger->info('Failed to connect to '.$serverHostIP.':'.$serverPort.': Connection refused');
$this->_setError(socketError,'socket_connect()',socket_strerror(socket_last_error()));
$this->closeConnection();
return;
case 1: break;
case 0: $this->_logger->info('Failed to connect to '.$serverHostIP.':'.$serverPort.': Timeout');
$this->_setError(socketError,'socket_connect()',socket_strerror(socket_last_error()));
$this->closeConnection();
return;
}
$this->_socketConnected = True;
$this->_logger->info('Connected to '.$serverHostIP.':'.$serverPort);
// Welcome Message
$this->waitForResponse();
// Connection Details
$this->waitForResponse();
if (!is_null($connectionName)) {
$this->sendMessage(':name '.$connectionName);
// Name Change Response Message
$this->waitForResponse();
}
return;
} // function constructor
/**
* @access destructor
* @return void
*/
function closeConnection()
{
if ($this->_socketConnected) {
$this->sendMessage(':quit');
$this->_socketConnected = False;
sleep(1);
}
if (!is_null($this->_socket)) {
@socket_close($this->_socket);
$this->_socket = Null;
}
} // function closeConnection()
/**
* @access destructor
* @return void
*/
function _destructor()
{
$this->closeConnection();
} // function destructor
} /* end of class IPC */
As all communication is straight binary data, you'd probably need to serialize in firstpage.php before sending the data back, and unserialize it again once secondpage.php has received it.
Note that this isn't complete working code, purely because of the size, but if you're a competent sockets coder, then the principles are straightforward enough
Can I recommend that you change the names of your scripts though: firstpage.php and secondpage.php suggest that this is simple web pages rather than a daemon (you are running firstpage.php as a daemon aren't you, else it'll terminate whenever you log off from the server)