assert(count($recipientIpArray) == 2);
// Debug message
- /* NOISY-DEBUG: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s), recipientIpArray[0]=' . $recipientIpArray[0] . ' ...');
+ /* NOISY-DEBUG: */ $this->debugOutput('POOL: Checking ' . count($this->getAllSockets()) . ' socket(s),recipientIpArray[0]=' . $recipientIpArray[0] . ',recipientIpArray[1]=' . $recipientIpArray[1] . ' ...');
// Get all sockets and check them, skip the server socket
foreach ($this->getAllSockets() as $socket) {
// Is this a server socket?
if ($socket === $this->getListenerInstance()->getSocketResource()) {
// Skip 'server' sockets (local socket)
- /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping local socket ' . $socket . ' ...');
+ /* NOISY-DEBUG: */ $this->debugOutput('POOL: Skipping server socket ' . $socket . ' ...');
continue;
} // END - if
$this->handleSocketError(__METHOD__, __LINE__, $socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
} // END - if
+ // Get
// If the "peer" IP and recipient is same, use it
if ($peerIp == $recipientIpArray[0]) {
// IPs match, so take the socket and quit this loop
* @return $key A string representation of the socket for the registry
*/
private function getSubRegistryKey (ProtocolHandler $protocolInstance) {
+ // Default address is invalid
+ $address = '*invalid*';
+
+ // Which instance is it?
+ // @TODO Tested again base class, rewrite it to a generic interface!
+ if ($protocolInstance instanceof BaseConnectionHelper) {
+ // Get address
+ $address = $protocolInstance->getAddress();
+ } elseif ($protocolInstance instanceof BaseListener) {
+ // Get listen address
+ $address = $protocolInstance->getListenAddress();
+ } else {
+ // Not supported!
+ $this->debugBackTrace('Unsupported protocolInstance=' . $protocolInstance->__toString() . ' detected. Please fix this!');
+ }
+
// Get protocol and port number and add both together
- $key = sprintf("%s:%s:%s",
+ $key = sprintf("%s:%s:%s:%s",
$protocolInstance->__toString(),
$protocolInstance->getProtocol(),
+ $address,
$protocolInstance->getPort()
);
* @return $isRegistered Whether the protocol is registered
*/
private function isProtocolRegistered (ProtocolHandler $protocolInstance) {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ' - ENTERED!');
+
// Get the key
$key = $this->getRegistryKeyFromProtocol($protocolInstance);
// Determine it
$isRegistered = $this->instanceExists($key);
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
+
// Return result
return $isRegistered;
}
* @return $isRegistered Whether the given socket resource is registered
*/
public function isSocketRegistered (ProtocolHandler $protocolInstance, $socketResource) {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
+
// Default is not registered
$isRegistered = false;
// That one is found so "get" a registry key from it
$key = $this->getRegistryKeyFromProtocol($protocolInstance);
+ // Debug message
+ /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ' - Trying to get instance ...');
+
// Get the registry
$registryInstance = $this->getInstance($key);
// "Get" a key for the socket
$socketKey = $this->getSubRegistryKey($protocolInstance);
- // And simply ask it
- $isRegistered = $registryInstance->instanceExists($socketKey);
+ // Debug message
+ /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',key=' . $key . ',socketKey=' . $socketKey . ' - Checking existence ...');
+
+ // Is it there?
+ if ($registryInstance->instanceExists($socketKey)) {
+ // Debug message
+ /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: Found instance for socketKey=' . $socketKey . ':' . $registryInstance->getInstance($socketKey));
+
+ // Get the instance
+ $registeredInstance = $registryInstance->getInstance($socketKey);
+
+ // Is it SocketContainer and same socket?
+ $isRegistered = (($registeredInstance instanceof SocketContainer) && ($registeredInstance->ifSocketResourceMatches($socketResource)));
+
+ // Debug message
+ /* NOISY-DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: Final result: isRegistered(' . $socketResource . ')=' . intval($isRegistered));
+ } // END - if
} // END - if
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ',isRegistered=' . intval($isRegistered) . ' - EXIT!');
+
// Return the result
return $isRegistered;
}
* @return void
*/
public function registerSocket (ProtocolHandler $protocolInstance, $socketResource, array $packageData = array()) {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - ENTERED!');
+
// Is the socket already registered?
if ($this->isSocketRegistered($protocolInstance, $socketResource)) {
// Throw the exception
$socketInstance = ObjectFactory::CreateObjectByConfiguredName('socket_container_class', array($socketResource, $protocolInstance, $packageData));
// We have a sub-registry, the socket key and the socket, now we need to put all together
- //* DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource=' . $socketResource . ' - adding socket container instance ...');
+ /* DEBUG: */ $this->debugOutput('SOCKET-REGISTRY: socketKey=' . $socketKey . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - adding socket container instance ...');
$registryInstance->addInstance($socketKey, $socketInstance);
}
* @throws NoSocketRegisteredException If the requested socket is not registered
*/
public function getRegisteredSocketResource (ProtocolHandler $protocolInstance) {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ' - ENTERED!');
+
// The socket must be registered before we can return it
- if (!$this->isSocketRegistered($protocolInstance, false)) {
+ if (!$this->isProtocolRegistered($protocolInstance)) {
// Throw the exception
throw new NoSocketRegisteredException ($protocolInstance, self::SOCKET_NOT_REGISTERED);
} // END - if
// And the final socket resource
$socketResource = $registryInstance->getInstance($socketKey)->getSocketResource();
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ':protocol=' . $protocolInstance->getProtocol() . ',socketResource[' . gettype($socketResource) . ']=' . $socketResource . ' - EXIT!');
+
// Return the resource
return $socketResource;
}