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