} // END - if
// Set the option to reuse the port
- if (!$socketInstance->enableReuseAddress()) {
+ if (!$socketInstance->enableSocketReuseAddress()) {
// Handle this socket error with a faked recipientData array
$socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
return $socketInstance;
}
+ /**
+ * Creates a listening socket instance from given listener instance
+ *
+ * @param $listenerInstance An instance of a Listenable class
+ * @return $socketInstance An instance of a StorableSocket class
+ * @throws InvalidSocketException Thrown if the socket could not be initialized
+ */
+ public static function createListenTcpSocket (Listenable $listenerInstance) {
+ // Create a streaming socket, of type TCP/IP
+ $socketResource = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+ // @TODO Init fake package data with address/port from listener
+ $packageData = array(
+ );
+
+ // Create socket instance
+ $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, 'file', $packageData, NULL));
+
+ // Is the socket resource valid?
+ if (!$socketInstance->isValidSocket()) {
+ // Something bad happened
+ throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
+ } // END - if
+
+ // Get socket error code for verification
+ $socketError = $socketInstance->getLastSocketErrorCode();
+
+ // Check if there was an error else
+ if ($socketError > 0) {
+ // Handle this socket error with a faked recipientData array
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Set the option to reuse the port
+ // @TODO: , SOL_SOCKET, SO_REUSEADDR, 1
+ if (!$socketInstance->enableSocketReuseAddress()) {
+ // Handle this socket error with a faked recipientData array
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+ } // END - if
+
+ /*
+ * "Bind" the socket to the given address, on given port so this means
+ * that all connections on this port are now our resposibility to
+ * send/recv data, disconnect, etc..
+ */
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
+ if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) {
+ // Handle this socket error with a faked recipientData array
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Start listen for connections
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Listening for connections.');
+ if (!$socketInstance->listenToSocket()) {
+ // Handle this socket error with a faked recipientData array
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Now, we want non-blocking mode
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Setting non-blocking mode.');
+ if (!$socketInstance->enableSocketNonBlocking()) {
+ // Handle this socket error with a faked recipientData array
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Return prepepared socket
+ return $socketInstance;
+ }
+
}
namespace Hub\Listener;
// Import application-specific stuff
+use Hub\Container\Socket\StorableSocket;
+use Hub\Listener\Listenable;
use Hub\Network\Networkable;
// Import framework stuff
*/
private $listenerType = 'invalid';
+ /**
+ * Name of used protocol
+ */
+ private $protocolName = 'invalid';
+
+ /**
+ * The decorated listener instance
+ */
+ private $listenerInstance = NULL;
+
/**
* Protected constructor
*
$receiverInstance->addRawDataToIncomingStack($handlerInstance);
}
+ /**
+ * Setter for socket instance
+ *
+ * @param $socketInstance A StorableSocket instance
+ * @return void
+ */
+ public final function setSocketInstance (StorableSocket $socketInstance) {
+ $this->socketInstance = $socketInstance;
+ }
+
+ /**
+ * Getter for socket instance
+ *
+ * @return $socketInstance An instance of a StorableSocket class
+ */
+ public final function getSocketInstance () {
+ return $this->socketInstance;
+ }
+
+ /**
+ * Checks whether start/end marker are set
+ *
+ * @param $data Data to be checked
+ * @return $isset Whether start/end marker are set
+ */
+ public function ifStartEndMarkersSet ($data) {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Getter for listener pool instance
+ *
+ * @return $listenerPoolInstance Our current listener pool instance
+ */
+ public function getListenerPoolInstance () {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Getter for info instance
+ *
+ * @return $infoInstance An instance of a ShareableInfo class
+ */
+ public function getInfoInstance () {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Getter for node id
+ *
+ * @return $nodeId Current node id
+ */
+ public function getNodeId () {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Getter for private key
+ *
+ * @return $privateKey Current private key
+ */
+ public function getPrivateKey () {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Getter for private key hash
+ *
+ * @return $privateKeyHash Current private key hash
+ */
+ public function getPrivateKeyHash () {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Getter for session id
+ *
+ * @return $sessionId Current session id
+ */
+ public function getSessionId () {
+ $this->partialSub('Please implement this method.');
+ }
+
+ /**
+ * Setter for listener instance
+ *
+ * @param $listenerInstance A Listenable instance
+ * @return void
+ */
+ protected final function setListenerInstance (Listenable $listenerInstance) {
+ $this->listenerInstance = $listenerInstance;
+ }
+
+ /**
+ * Getter for listener instance
+ *
+ * @return $listenerInstance A Listenable instance
+ */
+ protected final function getListenerInstance () {
+ return $this->listenerInstance;
+ }
+
+ /**
+ * Getter for protocol name
+ *
+ * @return $protocolName Name of used protocol
+ */
+ public final function getProtocolName () {
+ return $this->protocolName;
+ }
+
+ /**
+ * Setter for protocol name
+ *
+ * @param $protocolName Name of used protocol
+ * @return void
+ */
+ protected final function setProtocolName ($protocolName) {
+ $this->protocolName = $protocolName;
+ }
+
}
$this->setHandlerInstance($handlerInstance);
// Output message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Socket listener now ready on socket ' . $socketFile . ' for service.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Socket listener now ready on socket ' . $socketInstance->getSocketResource() . ' for service.');
}
/**
namespace Hub\Listener\Tcp;
// Import application-specific stuff
+use Hub\Factory\Socket\SocketFactory;
use Hub\Helper\Connection\BaseConnectionHelper;
use Hub\Listener\BaseListener;
use Hub\Listener\Listenable;
* @todo Needs rewrite!
*/
public function initListener () {
- // Create a streaming socket, of type TCP/IP
- $mainSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
-
- // Is the socket resource valid?
- if (!is_resource($mainSocket)) {
- // Something bad happened
- throw new InvalidSocketException(array($this, $mainSocket), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- // Get socket error code for verification
- $socketError = socket_last_error($mainSocket);
-
- // Check if there was an error else
- if ($socketError > 0) {
- // Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
- /*
- // Then throw again
- throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), self::EXCEPTION_INVALID_SOCKET);
- */
- } // END - if
-
- // Set the option to reuse the port
- if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
- // Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
- /*
- // Get socket error code for verification
- $socketError = socket_last_error($mainSocket);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($mainSocket);
-
- // And throw again
- throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
- */
- } // END - if
-
- /*
- * "Bind" the socket to the given address, on given port so this means
- * that all connections on this port are now our resposibility to
- * send/recv data, disconnect, etc..
- */
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
- if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
- // Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
- /*
- // Get socket error code for verification
- $socketError = socket_last_error($mainSocket);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($mainSocket);
-
- // And throw again
- throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
- */
- } // END - if
-
- // Start listen for connections
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Listening for connections.');
- if (!socket_listen($mainSocket)) {
- // Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
- /*
- // Get socket error code for verification
- $socketError = socket_last_error($mainSocket);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($mainSocket);
-
- // And throw again
- throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
- */
- } // END - if
-
- // Now, we want non-blocking mode
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TCP-LISTENER: Setting non-blocking mode.');
- if (!socket_set_nonblock($mainSocket)) {
- // Handle this socket error with a faked recipientData array
- $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
- /*
- // Get socket error code for verification
- $socketError = socket_last_error($mainSocket);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($mainSocket);
-
- // And throw again
- throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), self::EXCEPTION_INVALID_SOCKET);
- */
- } // END - if
-
- // Set the main socket
- $this->registerServerSocketInstance($mainSocket);
+ // Get instance from socket factory
+ $socketInstance = SocketFactory::createListenTcpSocket($this);
+
+ // Set socket instance
+ $this->registerServerSocketInstance($socketInstance);
// Initialize the peer pool instance
$poolInstance = ObjectFactory::createObjectByConfiguredName('node_pool_class', array($this));
// Add main socket
- $poolInstance->addPeer($mainSocket, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
+ $poolInstance->addPeer($socketInstance, BaseConnectionHelper::CONNECTION_TYPE_SERVER);
// And add it to this listener
$this->setPoolInstance($poolInstance);
$cfg->setConfigEntry('listener_pool_class', 'Hub\Pool\Listener\DefaultListenerPool');
// CFG: TCP-LISTENER-CLASS
-$cfg->setConfigEntry('tcp_listener_class', 'TcpListener');
+$cfg->setConfigEntry('tcp_listener_class', 'Hub\Listener\Tcp\TcpListener');
// CFG: UDP-LISTENER-CLASS
-$cfg->setConfigEntry('udp_listener_class', 'UdpListener');
+$cfg->setConfigEntry('udp_listener_class', 'Hub\Listener\Udp\UdpListener');
// CFG: SOCKET-FILE-LISTENER-CLASS
$cfg->setConfigEntry('socket_file_listener_class', 'Hub\Listener\Socket\SocketFileListener');