This commit is contained in:
Jan Philipp Timme 2010-11-21 22:48:11 +00:00
parent 01018862f7
commit 952af43b97
23 changed files with 3314 additions and 0 deletions

17
AutoLoader.php Normal file
View File

@ -0,0 +1,17 @@
<?php
/**
* Autoloader function.
* All classes will be in Classes/$Package$/$ClassName$.php
* @throws Exception_GeneralException
* @param string $className
* @return void
*/
function __autoload($className) {
$file = "Classes/" . str_replace("_", "/", $className) . ".php";
if(file_exists($file) === TRUE) {
require_once($file);
} else {
throw new Exception_GeneralException("Could not autoload class: '" . $className . "' - File does not exist: '" . $file . "'!", 1289659295);
}
}
?>

View File

@ -0,0 +1,62 @@
<?php
/**
* Abstract class that implements the basic api for any client.
* @author jpt
* @abstract
* @package Client
*/
abstract class Client_AbstractClient {
/**
* @var int
*/
protected $ID;
/**
* @var string
*/
protected $group;
/**
* Contains a reference to the ClientManager in order to change stuff on the fly.
* @var Client_ClientManager
*/
protected $clientManager;
/**
* This function will be the main entry point of any client.
* @param string $data
* @return string
*/
abstract public function processData($data);
/**
* This function will load the given config.
* @param array $config
*/
abstract public function loadConfig($config);
/**
* @param int $id
*/
public function setID($id) {
$this->ID = $id;
}
/**
* @param string $group
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* Injects ClientManager
* @param Client_ClientManager $clientManager
*/
public function injectClientManager($clientManager) {
$this->clientManager = $clientManager;
}
}
?>

View File

