[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:
parent
16ff75eb1d
commit
e702124895
|
@ -1,11 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
namespace JPT\IrcClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IrcClient class that contains all the Plugins
|
* The ClientDispatcher for this IrcBot.
|
||||||
|
*
|
||||||
* @author jpt
|
* @author jpt
|
||||||
* @package Client
|
* @package Client
|
||||||
* @depends Client_AbstractClient
|
|
||||||
*/
|
*/
|
||||||
class Client_IrcClient extends Client_AbstractClient {
|
class ClientDispatcher extends \JPT\SocketFramework\Client\AbstractClientDispatcher {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var boolean
|
* @var boolean
|
||||||
|
@ -68,27 +70,27 @@ class Client_IrcClient extends Client_AbstractClient {
|
||||||
$data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n";
|
$data = "USER poweruser as as :JPTs Bot\r\nNICK " . $this->nick . "\r\n";
|
||||||
$this->authed = TRUE;
|
$this->authed = TRUE;
|
||||||
}
|
}
|
||||||
$this->protocolHandler->sendRaw($data);
|
//$this->protocolHandler->sendRaw($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes the resulting ContentObject from a ProtocolHandler.
|
* Forwards incoming data to the ProtocolHandler
|
||||||
* Does all the hard work.
|
* Let's the ProtocolHandler do all the work and forward its results to the Clients.
|
||||||
* @param string $data
|
* Return resulting raw data.
|
||||||
* @return void
|
*
|
||||||
|
* @param string $rawData
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function processContentObject($contentObject) {
|
public function processRawData($rawData) {
|
||||||
$data = $contentObject->getRawData();
|
$data = $rawData;
|
||||||
//DEBUG
|
|
||||||
var_dump($contentObject);
|
|
||||||
var_dump($data);
|
var_dump($data);
|
||||||
|
|
||||||
//respond to pings
|
//respond to pings
|
||||||
if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams());
|
//if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams());
|
||||||
|
|
||||||
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data);
|
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data);
|
||||||
|
|
||||||
if($contentObject->getCommand() === "001") $this->got001 = TRUE;
|
//if($contentObject->getCommand() === "001") $this->got001 = TRUE;
|
||||||
|
|
||||||
$return = "";
|
$return = "";
|
||||||
|
|
||||||
|
@ -104,8 +106,7 @@ class Client_IrcClient extends Client_AbstractClient {
|
||||||
$return .= "QUIT :lol\r\n";
|
$return .= "QUIT :lol\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
//workaround. will be removed soon
|
return $return;
|
||||||
$this->protocolHandler->sendRaw($return);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
2
Main.php
2
Main.php
|
@ -2,6 +2,8 @@
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
require_once('SocketFramework' . DIRECTORY_SEPARATOR . 'Bootstrap.php');
|
require_once('SocketFramework' . DIRECTORY_SEPARATOR . 'Bootstrap.php');
|
||||||
|
|
||||||
|
$classLoader->addClassPathMapping('JPT\IrcClient', 'IrcClient' . \DIRECTORY_SEPARATOR . 'Classes');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php');
|
require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php');
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,9 @@ abstract class AbstractClientDispatcher implements \JPT\SocketFramework\Client\C
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
abstract public function loadConfig($config);
|
public function loadConfig($config) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will reset the connectionStatus of the client.
|
* Will reset the connectionStatus of the client.
|
||||||
|
|
|
@ -26,7 +26,7 @@ interface ClientDispatcherInterface {
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
abstract public function loadConfig($config);
|
public function loadConfig($config);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will reset the connectionStatus of the client.
|
* Will reset the connectionStatus of the client.
|
||||||
|
|
|
@ -99,7 +99,7 @@ class ClientManager {
|
||||||
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
|
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
|
||||||
if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) continue;
|
if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) continue;
|
||||||
//new connections might need a client, so we'll create one here.
|
//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.
|
//allow client to send initial stuff right after connecting to the server.
|
||||||
$this->initializeClientForConnectionHandler($connectionHandler);
|
$this->initializeClientForConnectionHandler($connectionHandler);
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,7 @@ class ClientManager {
|
||||||
}
|
}
|
||||||
//handle accepted sockets, adopt them and treat them with care :-)
|
//handle accepted sockets, adopt them and treat them with care :-)
|
||||||
if($connectionHandler->isListening() === TRUE) {
|
if($connectionHandler->isListening() === TRUE) {
|
||||||
$this->addClientForConnectionHandler($connectionHandler);
|
$this->addClientDispatcherForConnectionHandler($connectionHandler);
|
||||||
$this->initializeClientForConnectionHandler($connectionHandler);
|
$this->initializeClientForConnectionHandler($connectionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +233,7 @@ class ClientManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls Connection_Pool.
|
* Calls \JPT\SocketFramework\Connection\ConnectionPool.
|
||||||
*
|
*
|
||||||
* @see \JPT\SocketFramework\Connection\ConnectionPool
|
* @see \JPT\SocketFramework\Connection\ConnectionPool
|
||||||
* @param string $group
|
* @param string $group
|
||||||
|
@ -254,8 +254,8 @@ class ClientManager {
|
||||||
*/
|
*/
|
||||||
protected function createClientDispatcherForProtocol($protocolIdentifier) {
|
protected function createClientDispatcherForProtocol($protocolIdentifier) {
|
||||||
//look for the protocol
|
//look for the protocol
|
||||||
if(!in_array($protocolIdentifier, $this->registeredClientDispatchers)) {
|
if(isset($this->registeredClientDispatchers[$protocolIdentifier]) === FALSE) {
|
||||||
throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocol . "'!", 1290271651);
|
throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocolIdentifier . "'!", 1290271651);
|
||||||
}
|
}
|
||||||
$className = $this->registeredClientDispatchers[$protocolIdentifier];
|
$className = $this->registeredClientDispatchers[$protocolIdentifier];
|
||||||
//look for the class
|
//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);
|
if(!$clientDispatcher instanceof \JPT\SocketFramework\Client\AbstractClientDispatcher) throw new \JPT\SocketFramework\Exception\GeneralException("Class '" . $className . "' does not implement the AbstractClientDispatcher-API!", 1294837055);
|
||||||
return $clientDispatcher;
|
return $clientDispatcher;
|
||||||
} else {
|
} 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
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function initializeClientForConnectionHandler($connectionHandler) {
|
protected function initializeClientForConnectionHandler($connectionHandler) {
|
||||||
|
if(!isset($this->clientDispatcherPool[$connectionHandler->getID()])) return;
|
||||||
$this->clientDispatcherPool[$connectionHandler->getID()]->initializeConnection();
|
$this->clientDispatcherPool[$connectionHandler->getID()]->initializeConnection();
|
||||||
//after initializing, have it process an empty string in order to get stuff to write. >.<
|
//after initializing, have it process an empty string in order to get stuff to write. >.<
|
||||||
$result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData("");
|
$result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData("");
|
||||||
|
@ -315,7 +316,7 @@ class ClientManager {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function addClientDispatcherForConnectionHandler($connectionHandler) {
|
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.
|
//do not try this for "RAW" connections - might or might not be useful.
|
||||||
if($protocolIdentifier === "RAW") return;
|
if($protocolIdentifier === "RAW") return;
|
||||||
$clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier);
|
$clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier);
|
||||||
|
|
|
@ -8,20 +8,51 @@ namespace JPT\SocketFramework\Core;
|
||||||
*/
|
*/
|
||||||
class ClassLoader {
|
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.
|
* Autoloader function.
|
||||||
* All classes will be in Classes/$Package$/$ClassName$.php
|
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function loadClass($className) {
|
public function loadClass($className) {
|
||||||
if(substr($className, 0, 19) !== "JPT\\SocketFramework") return;
|
$matchingClassNamePrefix = FALSE;
|
||||||
$className = substr($className, 20);
|
foreach($this->classNameMapping AS $classNamePrefix => $pathPrefix) {
|
||||||
$fileName = "SocketFramework" . \DIRECTORY_SEPARATOR . "Classes";
|
if(substr($className, 0, strlen($classNamePrefix)) !== $classNamePrefix) continue;
|
||||||
$fileName .= \DIRECTORY_SEPARATOR . str_replace("\\", \DIRECTORY_SEPARATOR, $className) . ".php";
|
$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) {
|
if(file_exists($fileName) === TRUE) {
|
||||||
require_once($fileName);
|
require_once($fileName);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
$clientManager = new \JPT\SocketFramework\Client\ClientManager();
|
$clientManager = new \JPT\SocketFramework\Client\ClientManager();
|
||||||
$clientManager->registerProtocol("irc", "Irc");
|
$clientManager->registerClientDispatcherByProtocol("irc", "\JPT\IrcClient\ClientDispatcher");
|
||||||
$clientManager->registerProtocol("jpt", "Bot");
|
|
||||||
|
|
||||||
$freenode = $clientManager->createTcpConnection("freenode", "irc");
|
$freenode = $clientManager->createTcpConnection("freenode", "irc");
|
||||||
$clientManager->attachConfig(array(
|
$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->bind("localhost", 7777);
|
||||||
$srv->listen();
|
$srv->listen();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue