]> git.mxchange.org Git - hub.git/blob - application/hub/main/helper/connection/class_BaseConnectionHelper.php
685d079e31ff7fd774788dcbbe9de3a006cc66dd
[hub.git] / application / hub / main / helper / connection / class_BaseConnectionHelper.php
1 <?php
2 /**
3  * A general ConnectionHelper class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
25         // Exception codes
26         const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
27
28         /**
29          * Protocol used
30          */
31         private $protocol = 'invalid';
32
33         /**
34          * Port number used
35          */
36         private $port = 0;
37
38         /**
39          * (IP) Adress used
40          */
41         private $address = 0;
42
43         /**
44          * Sent data in bytes
45          */
46         private $sentData = 0;
47
48         /**
49          * Difference
50          */
51         private $diff = 0;
52
53         /**
54          * Whether this connection is initialized
55          */
56         private $isInitialized = false;
57
58         /**
59          * Whether this connection is shutted down
60          */
61         private $shuttedDown = false;
62
63         /**
64          * Currently queued chunks
65          */
66         private $queuedChunks = array();
67
68         /**
69          * Current final hash
70          */
71         private $currentFinalHash = '';
72
73         /**
74          * Protected constructor
75          *
76          * @param       $className      Name of the class
77          * @return      void
78          */
79         protected function __construct ($className) {
80                 // Call parent constructor
81                 parent::__construct($className);
82
83                 // Initialize output stream
84                 $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
85
86                 // And add it to this connection helper
87                 $this->setOutputStreamInstance($streamInstance);
88
89                 // Init state which sets the state to 'init'
90                 $this->initState();
91
92                 // Register this connection helper
93                 Registry::getRegistry()->addInstance('connection', $this);
94         }
95
96         /**
97          * Getter for real class name, overwrites generic method and is final
98          *
99          * @return      $class  Name of this class
100          */
101         public final function __toString () {
102                 // Class name representation
103                 $class = self::getConnectionClassName($this->getAddress(), $this->getPort(), parent::__toString());
104
105                 // Return it
106                 return $class;
107         }
108
109         /**
110          * Getter for port number to satify ProtocolHandler
111          *
112          * @return      $port   The port number
113          */
114         public final function getPort () {
115                 return $this->port;
116         }
117
118         /**
119          * Setter for port number to satify ProtocolHandler
120          *
121          * @param       $port   The port number
122          * @return      void
123          */
124         protected final function setPort ($port) {
125                 $this->port = $port;
126         }
127
128         /**
129          * Getter for protocol
130          *
131          * @return      $protocol       Used protocol
132          */
133         public final function getProtocol () {
134                 return $this->protocol;
135         }
136
137         /**
138          * Setter for protocol
139          *
140          * @param       $protocol       Used protocol
141          * @return      void
142          */
143         protected final function setProtocol ($protocol) {
144                 $this->protocol = $protocol;
145         }
146
147         /**
148          * Getter for IP address
149          *
150          * @return      $address        The IP address
151          */
152         public final function getAddress () {
153                 return $this->address;
154         }
155
156         /**
157          * Setter for IP address
158          *
159          * @param       $address        The IP address
160          * @return      void
161          */
162         protected final function setAddress ($address) {
163                 $this->address = $address;
164         }
165
166         /**
167          * Initializes the current connection
168          *
169          * @return      void
170          * @throws      SocketOptionException   If setting any socket option fails
171          */
172         protected function initConnection () {
173                 // Get socket resource
174                 $socketResource = $this->getSocketResource();
175
176                 // Set the option to reuse the port
177                 if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
178                         // Handle this socket error with a faked recipientData array
179                         $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
180
181                         // And throw again
182                         // @TODO Move this to the socket error handler
183                         throw new SocketOptionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
184                 } // END - if
185
186                 /*
187                  * Set socket to non-blocking mode before trying to establish a link to
188                  * it. This is now the default behaviour for all connection helpers who
189                  * call initConnection(); .
190                  */
191                 if (!socket_set_nonblock($socketResource)) {
192                         // Handle this socket error with a faked recipientData array
193                         $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0'));
194
195                         // And throw again
196                         throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
197                 } // END - if
198
199                 // Last step: mark connection as initialized
200                 $this->isInitialized = true;
201         }
202
203         /**
204          * Attempts to connect to a peer by given IP number and port from a valid
205          * recipientData array with currently configured timeout.
206          *
207          * @param       $recipientData  A valid recipient data array, 0=IP; 1=PORT
208          * @return      $isConnected    Whether the connection went fine
209          * @see         Please see http://de.php.net/manual/en/function.socket-connect.php#84465 for original code
210          * @todo        Rewrite the while() loop to a iterator to not let the software stay very long here
211          */
212         protected function connectToPeerByRecipientDataArray (array $recipientData) {
213                 // Only call this if the connection is initialized by initConnection()
214                 assert($this->isInitialized === true);
215
216                 // Get current time
217                 $time = time();
218
219                 // "Cache" socket resource and timeout config
220                 $socketResource = $this->getSocketResource();
221                 $timeout = $this->getConfigInstance()->getConfigEntry('socket_timeout_seconds');
222
223                 // Debug output
224                 $this->debugOutput('CONNECTION-HELPER: Trying to connect to ' . $recipientData[0] . ':' . $recipientData[1] . ' with socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' ...');
225
226                 // Try to connect until it is connected
227                 while ($isConnected = !@socket_connect($socketResource, $recipientData[0], $recipientData[1])) {
228                         // Get last socket error
229                         $socketError = socket_last_error($socketResource);
230
231                         // Skip any errors which may happen on non-blocking connections
232                         if (($socketError == SOCKET_EINPROGRESS) || ($socketError == SOCKET_EALREADY)) {
233                                 // Now, is that attempt within parameters?
234                                 if ((time() - $time) >= $timeout) {
235                                         // Didn't work within timeout
236                                         $isConnected = false;
237                                         break;
238                                 } // END - if
239
240                                 // Sleep about one second
241                                 $this->idle(1000);
242                         } elseif ($socketError != 0) {
243                                 // Stop on everything else pronto
244                                 $isConnected = false;
245                                 break;
246                         }
247                 } // END - while
248
249                 // Return status
250                 return $isConnected;
251         }
252
253         /**
254          * Static "getter" for this connection class' name
255          *
256          * @param       $address        IP address
257          * @param       $port           Port number
258          * @param       $className      Original class name
259          * @return      $class          Expanded class name
260          */
261         public static function getConnectionClassName ($address, $port, $className) {
262                 // Construct it
263                 $class = $address . ':' . $port . ':' . $className;
264
265                 // ... and return it
266                 return $class;
267         }
268
269         /**
270          * Initializes the peer's state which sets it to 'init'
271          *
272          * @return      void
273          */
274         private function initState() {
275                 /*
276                  * Get the state factory and create the initial state, we don't need
277                  * the state instance here
278                  */
279                 PeerStateFactory::createPeerStateInstanceByName('init', $this);
280         }
281
282         /**
283          * "Getter" for raw data from a package array. A fragmenter is used which
284          * will returns us only so many raw data which fits into the back buffer.
285          * The rest is being held in a back-buffer and waits there for the next
286          * cycle and while be then sent.
287          *
288          * This method does 4 simple steps:
289          * 1) Aquire fragmenter object instance from the factory
290          * 2) Handle over the package data array to the fragmenter
291          * 3) Request a chunk
292          * 4) Finally return the chunk (array) to the caller
293          *
294          * @param       $packageData    Raw package data array
295          * @return      $chunkData              Raw data chunk
296          */
297         private function getRawDataFromPackageArray (array $packageData) {
298                 // Get the fragmenter instance
299                 $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
300
301                 // Implode the package data array and fragement the resulting string, returns the final hash
302                 $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
303                 if ($finalHash !== true) {
304                         $this->currentFinalHash = $finalHash;
305                 } // END - if
306
307                 // Debug message
308                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: currentFinalHash=' . $this->currentFinalHash);
309
310                 // Get the next raw data chunk from the fragmenter
311                 $rawDataChunk = $fragmenterInstance->getNextRawDataChunk($this->currentFinalHash);
312
313                 // Get chunk hashes and chunk data
314                 $chunkHashes = array_keys($rawDataChunk);
315                 $chunkData   = array_values($rawDataChunk);
316
317                 // Is the required data there?
318                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
319                 if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
320                         // Remember this chunk as queued
321                         $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
322
323                         // Return the raw data
324                         return $chunkData[0];
325                 } else {
326                         // Return zero string
327                         return '';
328                 }
329         }
330
331         /**
332          * "Accept" a visitor by simply calling it back
333          *
334          * @param       $visitorInstance        A Visitor instance
335          * @return      void
336          */
337         protected final function accept (Visitor $visitorInstance) {
338                 // Just call the visitor
339                 $visitorInstance->visitConnectionHelper($this);
340         }
341
342         /**
343          * Sends raw package data to the recipient
344          *
345          * @param       $packageData            Raw package data
346          * @return      $totalSentBytes         Total sent bytes to the peer
347          * @throws      InvalidSocketException  If we got a problem with this socket
348          */
349         public function sendRawPackageData (array $packageData) {
350                 // The helper's state must be 'connected'
351                 $this->getStateInstance()->validatePeerStateConnected();
352
353                 // Cache buffer length
354                 $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
355
356                 // Init variables
357                 $rawData        = '';
358                 $dataStream     = ' ';
359                 $totalSentBytes = 0;
360
361                 // Fill sending buffer with data
362                 while ((strlen($rawData) < $bufferSize) && (strlen($dataStream) > 0)) {
363                         // Convert the package data array to a raw data stream
364                         $dataStream = $this->getRawDataFromPackageArray($packageData);
365                         //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
366                         $rawData .= $dataStream;
367                 } // END - while
368
369                 // Nothing to sent is bad news, so assert on it
370                 assert(strlen($rawData) > 0);
371
372                 // Encode the raw data with our output-stream
373                 $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
374
375                 // Calculate difference
376                 $this->diff = $bufferSize - strlen($encodedData);
377
378                 // Get socket resource
379                 $socketResource = $this->getSocketResource();
380
381                 // Init sent bytes
382                 $sentBytes = 0;
383
384                 // Deliver all data
385                 while ($sentBytes !== false) {
386                         // And deliver it
387                         //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
388                         $sentBytes = @socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
389
390                         // If there was an error, we don't continue here
391                         if ($sentBytes === false) {
392                                 // Handle the error with a faked recipientData array
393                                 $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
394
395                                 // And throw it
396                                 throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
397                         } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
398                                 // Nothing sent means we are done
399                                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
400                                 break;
401                         }
402
403                         // The difference between sent bytes and length of raw data should not go below zero
404                         assert((strlen($encodedData) - $sentBytes) >= 0);
405
406                         // Add total sent bytes
407                         $totalSentBytes += $sentBytes;
408
409                         // Cut out the last unsent bytes
410                         //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
411                         $encodedData = substr($encodedData, $sentBytes);
412
413                         // Calculate difference again
414                         $this->diff = $bufferSize - strlen($encodedData);
415
416                         // Can we abort?
417                         if (strlen($encodedData) <= 0) {
418                                 // Abort here, all sent!
419                                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
420                                 break;
421                         } // END - if
422                 } // END - while
423
424                 // Return sent bytes
425                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes);
426                 return $totalSentBytes;
427         }
428
429         /**
430          * Marks this connection as shutted down
431          *
432          * @return      void
433          */
434         protected final function markConnectionShuttedDown () {
435                 /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ' has been marked as shutted down');
436                 $this->shuttedDown = true;
437
438                 // And remove the (now invalid) socket
439                 $this->setSocketResource(false);
440         }
441
442         /**
443          * Getter for shuttedDown
444          *
445          * @return      $shuttedDown    Whether this connection is shutted down
446          */
447         public final function isShuttedDown () {
448                 /* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
449                 return $this->shuttedDown;
450         }
451
452         // ************************************************************************
453         //                 Socket error handler call-back methods
454         // ************************************************************************
455
456         /**
457          * Handles socket error 'connection timed out', but does not clear it for
458          * later debugging purposes.
459          *
460          * @param       $socketResource         A valid socket resource
461          * @return      void
462          * @throws      SocketConnectionException       The connection attempts fails with a time-out
463          */
464         protected function socketErrorConnectionTimedOutHandler ($socketResource) {
465                 // Get socket error code for verification
466                 $socketError = socket_last_error($socketResource);
467
468                 // Get error message
469                 $errorMessage = socket_strerror($socketError);
470
471                 // Shutdown this socket
472                 $this->shutdownSocket($socketResource);
473
474                 // Throw it again
475                 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
476         }
477
478         /**
479          * Handles socket error 'resource temporary unavailable', but does not
480          * clear it for later debugging purposes.
481          *
482          * @param       $socketResource         A valid socket resource
483          * @return      void
484          * @throws      SocketConnectionException       The connection attempts fails with a time-out
485          */
486         protected function socketErrorResourceUnavailableHandler ($socketResource) {
487                 // Get socket error code for verification
488                 $socketError = socket_last_error($socketResource);
489
490                 // Get error message
491                 $errorMessage = socket_strerror($socketError);
492
493                 // Shutdown this socket
494                 $this->shutdownSocket($socketResource);
495
496                 // Throw it again
497                 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
498         }
499
500         /**
501          * Handles socket error 'connection refused', but does not clear it for
502          * later debugging purposes.
503          *
504          * @param       $socketResource         A valid socket resource
505          * @return      void
506          * @throws      SocketConnectionException       The connection attempts fails with a time-out
507          */
508         protected function socketErrorConnectionRefusedHandler ($socketResource) {
509                 // Get socket error code for verification
510                 $socketError = socket_last_error($socketResource);
511
512                 // Get error message
513                 $errorMessage = socket_strerror($socketError);
514
515                 // Shutdown this socket
516                 $this->shutdownSocket($socketResource);
517
518                 // Throw it again
519                 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
520         }
521
522         /**
523          * Handles socket error 'no route to host', but does not clear it for later
524          * debugging purposes.
525          *
526          * @param       $socketResource         A valid socket resource
527          * @return      void
528          * @throws      SocketConnectionException       The connection attempts fails with a time-out
529          */
530         protected function socketErrorNoRouteToHostHandler ($socketResource) {
531                 // Get socket error code for verification
532                 $socketError = socket_last_error($socketResource);
533
534                 // Get error message
535                 $errorMessage = socket_strerror($socketError);
536
537                 // Shutdown this socket
538                 $this->shutdownSocket($socketResource);
539
540                 // Throw it again
541                 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
542         }
543
544         /**
545          * Handles socket error 'operation already in progress' which happens in
546          * method connectToPeerByRecipientDataArray() on timed out connection
547          * attempts.
548          *
549          * @param       $socketResource         A valid socket resource
550          * @return      void
551          * @throws      SocketConnectionException       The connection attempts fails with a time-out
552          */
553         protected function socketErrorOperationAlreadyProgressHandler ($socketResource) {
554                 // Get socket error code for verification
555                 $socketError = socket_last_error($socketResource);
556
557                 // Get error message
558                 $errorMessage = socket_strerror($socketError);
559
560                 // Half-shutdown this socket (see there for difference to shutdownSocket())
561                 $this->halfShutdownSocket($socketResource);
562
563                 // Throw it again
564                 throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
565         }
566
567         /**
568          * Handles socket "error" 'operation now in progress' which can be safely
569          * passed on with non-blocking connections.
570          *
571          * @param       $socketResource         A valid socket resource
572          * @return      void
573          */
574         protected function socketErrorOperationInProgressHandler ($socketResource) {
575                 $this->debugOutput('CONNECTION: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
576         }
577 }
578
579 // [EOF]
580 ?>