]> git.mxchange.org Git - hub.git/blob - application/hub/main/listener/class_BaseListener.php
0fce415d1de27246053ca13ea3582883f8cbffdf
[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 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          * Protected constructor
60          *
61          * @param       $className      Name of the class
62          * @return      void
63          */
64         protected function __construct ($className) {
65                 // Call parent constructor
66                 parent::__construct($className);
67         }
68
69         /**
70          * Setter for listen address
71          *
72          * @param       $listenAddress  The address this listener should listen on
73          * @return      void
74          */
75         protected final function setListenAddress ($listenAddress) {
76                 $this->listenAddress = (string) $listenAddress;
77         }
78
79         /**
80          * Getter for listen address
81          *
82          * @return      $listenAddress  The address this listener should listen on
83          */
84         public final function getListenAddress () {
85                 return $this->listenAddress;
86         }
87
88         /**
89          * Setter for listen port
90          *
91          * @param       $listenPort             The port this listener should listen on
92          * @return      void
93          */
94         protected final function setListenPort ($listenPort) {
95                 $this->listenPort = (int) $listenPort;
96         }
97
98         /**
99          * Getter for listen port
100          *
101          * @return      $listenPort             The port this listener should listen on
102          */
103         public final function getListenPort () {
104                 return $this->listenPort;
105         }
106
107         /**
108          * "Setter" to set listen address from configuration entry
109          *
110          * @param       $configEntry    The configuration entry holding our listen address
111          * @return      void
112          */
113         public final function setListenAddressByConfiguration ($configEntry) {
114                 $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
115         }
116
117         /**
118          * "Setter" to set listen port from configuration entry
119          *
120          * @param       $configEntry    The configuration entry holding our listen port
121          * @return      void
122          */
123         public final function setListenPortByConfiguration ($configEntry) {
124                 $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
125         }
126
127         /**
128          * Setter for protocol
129          *
130          * @param       $protocol       Used protocol
131          * @return      void
132          */
133         protected final function setProtocol ($protocol) {
134                 $this->protocol = (string) $protocol;
135         }
136
137         /**
138          * Getter for protocol
139          *
140          * @return      $protocol       Used protocol
141          */
142         public final function getProtocol () {
143                 return $this->protocol;
144         }
145
146         /**
147          * Setter for blocking-mode
148          *
149          * @param       $blockingMode   Wether blocking-mode is disabled (default) or enabled
150          * @return      void
151          */
152         protected final function setBlockingMode ($blockingMode) {
153                 $this->blockingMode = (boolean) $blockingMode;
154         }
155
156         /**
157          * Checks wether blocking-mode is enabled or disabled
158          *
159          * @return      $blockingMode   Wether blocking mode is disabled or enabled
160          */
161         public final function isBlockingModeEnabled () {
162                 return $this->blockingMode;
163         }
164
165         /**
166          * Setter for socket resource
167          *
168          * @param       $socketResource         The socket resource we shall set
169          * @return      void
170          */
171         protected final function setSocketResource ($socketResource) {
172                 $this->socketResource = $setSocketResource;
173         }
174
175         /**
176          * Getter for socket resource
177          *
178          * @return      $socketResource         The socket resource we shall set
179          */
180         protected final function getSocketResource () {
181                 return $this->socketResource;
182         }
183
184         /**
185          * Setter for client pool instance
186          *
187          * @param       $poolInstance   The client pool instance we shall set
188          * @return      void
189          */
190         protected final function setPoolInstance (PoolableClient $poolInstance) {
191                 $this->poolInstance = $setPoolInstance;
192         }
193
194         /**
195          * Getter for client pool instance
196          *
197          * @return      $poolInstance   The client pool instance we shall set
198          */
199         protected final function getPoolInstance () {
200                 return $this->poolInstance;
201         }
202
203         /**
204          * Accepts the visitor to rpocess the visit "request"
205          *
206          * @param       $visitorInstance        An instance of a Visitor class
207          * @return      void
208          */
209         public function accept (Visitor $visitorInstance) {
210                 // Debug message
211                 $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited - START');
212
213                 // Visit this listener
214                 $visitorInstance->visitListener($this);
215
216                 // Visit the pool
217                 $this->getPoolInstance()->accept($visitor);
218
219                 // Debug message
220                 $this->debugOutput('LISTENER: ' . $visitorInstance->__toString() . ' has visited - FINISHED');
221         }
222 }
223
224 // [EOF]
225 ?>