]> git.mxchange.org Git - hub.git/blob - application/hub/main/helper/connection/class_BaseConnectionHelper.php
b2b1d8121556854e84f2f3528c54b112c5069fb7
[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 Visitable 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          * Sends raw package data to the recipient
144          *
145          * @param       $packageData    Raw package data
146          * @return      void
147          *@ throws InvalidSocketException       If we got a problem with this socket
148          */
149         public function sendRawPackageData (array $packageData) {
150                 // We need to "package" all data. This is done by a implode()
151                 $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPERATOR, $packageData);
152
153                 // Get socket resource
154                 $socketResource = $this->getSocketResource();
155
156                 // And deliver it
157                 $numBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
158
159                 // If there was an error, we don't continue here
160                 if ($numBytes === false) {
161                         // Get socket error code for verification
162                         $socketError = socket_last_error($socketResource);
163
164                         // Get error message
165                         $errorMessage = socket_strerror($socketError);
166
167                         // Shutdown this socket
168                         $this->shutdownSocket($socketResource);
169
170                         // And throw it
171                         throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
172                 } // END - if
173
174                 // How much has been sent?
175                 die('num=' . $numBytes . "\n");
176         }
177
178         /**
179          * Getter for real class name
180          *
181          * @return      $class  Name of this class
182          */
183         public function __toString () {
184                 // Class name representation
185                 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
186
187                 // Return it
188                 return $class;
189         }
190
191         /**
192          * Checks wether the connect retry is exhausted
193          *
194          * @return      $isExhaused             Wether connect retry is exchausted
195          */
196         public final function isConnectRetryExhausted () {
197                 // Construct config entry
198                 $configEntry = $this->getProtocol() . '_connect_retry_max';
199
200                 // Check it out
201                 $isExhausted = ($this->retryCount >=  $this->getConfigInstance()->getConfigEntry($configEntry));
202
203                 // Return it
204                 return $isExhausted;
205         }
206
207         /**
208          * Increases the connect retry count
209          *
210          * @return      void
211          */
212         public final function increaseConnectRetry () {
213                 $this->retryCount++;
214         }
215
216         /**
217          * Marks this connection as shutted down
218          *
219          * @return      void
220          */
221         protected final function markConnectionShutdown () {
222                 $this->shuttedDown = true;
223         }
224
225         /**
226          * Getter for shuttedDown
227          *
228          * @return      $shuttedDown    Wether this connection is shutted down
229          */
230         public final function isShuttedDown () {
231                 return $this->shuttedDown;
232         }
233 }
234
235 // [EOF]
236 ?>