ircbot/Classes/Client/AbstractClient.php

123 lines
3.0 KiB
PHP

<?php
/**
* Abstract class that implements the basic api for any client.
* @author jpt
* @abstract
* @package Client
*/
abstract class Client_AbstractClient {
/**
* @var int
*/
protected $ID;
/**
* @var string
*/
protected $group;
/**
* @var mixed Instance of Protocol_AbstractProtocolHandler
*/
protected $protocolHandler;
/**
* Contains a reference to the ClientManager in order to change stuff on the fly.
* @var Client_ClientManager
*/
protected $clientManager;
/**
* Injects ProtocolHandler
* @param mixed $protocolHandler instance of Protocol_AbstractProtocolHandler
* @return void
* @throws Exception_WrongDatatypeException
*/
public function injectProtocolHandler($protocolHandler) {
if(!$protocolHandler instanceof Protocol_AbstractProtocolHandler) {
throw new Exception_WrongDatatypeException("Cannot inject ProtocolHandler! Expected an instance of Protocol_AbstractProtocolHandler, '" . get_type($protocolHandler) . "' given.", 1291156562);
}
$this->protocolHandler = $protocolHandler;
}
/**
* Forwards incoming data to the ProtocolHandler
* Let's the ProtocolHandler do all the work and forward its results to the Clients.
* Return resulting raw data.
* @param string $rawData
* @return string
*/
public function processRawData($rawData) {
$this->protocolHandler->pushRawData($rawData);
while($this->protocolHandler->canWork()) {
$this->processContentObject($this->protocolHandler->work());
}
return $this->protocolHandler->getRawData();
}
/**
* 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 void
*/
abstract protected function processContentObject($contentObject);
/**
* This function will load the given config.
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
/**
* Will reset the connectionStatus of the client.
* Implementation is optional and depends on the client.
* Should be used to reset internal variables.
* @return void
*/
public function resetConnectionStatus() {
}
/**
* 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
*/
public function setID($id) {
$this->ID = $id;
}
/**
* @param string $group
* @return void
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* Injects ClientManager
* @param Client_ClientManager $clientManager
* @return void
* @throws Exception_WrongDatatypeException
*/
public function injectClientManager($clientManager) {
if(!$clientManager instanceof Client_ClientManager) {
throw new Exception_WrongDatatypeException("Cannot inject ClientManager! Expected an instance of Client_ClientManager, '" . get_type($clientManager) . "' given.", 1291156565);
}
$this->clientManager = $clientManager;
}
}
?>