3 * A general hub system class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseHubSystem extends BaseFrameworkSystem {
26 const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
29 * Seperator for all bootstrap node entries
31 const BOOTSTRAP_NODES_SEPERATOR = ';';
34 * An instance of a node
36 private $nodeInstance = NULL;
39 * An instance of a cruncher
41 private $cruncherInstance = NULL;
46 private $listenerInstance = NULL;
49 * A network package handler instance
51 private $packageInstance = NULL;
54 * A Receivable instance
56 private $receiverInstance = NULL;
61 private $stateInstance = NULL;
64 * Listener pool instance
66 private $listenerPoolInstance = NULL;
69 * Protected constructor
71 * @param $className Name of the class
74 protected function __construct ($className) {
75 // Call parent constructor
76 parent::__construct($className);
80 * Getter for node instance
82 * @return $nodeInstance An instance of a node node
84 public final function getNodeInstance () {
85 return $this->nodeInstance;
89 * Setter for node instance
91 * @param $nodeInstance An instance of a node node
94 protected final function setNodeInstance (NodeHelper $nodeInstance) {
95 $this->nodeInstance = $nodeInstance;
99 * Getter for cruncher instance
101 * @return $cruncherInstance An instance of a cruncher cruncher
103 public final function getCruncherInstance () {
104 return $this->cruncherInstance;
108 * Setter for cruncher instance
110 * @param $cruncherInstance An instance of a cruncher cruncher
113 protected final function setCruncherInstance (CruncherHelper $cruncherInstance) {
114 $this->cruncherInstance = $cruncherInstance;
118 * Setter for listener instance
120 * @param $listenerInstance A Listenable instance
123 protected final function setListenerInstance (Listenable $listenerInstance) {
124 $this->listenerInstance = $listenerInstance;
128 * Getter for listener instance
130 * @return $listenerInstance A Listenable instance
132 protected final function getListenerInstance () {
133 return $this->listenerInstance;
137 * Setter for network package handler instance
139 * @param $packageInstance The network package handler instance we shall set
142 protected final function setPackageInstance (Networkable $packageInstance) {
143 $this->packageInstance = $packageInstance;
147 * Getter for network package handler instance
149 * @return $packageInstance The network package handler instance we shall set
151 protected final function getPackageInstance () {
152 return $this->packageInstance;
156 * Setter for receiver instance
158 * @param $receiverInstance A Receivable instance we shall set
161 protected final function setReceiverInstance (Receivable $receiverInstance) {
162 $this->receiverInstance = $receiverInstance;
166 * Getter for receiver instance
168 * @return $receiverInstance A Receivable instance we shall get
170 protected final function getReceiverInstance () {
171 return $this->receiverInstance;
175 * Setter for state instance
177 * @param $stateInstance A Stateable instance
180 public final function setStateInstance (Stateable $stateInstance) {
181 $this->stateInstance = $stateInstance;
185 * Getter for state instance
187 * @return $stateInstance A Stateable instance
189 public final function getStateInstance () {
190 return $this->stateInstance;
194 * Setter for listener pool instance
196 * @param $listenerPoolInstance Our new listener pool instance
199 protected final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) {
200 $this->listenerPoolInstance = $listenerPoolInstance;
204 * Getter for listener pool instance
206 * @return $listenerPoolInstance Our current listener pool instance
208 public final function getListenerPoolInstance () {
209 return $this->listenerPoolInstance;
213 * Constructs a callable method name from given socket error code. If the
214 * method is not found, a generic one is used.
216 * @param $errorCode Error code from socket_last_error()
217 * @return $methodName Call-back method name for the error handler
218 * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented
220 protected function getSocketErrorHandlerFromCode ($errorCode) {
221 // Set NULL, so everyone is forced to implement socket error handlers
224 // Temporary create a possible name from translated error code
225 $handlerName = 'socketError' . $this->convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler';
227 // Is the call-back method there?
228 if (!method_exists($this, $handlerName)) {
229 // Please implement this
230 throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), self::EXCEPTION_UNSUPPORTED_ERROR_HANDLER);
238 * Handles socket error for given socket resource and peer data. This method
239 * validates $socketResource if it is a valid resource (see is_resource())
240 * but assumes valid data in array $recipientData, except that
241 * count($recipientData) is always 2.
243 * @param $socketResource A valid socket resource
244 * @param $recipientData An array with two elements: 0=IP number, 1=port number
246 * @throws InvalidSocketException If $socketResource is no socket resource
247 * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
249 protected final function handleSocketError ($socketResource, array $recipientData) {
250 // This method handles only socket resources
251 if (!is_resource($socketResource)) {
252 // No resource, abort here
253 throw new InvalidSocketException(array($this, $socketResource), BaseListener::EXCEPTION_INVALID_SOCKET);
256 // Check count of array, should be two
257 assert(count($recipientData) == 2);
259 // Get error code for first validation (0 is not an error)
260 $errorCode = socket_last_error($socketResource);
262 // If the error code is zero, someone called this method without an error
263 if ($errorCode == 0) {
264 // No error detected (or previously cleared outside this method)
265 throw new NoSocketErrorDetectedException(array($this, $socketResource), BaseListener::EXCEPTION_NO_SOCKET_ERROR);
268 // Get handler (method) name
269 $handlerName = $this->getSocketErrorHandlerFromCode($errorCode);
271 // Call-back the error handler method
272 call_user_func(array($this, $handlerName), $socketResource);
274 // Finally clear the error because it has been handled
275 socket_clear_error($socketResource);
279 * Translates socket error codes into our own internal names which can be
280 * used for call-backs.
282 * @param $errorCode The error code from socket_last_error() to be translated
283 * @return $errorName The translated name (all lower-case, with underlines)
285 public function translateSocketErrorCodeToName ($errorCode) {
286 // Nothing bad happened by default
287 $errorName = BaseRawDataHandler::SOCKET_CONNECTED;
289 // Is the code a number, then we have to change it
290 switch ($errorCode) {
291 case 0: // Silently ignored, the socket is connected
294 case 107: // "Transport end-point not connected"
295 case 134: // On some (?) systems for 'transport end-point not connected'
296 // @TODO On some systems it is 134, on some 107?
297 $errorName = BaseRawDataHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT;
300 case 110: // "Connection timed out"
301 $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_TIMED_OUT;
304 case 111: // "Connection refused"
305 $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_REFUSED;
308 default: // Everything else <> 0
309 // Unhandled error code detected, so first debug it because we may want to handle it like the others
310 $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode));
312 // Change it only in this class
313 $errorName = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN;
317 // Return translated name
322 * Shuts down a given socket resource. This method does only ease calling
325 * @param $socketResource A valid socket resource
328 public function shutdownSocket ($socketResource) {
330 $this->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
332 // Set socket resource
333 $this->setSocketResource($socketResource);
335 // Get a visitor instance
336 $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class');
339 $this->accept($visitorInstance);
343 * "Getter" for a printable state name
345 * @return $stateName Name of the node's state in a printable format
347 public final function getPrintableState () {
351 // Get the state instance
352 $stateInstance = $this->getStateInstance();
354 // Is it an instance of Stateable?
355 if ($stateInstance instanceof Stateable) {
356 // Then use that state name
357 $stateName = $stateInstance->getStateName();