[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
+9
View File
@@ -82,6 +82,15 @@ abstract class Client_AbstractClient {
}
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
* @return void
*/
public function initializeConnection() {
}
/**
* @param int $id
* @return void
+13 -7
View File
@@ -17,25 +17,31 @@ class Client_BotClient extends Client_AbstractClient {
/**
* Does all the hard work.
* @param string $data
* @return string
* @param object $contentObject
* @return void
*/
public function processContentObject($contentObject) {
$return = "";
$data = $contentObject->getRawData();
if(strpos($data, "-") !== FALSE ) {
$data = explode("-", $data, 2);
$rawData = $contentObject->getRawData();
if(trim($rawData) === "list") {
$return = print_r(array("IDs" => $this->clientManager->getIDs(), "Groups" => $this->clientManager->getGroups()), TRUE);
}
if(strpos($rawData, "-") !== FALSE ) {
$data = explode("-", $rawData, 2);
$this->clientManager->sendToID((int) $data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
if(strpos($data, "+") !== FALSE ) {
$data = explode("+", $data, 2);
if(strpos($rawData, "+") !== FALSE ) {
$data = explode("+", $rawData, 2);
$this->clientManager->sendToGroup($data[0], $data[1]."\r\n");
$return = print_r($data, TRUE);
}
//TODO: implement this correctly
$return = str_replace("\n", "\r\n", $return);
$this->protocolHandler->sendRaw($return);
}
+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
+23 -15
View File
@@ -10,12 +10,12 @@ class Client_IrcClient extends Client_AbstractClient {
/**
* @var boolean
*/
protected $got_001;
protected $joined;
/**
* @var boolean
*/
protected $joined;
protected $got001;
/**
* @var boolean
@@ -53,44 +53,52 @@ class Client_IrcClient extends Client_AbstractClient {
*/
public function resetConnectionStatus() {
$this->lines = 0;
$this->got_001 = FALSE;
$this->got001 = FALSE;
$this->joined = FALSE;
$this->authed = FALSE;
}
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
* @return void
*/
public function initializeConnection() {
if(!$this->authed) {
$data = "USER poweruser as as :JPTs Bot\r\nNICK :" . $this->nick . "\r\n";
$this->authed = TRUE;
}
$this->protocolHandler->sendRaw($data);
}
/**
* Processes the resulting ContentObject from a ProtocolHandler.
* Does all the hard work.
* @param string $data
* @return void
*/
public function processContentObject($contentObject) {
protected function processContentObject($contentObject) {
$data = $contentObject->getRawData();
//DEBUG
//DEBUG
//var_dump($contentObject);
//echo "[RECV] ".$data;
//respond to pings
if($contentObject->getCommand() === "PING") $this->protocolHandler->pong($contentObject->getParams());
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data."\r\n\r\n");
$this->clientManager->sendToGroup("srv", "[#".$this->ID."] ".$data);
if($contentObject->getCommand() === "001") $this->got001 = TRUE;
$return = "";
if(preg_match("/001/", $data)) $this->got_001 = TRUE;
$this->lines++;
if(!$this->authed && $this->lines > 1) {
$return .= "USER as as as :Asdfg\r\nNICK :" . $this->nick . "\r\n";
$this->authed = TRUE;
}
if($this->got_001 && !$this->joined) {
if(!$this->joined && $this->got001) {
$this->joined = TRUE;
foreach($this->channels AS $channel) $return .= "JOIN " . $channel . "\r\n";
}
if(strpos($data, "hau ab") !== FALSE) {
if(strpos($data, "multivitamin") !== FALSE) {
$return .= "PRIVMSG ".$this->channels[0]." :roger that :D\r\n";
$return .= "QUIT :lol\r\n";
}