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