*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
*/
class BaseHubNode extends BaseHubSystem implements Updateable {
/**
- * Node id
+ * Node types
*/
- private $nodeId = '';
+ const NODE_TYPE_BOOT = 'boot';
+ const NODE_TYPE_MASTER = 'master';
+ const NODE_TYPE_LIST = 'list';
+ const NODE_TYPE_REGULAR = 'regular';
- /**
- * Session id
- */
- private $sessionId = '';
+ // Exception constants
+ const EXCEPTION_HUB_ALREADY_ANNOUNCED = 0xe00;
/**
* IP/port number of bootstrapping node
/**
* Query connector instance
*/
- private $connectorInstance = null;
+ private $queryConnectorInstance = null;
+
+ /**
+ * Queue connector instance
+ */
+ private $queueConnectorInstance = null;
/**
* Listener pool instance
private $listenerPoolInstance = null;
/**
- * Wether the hub is active (true/false)
+ * Wether this node is anncounced (KEEP ON false!)
+ * @deprecated
+ */
+ private $hubIsAnnounced = false;
+
+ /**
+ * State instance
+ */
+ private $stateInstance = null;
+
+ /**
+ * Wether this hub is active
*/
- private $hubIsActive = false;
+ private $isActive = false;
/**
* Protected constructor
protected function __construct ($className) {
// Call parent constructor
parent::__construct($className);
+
+ // Init state which sets the state to 'init'
+ $this->initState();
+ }
+
+ /**
+ * Initializes the node's state which sets it to 'init'
+ *
+ * @return void
+ */
+ private function initState() {
+ // Get the state factory and create the initial state, we don't need
+ // the state instance here
+ StateFactory::createStateInstanceByName('init', $this);
}
/**
* @return void
*/
private final function setNodeId ($nodeId) {
- $this->nodeId = (string) $nodeId;
+ // Set it config now
+ $this->getConfigInstance()->setConfigEntry('node_id', (string) $nodeId);
}
/**
* Getter for node id
*
- * @return $nodeId Our new node id
+ * @return $nodeId Current node id
*/
private final function getNodeId () {
- return $this->nodeId;
+ // Get it from config
+ return $this->getConfigInstance()->getConfigEntry('node_id');
}
/**
return $this->listenerPoolInstance;
}
+ /**
+ * Setter for state instance
+ *
+ * @param $stateInstance Node's current state instance
+ * @return void
+ */
+ public final function setStateInstance (Stateable $stateInstance) {
+ $this->stateInstance = $stateInstance;
+ }
+
+ /**
+ * Getter for state instance
+ *
+ * @return $stateInstance Node's current state instance
+ */
+ public final function getStateInstance () {
+ return $this->stateInstance;
+ }
+
/**
* Setter for session id
*
* @return void
*/
private final function setSessionId ($sessionId) {
- $this->sessionId = (string) $sessionId;
+ $this->getConfigInstance()->setConfigEntry('session_id', (string) $sessionId);
}
/**
* Getter for session id
*
- * @return $sessionId Our new session id
+ * @return $sessionId Current session id
*/
public final function getSessionId () {
- return $this->sessionId;
+ return $this->getConfigInstance()->getConfigEntry('session_id');
}
/**
* @return void
*/
private final function setQueryConnectorInstance (Connectable $connectorInstance) {
- $this->connectorInstance = $connectorInstance;
+ $this->queryConnectorInstance = $connectorInstance;
}
/**
* @return $connectorInstance Our new query instance
*/
public final function getQueryConnectorInstance () {
- return $this->connectorInstance;
+ return $this->queryConnectorInstance;
+ }
+
+ /**
+ * Setter for queue instance
+ *
+ * @param $connectorInstance Our new queue instance
+ * @return void
+ */
+ private final function setQueueConnectorInstance (Connectable $connectorInstance) {
+ $this->queueConnectorInstance = $connectorInstance;
+ }
+
+ /**
+ * Getter for queue instance
+ *
+ * @return $connectorInstance Our new queue instance
+ */
+ public final function getQueueConnectorInstance () {
+ return $this->queueConnectorInstance;
}
/**
return $this->bootIpPort;
}
+ /**
+ * "Getter" for a printable state name
+ *
+ * @return $stateName Name of the node's state in a printable format
+ */
+ public final function getPrintableState () {
+ // Default is 'null'
+ $stateName = 'null';
+
+ // Get the state instance
+ $stateInstance = $this->getStateInstance();
+
+ // Is it an instance of Stateable?
+ if ($stateInstance instanceof Stateable) {
+ // Then use that state name
+ $stateName = $stateInstance->getStateName();
+ } // END - if
+
+ // Return result
+ return $stateName;
+ }
+
/**
* Checks wether the given IP address matches one of the bootstrapping nodes
*
break;
} elseif ($ipPortArray[0] == $this->getConfigInstance()->getConfigEntry('node_listen_addr')) {
// IP matches listen address. At this point we really don't care
- // if we can also listen on that address!
+ // if we can really listen on that address
$isFound = true;
// Remember the port number
// Output all lines
$this->debugOutput(' ');
$this->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active');
- $this->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team');
+ $this->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2011 Hub Developer Team');
$this->debugOutput(' ');
$this->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
$this->debugOutput('This is free software, and you are welcome to redistribute it under certain');
* based on many pseudo-random data. On any later run, unless the id
* got not removed from database, it will be restored from the database.
*
+ * @param $requestInstance A Requestable class
+ * @param $responseInstance A Responseable class
* @return void
*/
- public function bootstrapAcquireHubId () {
+ public function bootstrapAcquireNodeId (Requestable $requestInstance, Responseable $responseInstance) {
// Get a wrapper instance
$wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
// Get a crypto instance
$cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
- // Hash and encrypt the string so we become a "node id" aka Hub-Id
+ // Hash and encrypt the string so we become a node id (also documented as "hub id")
$this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
// Register the node id with our wrapper
}
/**
- * Generates a session id which will be sent to the other hubs and clients
+ * Generates a session id which will be sent to the other hubs and peers
*
* @return void
*/
// Output message
$this->debugOutput('BOOTSTRAP: Created new session-id: ' . $this->getSessionId() . '');
+
+ // Change the state because the node has auired a hub id
+ $this->getStateInstance()->nodeGeneratedSessionId();
}
/**
// Run a test query
$this->getQueryConnectorInstance()->doTestQuery();
+ // Set the queue connector instance
+ $this->setQueueConnectorInstance(ObjectFactory::createObjectByConfiguredName('queue_connector_class', array($this)));
+
+ // Run a test queue
+ $this->getQueueConnectorInstance()->doTestQueue();
+
// Debug message
$this->debugOutput('BOOTSTRAP: Initialize queues: FINISHED');
}
}
/**
- * Getter for $hubIsActive attribute
+ * Announces this hub to the upper (bootstrap or list) hubs. After this is
+ * successfully done the given task is unregistered from the handler. This
+ * might look a bit overloaded here but the announcement phase isn't a
+ * simple "Hello there" message, it may later on also contain more
+ * informations like the object list.
*
- * @return $hubIsActive Wether the hub is activer
+ * @param $taskInstance The task instance running this announcement
+ * @return void
+ * @throws HubAlreadyAnnouncedException If this hub is already announced
+ * @todo Change the first if() block to check for a specific state
*/
- public final function isHubActive () {
- return $this->hubIsActive;
+ public function announceSelfToUpperNodes (Taskable $taskInstance) {
+ // Is this hub node announced?
+ if ($this->hubIsAnnounced === true) {
+ // Already announced!
+ throw new HubAlreadyAnnouncedException($this, self::EXCEPTION_HUB_ALREADY_ANNOUNCED);
+ } // END - if
+
+ // Debug output
+ $this->debugOutput('HUB: Announcement: START (taskInstance=' . $taskInstance->__toString(). ')');
+
+ // Get a helper instance
+ $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_announcement_helper_class', array($this));
+
+ // Load the announcement descriptor
+ $helperInstance->loadDescriptorXml();
+
+ // Set some dummy configuration entries, e.g. node_status
+ $this->getConfigInstance()->setConfigEntry('node_status', $this->getStateInstance()->getStateName());
+
+ // Compile all variables
+ $helperInstance->getTemplateInstance()->compileConfigInVariables();
+
+ // "Publish" the descriptor by sending it to the bootstrap/list nodes
+ $helperInstance->sendPackage();
+
+ // Change the state, this should be the last line except debug output
+ $this->getStateInstance()->nodeAnnouncedToUpperHubs();
+
+ // Debug output
+ $this->debugOutput('HUB: Announcement: FINISHED');
}
/**
- * Setter for $hubIsActive attribute
+ * Does a self-connect attempt on the public IP address. This should make
+ * it sure, we are reachable from outside world. For this kind of package we
+ * don't need that overload we have in the announcement phase.
*
- * @param $hubIsActive Wether the hub is activer
+ * @param $taskInstance The task instance running this announcement
+ * @return void
*/
- public final function enableHubIsActive ($hubIsActive = true) {
- $this->hubIsActive = $hubIsActive;
+ public function doSelfConnection (Taskable $taskInstance) {
+ // Debug output
+ $this->debugOutput('HUB: Self Connection: START (taskInstance=' . $taskInstance->__toString(). ')');
+
+ // Get a helper instance
+ $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_self_connect_helper_class', array($this));
+
+ // Load the descriptor (XML) file
+ $helperInstance->loadDescriptorXml();
+
+ // And send the package away
+ $helperInstance->sendPackage();
+
+ // Debug output
+ $this->debugOutput('HUB: Self Connection: FINISHED');
}
/**
* @param $responseInstance A Responseable class
* @return void
*/
- public function activateHub (Requestable $requestInstance, Responseable $responseInstance) {
+ public function activateNode (Requestable $requestInstance, Responseable $responseInstance) {
// Checks wether a listener is still active and shuts it down if one
- // is still listening
- if (($this->determineIfListenerIsActive()) && ($this->isHubActive())) {
+ // is still listening.
+ if (($this->determineIfListenerIsActive()) && ($this->getIsActive())) {
// Shutdown them down before they can hurt anything
$this->shutdownListenerPool();
} // END - if
// ----------------------- Last step from here ------------------------
// Activate the hub. This is ALWAYS the last step in this method
- $this->enableHubIsActive();
+ $this->getStateInstance()->nodeIsActivated();
// ---------------------- Last step until here ------------------------
}
// Setup address and port
$listenerInstance->setListenAddressByConfiguration('node_listen_addr');
- $listenerInstance->setListenPortByConfiguration('node_tcp_listen_port');
+ if ($this instanceof HubBootNode) {
+ // Bootstrap have different listening port
+ $listenerInstance->setListenPortByConfiguration('boot_node_tcp_listen_port');
+ } else {
+ // All other nodes use the default port
+ $listenerInstance->setListenPortByConfiguration('node_tcp_listen_port');
+ }
// Initialize the listener
$listenerInstance->initListener();
$this->getListenerPoolInstance()->addListener($decoratorInstance);
// Get a decorator class
- $decoratorInstance = ObjectFactory::createObjectByConfiguredName('client_tcp_listener_class', array($listenerInstance));
+ $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_tcp_listener_class', array($listenerInstance));
// Add this listener to the pool
$this->getListenerPoolInstance()->addListener($decoratorInstance);
// Setup address and port
$listenerInstance->setListenAddressByConfiguration('node_listen_addr');
- $listenerInstance->setListenPortByConfiguration('node_udp_listen_port');
+ if ($this instanceof HubBootNode) {
+ // Bootstrap have different listening port
+ $listenerInstance->setListenPortByConfiguration('boot_node_udp_listen_port');
+ } else {
+ // All other nodes use the default port
+ $listenerInstance->setListenPortByConfiguration('node_udp_listen_port');
+ }
// Initialize the listener
$listenerInstance->initListener();
$this->getListenerPoolInstance()->addListener($decoratorInstance);
// Get a decorator class
- $decoratorInstance = ObjectFactory::createObjectByConfiguredName('client_udp_listener_class', array($listenerInstance));
+ $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_udp_listener_class', array($listenerInstance));
// Add this listener to the pool
$this->getListenerPoolInstance()->addListener($decoratorInstance);
// Debug output
$this->debugOutput('HUB: Restore node list: FINISHED.');
}
+
+ /**
+ * Getter for isActive attribute
+ *
+ * @return $isActive Wether the hub is active
+ */
+ public final function getIsActive () {
+ return $this->isActive;
+ }
+
+ /**
+ * Enables (default) or disables isActive flag
+ *
+ * @param $isActive Wether the hub is active
+ * @return void
+ */
+ public final function enableIsActive ($isActive = true) {
+ $this->isActive = (bool) $isActive;
+ }
+
+ /**
+ * "Getter for address:port combination
+ *
+ * @param $handlerInstance A valid Networkable instance
+ * @return $addressPort A address:port combination for this node
+ */
+ public final function getAddressPort (Networkable $handlerInstance) {
+ // Construct config entry
+ $configEntry = 'node_' . $handlerInstance->getHandlerName() . '_listen_port';
+
+ // Get IP and port
+ $addressPort = $this->getConfigInstance()->detectServerAddress() . ':' . $this->getConfigInstance()->getConfigEntry($configEntry);
+
+ // Return it
+ return $addressPort;
+ }
}
// [EOF]