@ -0,0 +1,48 @@
<?php
/**
* IrcClient class that contains all the Plugins
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_BotClient extends Client_AbstractClient {
/**
* Default constructor.
*/
function __construct() {
}
/**
* Does all the hard work.
* @param string $data
* @return string
*/
public function processData($data) {
if(strpos($data, "-") !== FALSE ) {
$data = explode("-", $data);
$this->clientManager->sendToID((int) $data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
if(strpos($data, "+") !== FALSE ) {
$data = explode("+", $data);
$this->clientManager->sendToGroup($data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
return $return;
}
/**
* Loads the given configuration.
* @param array $config
* @return void
*/
public function loadConfig($config) {
}
}
?>

View File

@ -0,0 +1,242 @@
<?php
/**
* This class contains the connection handler.
* It handles all the incoming and outgoing data by forwarding it
* to the clients for the specific connection.
* @author jpt
* @package Client
* @depends Connection
*/
class Client_ClientManager {
/**
* @var Connection_ConnectionPool
*/
protected $connectionPool;
/**
* An array that contains all client instances for each connection.
* @var array
*/
protected $clientPool;
/**
* @var array
*/
protected $rawRoutingRules;
/**
* An array that contains all protocols we have clients for.
* @var array
*/
protected $registeredProtocols;
/**
* Contains Connection-attached configurations.
* @var array
*/
protected $configPool;
/**
* Default constructor.
* @param string $linebreak
* @return void
*/
function __construct($linebreak = "\r\n") {
$this->connectionPool = new Connection_ConnectionPool($linebreak);
$this->clientPool = array();
$this->registeredProtocols = array();
$this->rawRoutingRules = array();
$this->configPool = array();
}
/**
* Default destructor. Closes connections before being killed.
* @return void
*/
function __destruct() {
unset($this->clientPool);
unset($this->connectionPool);
}
/**
* @see Connection_ConnectionPool
* @return int
*/
public function countConnections() {
return $this->connectionPool->countConnections();
}
/**
* Main worker function.
* Processes incoming data, calls clients and much more.
* @return void
*/
public function work() {
$connectionHandlers = $this->connectionPool->select();
if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return;
foreach($connectionHandlers["read"] AS $connectionHandler) {
//handle disconnects
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
$this->removeClientForConnectionHandler($connectionHandler);
}
//ne warte mal, das geht nicht...oder so
if($connectionHandler->isListening() === TRUE) {
$this->addClientForConnectionHandler($connectionHandler);
}
//create a client for a connection without one.
if(!isset($this->clientPool[$connectionHandler->getID()])) {
$this->addClientForConnectionHandler($connectionHandler);
}
if(isset($this->clientPool[$connectionHandler->getID()])) {
//let the client process the data, send the results back.
while($data = $connectionHandler->read()) {
$result = $this->clientPool[$connectionHandler->getID()]->processData($data);
if($result !== "") {
$connectionHandler->write($result);
}
}
}
}
}
/**
* TODO: implement this!
* @param unknown_type $from
* @param unknown_type $to
*/
public function addRawRouting($from, $to) {
$this->rawRoutingRules[] = array("");
}
/**
* Calls ConnectionPool in order to create a simple connection.
* @see Connection_ConnectionPool
* @param string $group
* @param boolean $IPv6
* @return Connection_ConnectionHandler
*/
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6);
}
/**
* Closes and removes the connection.
* @param Connection_ConnectionHandler $connectionHandler
*/
public function removeConnection($connectionHandler) {
unset($this->configPool[$connectionHandler->getID()]);
unset($this->clientPool[$connectionHandler->getID()]);
$this->connectionPool->removeConnectionHandler($connectionHandler);
}
/**
* Registers a protocol with a specific client.
* @param string $protocol
* @param string $className
* @return void
*/
public function registerProtocol($protocol, $className) {
$this->registeredProtocols[$protocol] = $className;
}
/**
* Unregisters a protocol.
* @param string $protocol
* @return void
*/
public function unregisterProtocol($protocol) {
unset($this->registeredProtocols[$protocol]);
}
/**
* Searches for the registered client for the given protocol.
* Returns a client instance or void.
* @param string $protocol
* @return mixed
* @throws Exception_GeneralException
*/
protected function createClientForProtocol($protocol) {
//look for the protocol
if(!isset($this->registeredProtocols[$protocol])) {
throw new Exception_GeneralException("No client registered for protocol: '" . $protocol . "'!", 1290271651);
}
$className = $this->registeredProtocols[$protocol];
//look for the class
if(class_exists($className)) {
return new $className();
} else {
throw new Exception_GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773);
}
}
/**
* Adds a client to our client pool.
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
protected function addClientForConnectionHandler($connectionHandler) {
$protocol = $connectionHandler->getProtocol();
//do not try this for raw connections
if($protocol === "RAW") return;
$client = $this->createClientForProtocol($protocol);
if(isset($this->configPool[$connectionHandler->getID()])) {
$client->loadConfig($this->configPool[$connectionHandler->getID()]);
}
$client->injectClientManager($this);
$client->setID($connectionHandler->getID());
$client->setGroup($connectionHandler->getGroup());
$this->clientPool[$connectionHandler->getID()] = $client;
}
/**
* Removes a client from our client pool.
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
protected function removeClientForConnectionHandler($connectionHandler) {
unset($this->clientPool[$connectionHandler->getID()]);
$this->connectionPool->removeConnectionHandler($connectionHandler);
}
/**
* Attaches a configuration to a connection.
* Will overwrite the existing configuration for the connection.
* @param array $config
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
public function attachConfig($config, $connectionHandler) {
$this->configPool[$connectionHandler->getID()] = $config;
}
/**
* Calls Connection_Pool
* @see Connection_ConnectionPool
* @param int $id
* @param string $data
* @return void
*/
public function sendToID($id, $data) {
return $this->connectionPool->writeToID($id, $data);
}
/**
* Calls Connection_Pool
* @see Connection_ConnectionPool
* @param string $group
* @param string $data
* @return void
*/
public function sendToGroup($group, $data) {
return $this->connectionPool->writeToGroup($group, $data);
}
}
?>

View File

@ -0,0 +1,77 @@
<?php
/**
* IrcClient class that contains all the Plugins
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_IrcClient extends Client_AbstractClient {
protected $got_001;
protected $joined;
protected $authed;
protected $nick;
protected $channels;
protected $lines;
/**
* Default constructor.
*/
function __construct() {
$this->nick = "Serena";
$this->channels = array();
$this->lines = 0;
$this->got_001 = FALSE;
$this->joined = FALSE;
$this->authed = FALSE;
}
/**
* Does all the hard work.
* @param string $data
* @return string
*/
public function processData($data) {
//echo "[RECV] ".$data;
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data."\r\n\r\n");
$return = "";
if(strpos($data, "PING :") !== FALSE) $return .= str_replace("PING", "PONG", $data);
if(preg_match("/001/", $data)) $this->got_001 = TRUE;
$this->lines++;
if(!$this->authed && $this->lines > 1) {
$return .= "USER as as as :Asdfg\r\nNICK :" . $this->nick . "\r\n";
$this->authed = TRUE;
}
if($this->got_001 && !$this->joined) {
$this->joined = TRUE;
foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n";
}
if(strpos($data, "hau ab") !== FALSE) {
$return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n";
$return .= "QUIT :lol\r\n";
}
//if($return !== "") echo "[SEND] ".$return;
return $return;
}
/**
* Loads the given configuration.
* @param array $config
* @return void
*/
public function loadConfig($config) {
$this->nick = $config["nick"];
$this->channels = $config["channels"];
$this->userident = $config["userident"];
}
}
?>

