PHP Classes

File: foo_callback_server.php

Recommend this page to a friend!
  Packages of MC Breit   Simple Server   foo_callback_server.php   Download  
File: foo_callback_server.php
Role: Example script
Content type: text/plain
Description: A funny FOO server, demonstrates how to use the Callback based interface of SimpleServerCallback.class.php
Class: Simple Server
Implement TCP socket servers
Author: By
Last change:
Date: 20 years ago
Size: 4,948 bytes
 

 

Contents

Class file image Download
<?php
/***************************************************************************
 *                          SimpleServer Example 3
 *                          ----------------------
 *   begin                : Saturday, Dec 18, 2004 - 17:30
 *   copyright            : (C) 2004 MC Breit
 *   email                : support@mcb.cc - MCB.CC - Free and Open Sources
 *   last modified        : 18/12/04 - 18:01 - MC Breit
 *   version              : 1.0.0
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This example will show how easy to create your own Callback Based Server
 *   
 *   What should our FOO Server do?
 *   Our FOO Server should listen to port 8000 on the local ip address
 *   127.0.0.1 and waiting for clients conntcting in.
 *   While a client is connected, it should wait that the client is
 *   sending a "\r\n" and the return the message the cleint sended before.
 *   If the client entered "SHUTDOWN" it should close the connection to the
 *   client and say "Bye bye..".
 *   When the client types "FOO" it should print "BAR" and remove the handle
 *   of foo, but should initiate a handle to "BAR". Then if "BAR" was typed
 *   by the client the same should happend in reverse was..
 *
 *   To test the server, you can run it using the console and run telnet
 *   in an other console window.
 *   Then connect to "127.0.0.1 port 8000" with telnet and type anything.
 *   For example under Windows you can exec "cmd" and then type in:
 *      C:\> telnet 127.0.0.1 8000
 *   After that you should see the welcome message.
 *   Now, to test the server you can type the following:
 *      FOO is cool!
 *      FOO Rulez, yeha!
 *      BAR is nice to, well..
 *      FOO
 *      BAR BAR BAR BAR!
 *      FAZ
 *      SHUTDOWN
 *   Should produce some output like these:
 *      >> C:\> telnet 127.0.0.1 8000
 *         Welcome to my FOO Server!
 *      >> FOO is cool!
 *         BAR: is cool!
 *      >> FOO Rulez, yeha!
 *         Unknown signal: FOO
 *      >> BAR is nice too, well..
 *         FOO: is nice too, well..
 *      >> FOO
 *         BAR:
 *      >> BAR BAR BAR BAR!
 *         FOO: BAR BAR BAR!
 *      >> FAZ
 *         Unknown signal: FAZ
 *      >> SHUTDOWN
 *         ByeBye
 *
 *         Connection lost.
 *         C:\> _
 *
 *   !! Important: Dont forget your firewall settings while testing !!
 ***************************************************************************/
 
//Kill the max_ex_time (on pure installations not needed)
set_time_limit(0);

//Including the main class..
include('SimpleServerCallback.class.php');

//Creating a new server object using port 8000 and the local ip "127.0.0.1".
$srv = &new SimpleServerCallback(8000, '127.0.0.1');

//Register our callbacks..
$srv->register_callback(SSRV_CBS_CON, 'say_hello'); //The connected in signal..
$srv->register_callback('FOO', 'callback_foo'); //The "FOO" signal..
$srv->register_callback('SHUTDOWN', 'callback_shutdown'); //THe "SHUTDOWN" signal..
$srv->register_callback(SSRV_CBS_DEF, 'callback_default'); //All the unknown signals..

//Main loop to listen for clients..
$srv->main();

//Any errors inside the main loop?
if( $error = $srv->get_error_str() )
{
  print $error;
}

//The functions we use to handle the callbacks..
function say_hello(&$srv, $signal, $params)
{
  //Say hello to the new client..
  $srv->put('Welcome to my FOO Server!'."\r\n");
  return TRUE;
}

function callback_foo(&$srv, $signal, $params)
{
  //Return "BAR: .."
  $srv->put('BAR: '."$params\r\n");
  //Register the BAR signal..
  $srv->register_callback('BAR', 'callback_bar');
  //Return FALSE to unregister the own signal..
  return FALSE;
}

function callback_bar(&$srv, $signal, $params)
{
  //Return "FOO: .."
  $srv->put('FOO: '."$params\r\n");
  //Register the FOO signal..
  $srv->register_callback('FOO', 'callback_foo');
  //Return FALSE to unregister the own signal..
  return FALSE;
}

function callback_shutdown(&$srv, $signal, $params)
{
  //Send the Bye bye.. message to client..
  $srv->put('ByeBye');
  //Quit from main loop..
  $srv->main_quit();

  return TRUE;
}

function callback_default(&$srv, $signal, $params)
{
  //Tell the client that I dont know about this signal..
  $srv->put('Unknown signal: '.$signal."\r\n");
  
  return TRUE;
}

?>