Continued:
[core.git] / inc / main / classes / listener / socket / class_SocketFileListener.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Listener\Socket;
4
5 /**
6  * A file-based socket listener
7  *
8  * @author              Roland Haeder <webmaster@ship-simu.org>
9  * @version             0.0.0
10  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.ship-simu.org
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class SocketFileListener extends BaseListener implements Listenable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36
37                 // Set the protocol to file
38                 $this->setProtocolName('file');
39         }
40
41         /**
42          * Creates an instance of this class
43          *
44          * @return      $listenerInstance       An instance a prepared listener class
45          */
46         public final static function createSocketFileListener () {
47                 // Get new instance
48                 $listenerInstance = new SocketFileListener();
49
50                 // Return the prepared instance
51                 return $listenerInstance;
52         }
53
54         /**
55          * Initializes the listener by setting up the required socket server
56          *
57          * @return      void
58          */
59         public function initListener() {
60                 // Init socket
61                 $mainSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
62
63                 // Is the socket resource valid?
64                 if (!is_resource($mainSocket)) {
65                         // Something bad happened
66                         throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
67                 } // END - if
68
69                 // Get socket error code for verification
70                 $socketError = socket_last_error($mainSocket);
71
72                 // Check if there was an error else
73                 if ($socketError > 0) {
74                         // Handle this socket error with a faked recipientData array
75                         $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('null', '0'));
76                 } // END - if
77
78                 // Create file name
79                 $socketFile = self::createTempPathForFile($this->getConfigInstance()->getConfigEntry('ipc_socket_file_name'));
80
81                 // Debug message
82                 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: socketFile=' . $socketFile . ' ...');
83
84                 // File name must not be empty
85                 assert(!empty($socketFile));
86
87                 // Is the file there?
88                 if ((self::isReachableFilePath($socketFile)) && (file_exists($socketFile))) {
89                         // Old socket found
90                         self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: WARNING: Old socket at ' . $socketFile . ' found. Will not start.');
91
92                         // Shutdown this socket
93                         $this->shutdownSocket($mainSocket);
94
95                         // Quit here
96                         exit;
97                 } // END - if
98
99                 // Debug message
100                 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to ' . $socketFile . ' ...');
101
102                 // Try to bind to it
103                 if (!socket_bind($mainSocket, $socketFile)) {
104                         // Handle error here
105                         $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
106                         /*
107                         // Get socket error code for verification
108                         $socketError = socket_last_error($mainSocket);
109
110                         // Get error message
111                         $errorMessage = socket_strerror($socketError);
112
113                         // Shutdown this socket
114                         $this->shutdownSocket($mainSocket);
115
116                         // And throw again
117                         throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
118                         */
119                 } // END - if
120
121                 // Start listen for connections
122                 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Listening for connections.');
123                 if (!socket_listen($mainSocket)) {
124                         // Handle this socket error with a faked recipientData array
125                         $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
126                         /*
127                         // Get socket error code for verification
128                         $socketError = socket_last_error($mainSocket);
129
130                         // Get error message
131                         $errorMessage = socket_strerror($socketError);
132
133                         // Shutdown this socket
134                         $this->shutdownSocket($mainSocket);
135
136                         // And throw again
137                         throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
138                         */
139                 } // END - if
140
141                 // Now, we want non-blocking mode
142                 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
143                 if (!socket_set_nonblock($mainSocket)) {
144                         // Handle this socket error with a faked recipientData array
145                         $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array($socketFile, '0'));
146                         /*
147                         // Get socket error code for verification
148                         $socketError = socket_last_error($mainSocket);
149
150                         // Get error message
151                         $errorMessage = socket_strerror($socketError);
152
153                         // Shutdown this socket
154                         $this->shutdownSocket($mainSocket);
155
156                         // And throw again
157                         throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
158                         */
159                 } // END - if
160
161                 // Set the main socket
162                 $this->registerServerSocketResource($mainSocket);
163
164                 // Initialize the peer pool instance
165                 $poolInstance = ObjectFactory::createObjectByConfiguredName('application_pool_class', array($this));
166
167                 // Add main socket
168                 $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
169
170                 // And add it to this listener
171                 $this->setPoolInstance($poolInstance);
172
173                 // Initialize iterator for listening on packages
174                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('socket_listen_iterator_class', array($poolInstance->getPoolEntriesInstance()));
175
176                 // Rewind it and remember it in this class
177                 $iteratorInstance->rewind();
178                 $this->setIteratorInstance($iteratorInstance);
179
180                 // Initialize the raw data handler
181                 $handlerInstance = ObjectFactory::createObjectByConfiguredName('socket_raw_data_handler_class');
182
183                 // Set it in this class
184                 $this->setHandlerInstance($handlerInstance);
185
186                 // Output message
187                 self::createDebugInstance(__CLASS__)->debugOutput('SOCKET-FILE-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Socket listener now ready on socket ' . $socketFile . ' for service.');
188         }
189
190         /**
191          * "Listens" for incoming network packages
192          *
193          * @return      void
194          */
195         public function doListen() {
196                 // Call super method
197                 $this->doListenSocketSelect('');
198         }
199
200         /**
201          * Checks whether the listener would accept the given package data array
202          *
203          * @param       $packageData    Raw package data
204          * @return      $accepts                Whether this listener does accept
205          */
206         public function ifListenerAcceptsPackageData (array $packageData) {
207                 $this->partialStub('Need to implement this method.');
208         }
209
210         /**
211          * Monitors incoming raw data from the handler and transfers it to the
212          * given receiver instance.
213          *
214          * @return      void
215          */
216         public function monitorIncomingRawData () {
217                 $this->partialStub('Need to implement this method.');
218         }
219
220 }