View File

@ -0,0 +1,295 @@
<?php
/**
* This class is a decorator for the class SocketHandler.
* It provides a buffer for the SocketHandler, so reading and writing
* a full line won't be that much pain.
* @author jpt
* @package Connection
* @depends Socket
* @depends Misc
*/
class Connection_ConnectionHandler {
/**
* Buffer that contains incoming data.
* Contents were received from the SocketHandler.
* @var Misc_Buffer
*/
protected $buffer_incoming;
/**
* Buffer that contains outgoing data.
* Contents will be sent to the SocketHandler.
* @var Misc_Buffer
*/
protected $buffer_outgoing;
/**
* Contains the instance of the SocketHandler class.
* According to the Liskov substitution principle, decoration pattern must be used in this case.
* @var Socket_SocketHandler
*/
protected $socketHandler;
/**
* A boolean that indicates whether this Connection is a listening server socket or a usual client socket.
* @var boolean
*/
protected $is_server;
/**
* Unique Connection ID.
* @var int
*/
protected $id;
/**
* Connection Group.
* @var string
*/
protected $group;
/**
* @var string
*/
protected $protocol;
/**
* Calls parent constructor.
* @param $socket
* @param $linebreak
* @return void
*/
function __construct($socket, $id, $group = "", $protocol = "", $linebreak = "\r\n") {
$this->buffer_incoming = new Misc_Buffer($linebreak);
$this->buffer_outgoing = new Misc_Buffer($linebreak);
$this->socketHandler = new Socket_SocketHandler($socket);
$this->id = $id;
$this->group = $group;
$this->protocol = $protocol;
$this->is_server = FALSE;
}
/**
* Calls parent destructor.
* @return void
*/
function __destruct() {
unset($this->socketHandler);
}
/**
* @return string
*/
public function getProtocol() {
return $this->protocol;
}
/**
* @return string Connection Group
*/
public function getGroup() {
return $this->group;
}
/**
* @return int Connection ID
*/
public function getID() {
return $this->id;
}
/**
* Returns whether this connection is a listening socket or a general client connection socket.
* @return boolean
*/
public function isServer() {
return $this->is_server;
}
/**
* Reads from SocketHandler, writes into buffer_incoming.
* Returns a boolean that will indicate whether the socket is still okay.
* @throws Exception_SocketException
* @return boolean
*/
public function readToBuffer() {
$data = $this->socketHandler->read();
if($data === "") return FALSE;
$this->buffer_incoming->addData($data);
return TRUE;
}
/**
* Writes the buffer_outgoing to the SocketHandler.
* Returns a boolean that will indicate whether the socket is still okay.
* @throws Exception_SocketException
* @return boolean
*/
public function writeFromBuffer() {
while($this->buffer_outgoing->hasLines()) {
$result = $this->socketHandler->write($this->buffer_outgoing->getNextLine());
if($result === FALSE) return FALSE;
}
return TRUE;
}
/**
* Calls error() on Socket_SocketHandler.
* @throws Socket_SocketExceptions
* @return void
*/
public function handleSocketError() {
$this->socketHandler->error();
}
/**
* Determines whether this ConnectionHandler has data to read.
* @return boolean
*/
public function canRead() {
return $this->buffer_incoming->hasLines();
}
/**
* Determines whether this ConnectionHandler has data to write.
* @return boolean
*/
public function canWrite() {
return $this->buffer_outgoing->hasLines();
}
/**
* Reads new data into buffer_incoming.
* Returns a full line from buffer_incoming.
* @return string
*/
public function read() {
return $this->buffer_incoming->getNextLine();
}
/**
* Writes data into buffer_outgoing.
* Sends data from buffer_outgoing to the SocketHandler.
* @param $data
* @return void
*/
public function write($data) {
$this->buffer_outgoing->addData($data);
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return ressource
*/
public function accept() {
return $this->socketHandler->accept();
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return string
*/
public function getRemoteName() {
return $this->socketHandler->getRemoteName();
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return string
*/
public function getLocalName() {
return $this->socketHandler->getLocalName();
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return void
*/
public function close() {
return $this->socketHandler->close();
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return void
*/
public function connect($address, $port) {
return $this->socketHandler->connect($address, $port);
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return void
*/
public function bind($address, $port) {
return $this->socketHandler->bind($address, $port);
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return void
*/
public function listen() {
$this->is_server = TRUE;
return $this->socketHandler->listen();
}
/**
* @see Socket_SocketHandler
* @return boolean
*/
public function isConnected() {
return $this->socketHandler->isConnected();
}
/**
* @see Socket_SocketHandler
* @return boolean
*/
public function isListening() {
return $this->socketHandler->isListening();
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @throws Exception_SocketException
* @return ressource
*/
public function getSocket() {
return $this->socketHandler->getSocket();
}
/**
* @return Socket_SocketHandler
*/
public function getSocketHandler() {
return $this->socketHandler;
}
/**
* Calls SocketHandler
* @see Socket_SocketHandler
* @return void
*/
public function hasBeenAccepted() {
return $this->socketHandler->hasBeenAccepted();
}
}
?>

View File

@ -0,0 +1,181 @@
<?php
/**
* Connection pool class. Contains the SocketPool.
* @author jpt
* @package Connection
* @depends Socket
*/
class Connection_ConnectionPool {
/**
* SocketPool instance.
* @var Socket_SocketPool
*/
protected $socketPool;
/**
* Contains all ConnectionHandler instances.
* @var array
*/
protected $connectionHandlers;
/**
* @var int Next ID for a new ConnectionHandler
*/
protected $nextID;
/**
* Creates an Instance of SocketPool
* @return void
*/
function __construct($linebreak = "\r\n") {
$this->connectionHandlers = array();
$this->socketPool = new Socket_SocketPool($linebreak);
$this->nextID = 1;
}
/**
* Destroys the SocketPool
* @return void
*/
function __destruct() {
unset($this->socketPool);
}
/**
* Creates a new TcpConnection.
* @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6.
* @return Connection_ConnectionHandler
*/
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
$socket = $this->socketPool->createTcpSocket($IPv6);
$connectionHandler = new Connection_ConnectionHandler($socket, $this->nextID, $group, $protocol);
$this->addConnectionHandler($connectionHandler);
return $connectionHandler;
}
/**
* Adds a ConnectionHandler to the pool.
* @param Connection_ConnectionHandler $add_connectionHandler
* @return void
*/
public function addConnectionHandler($add_connectionHandler) {
array_push($this->connectionHandlers, $add_connectionHandler);
$this->nextID++;
}
/**
* Removes a ConnectionHandler from the pool.
* @param Connection_ConnectionHandler $remove_connectionHandler
* @return void
*/
public function removeConnectionHandler($remove_connectionHandler) {
foreach($this->connectionHandlers AS $key=>$connectionHandler) {
if($connectionHandler === $remove_connectionHandler) {
$this->socketPool->removeSocket($remove_connectionHandler->getSocket());
$remove_connectionHandler->close();
unset($this->connectionHandlers[$key]);
}
}
}
/**
* Returns ConnectionHandler for the given socket ressource.
* @param ressource $socketRessource
* @return Connection_ConnectionHandler
*/
protected function getConnectionHandlerForSocketRessource($socketRessource) {
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->getSocket() === $socketRessource) {
return $connectionHandler;
}
}
}
/**
* Calls select() on SocketPool and updates the ConnectionHandler.
* Will also accept incoming connections and add them to the pool.
* @return array An array containing the Connection_ConnectionHandler for each Socket with new data.
* @throws Exception_GeneralException
* @throws Exception_SocketException
*/
public function select() {
$read = array();
$write = array();
$except = array();
foreach($this->connectionHandlers AS $connectionHandler) {
$connectionSocket = $connectionHandler->getSocket();
$read[] = $connectionSocket;
if($connectionHandler->canWrite() && $connectionHandler->isServer() === FALSE) $write[] = $connectionSocket;
}
$except = $read;
$tempArray = array();
$selectedSockets = $this->socketPool->select($read, $write, $except);
foreach($selectedSockets AS $selectedType=>$selectedArray) { //read, write, except, this loop won't kill performance
foreach($selectedArray AS $socket) {
$connectionHandler = $this->getConnectionHandlerForSocketRessource($socket);
switch($selectedType) {
case "read":
if($connectionHandler->isServer() === FALSE) {
if($connectionHandler->readToBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler);
} else {
$acceptedSocket = $connectionHandler->accept();
$acceptedSocketHandler = new Connection_ConnectionHandler($acceptedSocket, $this->nextID, $connectionHandler->getGroup(), $connectionHandler->getProtocol());
$acceptedSocketHandler->hasBeenAccepted();
$this->addConnectionHandler($acceptedSocketHandler);
}
break;
case "write":
if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler);
break;
case "except":
$connectionHandler->handleSocketError();
break;
default:
throw new Exception_GeneralException("Unknown select type: '" . $selectedType . "'", 1289737080);
break;
}
$tempArray[$selectedType][] = $connectionHandler;
}
}
return $tempArray;
}
/**
* Writes the given data to all sockets in $group.
* @param string $group
* @param string $data
* @throws Exception_SocketException
* @return void
*/
public function writeToGroup($group, $data) {
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->getGroup() === $group && $connectionHandler->isServer() === FALSE) {
$connectionHandler->write($data);
}
}
}
/**
* Writes the given data to the socket with $id.
* @param int $id
* @param string $data
*/
public function writeToID($id, $data) {
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->getID() === $id && $connectionHandler->isServer() === FALSE) {
$connectionHandler->write($data);
}
}
}
/**
* @see Socket_SocketPool
* @return int
*/
public function countConnections() {
return $this->socketPool->countSockets();
}
}
?>

