3 namespace CoreFramework\Listener\Socket;
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Listener\BaseListener;
9 use CoreFramework\Listener\Listenable;
12 * A file-based socket listener
14 * @author Roland Haeder <webmaster@ship-simu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.ship-simu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class SocketFileListener extends BaseListener implements Listenable {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
43 // Set the protocol to file
44 $this->setProtocolName('file');
48 * Creates an instance of this class
50 * @return $listenerInstance An instance a prepared listener class
52 public final static function createSocketFileListener () {
54 $listenerInstance = new SocketFileListener();
56 // Return the prepared instance
57 return $listenerInstance;
61 * Initializes the listener by setting up the required socket server
65 public function initListener() {
67 $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
69 // Is the socket resource valid?
70 if (!is_resource($mainSocket)) {
71 // Something bad happened
72 throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
75 // Get socket error code for verification
76 $socketError = socket_last_error($mainSocket);
78 // Check if there was an error else
79 if ($socketError > 0) {
80 // Handle this socket error with a faked recipientData array
81 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('null', '0'));
85 $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
88 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...');
90 // File name must not be empty
91 assert(!empty($socketFile));
94 if ((FrameworkBootstrap::isReachableFilePath($socketFile)) && (file_exists($socketFile))) {
96 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.');
98 // Shutdown this socket
99 $this->shutdownSocket($mainSocket);
106 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...');
109 if (!socket_bind($mainSocket, $socketFile)) {
111 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
113 // Get socket error code for verification
114 $socketError = socket_last_error($mainSocket);
117 $errorMessage = socket_strerror($socketError);
119 // Shutdown this socket
120 $this->shutdownSocket($mainSocket);
123 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
127 // Start listen for connections
128 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
129 if (!socket_listen($mainSocket)) {
130 // Handle this socket error with a faked recipientData array
131 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
133 // Get socket error code for verification
134 $socketError = socket_last_error($mainSocket);
137 $errorMessage = socket_strerror($socketError);
139 // Shutdown this socket
140 $this->shutdownSocket($mainSocket);
143 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
147 // Now, we want non-blocking mode
148 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
149 if (!socket_set_nonblock($mainSocket)) {
150 // Handle this socket error with a faked recipientData array
151 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
153 // Get socket error code for verification
154 $socketError = socket_last_error($mainSocket);
157 $errorMessage = socket_strerror($socketError);
159 // Shutdown this socket
160 $this->shutdownSocket($mainSocket);
163 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
167 // Set the main socket
168 $this->registerServerSocketResource($mainSocket);
170 // Initialize the peer pool instance
171 $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this));
174 $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
176 // And add it to this listener
177 $this->setPoolInstance($poolInstance);
179 // Initialize iterator for listening on packages
180 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
182 // Rewind it and remember it in this class
183 $iteratorInstance->rewind();
184 $this->setIteratorInstance($iteratorInstance);
186 // Initialize the raw data handler
187 $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class');
189 // Set it in this class
190 $this->setHandlerInstance($handlerInstance);
193 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Socket listener now ready on socket ' . $socketFile . ' for service.');
197 * "Listens" for incoming network packages
201 public function doListen() {
203 $this->doListenSocketSelect('');
207 * Checks whether the listener would accept the given package data array
209 * @param $packageData Raw package data
210 * @return $accepts Whether this listener does accept
212 public function ifListenerAcceptsPackageData (array $packageData) {
213 $this->partialStub('Need to implement this method.');
217 * Monitors incoming raw data from the handler and transfers it to the
218 * given receiver instance.
222 public function monitorIncomingRawData () {
223 $this->partialStub('Need to implement this method.');