]> git.mxchange.org Git - hub.git/blob - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Introduced package fragmenter class to fragment network packages into smaller chunks
[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, 2010 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          * Offset
47          */
48         private $offset = 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          * Protected constructor
62          *
63          * @param       $className      Name of the class
64          * @return      void
65          */
66         protected function __construct ($className) {
67                 // Call parent constructor
68                 parent::__construct($className);
69
70                 // Register this connection helper
71                 Registry::getRegistry()->addInstance('connection', $this);
72         }
73
74         /**
75          * Getter for port number to satify ProtocolHandler
76          *
77          * @return      $port   The port number
78          */
79         public final function getPort () {
80                 return $this->port;
81         }
82
83         /**
84          * Setter for port number to satify ProtocolHandler
85          *
86          * @param       $port   The port number
87          * @return      void
88          */
89         protected final function setPort ($port) {
90                 $this->port = $port;
91         }
92
93         /**
94          * Getter for protocol
95          *
96          * @return      $protocol       Used protocol
97          */
98         public final function getProtocol () {
99                 return $this->protocol;
100         }
101
102         /**
103          * Setter for protocol
104          *
105          * @param       $protocol       Used protocol
106          * @return      void
107          */
108         protected final function setProtocol ($protocol) {
109                 $this->protocol = $protocol;
110         }
111
112         /**
113          * Getter for IP address
114          *
115          * @return      $address        The IP address
116          */
117         public final function getAddress () {
118                 return $this->address;
119         }
120
121         /**
122          * Setter for IP address
123          *
124          * @param       $address        The IP address
125          * @return      void
126          */
127         protected final function setAddress ($address) {
128                 $this->address = $address;
129         }
130
131         /**
132          * "Accept" a visitor by simply calling it back
133          *
134          * @param       $visitorInstance        A Visitor instance
135          * @return      void
136          */
137         protected final function accept (Visitor $visitorInstance) {
138                 // Just call the visitor
139                 $visitorInstance->visitConnectionHelper($this);
140         }
141
142         /**
143          * "Getter" for raw package data from a package array. This method does
144          * honor any unsend bytes in the back-buffer and the sending buffer size.
145          *
146          * @param       $packageData    Raw package data array
147          * @return      $rawData                Raw package data bytes
148          */
149         private function getRawDataFromPackageArray (array $packageData) {
150                 // Get the fragmenter instance
151                 $fragmenterInstance = ObjectFactory::createObjectByConfiguredName('package_fragmenter_class');
152                 $fragmenterInstance->debugInstance();
153
154                 // Return it
155                 return $rawData;
156         }
157
158         /**
159          * Sends raw package data to the recipient
160          *
161          * @param       $packageData    Raw package data
162          * @return      $sentBytes              Actual sent bytes to the peer
163          * @throws      InvalidSocketException  If we got a problem with this socket
164          */
165         public function sendRawPackageData (array $packageData) {
166                 // We need to "package" all data. This is done by a implode()
167                 $rawData = $this->getRawDataFromPackageArray($packageData);
168
169                 // Get socket resource
170                 $socketResource = $this->getSocketResource();
171
172                 // And deliver it
173                 $sentBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
174
175                 // If there was an error, we don't continue here
176                 if ($sentBytes === false) {
177                         // Get socket error code for verification
178                         $socketError = socket_last_error($socketResource);
179
180                         // Get error message
181                         $errorMessage = socket_strerror($socketError);
182
183                         // Shutdown this socket
184                         $this->shutdownSocket($socketResource);
185
186                         // And throw it
187                         throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
188                 } // END - if
189
190                 // The difference between sent bytes and length of raw data should not be below zero
191                 assert((strlen($rawData) - $sentBytes) >= 0);
192
193                 // Return sent bytes
194                 return $sentBytes;
195         }
196
197         /**
198          * Getter for real class name
199          *
200          * @return      $class  Name of this class
201          */
202         public function __toString () {
203                 // Class name representation
204                 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
205
206                 // Return it
207                 return $class;
208         }
209
210         /**
211          * Checks wether the connect retry is exhausted
212          *
213          * @return      $isExhaused             Wether connect retry is exchausted
214          */
215         public final function isConnectRetryExhausted () {
216                 // Construct config entry
217                 $configEntry = $this->getProtocol() . '_connect_retry_max';
218
219                 // Check it out
220                 $isExhausted = ($this->retryCount >=  $this->getConfigInstance()->getConfigEntry($configEntry));
221
222                 // Return it
223                 return $isExhausted;
224         }
225
226         /**
227          * Increases the connect retry count
228          *
229          * @return      void
230          */
231         public final function increaseConnectRetry () {
232                 $this->retryCount++;
233         }
234
235         /**
236          * Marks this connection as shutted down
237          *
238          * @return      void
239          */
240         protected final function markConnectionShutdown () {
241                 $this->shuttedDown = true;
242         }
243
244         /**
245          * Getter for shuttedDown
246          *
247          * @return      $shuttedDown    Wether this connection is shutted down
248          */
249         public final function isShuttedDown () {
250                 return $this->shuttedDown;
251         }
252 }
253
254 // [EOF]
255 ?>