View File

@ -0,0 +1,35 @@
<?php
/**
* General exception class.
* @author jpt
* @package Exception
* @exception
*/
class Exception_GeneralException extends Exception {
/**
* Constructor. Throws a new exception in case the exception is unknown.
* @throws UnknownException
* @param string $message
* @param int $code
*/
function __construct($message = "", $code = 0) {
if ($message === "") {
throw new $this('Unknown '. get_class($this));
}
parent::__construct($message, $code);
}
/**
* Puts all the important information into a string.
* @return string
*/
public function __toString() {
$string = get_class($this);
$string .= " :" . $this->message;
$string .= " in " . $this->file . "(" . $this->line . ")";
$string .= "\n" . $this->getTraceAsString();
return $string;
}
}
?>

View File

@ -0,0 +1,9 @@
<?php
/**
* SocketException
* @author jpt
* @package Exception
* @exception
*/
class Exception_SocketException extends Exception_GeneralException {}
?>

82
Classes/Misc/Buffer.php Normal file
View File

@ -0,0 +1,82 @@
<?php
/**
* Buffer class for a string.
* Will fix issues with sockets that don't care about linebreaks.
* Can also be used for all kinds of purpose.
* @author jpt
* @package Misc
*/
class Misc_Buffer {
/**
* Bufferstring - contains all the data.
* @var string
*/
protected $buffer;
/**
* Linebreak to use for pulling lines out of the buffer.
* @var string
*/
protected $linebreak;
/**
* Default constructor. Sets a default linebreak and initializes the buffer.
* @param $linebreak
* @return void
*/
function __construct($linebreak = "\r\n") {
$this->buffer = "";
$this->linebreak = $linebreak;
}
/**
* Prunes empty lines out of the buffer.
* @return void
*/
protected function pruneEmptyLines() {
if(strpos($this->buffer, $this->linebreak) === FALSE) return;
$has_linebreak_at_end = (substr($this->buffer, (-1) * strlen($this->linebreak)) === $this->linebreak) ? TRUE : FALSE;
$lines = explode($this->linebreak, $this->buffer);
foreach($lines AS $key=>$line) {
$line = str_replace($this->linebreak, "", $line);
$line = trim($line);
if($line === "") unset($lines[$key]);
}
$this->buffer = implode($this->linebreak, $lines);
if($has_linebreak_at_end) $this->buffer .= $this->linebreak;
}
/**
* Appends data to the buffer.
* @param string $data
* @return void
*/
public function addData($data) {
$this->buffer .= $data;
$this->pruneEmptyLines();
}
/**
* Returns the next line in the buffer and removes it from the buffer.
* @return string
*/
public function getNextLine() {
if(!$this->hasLines()) return "";
list($line) = explode($this->linebreak, $this->buffer);
$this->buffer = str_replace($line.$this->linebreak, "", $this->buffer);
$line = trim($line);
$result = ($line !== "") ? $line.$this->linebreak : "";
return $result;
}
/**
* Checks whether the buffer contains more lines to process.
* @return boolean
*/
public function hasLines() {
return (trim(strstr($this->buffer, $this->linebreak, TRUE)) !== "") ? TRUE : FALSE;
}
}
?>

