]> git.mxchange.org Git - hub.git/blob - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Added a new task for listener pools and network package readers (for abstract Network...
[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         /**
26          * Protocol used
27          */
28         private $protocol = 'invalid';
29
30         /**
31          * Port number used
32          */
33         private $port = 0;
34
35         /**
36          * (IP) Adress used
37          */
38         private $address = 0;
39
40         /**
41          * Sent data in bytes
42          */
43         private $sentData = 0;
44
45         /**
46          * Difference
47          */
48         private $diff = 0;
49
50         /**
51          * Connect retries for this connection
52          */
53         private $retryCount = 0;
54
55         /**
56          * Wether this connection is shutted down
57          */
58         private $shuttedDown = false;
59
60         /**
61          * Currently queued chunks
62          */
63         private $queuedChunks = array();
64
65         /**
66          * Current final hash
67          */
68         private $currentFinalHash = '';
69
70         /**
71          * Protected constructor
72          *
73          * @param       $className      Name of the class
74          * @return      void
75          */
76         protected function __construct ($className) {
77                 // Call parent constructor
78                 parent::__construct($className);
79
80                 // Register this connection helper
81                 Registry::getRegistry()->addInstance('connection', $this);
82         }
83
84         /**
85          * Getter for port number to satify ProtocolHandler
86          *
87          * @return      $port   The port number
88          */
89         public final function getPort () {
90                 return $this->port;
91         }
92
93         /**
94          * Setter for port number to satify ProtocolHandler
95          *
96          * @param       $port   The port number
97          * @return      void
98          */
99         protected final function setPort ($port) {
100                 $this->port = $port;
101         }
102
103         /**
104          * Getter for protocol
105          *
106          * @return      $protocol       Used protocol
107          */
108         public final function getProtocol () {
109                 return $this->protocol;
110         }
111
112         /**
113          * Setter for protocol
114          *
115          * @param       $protocol       Used protocol
116          * @return      void
117          */
118         protected final function setProtocol ($protocol) {
119                 $this->protocol = $protocol;
120         }
121
122         /**
123          * Getter for IP address
124          *
125          * @return      $address        The IP address
126          */
127         public final function getAddress () {
128                 return $this->address;
129         }
130
131         /**
132          * Setter for IP address
133          *
134          * @param       $address        The IP address
135          * @return      void
136          */
137         protected final function setAddress ($address) {
138                 $this->address = $address;
139         }
140
141         /**
142          * "Getter" for raw data from a package array. A fragmenter is used which
143          * will returns us only so many raw data which fits into the back buffer.
144          * The rest is being held in a back-buffer and waits there for the next
145          * cycle and while be then sent.
146          *
147          * This method does 4 simple steps:
148          * 1) Aquire fragmenter object instance from the factory
149          * 2) Handle over the package data array to the fragmenter
150          * 3) Request a chunk
151          * 4) Finally return the chunk (array) to the caller
152          *
153          * @param       $packageData    Raw package data array
154          * @return      $chunkData              Raw data chunk
155          */
156         private function getRawDataFromPackageArray (array $packageData) {
157                 // If there is no fragmenter?
158                 if (!Registry::getRegistry()->instanceExists('package_fragmenter')) {
159                         // Get the fragmenter instance
160                         $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class');
161
162                         // Add it to the registry
163                         Registry::getRegistry()->addInstance('package_fragmenter', $fragmenterInstance);
164                 } else {
165                         // Get fragmenter from registry
166                         $fragmenterInstance = Registry::getRegistry()->getInstance('package_fragmenter');
167                 }
168
169                 // Implode the package data array and fragement the resulting string, returns the final hash
170                 $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
171                 if ($finalHash !== true) {
172                         $this->currentFinalHash = $finalHash;
173                 } // END - if
174
175                 // Debug message
176                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: currentFinalHash=' . $this->currentFinalHash);
177
178                 // Get the next raw data chunk from the fragmenter
179                 $rawDataChunk = $fragmenterInstance->getNextRawDataChunk($this->currentFinalHash);
180
181                 // Get chunk hashes and chunk data
182                 $chunkHashes = array_keys($rawDataChunk);
183                 $chunkData   = array_values($rawDataChunk);
184
185                 // Is the required data there?
186                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
187                 if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
188                         // Remember this chunk as queued
189                         $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
190
191                         // Return the raw data
192                         return $chunkData[0];
193                 } else {
194                         // Return zero string
195                         return '';
196                 }
197         }
198
199         /**
200          * "Accept" a visitor by simply calling it back
201          *
202          * @param       $visitorInstance        A Visitor instance
203          * @return      void
204          */
205         protected final function accept (Visitor $visitorInstance) {
206                 // Just call the visitor
207                 $visitorInstance->visitConnectionHelper($this);
208         }
209
210         /**
211          * Sends raw package data to the recipient
212          *
213          * @param       $packageData            Raw package data
214          * @return      $totalSentBytes         Total sent bytes to the peer
215          * @throws      InvalidSocketException  If we got a problem with this socket
216          */
217         public function sendRawPackageData (array $packageData) {
218                 // Cache buffer length
219                 $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
220
221                 // Init variables
222                 $rawData = '';
223                 $dataStream = ' ';
224                 $totalSentBytes = 0;
225
226                 // Fill sending buffer with data
227                 while ((strlen($rawData) < $bufferSize) && (strlen($dataStream) > 0)) {
228                         // Convert the package data array to a raw data stream
229                         $dataStream = $this->getRawDataFromPackageArray($packageData);
230                         //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
231                         $rawData .= $dataStream;
232                 } // END - while
233
234                 // Nothing to sent is bad news!
235                 assert(strlen($rawData) > 0);
236
237                 // Calculate difference
238                 $this->diff = $bufferSize - strlen($rawData);
239
240                 // Get socket resource
241                 $socketResource = $this->getSocketResource();
242
243                 // Init sent bytes
244                 $sentBytes = 0;
245
246                 // Deliver all data
247                 while ($sentBytes !== false) {
248                         // And deliver it
249                         //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($rawData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
250                         $sentBytes = @socket_write($socketResource, $rawData, ($bufferSize - $this->diff));
251
252                         // If there was an error, we don't continue here
253                         if ($sentBytes === false) {
254                                 // Get socket error code for verification
255                                 $socketError = socket_last_error($socketResource);
256
257                                 // Get error message
258                                 $errorMessage = socket_strerror($socketError);
259
260                                 // Shutdown this socket
261                                 $this->shutdownSocket($socketResource);
262
263                                 // And throw it
264                                 throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
265                         } elseif (($sentBytes == 0) && (strlen($rawData) > 0)) {
266                                 // Nothing sent means we are done
267                                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
268                                 break;
269                         }
270
271                         // The difference between sent bytes and length of raw data should not be below zero
272                         assert((strlen($rawData) - $sentBytes) >= 0);
273
274                         // Add total sent bytes
275                         $totalSentBytes += $sentBytes;
276
277                         // Cut out the last unsent bytes
278                         //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($rawData) . ' bytes ...');
279                         $rawData = substr($rawData, $sentBytes);
280
281                         // Calculate difference again
282                         $this->diff = $bufferSize - strlen($rawData);
283
284                         // Can we abort?
285                         if (strlen($rawData) <= 0) {
286                                 // Abort here, all sent!
287                                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
288                                 break;
289                         } // END - if
290                 } // END - while
291
292                 // Return sent bytes
293                 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes);
294                 return $totalSentBytes;
295         }
296
297         /**
298          * Getter for real class name
299          *
300          * @return      $class  Name of this class
301          */
302         public function __toString () {
303                 // Class name representation
304                 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
305
306                 // Return it
307                 return $class;
308         }
309
310         /**
311          * Checks wether the connect retry is exhausted
312          *
313          * @return      $isExhaused             Wether connect retry is exchausted
314          */
315         public final function isConnectRetryExhausted () {
316                 // Construct config entry
317                 $configEntry = $this->getProtocol() . '_connect_retry_max';
318
319                 // Check it out
320                 $isExhausted = ($this->retryCount >=  $this->getConfigInstance()->getConfigEntry($configEntry));
321
322                 // Return it
323                 return $isExhausted;
324         }
325
326         /**
327          * Increases the connect retry count
328          *
329          * @return      void
330          */
331         public final function increaseConnectRetry () {
332                 $this->retryCount++;
333         }
334
335         /**
336          * Marks this connection as shutted down
337          *
338          * @return      void
339          */
340         protected final function markConnectionShutdown () {
341                 $this->shuttedDown = true;
342         }
343
344         /**
345          * Getter for shuttedDown
346          *
347          * @return      $shuttedDown    Wether this connection is shutted down
348          */
349         public final function isShuttedDown () {
350                 return $this->shuttedDown;
351         }
352 }
353
354 // [EOF]
355 ?>