class HubCoreLoop extends BaseFrameworkSystem {
/**
* Wether the hub is active and running
- */
+ */
private $hubActivated = false;
/**
* Wether the hub is shutting down
- */
+ */
private $hubShutsDown = false;
/**
private $masterConnector = null;
// Exception codes
- const EXCEPTION_SOCKET_PROBLEM = 0xb00;
+ const EXCEPTION_SOCKET_PROBLEM = 0xb00;
const EXCEPTION_HUB_PEER_TIMEOUT = 0xb01;
const EXCEPTION_HUB_PEER_FAILED_AUTH = 0xb02;
- const EXCEPTION_HELLO_TIMED_OUT = 0xb03;
+ const EXCEPTION_HELLO_TIMED_OUT = 0xb03;
/**
* The private constructor
);
// Transfer readers and writers
- $this->readPeers = $read;
- $this->writePeers = $write;
+ $this->readPeers = $read;
+ $this->writePeers = $write;
// Return the number
return $num;
// Do the ping and update our authPeer list (LATER!)
$this->lastPeerInstance->handlePingPeer();
+
+ // Avoids a notice...
+ if (is_null($this->getCurrPeerInstance())) break;
} // END - for
} // END - for
}
if ($this->getCurrPeerInstance()->equals($peer)) {
// Remove him!
$idx->offsetUnset($idx->key());
+
+ // Remove the last instance as well
$this->lastPeerInstance = null;
+
+ // Remove the socket as well
+ $this->removeSocket($peer);
+
+ // Stop searching here
break;
}
}
}
/**
- * Do the main loop
- *
- * @return void
- * @throws SocketCreationException If the main socket is not a resource
- */
+ * Do the main loop
+ *
+ * @return void
+ * @throws SocketCreationException If the main socket is not a resource
+ */
public function coreLoop () {
// Is the main socket vailid?
if (!is_resource($this->main_socket)) {
$this->__toString(),
$e->getMessage()
));
+
+ // Get new total connected peers
+ $num = $this->getTotalConnectedPeers();
} catch (BrokenPipeException $e) {
// Broken pipes are bad for us... :(
$this->removePeerFromConnectList();
$num = $this->getTotalConnectedPeers();
} catch (FrameworkException $e) {
// Catch all other exceptions and output them
- echo "CATCH".__LINE__.":".$e->__toString()."\n";
hub_exception_handler($e);
// Get new total connected peers
$num = $this->getTotalConnectedPeers();
} catch (FrameworkException $e) {
// Catch all exceptions and output them to avoid aborting the program unexpectly
- echo "CATCH".__LINE__.":".$e->__toString()."\n";
hub_exception_handler($e);
// Get new total connected peers
$this->announceToMasterHub();
} catch (FrameworkException $e) {
// Catch all exceptions and output them
- echo "CATCH".__LINE__.":".$e->__toString()."\n";
hub_exception_handler($e);
}
} // END - else
* @return void
*/
public function disconnectPeerWithReason ($reasonEntry) {
+ // Default is that we cannot read the peer's IP number
$ip = "0.0.0.0";
// Try to disconnect here
$this->getCurrPeerInstance()->disconnectWithReason($this->getConfigInstance()->readConfig($reasonEntry));
} catch (FrameworkException $e) {
// Catch all exceptions and output them
- echo "CATCH".__LINE__.":".$e->__toString()."\n";
hub_exception_handler($e);
}
}
}
+ /**
+ * Removes a given peer instance (socket) from all lists
+ *
+ * @param $peerInstance An instance of a HubPeer class
+ * @return void
+ */
+ private function removeSocket (HubPeer $peerInstance) {
+ // Get socket from peer
+ $socket = $peerInstance->getPeerSocket();
+
+ // Search for readers
+ $key = array_search($socket, $this->readPeers, true);
+ if ($key !== false) {
+ // Remove from reader list
+ unset($this->readPeers[$key]);
+ }
+
+ // Search for writers
+ $key = array_search($socket, $this->writePeers, true);
+ if ($key !== false) {
+ // Remove from writer list
+ unset($this->writePeers[$key]);
+ }
+
+ // Remove from auth peers as well
+ for ($idx = $this->authPeers->getIterator(); $idx->valid(); $idx->next()) {
+ // Get current entry
+ $current = $idx->current();
+
+ // Is it the same?
+ if ($current->equals($peerInstance)) {
+ // Remove from auth (-awaiting) list
+ $idx->offsetUnset($idx->key());
+ break;
+ }
+ }
+ }
+
} // END - class
// [EOF]
//----------------------------------------------------------
// Exceptions
//----------------------------------------------------------
- const EXCEPTION_PEER_SOCKET_INVALID = 0x200;
- const EXCEPTION_PEER_IP_CHANGED = 0x201;
+ const EXCEPTION_PEER_SOCKET_INVALID = 0x200;
+ const EXCEPTION_PEER_IP_CHANGED = 0x201;
const EXCEPTION_PEER_SOCKET_BROKEN = 0x202;
//----------------------------------------------------------
/**
* Setter for peer socket
*
- * @param $peerSocket The peer's socket
+ * @param $peerSocket The peer's socket
* @return void
*/
public final function setPeerSocket ($peerSocket) {
}
}
+ /**
+ * Getter for peer socket
+ *
+ * @return $peerSocket The peer's socket
+ */
+ public final function getPeerSocket () {
+ return $this->peerSocket;
+ }
+
/**
* Setter for connection timestamp only once
*
/**
* Disconnects the peer with a special reason (status)
*
- * @param $reason The special reason
+ * @param $reason The special reason
* @return void
*/
public final function disconnectWithReason ($reason) {
/**
* Sends a specified message to the peer's socket
*
- * @param $message The special message
+ * @param $message The special message
* @return void
* @throws BrokenPipeException If a pipe to a socket peer is lost
*/
$this->getValidatedIP()
));
+ // Is the socket still valid?
+ if (!is_resource($this->peerSocket)) {
+ // The socket is no longer valid!
+ throw new BrokenPipeException (
+ array(
+ 'this' => $this,
+ 'code' => socket_last_error()
+ ), self::EXCEPTION_PEER_SOCKET_BROKEN
+ );
+ }
+
// Send it to the peer
if (socket_write($this->peerSocket, $message."\n") !== false) {
// Set the timestamp and message