00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010 class Client_ClientManager {
00011
00012
00013
00014
00015 protected $connectionPool;
00016
00017
00018
00019
00020
00021 protected $clientPool;
00022
00023
00024
00025
00026 protected $rawRoutingRules;
00027
00028
00029
00030
00031
00032 protected $registeredProtocols;
00033
00034
00035
00036
00037
00038 protected $configPool;
00039
00040
00041
00042
00043
00044
00045
00046 function __construct($linebreak = "\r\n") {
00047 $this->connectionPool = new Connection_ConnectionPool($linebreak);
00048 $this->clientPool = array();
00049 $this->registeredProtocols = array();
00050 $this->rawRoutingRules = array();
00051 $this->configPool = array();
00052 }
00053
00054
00055
00056
00057
00058 function __destruct() {
00059 unset($this->clientPool);
00060 unset($this->connectionPool);
00061 }
00062
00063
00064
00065
00066
00067 public function countConnections() {
00068 return $this->connectionPool->countConnections();
00069 }
00070
00071
00072
00073
00074
00075
00076 public function work() {
00077 $connectionHandlers = $this->connectionPool->select();
00078 if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return;
00079 foreach($connectionHandlers["read"] AS $connectionHandler) {
00080
00081 if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
00082 $this->removeClientForConnectionHandler($connectionHandler);
00083 }
00084
00085
00086 if($connectionHandler->isListening() === TRUE) {
00087 $this->addClientForConnectionHandler($connectionHandler);
00088 }
00089
00090
00091 if(!isset($this->clientPool[$connectionHandler->getID()])) {
00092 $this->addClientForConnectionHandler($connectionHandler);
00093 }
00094
00095
00096 if(isset($this->clientPool[$connectionHandler->getID()])) {
00097
00098 while($data = $connectionHandler->read()) {
00099 $result = $this->clientPool[$connectionHandler->getID()]->processData($data);
00100 if($result !== "") {
00101 $connectionHandler->write($result);
00102 }
00103 }
00104 }
00105
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114 public function addRawRouting($from, $to) {
00115 $this->rawRoutingRules[] = array("");
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125 public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
00126 return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6);
00127 }
00128
00129
00130
00131
00132
00133 public function removeConnection($connectionHandler) {
00134 unset($this->configPool[$connectionHandler->getID()]);
00135 unset($this->clientPool[$connectionHandler->getID()]);
00136 $this->connectionPool->removeConnectionHandler($connectionHandler);
00137 }
00138
00139
00140
00141
00142
00143
00144
00145 public function registerProtocol($protocol, $className) {
00146 $this->registeredProtocols[$protocol] = $className;
00147 }
00148
00149
00150
00151
00152
00153
00154 public function unregisterProtocol($protocol) {
00155 unset($this->registeredProtocols[$protocol]);
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165 protected function createClientForProtocol($protocol) {
00166
00167 if(!isset($this->registeredProtocols[$protocol])) {
00168 throw new Exception_GeneralException("No client registered for protocol: '" . $protocol . "'!", 1290271651);
00169 }
00170 $className = $this->registeredProtocols[$protocol];
00171
00172 if(class_exists($className)) {
00173 return new $className();
00174 } else {
00175 throw new Exception_GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773);
00176 }
00177 }
00178
00179
00180
00181
00182
00183
00184 protected function addClientForConnectionHandler($connectionHandler) {
00185 $protocol = $connectionHandler->getProtocol();
00186
00187 if($protocol === "RAW") return;
00188 $client = $this->createClientForProtocol($protocol);
00189 if(isset($this->configPool[$connectionHandler->getID()])) {
00190 $client->loadConfig($this->configPool[$connectionHandler->getID()]);
00191 }
00192 $client->injectClientManager($this);
00193 $client->setID($connectionHandler->getID());
00194 $client->setGroup($connectionHandler->getGroup());
00195 $this->clientPool[$connectionHandler->getID()] = $client;
00196 }
00197
00198
00199
00200
00201
00202
00203 protected function removeClientForConnectionHandler($connectionHandler) {
00204 unset($this->clientPool[$connectionHandler->getID()]);
00205 $this->connectionPool->removeConnectionHandler($connectionHandler);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 public function attachConfig($config, $connectionHandler) {
00216 $this->configPool[$connectionHandler->getID()] = $config;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226 public function sendToID($id, $data) {
00227 return $this->connectionPool->writeToID($id, $data);
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 public function sendToGroup($group, $data) {
00238 return $this->connectionPool->writeToGroup($group, $data);
00239 }
00240
00241 }
00242 ?>