3 namespace CoreFramework\Listener\Socket;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Listener\Listenable;
10 * A file-based socket listener
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.ship-simu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class SocketFileListener extends BaseListener implements Listenable {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
41 // Set the protocol to file
42 $this->setProtocolName('file');
46 * Creates an instance of this class
48 * @return $listenerInstance An instance a prepared listener class
50 public final static function createSocketFileListener () {
52 $listenerInstance = new SocketFileListener();
54 // Return the prepared instance
55 return $listenerInstance;
59 * Initializes the listener by setting up the required socket server
63 public function initListener() {
65 $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
67 // Is the socket resource valid?
68 if (!is_resource($mainSocket)) {
69 // Something bad happened
70 throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
73 // Get socket error code for verification
74 $socketError = socket_last_error($mainSocket);
76 // Check if there was an error else
77 if ($socketError > 0) {
78 // Handle this socket error with a faked recipientData array
79 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('null', '0'));
83 $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
86 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...');
88 // File name must not be empty
89 assert(!empty($socketFile));
92 if ((self::isReachableFilePath($socketFile)) && (file_exists($socketFile))) {
94 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.');
96 // Shutdown this socket
97 $this->shutdownSocket($mainSocket);
104 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...');
107 if (!socket_bind($mainSocket, $socketFile)) {
109 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
111 // Get socket error code for verification
112 $socketError = socket_last_error($mainSocket);
115 $errorMessage = socket_strerror($socketError);
117 // Shutdown this socket
118 $this->shutdownSocket($mainSocket);
121 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
125 // Start listen for connections
126 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
127 if (!socket_listen($mainSocket)) {
128 // Handle this socket error with a faked recipientData array
129 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
131 // Get socket error code for verification
132 $socketError = socket_last_error($mainSocket);
135 $errorMessage = socket_strerror($socketError);
137 // Shutdown this socket
138 $this->shutdownSocket($mainSocket);
141 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
145 // Now, we want non-blocking mode
146 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
147 if (!socket_set_nonblock($mainSocket)) {
148 // Handle this socket error with a faked recipientData array
149 $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
151 // Get socket error code for verification
152 $socketError = socket_last_error($mainSocket);
155 $errorMessage = socket_strerror($socketError);
157 // Shutdown this socket
158 $this->shutdownSocket($mainSocket);
161 throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
165 // Set the main socket
166 $this->registerServerSocketResource($mainSocket);
168 // Initialize the peer pool instance
169 $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this));
172 $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
174 // And add it to this listener
175 $this->setPoolInstance($poolInstance);
177 // Initialize iterator for listening on packages
178 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
180 // Rewind it and remember it in this class
181 $iteratorInstance->rewind();
182 $this->setIteratorInstance($iteratorInstance);
184 // Initialize the raw data handler
185 $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class');
187 // Set it in this class
188 $this->setHandlerInstance($handlerInstance);
191 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Socket listener now ready on socket ' . $socketFile . ' for service.');
195 * "Listens" for incoming network packages
199 public function doListen() {
201 $this->doListenSocketSelect('');
205 * Checks whether the listener would accept the given package data array
207 * @param $packageData Raw package data
208 * @return $accepts Whether this listener does accept
210 public function ifListenerAcceptsPackageData (array $packageData) {
211 $this->partialStub('Need to implement this method.');
215 * Monitors incoming raw data from the handler and transfers it to the
216 * given receiver instance.
220 public function monitorIncomingRawData () {
221 $this->partialStub('Need to implement this method.');