// Import framework stuff
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
use Org\Mxchange\CoreFramework\Registry\Registerable;
use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
use Org\Mxchange\CoreFramework\Socket\NoSocketErrorDetectedException;
use \BadMethodCallException;
use \InvalidArgumentException;
use \LogicException;
+use \SplFileInfo;
/**
* A Socket Container class
*/
private $packageDataInstance = NULL;
+ /**
+ * An instance of a SplFileInfo class
+ */
+ private $socketFile;
+
+ /**
+ * Socket type
+ */
+ private $socketType;
+
/**
* Protected constructor
*
}
/**
- * Tries to bind the socket.
+ * Tries to bind to socket file
*
* @param $bindAddress Where to bind the socket to (e.g. Uni* socket file)
* @param $bindPort Optional port to bind to
* @return $result Result from binding socket
* @throws InvalidSocketException If socket is invalid
*/
- public function bindSocketTo ($bindAddress, $bindPort = 0) {
+ public function bindSocketToFile () {
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
+ }
+ /**
+ * Tries to bind to listener's address and port
+ *
+ * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file)
+ * @param $bindPort Optional port to bind to
+ * @return $result Result from binding socket
+ * @throws InvalidSocketException If socket is invalid
+ * @throws NullPointerException If listener instance is not given
+ */
+ public function bindSocketToListener () {
// Should be valid socket
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: CALLED!', strtoupper($this->getSocketProtocol())));
if (!$this->isValidSocket()) {
// Throw exception
throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
+ } elseif (is_null($this->getListenerInstance())) {
+ // Required listener not set
+ throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+ }
- // Try to bind it to
- $result = socket_bind($this->getSocketResource(), $bindAddress, $bindPort);
+ // Get bind address and port
+ $address = $this->getListenerInstance()->getListenAddress();
+ $port = $this->getListenerInstance()->getListenAddress();
- // Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
+ // Try to bind it to
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Binding socket to %s:%d ...', strtoupper($this->getSocketProtocol()), $address, $port));
+ $result = socket_bind($this->getSocketResource(), $address, $port);
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: result=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($result)));
return $result;
}
return $result;
}
+ /**
+ * Getter for socket file instance
+ *
+ * @return $socketFile An instance of a SplFileInfo class
+ */
+ public function getSocketFile () {
+ return $this->socketFile;
+ }
+
+ /**
+ * Setter for socket file instance
+ *
+ * @param $socketFile An instance of a SplFileInfo class
+ * @return void
+ */
+ public function setSocketFile (SplFileInfo $socketFile) {
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Setting socketFile=%s ...', strtoupper($this->getSocketType()), $socketFile));
+ $this->socketFile = $socketFile;
+ }
+
+ /**
+ * Getter for socket type
+ *
+ * @return $socketType Stocket type
+ */
+ public function getSocketType () {
+ return $this->socketType;
+ }
+
+ /**
+ * Setter for socket type
+ *
+ * @param $socketType Socket type
+ * @return void
+ */
+ public function setSocketType ($socketType) {
+ /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Setting socketType=%s ...', strtoupper($this->getSocketType()), $socketType));
+ $this->socketType = $socketType;
+ }
+
}
// Init package instance
$packageInstance = PackageDataFactory::createPackageDataInstance();
- // Set file socket data
- $packageInstance->setSocketFile($socketFile);
- $packageInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
-
// Init main socket
$socketResource = socket_create(AF_UNIX, SOCK_STREAM, 0);
$packageInstance,
));
+ // Set file socket data
+ $socketInstance->setSocketFile($socketFile);
+ $socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
+
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FACTORY: socketInstance[]=%s', gettype($socketInstance)));
} // END - if
// Is the file there?
- if ((FrameworkBootstrap::isReachableFilePath($packageInstance->getSocketFile())) && (file_exists($packageInstance->getSocketFile()))) {
+ if ((FrameworkBootstrap::isReachableFilePath($socketInstance->getSocketFile())) && (file_exists($socketInstance->getSocketFile()))) {
// Old socket found
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $packageInstance->getSocketFile() . ' found. Will not start.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: WARNING: Old socket at ' . $socketInstance->getSocketFile() . ' found. Will not start.');
// Shutdown this socket
$socketInstance->shutdownSocket();
} // END - if
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $packageInstance->getSocketFile() . ' ...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to ' . $socketInstance->getSocketFile() . ' ...');
// Try to bind to it
- if (!$socketInstance->bindSocketTo($packageInstance->getSocketFile())) {
+ if (!$socketInstance->bindSocketToFile()) {
// Handle error here
$socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
} // END - if
// Set listener instance and type
$socketInstance->setListenerInstance($listenerInstance);
$socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
- $socketInstance->setSocketListenAddress($listenerInstance->getListenAddress());
- $socketInstance->setSocketListenPort($listenerInstance->getListenPort());
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
* send/recv data, disconnect, etc..
*/
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
- if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) {
+ if (!$socketInstance->bindSocketToListener()) {
// Handle this socket error with a faked recipientData array
$socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
// Set listener instance and socket type
$socketInstance->setListenerInstance($listenerInstance);
$socketInstance->setSocketType(StorableSocket::CONNECTION_TYPE_SERVER);
- $socketInstance->setSocketListenAddress($listenerInstance->getListenAddress());
- $socketInstance->setSocketListenPort($listenerInstance->getListenPort());
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
* send/recv data, disconnect, etc..
*/
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FACTORY: Binding to address ' . $listenerInstance->getListenAddress() . ':' . $listenerInstance->getListenPort());
- if (!$socketInstance->bindSocketTo($listenerInstance->getListenAddress(), $listenerInstance->getListenPort())) {
+ if (!$socketInstance->bindSocketToListener()) {
// Handle this socket error with a faked recipientData array
$socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if
// Import framework stuff
use Org\Mxchange\CoreFramework\Registry\Registerable;
-// Import SPL stuff
-use \SplFileInfo;
-
/**
* A DeliverablePackage class for raw package data
*
*/
private $packageContent;
- /**
- * An instance of a SplFileInfo class
- */
- private $socketFile;
-
- /**
- * Socket type
- */
- private $socketType;
-
/**
* Protected constructor
*
return $packageInstance;
}
- /**
- * Getter for socket file instance
- *
- * @return $socketFile An instance of a SplFileInfo class
- */
- public function getSocketFile () {
- return $this->socketFile;
- }
-
- /**
- * Setter for socket file instance
- *
- * @param $socketFile An instance of a SplFileInfo class
- * @return void
- */
- public function setSocketFile (SplFileInfo $socketFile) {
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-DATA: Setting socketFile=%s ...', $socketFile));
- $this->socketFile = $socketFile;
- }
-
- /**
- * Getter for socket type
- *
- * @return $socketType Stocket type
- */
- public function getSocketType () {
- return $this->socketType;
- }
-
- /**
- * Setter for socket type
- *
- * @param $socketType Socket type
- * @return void
- */
- public function setSocketType ($socketType) {
- /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('PACKAGE-DATA: Setting socketType=%s ...', $socketType));
- $this->socketType = $socketType;
- }
-
/**
* Getter for package content
*
// Inport frameworks stuff
use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+// Import PHP stuff
+use \SplFileInfo;
+
/**
* An interface for socket containers
*
const CONNECTION_TYPE_SERVER = 'server';
/**
- * Tries to bind the socket.
+ * Tries to bind to attached socket file
+ *
+ * @return $result Result from binding socket
+ * @throws InvalidSocketException If stored socket is invalid
+ */
+ function bindSocketToFile ();
+
+ /**
+ * Tries to bind to attached listener's address and port
*
- * @param $bindAddress Where to bind the socket to (e.g. Uni* socket file)
* @return $result Result from binding socket
* @throws InvalidSocketException If stored socket is invalid
+ * @throws NullPointerException If listener instance is not given
*/
- function bindSocketTo ($bindAddress);
+ function bindSocketToListener ();
/**
* Tries to listen on the socket
*/
function readDataFromSocket ();
+ /**
+ * Getter for socket file instance
+ *
+ * @return $socketFile An instance of a SplFileInfo class
+ */
+ public function getSocketFile ();
+
+ /**
+ * Setter for socket file instance
+ *
+ * @param $socketFile An instance of a SplFileInfo class
+ * @return void
+ */
+ public function setSocketFile (SplFileInfo $socketFile);
+
+ /**
+ * Getter for socket type
+ *
+ * @return $socketFile Stocket type
+ */
+ public function getSocketType ();
+
+ /**
+ * Setter for socket type
+ *
+ * @param $socketType Socket type
+ * @return void
+ */
+ public function setSocketType ($socketType);
+
}
// Import application-specific stuff
use Org\Shipsimu\Hub\Generic\HubInterface;
-// Import PHP stuff
-use \SplFileInfo;
-
/**
* An interface for package delivery boys... ;-)
*
*/
interface DeliverablePackage extends HubInterface {
- /**
- * Getter for socket file instance
- *
- * @return $socketFile An instance of a SplFileInfo class
- */
- public function getSocketFile ();
-
- /**
- * Setter for socket file instance
- *
- * @param $socketFile An instance of a SplFileInfo class
- * @return void
- */
- public function setSocketFile (SplFileInfo $socketFile);
-
- /**
- * Getter for socket type
- *
- * @return $socketFile Stocket type
- */
- public function getSocketType ();
-
- /**
- * Setter for socket type
- *
- * @param $socketType Socket type
- * @return void
- */
- public function setSocketType ($socketType);
-
}