]> git.mxchange.org Git - hub.git/blob - application/hub/main/listener/class_BaseListener.php
New wider interface added, several refacturings:
[hub.git] / application / hub / main / listener / class_BaseListener.php
1 <?php
2 /**
3  * A general listener 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 BaseListener extends BaseHubSystem implements Visitable {
25         // Exception code constants
26         const EXCEPTION_INVALID_SOCKET            = 0xa00;
27         const EXCEPTION_SOCKET_ALREADY_REGISTERED = 0xa01;
28
29         /**
30          * Used protocol (Default: invalid, which is indeed invalid...)
31          */
32         private $protcol = 'invalid';
33
34         /**
35          * Address (IP mostly) we shall listen on
36          */
37         private $listenAddress = '0.0.0.0'; // This is the default and listens on all interfaces
38
39         /**
40          * Port we shall listen on (or wait for incoming data)
41          */
42         private $listenPort = 0; // This port MUST be changed by your application
43
44         /**
45          * Wether we are in blocking or non-blocking mode (default: non-blocking
46          */
47         private $blockingMode = false;
48
49         /**
50          * A client pool instance
51          */
52         private $poolInstance = null;
53
54         /**
55          * A network package handler instance
56          */
57         private $packageInstance = null;
58
59         /**
60          * Protected constructor
61          *
62          * @param       $className      Name of the class
63          * @return      void
64          */
65         protected function __construct ($className) {
66                 // Call parent constructor
67                 parent::__construct($className);
68         }
69
70         /**
71          * Checks wether the given socket resource is a server socket
72          *
73          * @param       $socketResource         A valid socket resource
74          * @return      $isServerSocket         Wether the socket resource is a server socket
75          */
76         protected function isServerSocketResource ($socketResource) {
77                 // Check it
78                 $isServerSocket = ((is_resource($socketResource)) && (!@socket_getpeername($socketResource, $peerName)));
79
80                 // We need to clear the error here
81                 socket_clear_error($socketResource);
82
83                 // Check peer name, it must be empty
84                 $isServerSocket = (($isServerSocket) && (empty($peerName)));
85
86                 // Return result
87                 return $isServerSocket;
88         }
89
90         /**
91          * Setter for listen address
92          *
93          * @param       $listenAddress  The address this listener should listen on
94          * @return      void
95          */
96         protected final function setListenAddress ($listenAddress) {
97                 $this->listenAddress = (string) $listenAddress;
98         }
99
100         /**
101          * Getter for listen address
102          *
103          * @return      $listenAddress  The address this listener should listen on
104          */
105         public final function getListenAddress () {
106                 return $this->listenAddress;
107         }
108
109         /**
110          * Setter for listen port
111          *
112          * @param       $listenPort             The port this listener should listen on
113          * @return      void
114          */
115         protected final function setListenPort ($listenPort) {
116                 $this->listenPort = (int) $listenPort;
117         }
118
119         /**
120          * Getter for listen port
121          *
122          * @return      $listenPort             The port this listener should listen on
123          */
124         public final function getListenPort () {
125                 return $this->listenPort;
126         }
127
128         /**
129          * "Setter" to set listen address from configuration entry
130          *
131          * @param       $configEntry    The configuration entry holding our listen address
132          * @return      void
133          */
134         public final function setListenAddressByConfiguration ($configEntry) {
135                 $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
136         }
137
138         /**
139          * "Setter" to set listen port from configuration entry
140          *
141          * @param       $configEntry    The configuration entry holding our listen port
142          * @return      void
143          */
144         public final function setListenPortByConfiguration ($configEntry) {
145                 $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
146         }
147
148         /**
149          * Setter for protocol
150          *
151          * @param       $protocol       Used protocol
152          * @return      void
153          */
154         protected final function setProtocol ($protocol) {
155                 $this->protocol = (string) $protocol;
156         }
157
158         /**
159          * Getter for protocol
160          *
161          * @return      $protocol       Used protocol
162          */
163         public final function getProtocol () {
164                 return $this->protocol;
165         }
166
167         /**
168          * Setter for blocking-mode
169          *
170          * @param       $blockingMode   Wether blocking-mode is disabled (default) or enabled
171          * @return      void
172          */
173         protected final function setBlockingMode ($blockingMode) {
174                 $this->blockingMode = (boolean) $blockingMode;
175         }
176
177         /**
178          * Checks wether blocking-mode is enabled or disabled
179          *
180          * @return      $blockingMode   Wether blocking mode is disabled or enabled
181          */
182         public final function isBlockingModeEnabled () {
183                 return $this->blockingMode;
184         }
185
186         /**
187          * Setter for client pool instance
188          *
189          * @param       $poolInstance   The client pool instance we shall set
190          * @return      void
191          */
192         protected final function setPoolInstance (PoolableClient $poolInstance) {
193                 $this->poolInstance = $poolInstance;
194         }
195
196         /**
197          * Getter for client pool instance
198          *
199          * @return      $poolInstance   The client pool instance we shall set
200          */
201         protected final function getPoolInstance () {
202                 return $this->poolInstance;
203         }
204
205         /**
206          * Setter for network package handler instance
207          *
208          * @param       $packageInstance        The network package handler instance we shall set
209          * @return      void
210          */
211         protected final function setPackageInstance (Networkable $packageInstance) {
212                 $this->packageInstance = $packageInstance;
213         }
214
215         /**
216          * Getter for network package handler instance
217          *
218          * @return      $packageInstance        The network package handler instance we shall set
219          */
220         protected final function getPackageInstance () {
221                 return $this->packageInstance;
222         }
223
224         /**
225          * Registeres the given socket resource for "this" listener instance. This
226          * will be done in a seperate class to allow package writers to use it
227          * again.
228          *
229          * @param       $socketResource         A valid server socket resource
230          * @return      void
231          * @throws      InvalidServerSocketException            If the given resource is no server socket
232          * @throws      SocketAlreadyRegisteredException        If the given resource is already registered
233          */
234         protected function registerServerSocketResource ($socketResource) {
235                 // First check if it is valid
236                 if (!$this->isServerSocketResource($socketResource)) {
237                         // No server socket
238                         throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET);
239                 } elseif ($this->isServerSocketRegistered($socketResource)) {
240                         // Already registered
241                         throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
242                 }
243
244                 // Get a socket registry instance (singleton)
245                 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
246
247                 // Register the socket
248                 $registryInstance->registerSocket($this, $socketResource);
249         }
250
251         /**
252          * Checks wether given socket resource is registered in socket registry
253          *
254          * @param       $socketResource         A valid server socket resource
255          * @return      $isRegistered           Wether given server socket is registered
256          */
257         protected function isServerSocketRegistered ($socketResource) {
258                 // Get a socket registry instance (singleton)
259                 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
260
261                 // Check it
262                 $isRegistered = $registryInstance->isSocketRegistered($this, $socketResource);
263
264                 // Return result
265                 return $isRegistered;
266         }
267
268         /**
269          * Getter for "this" socket resource
270          *
271          * @return      $socketResource         A valid socket resource
272          */
273         public final function getSocketResource () {
274                 // Get a socket registry instance (singleton)
275                 $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
276
277                 // Get the socket resource
278                 $socketResource = $registryInstance->getSocketResource($this);
279
280                 // Return it
281                 return $socketResource;
282         }
283
284         /**
285          * Accepts the visitor to process the visit "request"
286          *
287          * @param       $visitorInstance        An instance of a Visitor class
288          * @return      void
289          */
290         public function accept (Visitor $visitorInstance) {
291                 // Debug message
292                 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
293
294                 // Visit this listener
295                 $visitorInstance->visitListener($this);
296
297                 // Visit the pool if set
298                 if ($this->getPoolInstance() instanceof Poolable) {
299                         $this->getPoolInstance()->accept($visitorInstance);
300                 } // END - if
301
302                 // Debug message
303                 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');
304         }
305 }
306
307 // [EOF]
308 ?>