3 * A general ConnectionHelper 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 BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
28 private $protocol = 'invalid';
43 private $sentData = 0;
51 * Connect retries for this connection
53 private $retryCount = 0;
56 * Wether this connection is shutted down
58 private $shuttedDown = false;
61 * Currently queued chunks
63 private $queuedChunks = array();
68 private $currentFinalHash = '';
71 * Protected constructor
73 * @param $className Name of the class
76 protected function __construct ($className) {
77 // Call parent constructor
78 parent::__construct($className);
80 // Register this connection helper
81 Registry::getRegistry()->addInstance('connection', $this);
83 // Initialize output stream
84 $streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
86 // And add it to this connection helper
87 $this->setOutputStreamInstance($streamInstance);
91 * Getter for port number to satify ProtocolHandler
93 * @return $port The port number
95 public final function getPort () {
100 * Setter for port number to satify ProtocolHandler
102 * @param $port The port number
105 protected final function setPort ($port) {
110 * Getter for protocol
112 * @return $protocol Used protocol
114 public final function getProtocol () {
115 return $this->protocol;
119 * Setter for protocol
121 * @param $protocol Used protocol
124 protected final function setProtocol ($protocol) {
125 $this->protocol = $protocol;
129 * Getter for IP address
131 * @return $address The IP address
133 public final function getAddress () {
134 return $this->address;
138 * Setter for IP address
140 * @param $address The IP address
143 protected final function setAddress ($address) {
144 $this->address = $address;
148 * "Getter" for raw data from a package array. A fragmenter is used which
149 * will returns us only so many raw data which fits into the back buffer.
150 * The rest is being held in a back-buffer and waits there for the next
151 * cycle and while be then sent.
153 * This method does 4 simple steps:
154 * 1) Aquire fragmenter object instance from the factory
155 * 2) Handle over the package data array to the fragmenter
157 * 4) Finally return the chunk (array) to the caller
159 * @param $packageData Raw package data array
160 * @return $chunkData Raw data chunk
162 private function getRawDataFromPackageArray (array $packageData) {
163 // Get the fragmenter instance
164 $fragmenterInstance = FragmenterFactory::createFragmenterInstance('package');
166 // Implode the package data array and fragement the resulting string, returns the final hash
167 $finalHash = $fragmenterInstance->fragmentPackageArray($packageData, $this);
168 if ($finalHash !== true) {
169 $this->currentFinalHash = $finalHash;
173 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: currentFinalHash=' . $this->currentFinalHash);
175 // Get the next raw data chunk from the fragmenter
176 $rawDataChunk = $fragmenterInstance->getNextRawDataChunk($this->currentFinalHash);
178 // Get chunk hashes and chunk data
179 $chunkHashes = array_keys($rawDataChunk);
180 $chunkData = array_values($rawDataChunk);
182 // Is the required data there?
183 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: chunkHashes[]=' . count($chunkHashes) . ',chunkData[]=' . count($chunkData));
184 if ((isset($chunkHashes[0])) && (isset($chunkData[0]))) {
185 // Remember this chunk as queued
186 $this->queuedChunks[$chunkHashes[0]] = $chunkData[0];
188 // Return the raw data
189 return $chunkData[0];
191 // Return zero string
197 * "Accept" a visitor by simply calling it back
199 * @param $visitorInstance A Visitor instance
202 protected final function accept (Visitor $visitorInstance) {
203 // Just call the visitor
204 $visitorInstance->visitConnectionHelper($this);
208 * Sends raw package data to the recipient
210 * @param $packageData Raw package data
211 * @return $totalSentBytes Total sent bytes to the peer
212 * @throws InvalidSocketException If we got a problem with this socket
214 public function sendRawPackageData (array $packageData) {
215 // Cache buffer length
216 $bufferSize = $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length');
223 // Fill sending buffer with data
224 while ((strlen($rawData) < $bufferSize) && (strlen($dataStream) > 0)) {
225 // Convert the package data array to a raw data stream
226 $dataStream = $this->getRawDataFromPackageArray($packageData);
227 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Adding ' . strlen($dataStream) . ' bytes to the sending buffer ...');
228 $rawData .= $dataStream;
231 // Nothing to sent is bad news!
232 assert(strlen($rawData) > 0);
234 // Encode the raw data with our output-stream
235 $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
237 // Calculate difference
238 $this->diff = $bufferSize - strlen($encodedData);
240 // Get socket resource
241 $socketResource = $this->getSocketResource();
247 while ($sentBytes !== false) {
249 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sending out ' . strlen($encodedData) . ' bytes,bufferSize=' . $bufferSize . ',diff=' . $this->diff);
250 $sentBytes = @socket_write($socketResource, $encodedData, ($bufferSize - $this->diff));
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);
258 $errorMessage = socket_strerror($socketError);
260 // Shutdown this socket
261 $this->shutdownSocket($socketResource);
264 throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
265 } elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
266 // Nothing sent means we are done
267 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
271 // The difference between sent bytes and length of raw data should not be below zero
272 assert((strlen($encodedData) - $sentBytes) >= 0);
274 // Add total sent bytes
275 $totalSentBytes += $sentBytes;
277 // Cut out the last unsent bytes
278 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: Sent out ' . $sentBytes . ' of ' . strlen($encodedData) . ' bytes ...');
279 $encodedData = substr($encodedData, $sentBytes);
281 // Calculate difference again
282 $this->diff = $bufferSize - strlen($encodedData);
285 if (strlen($encodedData) <= 0) {
286 // Abort here, all sent!
287 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
293 //* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: totalSentBytes=' . $totalSentBytes);
294 return $totalSentBytes;
298 * Getter for real class name
300 * @return $class Name of this class
302 public function __toString () {
303 // Class name representation
304 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
311 * Checks wether the connect retry is exhausted
313 * @return $isExhaused Wether connect retry is exchausted
315 public final function isConnectRetryExhausted () {
316 // Construct config entry
317 $configEntry = $this->getProtocol() . '_connect_retry_max';
320 $isExhausted = ($this->retryCount >= $this->getConfigInstance()->getConfigEntry($configEntry));
327 * Increases the connect retry count
331 public final function increaseConnectRetry () {
336 * Marks this connection as shutted down
340 protected final function markConnectionShutdown () {
341 $this->shuttedDown = true;
345 * Getter for shuttedDown
347 * @return $shuttedDown Wether this connection is shutted down
349 public final function isShuttedDown () {
350 return $this->shuttedDown;