All pastes #179665 Raw Edit

Unnamed

public text v1 · immutable
#179665 ·published 2006-09-22 11:58 UTC
rendered paste body
ioe exposed api:

class ioe {
  ioe_client = function connect ($host, $port, $callbacks);
  //connect to a host, $object and $callbacks are used to trigger functions on an instance of an object
  $callbacks = array (
    "connected" => "function()",
    "read" => "function($string)",
    "disconnected" => "function()",
  );

  function serv ($port, $callbacks);
  //listen on a port and fire events, note the $client object is passed back when connected
  $callbacks = array (
    "connected" => "function($client, $idx)",
    "disconnected" => "function($idx)"
    "read" => "function($idx, $string)",
    "disconnected" => "function($idx)",
  )

  ioe_event = function event($time, $object, $function, $args = array());
  //$time is either in seconds or a unix timestamp, float or int; $object, $function and $args are the event to fire, returns an $event object

  bool = function delEvent(ioe_event);
  //give it the result from calling event and it will remove the event without firing it, return true on succes, otherwise false

  function go();
  //start running the program! this is the start and end of the program within the main scope; the application will exit when there is nothing left to do
}

class ioe_client {
  function write($string);
  function disconnect();
}

example usage for connecting to servers:
class getWeb {
  private $ioe, $con, $url;
  private $buffer = "";

  function __construct($ioe, $url) {
    $r = parse_url($url);
    $this->host = $r['host'];
    $this->port = (isset($r['port']) ? $r['port'] : 80);
    $this->path = (isset($r['path']) ? $r['path'] : "/");

    $cb = array(
      "read" => array($this, "ioe_read"),
      "connected" => array($this, "ioe_connected"),
      "disconnected" => array($this, "ioe_disconnected"),
    );
    $this->con = $ioe->connect($this->host, $this->port, $cb, 5);
    $this->ioe =& $ioe;
  }

  public function ioe_read($str) {
    $this->buffer .= $str;
  }

  public function ioe_timeout() {
    $this->con->disconnect();
  }

  public function ioe_connected() {
    $this->con->write("GET ".$this->path." HTTP/1.1\r\nHOST: ".$this->host."\r\nConnection: close\r\n\r");
  }

  public function ioe_disconnected() {
    $this->ioe->event(5, array($this, "spam"));
  }

  public function spam() {
    echo $this->buffer;
  }
}

class dostuff {
  private $ioe;
  
  function __construct($ioe) {
    $this->ioe =& $ioe;
    $this->ioe->event(3, array($this, "doYahoo"));
    $this->ioe->event(1, array($this, "doGoogle"));
  }
  public function doGoogle() {
    $google = new getWeb($this->ioe, "http://www.google.co.uk");
  }
  
  public function doYahoo() {
    $yahoo = new getWeb($this->ioe, "http://www.yahoo.co.uk/");
  }
}

$ioe = new ioe;
$dostuff = new dostuff($ioe);
$ioe->go();