PHP Classes

File: daemon.php

Recommend this page to a friend!
  Classes of sunson   Simple TCP Daemon   daemon.php   Download  
File: daemon.php
Role: Example script
Content type: text/plain
Description: Script that starts a simple daemon
Class: Simple TCP Daemon
Simple TCP Daemon
Author: By
Last change: 'Kural' command needs a script (that outputs a verse from the famous tamil literature - Thirukural). This has been removed now.
Date: 21 years ago
Size: 1,617 bytes
 

Contents

Class file image Download
#!/usr/lib/cgi-bin/php4 -q
<?

require ("class.daemon.php");

$dmn = new daemon;
$dmn->setAddress ('127.0.0.1');
$dmn->setPort (19123);
$dmn->Header = "GNUPHPtial server (v0.0.1b) (Debian GNU/Linux)";

$dmn->start (); //this routine creates a socket

$dmn->setCommands (array ("help", "quit", "date", "shutdown"));
//list of valid commands that must be accepted by the server

$dmn->CommandAction ("help", "command_handler"); //add callback
$dmn->CommandAction ("quit", "command_handler"); // by calling
$dmn->CommandAction ("date", "command_handler"); //this routine
$dmn->CommandAction ("shutdown", "command_handler");
$dmn->listen (); //from here... your program will enter an endless loop
//until manually broken. This example contains a shutdown command that
//would shutdown this daemon.

function command_handler ($command, $arguments, $dmn) {
        switch (
$command) {
        case
'HELP':
               
//commands are converted to uppercase by default. If you want to
                //disable this, look into tokenise().
               
$commands = implode (' ', $dmn->valid_commands);
               
$dmn->Println ('Valid Commands: ');
               
$dmn->Println ($commands);
                return
true;
                break;
        case
'QUIT':
                return
false;
                break;
        case
'DATE':
               
$dmn->Println (date ("H:i:s d/m/Y"));
                return
true;
                break;
        case
'SHUTDOWN':
               
$dmn->shutdown ();
                exit;
                break;
        }
}

?>