ircbot/Classes/Client/ClientManager.php

233 lines
6.6 KiB
PHP
Raw Normal View History

2010-11-21 23:48:11 +01:00
<?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
* TODO: implement routing with some more detailed rules, remove hardcoded client-based routing.
2010-11-21 23:48:11 +01:00
*/
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 $routingRules;
2010-11-21 23:48:11 +01:00
/**
* 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->routingRules = array();
2010-11-21 23:48:11 +01:00
$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);
}
//handle accepted sockets, adopt them and treat them with care :-)
2010-11-21 23:48:11 +01:00
if($connectionHandler->isListening() === TRUE) {
$this->addClientForConnectionHandler($connectionHandler);
}
//create a client for a connection without one.
if(!isset($this->clientPool[$connectionHandler->getID()])) {
$this->addClientForConnectionHandler($connectionHandler);
}
//call the registered client
2010-11-21 23:48:11 +01:00
if(isset($this->clientPool[$connectionHandler->getID()])) {
//let the client process the data, send the results back.
while($data = $connectionHandler->read()) {
2010-11-28 02:15:20 +01:00
$result = $this->clientPool[$connectionHandler->getID()]->processRawData($data);
2010-11-21 23:48:11 +01:00
if($result !== "") {
$connectionHandler->write($result);
}
}
}
}
}
/**
* 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);
2010-11-28 02:15:20 +01:00
$className = "Protocol_".$protocol."ProtocolHandler";
$protocolHandler = new $className();
$client->injectProtocolHandler($protocolHandler);
2010-11-21 23:48:11 +01:00
$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);
}
}
?>