]> git.mxchange.org Git - hub.git/blob - application/hub/main/listener/class_BaseListener.php
Package class renamed, continued (sorry for lame description)
[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
28         /**
29          * Used protocol (Default: invalid, which is indeed invalid...)
30          */
31         private $protcol = 'invalid';
32
33         /**
34          * Address (IP mostly) we shall listen on
35          */
36         private $listenAddress = '0.0.0.0'; // This is the default and listens on all interfaces
37
38         /**
39          * Port we shall listen on (or wait for incoming data)
40          */
41         private $listenPort = 0; // This port MUST be changed by your application
42
43         /**
44          * Wether we are in blocking or non-blocking mode (default: non-blocking
45          */
46         private $blockingMode = false;
47
48         /**
49          * Socket resource
50          */
51         private $socketResource = false;
52
53         /**
54          * A client pool instance
55          */
56         private $poolInstance = null;
57
58         /**
59          * A network package handler instance
60          */
61         private $packageInstance = null;
62
63         /**
64          * Protected constructor
65          *
66          * @param       $className      Name of the class
67          * @return      void
68          */
69         protected function __construct ($className) {
70                 // Call parent constructor
71                 parent::__construct($className);
72         }
73
74         /**
75          * Setter for listen address
76          *
77          * @param       $listenAddress  The address this listener should listen on
78          * @return      void
79          */
80         protected final function setListenAddress ($listenAddress) {
81                 $this->listenAddress = (string) $listenAddress;
82         }
83
84         /**
85          * Getter for listen address
86          *
87          * @return      $listenAddress  The address this listener should listen on
88          */
89         public final function getListenAddress () {
90                 return $this->listenAddress;
91         }
92
93         /**
94          * Setter for listen port
95          *
96          * @param       $listenPort             The port this listener should listen on
97          * @return      void
98          */
99         protected final function setListenPort ($listenPort) {
100                 $this->listenPort = (int) $listenPort;
101         }
102
103         /**
104          * Getter for listen port
105          *
106          * @return      $listenPort             The port this listener should listen on
107          */
108         public final function getListenPort () {
109                 return $this->listenPort;
110         }
111
112         /**
113          * "Setter" to set listen address from configuration entry
114          *
115          * @param       $configEntry    The configuration entry holding our listen address
116          * @return      void
117          */
118         public final function setListenAddressByConfiguration ($configEntry) {
119                 $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
120         }
121
122         /**
123          * "Setter" to set listen port from configuration entry
124          *
125          * @param       $configEntry    The configuration entry holding our listen port
126          * @return      void
127          */
128         public final function setListenPortByConfiguration ($configEntry) {
129                 $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
130         }
131
132         /**
133          * Setter for protocol
134          *
135          * @param       $protocol       Used protocol
136          * @return      void
137          */
138         protected final function setProtocol ($protocol) {
139                 $this->protocol = (string) $protocol;
140         }
141
142         /**
143          * Getter for protocol
144          *
145          * @return      $protocol       Used protocol
146          */
147         public final function getProtocol () {
148                 return $this->protocol;
149         }
150
151         /**
152          * Setter for blocking-mode
153          *
154          * @param       $blockingMode   Wether blocking-mode is disabled (default) or enabled
155          * @return      void
156          */
157         protected final function setBlockingMode ($blockingMode) {
158                 $this->blockingMode = (boolean) $blockingMode;
159         }
160
161         /**
162          * Checks wether blocking-mode is enabled or disabled
163          *
164          * @return      $blockingMode   Wether blocking mode is disabled or enabled
165          */
166         public final function isBlockingModeEnabled () {
167                 return $this->blockingMode;
168         }
169
170         /**
171          * Setter for socket resource
172          *
173          * @param       $socketResource         The socket resource we shall set
174          * @return      void
175          */
176         protected final function setSocketResource ($socketResource) {
177                 $this->socketResource = $socketResource;
178         }
179
180         /**
181          * Getter for socket resource
182          *
183          * @return      $socketResource         The socket resource we shall set
184          */
185         public final function getSocketResource () {
186                 return $this->socketResource;
187         }
188
189         /**
190          * Setter for client pool instance
191          *
192          * @param       $poolInstance   The client pool instance we shall set
193          * @return      void
194          */
195         protected final function setPoolInstance (PoolableClient $poolInstance) {
196                 $this->poolInstance = $poolInstance;
197         }
198
199         /**
200          * Getter for client pool instance
201          *
202          * @return      $poolInstance   The client pool instance we shall set
203          */
204         protected final function getPoolInstance () {
205                 return $this->poolInstance;
206         }
207
208         /**
209          * Setter for network package handler instance
210          *
211          * @param       $packageInstance        The network package handler instance we shall set
212          * @return      void
213          */
214         protected final function setPackageInstance (Networkable $packageInstance) {
215                 $this->packageInstance = $packageInstance;
216         }
217
218         /**
219          * Getter for network package handler instance
220          *
221          * @return      $packageInstance        The network package handler instance we shall set
222          */
223         protected final function getPackageInstance () {
224                 return $this->packageInstance;
225         }
226
227         /**
228          * Accepts the visitor to process the visit "request"
229          *
230          * @param       $visitorInstance        An instance of a Visitor class
231          * @return      void
232          */
233         public function accept (Visitor $visitorInstance) {
234                 // Debug message
235                 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - START');
236
237                 // Visit this listener
238                 $visitorInstance->visitListener($this);
239
240                 // Visit the pool if set
241                 if ($this->getPoolInstance() instanceof Poolable) {
242                         $this->getPoolInstance()->accept($visitorInstance);
243                 } // END - if
244
245                 // Debug message
246                 //* DEBUG: */ $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - FINISHED');
247         }
248 }
249
250 // [EOF]
251 ?>