3 namespace CoreFramework\Listener\Socket;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
9 * A file-based socket listener
11 * @author Roland Haeder <webmaster@ship-simu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.ship-simu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class SocketFileListener extends BaseListener implements Listenable {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
40 // Set the protocol to file
41 $this->setProtocolName('file');
45 * Creates an instance of this class
47 * @return $listenerInstance An instance a prepared listener class
49 public final static function createSocketFileListener () {
51 $listenerInstance = new SocketFileListener();
53 // Return the prepared instance
54 return $listenerInstance;
58 * Initializes the listener by setting up the required socket server
62 public function initListener() {
64 $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
66 // Is the socket resource valid?
67 if (!is_resource($mainSocket)) {
68 // Something bad happened
69 throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
72 // Get socket error code for verification
73 $socketError = socket_last_error($mainSocket);
75 // Check if there was an error else
76 if ($socketError > 0) {
77 // Handle this socket error with a faked recipientData array
78 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('null', '0'));
82 $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
85 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...');
87 // File name must not be empty
88 assert(!empty($socketFile));
91 if ((self::isReachableFilePath($socketFile)) && (file_exists($socketFile))) {
93 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.');
95 // Shutdown this socket
96 $this->shutdownSocket($mainSocket);
103 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...');
106 if (!socket_bind($mainSocket, $socketFile)) {
108 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
110 // Get socket error code for verification
111 $socketError = socket_last_error($mainSocket);
114 $errorMessage = socket_strerror($socketError);
116 // Shutdown this socket
117 $this->shutdownSocket($mainSocket);
120 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
124 // Start listen for connections
125 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
126 if (!socket_listen($mainSocket)) {
127 // Handle this socket error with a faked recipientData array
128 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
130 // Get socket error code for verification
131 $socketError = socket_last_error($mainSocket);
134 $errorMessage = socket_strerror($socketError);
136 // Shutdown this socket
137 $this->shutdownSocket($mainSocket);
140 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
144 // Now, we want non-blocking mode
145 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
146 if (!socket_set_nonblock($mainSocket)) {
147 // Handle this socket error with a faked recipientData array
148 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
150 // Get socket error code for verification
151 $socketError = socket_last_error($mainSocket);
154 $errorMessage = socket_strerror($socketError);
156 // Shutdown this socket
157 $this->shutdownSocket($mainSocket);
160 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
164 // Set the main socket
165 $this->registerServerSocketResource($mainSocket);
167 // Initialize the peer pool instance
168 $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this));
171 $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
173 // And add it to this listener
174 $this->setPoolInstance($poolInstance);
176 // Initialize iterator for listening on packages
177 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
179 // Rewind it and remember it in this class
180 $iteratorInstance->rewind();
181 $this->setIteratorInstance($iteratorInstance);
183 // Initialize the raw data handler
184 $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class');
186 // Set it in this class
187 $this->setHandlerInstance($handlerInstance);
190 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Socket listener now ready on socket ' . $socketFile . ' for service.');
194 * "Listens" for incoming network packages
198 public function doListen() {
200 $this->doListenSocketSelect('');
204 * Checks whether the listener would accept the given package data array
206 * @param $packageData Raw package data
207 * @return $accepts Whether this listener does accept
209 public function ifListenerAcceptsPackageData (array $packageData) {
210 $this->partialStub('Need to implement this method.');
214 * Monitors incoming raw data from the handler and transfers it to the
215 * given receiver instance.
219 public function monitorIncomingRawData () {
220 $this->partialStub('Need to implement this method.');