0010797edf67c2c887194edd88c2f92abad4250d
[core.git] / framework / main / classes / listener / socket / class_SocketFileListener.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Listener\Socket;
4
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Listener\BaseListener;
9 use CoreFramework\Listener\Listenable;
10
11 /**
12  * A file-based socket listener
13  *
14  * @author              Roland Haeder <webmaster@ship-simu.org>
15  * @version             0.0.0
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
19  *
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.
24  *
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.
29  *
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/>.
32  */
33 class SocketFileListener extends BaseListener implements Listenable {
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42
43                 // Set the protocol to file
44                 $this->setProtocolName('file');
45         }
46
47         /**
48          * Creates an instance of this class
49          *
50          * @return      $listenerInstance       An instance a prepared listener class
51          */
52         public final static function createSocketFileListener () {
53                 // Get new instance
54                 $listenerInstance = new SocketFileListener();
55
56                 // Return the prepared instance
57                 return $listenerInstance;
58         }
59
60         /**
61          * Initializes the listener by setting up the required socket server
62          *
63          * @return      void
64          */
65         public function initListener() {
66                 // Init socket
67                 $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
68
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);
73                 } // END - if
74
75                 // Get socket error code for verification
76                 $socketError = socket_last_error($mainSocket);
77
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'));
82                 } // END - if
83
84                 // Create file name
85                 $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
86
87                 // Debug message
88                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...');
89
90                 // File name must not be empty
91                 assert(!empty($socketFile));
92
93                 // Is the file there?
94                 if ((FrameworkBootstrap::isReachableFilePath($socketFile)) && (file_exists($socketFile))) {
95                         // Old socket found
96                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.');
97
98                         // Shutdown this socket
99                         $this->shutdownSocket($mainSocket);
100
101                         // Quit here
102                         exit;
103                 } // END - if
104
105                 // Debug message
106                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...');
107
108                 // Try to bind to it
109                 if (!socket_bind($mainSocket, $socketFile)) {
110                         // Handle error here
111                         $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
112                         /*
113                         // Get socket error code for verification
114                         $socketError = socket_last_error($mainSocket);
115
116                         // Get error message
117                         $errorMessage = socket_strerror($socketError);
118
119                         // Shutdown this socket
120                         $this->shutdownSocket($mainSocket);
121
122                         // And throw again
123                         throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
124                         */
125                 } // END - if
126
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'));
132                         /*
133                         // Get socket error code for verification
134                         $socketError = socket_last_error($mainSocket);
135
136                         // Get error message
137                         $errorMessage = socket_strerror($socketError);
138
139                         // Shutdown this socket
140                         $this->shutdownSocket($mainSocket);
141
142                         // And throw again
143                         throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
144                         */
145                 } // END - if
146
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'));
152                         /*
153                         // Get socket error code for verification
154                         $socketError = socket_last_error($mainSocket);
155
156                         // Get error message
157                         $errorMessage = socket_strerror($socketError);
158
159                         // Shutdown this socket
160                         $this->shutdownSocket($mainSocket);
161
162                         // And throw again
163                         throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
164                         */
165                 } // END - if
166
167                 // Set the main socket
168                 $this->registerServerSocketResource($mainSocket);
169
170                 // Initialize the peer pool instance
171                 $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this));
172
173                 // Add main socket
174                 $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
175
176                 // And add it to this listener
177                 $this->setPoolInstance($poolInstance);
178
179                 // Initialize iterator for listening on packages
180                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
181
182                 // Rewind it and remember it in this class
183                 $iteratorInstance->rewind();
184                 $this->setIteratorInstance($iteratorInstance);
185
186                 // Initialize the raw data handler
187                 $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class');
188
189                 // Set it in this class
190                 $this->setHandlerInstance($handlerInstance);
191
192                 // Output message
193                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Socket listener now ready on socket ' . $socketFile . ' for service.');
194         }
195
196         /**
197          * "Listens" for incoming network packages
198          *
199          * @return      void
200          */
201         public function doListen() {
202                 // Call super method
203                 $this->doListenSocketSelect('');
204         }
205
206         /**
207          * Checks whether the listener would accept the given package data array
208          *
209          * @param       $packageData    Raw package data
210          * @return      $accepts                Whether this listener does accept
211          */
212         public function ifListenerAcceptsPackageData (array $packageData) {
213                 $this->partialStub('Need to implement this method.');
214         }
215
216         /**
217          * Monitors incoming raw data from the handler and transfers it to the
218          * given receiver instance.
219          *
220          * @return      void
221          */
222         public function monitorIncomingRawData () {
223                 $this->partialStub('Need to implement this method.');
224         }
225
226 }