PHP Classes

File: test_daemon.php

Recommend this page to a friend!
  Classes of Daniel Andres Marjos   clsDaemonize   test_daemon.php   Download  
File: test_daemon.php
Role: Example script
Content type: text/plain
Description: Example Script
Class: clsDaemonize
Create and manage daemon processes on Unix systems
Author: By
Last change:
Date: 20 years ago
Size: 3,234 bytes
 

Contents

Class file image Download
#!/opt/php/bin/php -q
<?

/*

This scripts will show the implementation of both clsDaemonize and clsTCPServer

*/

include("clsSocket.php");
include(
"clsDaemonize.php");

/*
Various constants needed for clsTCPServer
*/
define('MAXLINE', 2048); // how much to read from a socket at a time
define('LISTENQ', 30); // listening queue
define('PORT', 10000); // the default port to run on
define('FD_SETSIZE', 30); // file descriptor set size (max number of concurrent clients)...

/*
Function to be called when a connection is received.
*/
function welcome($childSocket) {
    global
$myDaemon;
   
$myDaemon->writeLog("Connection established.[".$childSocket->sckRemoteAddress.":".$childSocket->sckRemotePort."]");
}

/*
Function to be called when a connection is closed.
*/
function byebye($childSocket) {
    global
$myDaemon;
   
$myDaemon->writeLog("Connection closed [".$childSocket->sckRemoteAddress.":".$childSocket->sckRemotePort."].");
}

function
hex2dec($hex) {

   
$nib=array(
   
"0"=>0,
   
"1"=>1,
   
"2"=>2,
   
"3"=>3,
   
"4"=>4,
   
"5"=>5,
   
"6"=>6,
   
"7"=>7,
   
"8"=>8,
   
"9"=>9,
   
"A"=>10,
   
"B"=>11,
   
"C"=>12,
   
"D"=>13,
   
"E"=>14,
   
"F"=>15
   
);

   
$hi1=(($nib[substr($hex,0,1)]*16)+$nib[substr($hex,1,1)])*256+(($nib[substr($hex,2,1)]*16)+$nib[substr($hex,3,1)]);
   
    return
$hi1;
}

/*
Function to be called to handle the incoming connection
*/
function handle_connection($childSocket) {
    global
$myTCPServer,$myDaemon;
   
    do {
        if (
false === ($buf = $childSocket->read_normal(MAXLINE))) {
           
$myDaemon->writeLog($childSocket->sckError);
        }
       
        if (
$buf == 'shutdown') {
           
$myTCPServer->closeAll();
           
$myDaemon->shutdown();
            exit(
0);
            break;
        }
       
        if (
$buf == 'quit') {
           
$myTCPServer->close_client($childSocket->sckIndex);
            break;
        }

       
$myDaemon->writeLog("Received: $buf");
               
       
$childSocket->write("$buf to you!\n");
    } while (
true);
}

/*
Signal handling for daemon.
*/
function sig_handler($signal) {
    global
$myDaemon,$myTCPServer;

    switch (
$signal) {
        case
SIGTERM:
           
$myTCPServer->closeAll();
           
$myDaemon->shutdown();
            exit(
0);
            break;
        case
SIGHUP:
            break;
    }

}

/*
Function to be forked to when daemon starts execution
*/
function start() {
    global
$myTCPServer,$myDaemon;
   
   
$myDaemon->writeLog("Listening for connections");
   
$myTCPServer->start();

}

$myTCPServer=new clsTCPServer("192.168.1.4", PORT, FD_SETSIZE); // Create an instance of class clsTCPServer
$myTCPServer->initServer(AF_INET, SOCK_STREAM, SOL_TCP, LISTENQ); // Initialization of server features.
$myTCPServer->srvMaxLineLen=MAXLINE; // Sets the max length to be read from socket
$myTCPServer->setInitHandler("welcome"); // Sets connection open handler to function "Welcome"
$myTCPServer->setCloseHandler("byebye"); // Sets connection close handler to function "ByeBye"
$myTCPServer->setConnectionHandler("handle_connection"); // Sets conenction flow handler to function "handle_connection"

$myDaemon=new clsDaemonize(); // Creates an instance of clsDaemonize
$myDaemon->setup_sighandler(SIGTERM,"sig_handler"); // Sets various signal handlers
$myDaemon->setup_sighandler(SIGHUP,"sig_handler");
$myDaemon->fork("start"); // Start daemonization process

?>