]> git.mxchange.org Git - hub.git/blob - application/hub/main/listener/tcp/class_TcpListener.php
Moved a lot classes, added visitors and methods
[hub.git] / application / hub / main / listener / tcp / class_TcpListener.php
1 <?php
2 /**
3  * A TCP connection listener
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 TcpListener extends BaseListener implements Listenable {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Creates an instance of this class
37          *
38          * @param       $nodeInstance           A NodeHelper instance
39          * @return      $listenerInstance       An instance a prepared listener class
40          */
41         public final static function createTcpListener (NodeHelper $nodeInstance) {
42                 // Get new instance
43                 $listenerInstance = new TcpListener();
44
45                 // Set the application instance
46                 $listenerInstance->setNodeInstance($nodeInstance);
47
48                 // Set the protocol to TCP
49                 $listenerInstance->setProtocol('tcp');
50
51                 // Return the prepared instance
52                 return $listenerInstance;
53         }
54
55         /**
56          * Initializes the listener by setting up the required socket server
57          *
58          * @return      void
59          * @throws      InvalidSocketException  Thrown if the socket could not be initialized
60          */
61         public function initListener() {
62                 // Create a streaming socket, of type TCP/IP
63                 $mainSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
64
65                 // Is the socket resource valid?
66                 if (!is_resource($mainSocket)) {
67                         // Something bad happened
68                         throw new InvalidSocketException(array($this, gettype($mainSocket), 0, 'invalid'), BaseListener::EXCEPTION_INVALID_SOCKET);
69                 } // END - if
70
71                 // Get socket error code for verification
72                 $socketError = socket_last_error($mainSocket);
73
74                 // Check if there was an error else
75                 if ($socketError > 0) {
76                         // Then throw again
77                         throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
78                 } // END - if
79
80                 // Set the option to reuse the port
81                 if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
82                         // Get socket error code for verification
83                         $socketError = socket_last_error($mainSocket);
84
85                         // Get error message
86                         $errorMessage = socket_strerror($socketError);
87
88                         // Shutdown this socket
89                         $this->shutdownSocket($mainSocket);
90
91                         // And throw again
92                         throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
93                 } // END - if
94
95                 // "Bind" the socket to the given address, on given port so this means
96                 // that all connections on this port are now our resposibility to
97                 // send/recv data, disconnect, etc..
98                 if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
99                         // Get socket error code for verification
100                         $socketError = socket_last_error($mainSocket);
101
102                         // Get error message
103                         $errorMessage = socket_strerror($socketError);
104
105                         // Shutdown this socket
106                         $this->shutdownSocket($mainSocket);
107
108                         // And throw again
109                         throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
110                 } // END - if
111
112                 // Start listen for connections
113                 if (!socket_listen($mainSocket)) {
114                         // Get socket error code for verification
115                         $socketError = socket_last_error($mainSocket);
116
117                         // Get error message
118                         $errorMessage = socket_strerror($socketError);
119
120                         // Shutdown this socket
121                         $this->shutdownSocket($mainSocket);
122
123                         // And throw again
124                         throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
125                 } // END - if
126
127                 // Set the main socket
128                 $this->setSocketResource($mainSocket);
129
130                 // Initialize the client pool instance
131                 $poolInstance = ObjectFactory::createObjectByConfiguredName('client_pool_class', array($this));
132
133                 // Add main socket
134                 $poolInstance->addClient($mainSocket);
135
136                 // And add it to this listener
137                 $this->setPoolInstance($poolInstance);
138
139                 // Output message
140                 $this->debugOutput('LISTENER: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
141         }
142
143         /**
144          * "Listens" for incoming network packages
145          *
146          * @return      void
147          * @todo        0% done
148          */
149         public function doListen() {
150                 $this->partialStub('Need to implement this method.');
151         }
152 }
153
154 // [EOF]
155 ?>