[TASK] Added function for clients, so they're able to send initial data right after connecting to a server.

[TASK] Restructured parts of the core to support sending data right after connecting.
[TASK] Adapted core part for automatic reconnect to support sending initial data, too.
This commit is contained in:
Jan Philipp Timme
2011-06-25 22:13:53 +00:00
parent 4c9f5ba7f3
commit 2c5e520c9f
11 changed files with 159 additions and 66 deletions
+48 -5
View File
@@ -84,6 +84,20 @@ class Client_ClientManager {
* @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);
}
}
//then, process all connections that have stuff to read.
$connectionHandlers = $this->connectionPool->select();
if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return;
foreach($connectionHandlers["read"] AS $connectionHandler) {
@@ -103,19 +117,23 @@ class Client_ClientManager {
$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);
}
$this->removeClientForConnectionHandler($connectionHandler);
//reconnect or not - this connectionHandler won't do that much.
//this connectionHandler won't do much anymore - kill it.
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();
}
//create a client for a connection without one.
if(!isset($this->clientPool[$connectionHandler->getID()])) {
$this->addClientForConnectionHandler($connectionHandler);
}
//call the registered client
if(isset($this->clientPool[$connectionHandler->getID()])) {
//let the client process the data, send the results back.
@@ -127,6 +145,7 @@ class Client_ClientManager {
}
}
}
//after that, we're done here.
}
/**
@@ -247,6 +266,30 @@ class Client_ClientManager {
return $this->connectionPool->writeToID($id, $data);
}
/**
* Returns a list of available connection IDs.
* @return array
*/
public function getIDs() {
$list = array();
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
$list[] = $connectionHandler->getID();
}
return $list;
}
/**
* Returns a list of available connection groups.
* @return array
*/
public function getGroups() {
$list = array();
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
$list[] = $connectionHandler->getGroup();
}
return $list;
}
/**
* Calls Connection_Pool
* @see Connection_ConnectionPool