]> git.mxchange.org Git - hub.git/blob - application/hub/main/helper/connection/class_BaseConnectionHelper.php
Many classes/interfaces added/continued:
[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          * Protected constructor
52          *
53          * @param       $className      Name of the class
54          * @return      void
55          */
56         protected function __construct ($className) {
57                 // Call parent constructor
58                 parent::__construct($className);
59
60                 // Register this connection helper
61                 Registry::getRegistry()->addInstance('connection', $this);
62         }
63
64         /**
65          * Getter for port number to satify ProtocolHandler
66          *
67          * @return      $port   The port number
68          */
69         public final function getPort () {
70                 return $this->port;
71         }
72
73         /**
74          * Setter for port number to satify ProtocolHandler
75          *
76          * @param       $port   The port number
77          * @return      void
78          */
79         protected final function setPort ($port) {
80                 $this->port = $port;
81         }
82
83         /**
84          * Getter for protocol
85          *
86          * @return      $protocol       Used protocol
87          */
88         public final function getProtocol () {
89                 return $this->protocol;
90         }
91
92         /**
93          * Setter for protocol
94          *
95          * @param       $protocol       Used protocol
96          * @return      void
97          */
98         protected final function setProtocol ($protocol) {
99                 $this->protocol = $protocol;
100         }
101
102         /**
103          * Getter for IP address
104          *
105          * @return      $address        The IP address
106          */
107         public final function getAddress () {
108                 return $this->address;
109         }
110
111         /**
112          * Setter for IP address
113          *
114          * @param       $address        The IP address
115          * @return      void
116          */
117         protected final function setAddress ($address) {
118                 $this->address = $address;
119         }
120
121         /**
122          * Sends raw package data to the recipient
123          *
124          * @param       $packageData    Raw package data
125          * @return      void
126          *@ throws InvalidSocketException       If we got a problem with this socket
127          */
128         public function sendRawPackageData (array $packageData) {
129                 // We need to "package" all data. This is done by a implode()
130                 $rawData = implode(NetworkPackage::PACKAGE_DATA_SEPERATOR, $packageData);
131
132                 // Get socket resource
133                 $socketResource = $this->getSocketResource();
134
135                 // And deliver it
136                 $numBytes = @socket_write($socketResource, $rawData, $this->getConfigInstance()->getConfigEntry($this->getProtocol() . '_buffer_length') - $this->offset);
137
138                 // If there was an error, we don't continue here
139                 if ($numBytes === false) {
140                         // Get socket error code for verification
141                         $socketError = socket_last_error($socketResource);
142
143                         // Get error message
144                         $errorMessage = socket_strerror($socketError);
145
146                         // Shutdown this socket
147                         $this->shutdownSocket($socketResource);
148
149                         // And throw it
150                         throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
151                 } // END - if
152
153                 // How much has been sent?
154                 die('num='.$numBytes."\n");
155         }
156
157         /**
158          * Getter for real class name
159          *
160          * @return      $class  Name of this class
161          */
162         public function __toString () {
163                 // Class name representation
164                 $class = $this->getAddress() . ':' . $this->getPort() . ':' . parent::__toString();
165
166                 // Return it
167                 return $class;
168         }
169 }
170
171 // [EOF]
172 ?>