View File

@ -0,0 +1,216 @@
<?php
/**
* The SocketHandler class will handle a single socket.
* It takes care of the very basic socket functions.
* @author jpt
* @package Socket
*/
class Socket_SocketHandler {
/**
* Socket ressource
* @var ressource
*/
protected $socket;
/**
* @var boolean
*/
protected $is_connected;
/**
* @var boolean
*/
protected $is_bound;
/**
* @var boolean
*/
protected $is_listening;
/**
* Default constructor. Sets the socket.
* @throws Exception_SocketException
* @param ressource $socket
* @return void
*/
function __construct($socket) {
if(is_resource($socket) === FALSE) throw new Exception_SocketException("Invalid socket ressource!", 1289663108);
$this->socket = $socket;
$this->is_bound = FALSE;
$this->is_connected = FALSE;
$this->is_listening = FALSE;
}
/**
* Destructor. Closes socket.
* @return void
*/
function __destruct() {
$this->close();
}
/**
* Returns socket
* @return ressource
*/
public function getSocket() {
return $this->socket;
}
/**
* @return boolean
*/
public function isConnected() {
return $this->is_connected;
}
/**
* @return boolean
*/
public function isListening() {
return $this->is_listening;
}
/**
* Connects to a specified address.
* @throws Exception_SocketException
* @param string $address
* @param int $port
* @return void
*/
public function connect($address, $port) {
if($this->is_connected === TRUE) throw new Exception_SocketException("Socket is already connected!", 1289663170);
$result = socket_connect($this->socket, $address, $port);
if($result === FALSE) $this->error();
$this->is_connected = TRUE;
}
/**
* Binds the socket to an address + port.
* @throws Exception_SocketException
* @param string $address
* @param int $port
* @return void
*/
public function bind($address, $port) {
if($this->is_bound === TRUE) throw new Exception_SocketException("Socket is already bound!", 1289663212);
$result = socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
if($result === FALSE) $this->error();
$result = socket_bind($this->socket, $address, $port);
if($result === FALSE) $this->error();
$this->is_bound = TRUE;
}
/**
* Let's the socket listen for incoming connections.
* @throws Exception_SocketException
* @return void
*/
public function listen() {
if($this->is_bound === FALSE) throw new Exception_SocketException("Cannot listen on unbound socket!", 1289663220);
$result = socket_listen($this->socket);
if($result === FALSE) $this->error();
$this->is_listening = TRUE;
}
/**
* Tells the SocketHandler that he got accepted.
* @return void
*/
public function hasBeenAccepted() {
$this->is_connected = TRUE;
}
/**
* Accepts a connection to a socket that is bound and listens
* The ressource has to be added to the SocketPool manually
* @throws Exception_SocketException
* @return ressource
*/
public function accept() {
if($this->is_bound === FALSE) throw new Exception_SocketException("Cannot accept connections from unbound socket!", 1289663239);
if($this->is_listening === FALSE) throw new Exception_SocketException("Cannot accept connections from socket that is not listening!", 1289663241);
$accept = socket_accept($this->socket);
if($accept === FALSE) $this->error();
return $accept;
}
/**
* Returns ip + port of the remote socket.
* @throws Exception_SocketException
* @return string
*/
public function getRemoteName() {
if($this->is_connected === FALSE) throw new Exception_SocketException("Socket not connected, cannot retrieve remote name!", 1289928192);
$result = socket_getpeername($this->socket, $address, $port);
if($result === FALSE) $this->error();
return $address . ":" . $port;
}
/**
* Returns ip + port of the local socket.
* @throws Exception_SocketException
* @return string
*/
public function getLocalName() {
if($this->is_bound === FALSE ^ $this->is_connected === FALSE) throw new Exception_SocketException("Socket is not bound or connected, no local name available!", 1289928256);
$result = socket_getsockname($this->socket, $address, $port);
if($result === FALSE) $this->error();
return $address . ":" . $port;
}
/**
* Writes data to the socket.
* @throws Exception_SocketException
* @param string $data
* @return void
*/
public function write($data) {
$result = socket_write($this->socket, $data);
if($result === FALSE) $this->error();
}
/**
* Reads data from the socket.
* @throws Exception_SocketException
* @param int $length
* @return string
*/
public function read($length = 16384) {
$result = socket_read($this->socket, $length, PHP_BINARY_READ);
if($result === FALSE) $this->error();
return $result;
}
/**
* Shuts the socket down after waiting a bit (waiting might be optional).
* @throws Exception_SocketException
* @return void
*/
public function close() {
usleep(100000);
if($this->is_connected === TRUE) {
$result = socket_shutdown($this->socket);
if($result === FALSE) $this->error();
}
if(is_resource($this->socket)) {
$result = socket_close($this->socket);
if($result === FALSE) $this->error();
}
$this->is_connected = FALSE;
}
/**
* Gets last error from socket.
* @throws Exception_SocketException
*/
protected function error() {
$errno = socket_last_error($this->socket);
$error = socket_strerror($errno);
socket_clear_error();
$errormsg = "[" . $errno . "] " . $error;
throw new Exception_SocketException("A socket error occured: " . $errormsg, 1289663360);
}
}
?>

