[TASK] Optimized structure for public function work()

This commit is contained in:
Jan Philipp Timme 2011-06-25 23:04:19 +00:00
parent 2c5e520c9f
commit 78866ed149
2 changed files with 47 additions and 33 deletions

View File

@ -80,21 +80,16 @@ class Client_ClientManager {
/**
* Main worker function.
* Processes incoming data, calls clients and much more.
* TODO: refactor this? (split it into handleFoobar() functions)
* @return void
*/
public function work() {
//firstly, create clients for connections without one.
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
if(!isset($this->clientPool[$connectionHandler->getID()])) {
//new connections might need a client, so we'll create one here.
$this->addClientForConnectionHandler($connectionHandler);
//allow client to send initial stuff right after connecting to the server.
$this->clientPool[$connectionHandler->getID()]->initializeConnection();
//after initializing, have it process an empty string in order to get stuff to write. >.<
$result = $this->clientPool[$connectionHandler->getID()]->processRawData("");
if($result !== "") $connectionHandler->write($result);
}
if(isset($this->clientPool[$connectionHandler->getID()])) continue;
//new connections might need a client, so we'll create one here.
$this->addClientForConnectionHandler($connectionHandler);
//allow client to send initial stuff right after connecting to the server.
$this->initializeClientForConnectionHandler($connectionHandler);
}
//then, process all connections that have stuff to read.
@ -105,33 +100,18 @@ class Client_ClientManager {
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
//check whether the reconnect-flag is set
if($connectionHandler->getReconnect() === TRUE) {
//have the connectionHandler do the reconnect.
$newConnectionHandler = $connectionHandler->reconnect();
//get the client and reset it.
$client = $this->clientPool[$connectionHandler->getID()];
$client->resetConnectionStatus();
//assign the client to the new connectionHandler.
$this->clientPool[$newConnectionHandler->getID()] = $client;
//get the config and assign it to the new connectionHandler, too.
$config = $this->configPool[$connectionHandler->getID()];
$this->configPool[$newConnectionHandler->getID()] = $config;
//remove old connection
$this->removeConnection($connectionHandler);
//re-initialize the client
$this->clientPool[$newConnectionHandler->getID()]->initializeConnection();
//and of course, process stuff to get the buffer filled. :/
$result = $this->clientPool[$newConnectionHandler->getID()]->processRawData("");
if($result !== "") $newConnectionHandler->write($result);
//do the reconnect stuff...
$this->handleReconnectForConnectionHandler($connectionHandler);
}
$this->removeClientForConnectionHandler($connectionHandler);
//this connectionHandler won't do much anymore - kill it.
$this->removeClientForConnectionHandler($connectionHandler);
continue;
}
//handle accepted sockets, adopt them and treat them with care :-)
if($connectionHandler->isListening() === TRUE) {
$this->addClientForConnectionHandler($connectionHandler);
//TODO: maybe we want to trigger the "client" here, too? -- YES
$this->clientPool[$connectionHandler->getID()]->initializeConnection();
$this->initializeClientForConnectionHandler($connectionHandler);
}
//call the registered client
@ -139,9 +119,7 @@ class Client_ClientManager {
//let the client process the data, send the results back.
while($data = $connectionHandler->read()) {
$result = $this->clientPool[$connectionHandler->getID()]->processRawData($data);
if($result !== "") {
$connectionHandler->write($result);
}
if($result !== "") $connectionHandler->write($result);
}
}
}
@ -212,6 +190,42 @@ class Client_ClientManager {
}
}
/**
* Initializes the client of a given connectionHandler.
* This should be done after establishing a connection.
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
protected function initializeClientForConnectionHandler($connectionHandler) {
$this->clientPool[$connectionHandler->getID()]->initializeConnection();
//after initializing, have it process an empty string in order to get stuff to write. >.<
$result = $this->clientPool[$connectionHandler->getID()]->processRawData("");
if($result !== "") $connectionHandler->write($result);
}
/**
* Creates a new connectionHandler, resets the clients connection status,
* copies the old config and assigns the new connectionHandler to the client.
* @param Connection_ConnectionHandler $connectionHandler
* @return void
*/
protected function handleReconnectForConnectionHandler($connectionHandler) {
//have the connectionHandler do the reconnect.
$newConnectionHandler = $connectionHandler->reconnect();
//get the client and reset it.
$client = $this->clientPool[$connectionHandler->getID()];
$client->resetConnectionStatus();
//assign the client to the new connectionHandler.
$this->clientPool[$newConnectionHandler->getID()] = $client;
//get the config and assign it to the new connectionHandler, too.
$config = $this->configPool[$connectionHandler->getID()];
$this->configPool[$newConnectionHandler->getID()] = $config;
//re-initialize the client
$this->initializeClientForConnectionHandler($newConnectionHandler);
//remove old connection
$this->removeConnection($connectionHandler);
}
/**
* Adds a client to our client pool.
* @param Connection_ConnectionHandler $connectionHandler

2
README
View File

@ -14,4 +14,4 @@ The Code itself should also give you an idea of how this works :-)
# License
This project is a private one, i don't know what license i will use yet.
This project is a private one, i don't know what license i will use yet. Maybe something like GPL v2+