3 * A file-based socket listener
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
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.
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.
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/>.
24 class SocketFileListener extends BaseListener implements Listenable {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
34 // Set the protocol to file
35 $this->setProtocolName('file');
39 * Creates an instance of this class
41 * @return $listenerInstance An instance a prepared listener class
43 public final static function createSocketFileListener () {
45 $listenerInstance = new SocketFileListener();
47 // Return the prepared instance
48 return $listenerInstance;
52 * Initializes the listener by setting up the required socket server
56 public function initListener() {
58 $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
60 // Is the socket resource valid?
61 if (!is_resource($mainSocket)) {
62 // Something bad happened
63 throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
66 // Get socket error code for verification
67 $socketError = socket_last_error($mainSocket);
69 // Check if there was an error else
70 if ($socketError > 0) {
71 // Handle this socket error with a faked recipientData array
72 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('null', '0'));
76 $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
79 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...');
81 // File name must not be empty
82 assert(!empty($socketFile));
85 if ((self::isReachableFilePath($socketFile)) && (file_exists($socketFile))) {
87 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.');
89 // Shutdown this socket
90 $this->shutdownSocket($mainSocket);
97 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...');
100 if (!socket_bind($mainSocket, $socketFile)) {
102 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
104 // Get socket error code for verification
105 $socketError = socket_last_error($mainSocket);
108 $errorMessage = socket_strerror($socketError);
110 // Shutdown this socket
111 $this->shutdownSocket($mainSocket);
114 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
118 // Start listen for connections
119 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
120 if (!socket_listen($mainSocket)) {
121 // Handle this socket error with a faked recipientData array
122 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
124 // Get socket error code for verification
125 $socketError = socket_last_error($mainSocket);
128 $errorMessage = socket_strerror($socketError);
130 // Shutdown this socket
131 $this->shutdownSocket($mainSocket);
134 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
138 // Now, we want non-blocking mode
139 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
140 if (!socket_set_nonblock($mainSocket)) {
141 // Handle this socket error with a faked recipientData array
142 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
144 // Get socket error code for verification
145 $socketError = socket_last_error($mainSocket);
148 $errorMessage = socket_strerror($socketError);
150 // Shutdown this socket
151 $this->shutdownSocket($mainSocket);
154 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
158 // Set the main socket
159 $this->registerServerSocketResource($mainSocket);
161 // Initialize the peer pool instance
162 $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this));
165 $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
167 // And add it to this listener
168 $this->setPoolInstance($poolInstance);
170 // Initialize iterator for listening on packages
171 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
173 // Rewind it and remember it in this class
174 $iteratorInstance->rewind();
175 $this->setIteratorInstance($iteratorInstance);
177 // Initialize the raw data handler
178 $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class');
180 // Set it in this class
181 $this->setHandlerInstance($handlerInstance);
184 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: TCP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
188 * "Listens" for incoming network packages
193 public function doListen() {
194 $this->partialStub('Need to implement this method.');
198 * Checks whether the listener would accept the given package data array
200 * @param $packageData Raw package data
201 * @return $accepts Whether this listener does accept
203 public function ifListenerAcceptsPackageData (array $packageData) {
204 $this->partialStub('Need to implement this method.');
208 * Monitors incoming raw data from the handler and transfers it to the
209 * given receiver instance.
213 public function monitorIncomingRawData () {
214 $this->partialStub('Need to implement this method.');