View File

@ -0,0 +1,100 @@
<?php
/**
* The SocketPool class that will handle all the sockets.
* Manages a pool of socket ressources with socket_select()
* @author jpt
* @package Socket
*/
class Socket_SocketPool {
/**
* Pool that contains socket ressources.
* @var array
*/
protected $sockets;
/**
* Creates sockets.
* @return void
*/
function __construct() {
$this->sockets = array();
}
/**
* Closes all connections.
* @return void
*/
function __destruct() {
unset($this->sockets);
}
/**
* Returns the amount of active sockets.
* @return int
*/
public function countSockets() {
return count($this->sockets);
}
/**
* Adds a socket to the pool
* @param ressource $add_socket
* @return void
*/
public function addSocket($add_socket) {
array_push($this->sockets, $add_socket);
}
/**
* Removes the given socket from the pool.
* The socket will be shutdown.
* @param ressource $remove_socket
* @return void
*/
public function removeSocket($remove_socket) {
foreach($this->sockets AS $key=>$socket) {
if($socket !== $remove_socket) {
continue;
} else {
unset($socket);
unset($this->sockets[$key]);
break;
}
}
}
/**
* Creates a new TcpSocket and adds it to the pool.
* @throws Exception_SocketException
* @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6.
* @return ressource
*/
public function createTcpSocket($IPv6 = FALSE) {
$Domain = ($IPv6) ? AF_INET6 : AF_INET;
$socket = socket_create($Domain, SOCK_STREAM, SOL_TCP);
socket_set_block($socket);
if($socket === FALSE) throw new Exception_SocketException("socket_create() failed!", 1290273709);
$this->addSocket($socket);
return $socket;
}
/**
* Returns an array of sockets one can read from.
* @param array $read
* @param array $write
* @param array $except
* @param int $timeout
* @throws Exception_SocketException
* @return array
*/
public function select($read, $write, $except, $timeout = NULL) {
$n = socket_select($read, $write, $except, $timeout);
if($n === FALSE) throw new Exception_SocketException("socket_select() failed!", 1290273693);
if($n === 0) return array("read" => array(), "write" => array(), "except" => array());
return array("read" => $read, "write" => $write, "except" => $except);
}
}
?>

