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 * Protected constructor
63 * @param $className Name of the class
66 protected function __construct ($className) {
67 // Call parent constructor
68 parent::__construct($className);
70 // Register this connection helper
71 Registry::getRegistry()->addInstance('connection', $this);
75 * Getter for port number to satify ProtocolHandler
77 * @return $port The port number
79 public final function getPort () {
84 * Setter for port number to satify ProtocolHandler
86 * @param $port The port number
89 protected final function setPort ($port) {
96 * @return $protocol Used protocol
98 public final function getProtocol () {
99 return $this->protocol;
103 * Setter for protocol
105 * @param $protocol Used protocol
108 protected final function setProtocol ($protocol) {
109 $this->protocol = $protocol;
113 * Getter for IP address
115 * @return $address The IP address
117 public final function getAddress () {
118 return $this->address;
122 * Setter for IP address
124 * @param $address The IP address
127 protected final function setAddress ($address) {
128 $this->address = $address;
132 * "Accept" a visitor by simply calling it back
134 * @param $visitorInstance A Visitor instance
137 protected final function accept (Visitor $visitorInstance) {
138 // Just call the visitor
139 $visitorInstance->visitConnectionHelper($this);
143 * "Getter" for raw data from a package array. A fragmenter is used which
144 * will returns us only so many raw data which fits into the back buffer.
145 * The rest is being held in a back-buffer and waits there for the next
146 * cycle and while be then sent. This is done by a FIFO.
148 * This method does 4 simple steps:
149 * 1) Aquire fragmenter object instance from the factory
150 * 2) Handle over the package data array to the fragmenter
151 * 3) Request a chunk (which "pops" the chunk from the fragmenter's FIFO)
152 * 4) Finally return the chunk to the caller
154 * @param $packageData Raw package data array
155 * @return $rawData Raw package data bytes
157 private function getRawDataFromPackageArray (array $packageData) {
158 // Get the fragmenter instance
159 $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class');
161 // Implode the package data array and fragement the resulting string
162 $fragmenterInstance->fragmentPackageArray($packageData, $this);
164 // Get the next raw data chunk from the fragmenter's FIFO
165 $rawData = $fragmenterInstance->getNextRawDataChunk($packageData);
166 /* DEBUG: */ $this->debugOutput('rawData['.strlen($rawData).']='.$rawData);
174 * Sends raw package data to the recipient
176 * @param $packageData Raw package data
177 * @return $sentBytes Actual sent bytes to the peer
178 * @throws InvalidSocketException If we got a problem with this socket
180 public function sendRawPackageData (array $packageData) {
181 // Convert the package data array to a raw data stream
182 $rawData = $this->getRawDataFromPackageArray($packageData);
184 // Get socket resource
185 $socketResource = $this->getSocketResource();
188 $sentBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
190 // If there was an error, we don't continue here
191 if ($sentBytes === false) {
192 // Get socket error code for verification
193 $socketError = socket_last_error($socketResource);
196 $errorMessage = socket_strerror($socketError);
198 // Shutdown this socket
199 $this->shutdownSocket($socketResource);
202 throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
205 // The difference between sent bytes and length of raw data should not be below zero
206 assert((strlen($rawData) - $sentBytes) >= 0);
213 * Getter for real class name
215 * @return $class Name of this class
217 public function __toString () {
218 // Class name representation
219 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
226 * Checks wether the connect retry is exhausted
228 * @return $isExhaused Wether connect retry is exchausted
230 public final function isConnectRetryExhausted () {
231 // Construct config entry
232 $configEntry = $this->getProtocol() . '_connect_retry_max';
235 $isExhausted = ($this->retryCount >= $this->getConfigInstance()->getConfigEntry($configEntry));
242 * Increases the connect retry count
246 public final function increaseConnectRetry () {
251 * Marks this connection as shutted down
255 protected final function markConnectionShutdown () {
256 $this->shuttedDown = true;
260 * Getter for shuttedDown
262 * @return $shuttedDown Wether this connection is shutted down
264 public final function isShuttedDown () {
265 return $this->shuttedDown;