[TASK] Implemented the ProtocolHandler things so it works again.

[TASK] Added some comments.
[TASK] Added TODOs concerning refactoring and more functions that are needed.
This commit is contained in:
Jan Philipp Timme 2010-12-08 16:47:04 +00:00
parent 2b8263a49d
commit dee3333578
10 changed files with 114 additions and 17 deletions

View File

@ -58,14 +58,17 @@ abstract class Client_AbstractClient {
/**
* This function will be the main entry point of any client.
* Any reactions will be passed to the ProtocolHandler.
* The ProtocolHandler will then pass the data back to the socket.
* @param string $data
* @return string
* @return void
*/
abstract protected function processContentObject($contentObject);
/**
* This function will load the given config.
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
@ -81,6 +84,7 @@ abstract class Client_AbstractClient {
/**
* @param int $id
* @return void
*/
public function setID($id) {
$this->ID = $id;
@ -88,6 +92,7 @@ abstract class Client_AbstractClient {
/**
* @param string $group
* @return void
*/
public function setGroup($group) {
$this->group = $group;
@ -96,6 +101,7 @@ abstract class Client_AbstractClient {
/**
* Injects ClientManager
* @param Client_ClientManager $clientManager
* @return void
* @throws Exception_WrongDatatypeException
*/
public function injectClientManager($clientManager) {

View File

@ -34,7 +34,8 @@ class Client_BotClient extends Client_AbstractClient {
$return = print_r($data, TRUE);
}
return $return;
//TODO: implement this correctly
$this->protocolHandler->sendRaw($return);
}
/**

View File

@ -61,16 +61,18 @@ class Client_ClientManager {
}
/**
* TODO: Implement a function that is able to count active connections instead of sockets!
* @see Connection_ConnectionPool
* @return int
*/
public function countConnections() {
return $this->connectionPool->countConnections();
public function countSockets() {
return $this->connectionPool->countSockets();
}
/**
* Main worker function.
* Processes incoming data, calls clients and much more.
* TODO: refactor this? (split it into handleFoobar() functions)
* @return void
*/
public function work() {

View File

@ -39,7 +39,7 @@ class Client_IrcClient extends Client_AbstractClient {
* Processes the resulting ContentObject from a ProtocolHandler.
* Does all the hard work.
* @param string $data
* @return string
* @return void
*/
public function processContentObject($contentObject) {
$data = $contentObject->rawData;
@ -68,8 +68,8 @@ class Client_IrcClient extends Client_AbstractClient {
$return .= "QUIT :lol\r\n";
}
//if($return !== "") echo "[SEND] ".$return;
return $return;
//workaround. will be removed soon
$this->protocolHandler->sendRaw($return);
}
/**

View File

@ -138,6 +138,7 @@ class Connection_ConnectionPool {
throw new Exception_GeneralException("Unknown select type: '" . $selectedType . "'", 1289737080);
break;
}
//Put the ConnectionHandler into the array. We'll return this for further operations.
$tempArray[$selectedType][] = $connectionHandler;
}
}
@ -176,7 +177,7 @@ class Connection_ConnectionPool {
* @see Socket_SocketPool
* @return int
*/
public function countConnections() {
public function countSockets() {
return $this->socketPool->countSockets();
}
}

View File

@ -81,7 +81,7 @@ class Misc_Buffer {
*/
public function getNextChars($length) {
//TODO: substr und so
return "";
throw new Exception_GeneralException("This feature is not implemented yet!", 1291824224);
}
/**

View File

@ -23,6 +23,23 @@ abstract class Protocol_AbstractProtocolHandler {
*/
protected $buffer_outgoing;
/**
* This function will be called by the constructor.
* It will create the necessary instances of Misc_Buffer and
* put them in $buffer_incoming and $buffer_outgoing.
* @return void
*/
abstract public function createBuffers();
/**
* General constructor.
* Calls createBuffers()
* @return void
*/
function __construct() {
$this->createBuffers();
}
/**
* Returns whether work() can be called or not.
* Depends on the protocol and its implementation.

View File

@ -7,11 +7,44 @@
class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler {
/**
* @param string $data
* @return Protocol_RawProtocolContentObject
* Is called by the constructor.
* Shall create the two buffers and set them up.
* @return void
*/
public function parse($data) {
public function createBuffers() {
$linebreak = "\r\n";
$this->buffer_incoming = new Misc_Buffer($linebreak);
$this->buffer_outgoing = new Misc_Buffer($linebreak);
}
/**
* Main worker function. It will be called in a loop.
* The returned ContentObject will be passed to the client.
* @return Protocol_BotProtocolContentObject
*/
public function work() {
$data = $this->buffer_incoming->getNextLine();
return new Protocol_BotProtocolContentObject($data);
}
/**
* Returns whether there is work to be done.
* Important in order to assure that a ContentObject is created and passed to the Client.
* @return boolean
*/
public function canWork() {
return $this->buffer_incoming->hasLines();
}
/**
* Will be replaced soon. Passes raw data into the outgoing buffer.
* @deprecated
* @param string $data
* @return void
*/
public function sendRaw($data) {
$this->buffer_incoming->addData($data);
}
}
?>

View File

@ -7,11 +7,46 @@
class Protocol_IrcProtocolHandler extends Protocol_AbstractProtocolHandler {
/**
* @param string $data
* Is called by the constructor.
* Shall create the two buffers and set them up.
* @return void
*/
public function createBuffers() {
$linebreak = "\r\n";
$this->buffer_incoming = new Misc_Buffer($linebreak);
$this->buffer_outgoing = new Misc_Buffer($linebreak);
}
/**
* Main worker function. It will be called in a loop.
* The returned ContentObject will be passed to the client.
* @return Protocol_IrcProtocolContentObject
*/
public function parse($data) {
public function work() {
$data = $this->buffer_incoming->getNextLine();
return new Protocol_IrcProtocolContentObject($data);
}
/**
* Returns whether there is work to be done.
* Important in order to assure that a ContentObject is created and passed to the Client.
* @return boolean
*/
public function canWork() {
return $this->buffer_incoming->hasLines();
}
/**
* Will put raw data into the outgoing buffer.
* This function will be removed soon.
* The ProtocolHandler shall take care of this directly.
* @deprecated
* @param string $data
* @return void
*/
public function sendRaw($data) {
$this->buffer_outgoing->addData($data);
}
}
?>

View File

@ -3,7 +3,7 @@ $clientManager = new Client_ClientManager();
$clientManager->registerProtocol("irc", "Irc");
$clientManager->registerProtocol("jpt", "Bot");
$freenode = $clientManager->createTcpConnection("freenode", "irc");
/*$freenode = $clientManager->createTcpConnection("freenode", "irc");
$clientManager->attachConfig(array(
"nick" => "Testinstanz",
"userident" => "uzuguck",
@ -11,6 +11,7 @@ $clientManager->attachConfig(array(
), $freenode);
$freenode->connect("irc.freenode.net", 6667);
$freenode->setReconnect(TRUE);
*/
$freenode = $clientManager->createTcpConnection("freenode", "irc");
$clientManager->attachConfig(array(
@ -20,7 +21,6 @@ $clientManager->attachConfig(array(
), $freenode);
$freenode->connect("irc.freenode.net", 6667);
/*$config_eloxoph = array(
"nick" => "Frischmilch",
"userident" => "olefolol",
@ -31,11 +31,13 @@ $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) {
while($clientManager->countSockets() > 0) {
$clientManager->work();
}
?>