1630
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

12
Main.php Normal file
View File

@ -0,0 +1,12 @@
<?php
error_reporting(E_ALL);
require_once('AutoLoader.php');
try {
//require_once('Testcode/Connection/IrcClientTest.php');
require_once('Testcode/Client/ClientManagerTest.php');
}
catch(Exception $e) {
echo "\n\nCaught Exception: " . $e . "\n";
}
?>

18
TODO Normal file
View File

@ -0,0 +1,18 @@
###################################
# Geplant sind folgende Konzepte: #
###################################
- eval("class foobar {}") in einem funktions-kontext
- Plugins via foreach($plugins AS $plugin) { $plugin->work(); } laufen lassen
-
- Logging auf Verbindungs/Channel und Verbindung/Query-Basis für IRC.
- RAW-Logging für den Rest?
- Settings?!
"<Serena> I'll ask Lucene about that ;-)"
- Logging mit Timestamp für:
- Sollte das Teil mal abkacken, so will ich zumindest über Verbindungsabbrüche,
Disconnects und Protokollseitige Quits, X-Lines und Errors einen Nachweis haben.
- Auch php-errors sollten in einem logfile landen.

View File

@ -0,0 +1,40 @@
<?php
$clientManager = new Client_ClientManager();
$clientManager->registerProtocol("irc", "Client_IrcClient");
$clientManager->registerProtocol("jpt", "Client_BotClient");
$freenode = $clientManager->createTcpConnection("freenode", "irc");
$clientManager->attachConfig(array(
"nick" => "Testinstanz",
"userident" => "uzuguck",
"channels" => array("#mstasty")
), $freenode);
$freenode->connect("irc.freenode.net", 6667);
$freenode = $clientManager->createTcpConnection("freenode", "irc");
$clientManager->attachConfig(array(
"nick" => "Testinstanz2",
"userident" => "uzuguck",
"channels" => array("#mstasty")
), $freenode);
$freenode->connect("irc.freenode.net", 6667);
/*$config_eloxoph = array(
"nick" => "Frischmilch",
"userident" => "olefolol",
"channels" => array("#eloxoph")
);
$eloxoph = $clientManager->createTcpConnection("eloxoph", "irc");
$clientManager->attachConfig($config_eloxoph, $eloxoph);
$eloxoph->connect("irc.eloxoph.com", 6667);*/
$srv = $clientManager->createTcpConnection("srv", "jpt");
$srv->bind("localhost", 7777);
$srv->listen();
while($clientManager->countConnections() > 0) {
$clientManager->work();
}
?>

View File

