[TASK] Finalized modifications on ClientManager.

[TASK] Added support for further class paths to ClassLoader.
[TASK] Started to implement the irc client dispatcher. (partially works now)
This commit is contained in:
Jan Philipp Timme 2011-12-03 20:58:08 +01:00
parent 16ff75eb1d
commit e702124895
7 changed files with 90 additions and 54 deletions

View File

@ -1,42 +1,44 @@
<?php
namespace JPT\IrcClient;
/**
* IrcClient class that contains all the Plugins
* The ClientDispatcher for this IrcBot.
*
* @author jpt
* @package Client
* @depends Client_AbstractClient
*/
class Client_IrcClient extends Client_AbstractClient {
class ClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatcher {
/**
* @var boolean
*/
protected $joined;
/**
* @var boolean
*/
protected $got001;
/**
* @var boolean
*/
protected $authed;
/**
* @var string
*/
protected $nick;
/**
* @var array
*/
protected $channels;
/**
* @var int
*/
protected $lines;
/**
* Default constructor.
* @return void
@ -46,7 +48,7 @@ class Client_IrcClient extends Client_AbstractClient {
$this->channels = array();
$this->resetConnectionStatus();
}
/**
* Will reset the clients internal variables concerning the connection status.
* @return void
@ -57,7 +59,7 @@ class Client_IrcClient extends Client_AbstractClient {
$this->joined = FALSE;
$this->authed = FALSE;
}
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
@ -68,46 +70,45 @@ class Client_IrcClient extends Client_AbstractClient {
$data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n";
$this->authed = TRUE;
}
$this->protocolHandler->sendRaw($data);
//$this->protocolHandler->sendRaw($data);
}
/**
* Processes the resulting ContentObject from a ProtocolHandler.
* Does all the hard work.
* @param string $data
* @return void
* Forwards incoming data to the ProtocolHandler
* Let's the ProtocolHandler do all the work and forward its results to the Clients.
* Return resulting raw data.
*
* @param string $rawData
* @return string
*/
protected function processContentObject($contentObject) {
$data = $contentObject->getRawData();
//DEBUG
var_dump($contentObject);
public function processRawData($rawData) {
$data = $rawData;
var_dump($data);
//respond to pings
if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams());
//respond to pings
//if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams());
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data);
if($contentObject->getCommand() === "001") $this->got001 = TRUE;
//if($contentObject->getCommand() === "001") $this->got001 = TRUE;
$return = "";
$this->lines++;
if(!$this->joined && $this->got001) {
$this->joined = TRUE;
foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n";
}
if(strpos($data, "musdsdsafgagltivitamin") !== FALSE) {
$return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n";
$return .= "QUIT :lol\r\n";
}
//workaround. will be removed soon
$this->protocolHandler->sendRaw($return);
return $return;
}
/**
* Loads the given configuration.
* @param array $config
@ -118,6 +119,6 @@ class Client_IrcClient extends Client_AbstractClient {
$this->channels = $config["channels"];
$this->userident = $config["userident"];
}
}
?>

View File

@ -2,6 +2,8 @@
error_reporting(E_ALL);
require_once('SocketFramework' . DIRECTORY_SEPARATOR . 'Bootstrap.php');
$classLoader->addClassPathMapping('JPT\IrcClient', 'IrcClient' . \DIRECTORY_SEPARATOR . 'Classes');
try {
require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php');
}

View File

@ -47,7 +47,9 @@ abstract class AbstractClientDispatcher implements \JPT\SocketFramework\Client\C
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
public function loadConfig($config) {
}
/**
* Will reset the connectionStatus of the client.

View File

@ -26,7 +26,7 @@ interface ClientDispatcherInterface {
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
public function loadConfig($config);
/**
* Will reset the connectionStatus of the client.

View File

@ -99,7 +99,7 @@ class ClientManager {
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) continue;
//new connections might need a client, so we'll create one here.
$this->addClientForConnectionHandler($connectionHandler);
$this->addClientDispatcherForConnectionHandler($connectionHandler);
//allow client to send initial stuff right after connecting to the server.
$this->initializeClientForConnectionHandler($connectionHandler);
}
@ -120,7 +120,7 @@ class ClientManager {
}
//handle accepted sockets, adopt them and treat them with care :-)
if($connectionHandler->isListening() === TRUE) {
$this->addClientForConnectionHandler($connectionHandler);
$this->addClientDispatcherForConnectionHandler($connectionHandler);
$this->initializeClientForConnectionHandler($connectionHandler);
}
@ -233,7 +233,7 @@ class ClientManager {
}
/**
* Calls Connection_Pool.
* Calls \JPT\SocketFramework\Connection\ConnectionPool.
*
* @see \JPT\SocketFramework\Connection\ConnectionPool
* @param string $group
@ -254,8 +254,8 @@ class ClientManager {
*/
protected function createClientDispatcherForProtocol($protocolIdentifier) {
//look for the protocol
if(!in_array($protocolIdentifier, $this->registeredClientDispatchers)) {
throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocol . "'!", 1290271651);
if(isset($this->registeredClientDispatchers[$protocolIdentifier]) === FALSE) {
throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocolIdentifier . "'!", 1290271651);
}
$className = $this->registeredClientDispatchers[$protocolIdentifier];
//look for the class
@ -265,7 +265,7 @@ class ClientManager {
if(!$clientDispatcher instanceof \JPT\SocketFramework\Client\AbstractClientDispatcher) throw new \JPT\SocketFramework\Exception\GeneralException("Class '" . $className . "' does not implement the AbstractClientDispatcher-API!", 1294837055);
return $clientDispatcher;
} else {
throw new \JPT\SocketFramework\Exception\GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773);
throw new \JPT\SocketFramework\Exception\GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocolIdentifier . "'!", 1290271773);
}
}
@ -277,6 +277,7 @@ class ClientManager {
* @return void
*/
protected function initializeClientForConnectionHandler($connectionHandler) {
if(!isset($this->clientDispatcherPool[$connectionHandler->getID()])) return;
$this->clientDispatcherPool[$connectionHandler->getID()]->initializeConnection();
//after initializing, have it process an empty string in order to get stuff to write. >.<
$result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData("");
@ -315,7 +316,7 @@ class ClientManager {
* @return void
*/
protected function addClientDispatcherForConnectionHandler($connectionHandler) {
$protocolIdentifier = $this->registeredProtocols[$connectionHandler->getProtocol()];
$protocolIdentifier = $connectionHandler->getProtocol();
//do not try this for "RAW" connections - might or might not be useful.
if($protocolIdentifier === "RAW") return;
$clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier);

View File

@ -8,20 +8,51 @@ namespace JPT\SocketFramework\Core;
*/
class ClassLoader {
/**
* Contains class name prefix and path prefix for all class paths it shall handle
*
* @var array
*/
protected $classNameMapping = array();
/**
* Default constructor.
* Registers the main class path for the socket framework.
*/
public function __construct() {
$this->classNameMapping = array(
'JPT\SocketFramework' => 'SocketFramework' . \DIRECTORY_SEPARATOR . 'Classes'
);
}
/**
* Registers a new class path for the autoloader
*
* @param string $classNamePrefix
* @param string $pathPrefix
*/
public function addClassPathMapping($classNamePrefix, $pathPrefix) {
$this->classNameMapping[$classNamePrefix] = $pathPrefix;
}
/**
* Autoloader function.
* All classes will be in Classes/$Package$/$ClassName$.php
*
* @throws \Exception
* @param string $className
* @return void
*/
public function loadClass($className) {
if(substr($className, 0, 19) !== "JPT\\SocketFramework") return;
$className = substr($className, 20);
$fileName = "SocketFramework" . \DIRECTORY_SEPARATOR . "Classes";
$fileName .= \DIRECTORY_SEPARATOR . str_replace("\\", \DIRECTORY_SEPARATOR, $className) . ".php";
$matchingClassNamePrefix = FALSE;
foreach($this->classNameMapping AS $classNamePrefix => $pathPrefix) {
if(substr($className, 0, strlen($classNamePrefix)) !== $classNamePrefix) continue;
$matchingClassNamePrefix = TRUE;
$classNameSuffix = substr($className, strlen($classNamePrefix), strlen($className));
$fileName = $this->classNameMapping[$classNamePrefix];
$fileName .= \DIRECTORY_SEPARATOR . str_replace('\\', \DIRECTORY_SEPARATOR, $classNameSuffix) . ".php";
break;
}
if($matchingClassNamePrefix === FALSE) return;
if(file_exists($fileName) === TRUE) {
require_once($fileName);
} else {

View File

@ -1,7 +1,6 @@
<?php
$clientManager = new \JPT\SocketFramework\Client\ClientManager();
$clientManager->registerProtocol("irc", "Irc");
$clientManager->registerProtocol("jpt", "Bot");
$clientManager->registerClientDispatcherByProtocol("irc", "\JPT\IrcClient\ClientDispatcher");
$freenode = $clientManager->createTcpConnection("freenode", "irc");
$clientManager->attachConfig(array(
@ -46,7 +45,7 @@ $eloxoph->connect("irc.eloxoph.com", 6667);*/
$srv = $clientManager->createTcpConnection("srv", "jpt");
$srv = $clientManager->createTcpConnection("srv", "RAW");
$srv->bind("localhost", 7777);
$srv->listen();