@ -0,0 +1,39 @@
<?php
$pool = new Connection_ConnectionPool();
$srv = $pool->createTcpConnection("raw_input");
$srv->bind("localhost", 7777);
$srv->listen();
$sock = $pool->createTcpConnection("freenode");
$sock->bind("192.168.0.101", 5456);
$sock->connect("irc.freenode.net", 6667);
$got_001 = FALSE;
$lines = 0;
$i = 0;
$authed = FALSE;
$joined = FALSE;
while(TRUE) {
$select = $pool->select();
while($read = $sock->read()) {
$pool->writeToGroup("raw_input", $read);
if(preg_match("/001/", $read)) $got_001 = TRUE;
$lines++;
}
if(!$authed && $lines > 1) {
$sock->write("USER as as as :Asdfg\r\n");
$sock->write("NICK :phptbt42\r\n");
$authed = TRUE;
}
if($got_001 && !$joined) {
$joined = TRUE;
$sock->write("JOIN #mstasty\r\n");
//$sock->write("PRIVMSG #mstasty :SocketTest abgeschlossen!\r\n");
}
echo $i++ . " ---\n";
}
?>

View File

@ -0,0 +1,61 @@
<?php
class TestException extends Exception{
function __construct($message) {
$this->message = $message;
}
}
class TestFailException extends Exception {
function __construct($msg) {
$this->message = "Error: ".$msg;
}
}
class Service_ExceptionTest {
function __construct() {
}
public function TestTheFail() {
$this->failAtThis();
}
public function CatchAndThrow() {
try {
$this->failAtThis();
}
catch (Exception $e) {
throw new TestFailException("moep:D");
}
}
public function internalCatch() {
try {
$this->failAtThis();
}
catch (Exception $e) {
var_dump($e);
}
}
function failAtThis() {
throw new TestException("I had to do this. Please forgive me :/");
}
}
try {
$ExceptionTest = new Service_ExceptionTest;
$ExceptionTest->CatchAndThrow();
} catch (Exception $e){
var_dump($e);
}
?>

View File

@ -0,0 +1,79 @@
<?php
/**
* Testclass
* @author jpt
*/
class Foo {
/**
* @var array Instances of Bar.
*/
protected $pool;
function __construct() {
$this->pool = array();
}
/**
* Adds an Instance of Bar to the Pool.
* @param Bar $bar
* @return void
*/
public function addBar($bar) {
array_push($this->pool, $bar);
}
/**
* Returns the internal instance of Bar for the specific ID.
* @param int $id
* @return Bar
*/
public function getBar($id) {
return $this->pool[$id];
}
/**
* Outputs the current state of the bars in the pool.
* @return void
*/
public function getTestResult() {
foreach($this->pool AS $id=>$bar) {
echo "ID: " . $id . ", Test: " . $bar->test . "\n";
}
}
}
/**
* Testclass #2
* @author jpt
*/
class Bar {
/**
* @var string Teststring.
*/
public $test;
/**
* Default constructor. Initializes test variable.
* @return void
*/
function __construct() {
$this->test = "failed";
}
}
$f = new Foo();
$f->addBar(new Bar());
$f->addBar(new Bar());
$f->addBar(new Bar());
$f->addBar(new Bar());
$f->getBar(1)->test = "success";
$f->getTestResult();
?>

View File

@ -0,0 +1,67 @@
<?php
/**
* Testclass in order to test phpdoc-stuff with the php reflection api
* Shall help in order to find out how this stuff works.
* @author jpt
*/
class TestClassForTestsWithReflection {
/**
* @var string
*/
public $public;
/**
* @var string
*/
protected $protected;
/**
* @var string
*/
private $privateStatic;
/**
* Testfunction
* @param Klasse $objekt
* @return void
*/
public function blablubblol(Klasse $objekt) {
$objekt = new ReflectionObject($objekt);
print $objekt->getName();
}
/**
* Tests the getDocComment() method
*
* @param array $ray
* @param int $zero
* @return string
* @specialtag
*/
protected function testfunctionWithCamelCase(array $ray, $zero = 0) {
return "bla";
}
}
//TestClassForTestsWithReflection ends here.
//create ReflectionClass
$class = new ReflectionClass("TestClassForTestsWithReflection");
//print class name
echo "Information about: " . $class->name . "\n";
//getDocs for all methods
foreach($class->getMethods() as $key=>$method) {
var_dump($method->name, $method->getDocComment());
echo "\n";
}
//getDocs for all properties
foreach($class->getProperties() as $key=>$property) {
var_dump($property->name, $property->getDocComment());
echo "\n";
}
?>

2
Testcode/WHATS_THIS Normal file
View File

@ -0,0 +1,2 @@
All the code in this directory is for testing only.
Just to make that clear ;-)

1
start.sh Executable file
View File

@ -0,0 +1 @@
clear && php Main.php

1
update-docs.sh Executable file
View File

@ -0,0 +1 @@
doxygen