3 * A general hub node class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseHubNode extends BaseHubSystem implements Updateable {
28 const NODE_TYPE_BOOT = 'boot';
29 const NODE_TYPE_MASTER = 'master';
30 const NODE_TYPE_LIST = 'list';
31 const NODE_TYPE_REGULAR = 'regular';
33 // Exception constants
34 const EXCEPTION_HUB_ALREADY_ANNOUNCED = 0xe00;
37 * IP/port number of bootstrapping node
39 private $bootIpPort = '';
42 * Query connector instance
44 private $queryConnectorInstance = null;
47 * Queue connector instance
49 private $queueConnectorInstance = null;
52 * Listener pool instance
54 private $listenerPoolInstance = null;
57 * Wether this node is anncounced (KEEP ON false!)
60 private $hubIsAnnounced = false;
65 private $stateInstance = null;
68 * Wether this hub is active
70 private $isActive = false;
73 * Protected constructor
75 * @param $className Name of the class
78 protected function __construct ($className) {
79 // Call parent constructor
80 parent::__construct($className);
82 // Init state which sets the state to 'init'
87 * Initializes the node's state which sets it to 'init'
91 private function initState() {
92 // Get the state factory and create the initial state, we don't need
93 // the state instance here
94 StateFactory::createStateInstanceByName('init', $this);
100 * @param $nodeId Our new node id
103 private final function setNodeId ($nodeId) {
105 $this->getConfigInstance()->setConfigEntry('node_id', (string) $nodeId);
111 * @return $nodeId Current node id
113 private final function getNodeId () {
114 // Get it from config
115 return $this->getConfigInstance()->getConfigEntry('node_id');
119 * Setter for listener pool instance
121 * @param $listenerPoolInstance Our new listener pool instance
124 private final function setListenerPoolInstance (PoolableListener $listenerPoolInstance) {
125 $this->listenerPoolInstance = $listenerPoolInstance;
129 * Getter for listener pool instance
131 * @return $listenerPoolInstance Our current listener pool instance
133 public final function getListenerPoolInstance () {
134 return $this->listenerPoolInstance;
138 * Setter for state instance
140 * @param $stateInstance Node's current state instance
143 public final function setStateInstance (Stateable $stateInstance) {
144 $this->stateInstance = $stateInstance;
148 * Getter for state instance
150 * @return $stateInstance Node's current state instance
152 public final function getStateInstance () {
153 return $this->stateInstance;
157 * Setter for session id
159 * @param $sessionId Our new session id
162 private final function setSessionId ($sessionId) {
163 $this->getConfigInstance()->setConfigEntry('session_id', (string) $sessionId);
167 * Getter for session id
169 * @return $sessionId Current session id
171 public final function getSessionId () {
172 return $this->getConfigInstance()->getConfigEntry('session_id');
176 * Setter for query instance
178 * @param $connectorInstance Our new query instance
181 private final function setQueryConnectorInstance (Connectable $connectorInstance) {
182 $this->queryConnectorInstance = $connectorInstance;
186 * Getter for query instance
188 * @return $connectorInstance Our new query instance
190 public final function getQueryConnectorInstance () {
191 return $this->queryConnectorInstance;
195 * Setter for queue instance
197 * @param $connectorInstance Our new queue instance
200 private final function setQueueConnectorInstance (Connectable $connectorInstance) {
201 $this->queueConnectorInstance = $connectorInstance;
205 * Getter for queue instance
207 * @return $connectorInstance Our new queue instance
209 public final function getQueueConnectorInstance () {
210 return $this->queueConnectorInstance;
214 * Getter for boot IP/port combination
216 * @return $bootIpPort The IP/port combination of the boot node
218 protected final function getBootIpPort () {
219 return $this->bootIpPort;
223 * "Getter" for a printable state name
225 * @return $stateName Name of the node's state in a printable format
227 public final function getPrintableState () {
231 // Get the state instance
232 $stateInstance = $this->getStateInstance();
234 // Is it an instance of Stateable?
235 if ($stateInstance instanceof Stateable) {
236 // Then use that state name
237 $stateName = $stateInstance->getStateName();
245 * Checks wether the given IP address matches one of the bootstrapping nodes
247 * @param $remoteAddr IP address to checkout against our bootstrapping list
248 * @return $isFound Wether the IP is found
250 protected function ifAddressMatchesBootstrappingNodes ($remoteAddr) {
251 // By default nothing is found
254 // Run through all configured IPs
255 foreach (explode(',', $this->getConfigInstance()->getConfigEntry('hub_bootstrap_nodes')) as $ipPort) {
256 // Split it up in IP/port
257 $ipPortArray = explode(':', $ipPort);
260 if ($ipPortArray[0] == $remoteAddr) {
264 // Remember the port number
265 $this->bootIpPort = $ipPort;
268 $this->debugOutput('BOOTSTRAP: ' . __FUNCTION__ . '[' . __LINE__ . ']: IP matches remote address ' . $ipPort . '.');
270 // Stop further searching
272 } elseif ($ipPortArray[0] == $this->getConfigInstance()->getConfigEntry('node_listen_addr')) {
273 // IP matches listen address. At this point we really don't care
274 // if we can really listen on that address
277 // Remember the port number
278 $this->bootIpPort = $ipPort;
281 $this->debugOutput('BOOTSTRAP: ' . __FUNCTION__ . '[' . __LINE__ . ']: IP matches listen address ' . $ipPort . '.');
283 // Stop further searching
293 * Outputs the console teaser. This should only be executed on startup or
294 * full restarts. This method generates some space around the teaser.
298 public function outputConsoleTeaser () {
299 // Get the app instance (for shortening our code)
300 $app = $this->getApplicationInstance();
303 $this->debugOutput(' ');
304 $this->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active');
305 $this->debugOutput('Copyright (c) 2007 - 2008 Roland Haeder, 2009 - 2011 Hub Developer Team');
306 $this->debugOutput(' ');
307 $this->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
308 $this->debugOutput('This is free software, and you are welcome to redistribute it under certain');
309 $this->debugOutput('conditions; see docs/COPYING for details.');
310 $this->debugOutput(' ');
314 * Generic method to acquire a hub-id. On first run this generates a new one
315 * based on many pseudo-random data. On any later run, unless the id
316 * got not removed from database, it will be restored from the database.
318 * @param $requestInstance A Requestable class
319 * @param $responseInstance A Responseable class
322 public function bootstrapAcquireNodeId (Requestable $requestInstance, Responseable $responseInstance) {
323 // Get a wrapper instance
324 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
326 // Now get a search criteria instance
327 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
329 // Search for the node number zero which is hard-coded the default
330 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
331 $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->getRequestInstance()->getRequestElement('mode'));
332 $searchInstance->setLimit(1);
335 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
338 if ($resultInstance->next()) {
339 // Save the result instance in this class
340 $this->setResultInstance($resultInstance);
342 // Get the node id from result and set it
343 $this->setNodeId($this->getField(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID));
346 $this->debugOutput('BOOTSTRAP: Re-using found node-id: ' . $this->getNodeId() . '');
348 // Get an RNG instance (Random Number Generator)
349 $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
351 // Generate a pseudo-random string
352 $randomString = $rngInstance->randomString(255) . ':' . $this->getBootIpPort() . ':' . $this->getRequestInstance()->getRequestElement('mode');
354 // Get a crypto instance
355 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
357 // Hash and encrypt the string so we become a node id (also documented as "hub id")
358 $this->setNodeId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
360 // Register the node id with our wrapper
361 $wrapperInstance->registerNodeId($this, $this->getRequestInstance());
364 $this->debugOutput('BOOTSTRAP: Created new node-id: ' . $this->getNodeId() . '');
369 * Generates a session id which will be sent to the other hubs and peers
373 public function bootstrapGenerateSessionId () {
374 // Get an RNG instance
375 $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
377 // Generate a pseudo-random string
378 $randomString = $rngInstance->randomString(255) . ':' . $this->getBootIpPort() . ':' . $this->getRequestInstance()->getRequestElement('mode');
380 // Get a crypto instance
381 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
383 // Hash and encrypt the string so we become a "node id" aka Hub-Id
384 $this->setSessionId($cryptoInstance->hashString($cryptoInstance->encryptString($randomString)));
386 // Get a wrapper instance
387 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_info_db_wrapper_class');
389 // Register the node id with our wrapper
390 $wrapperInstance->registerSessionId($this, $this->getRequestInstance());
393 $this->debugOutput('BOOTSTRAP: Created new session-id: ' . $this->getSessionId() . '');
395 // Change the state because the node has auired a hub id
396 $this->getStateInstance()->nodeGeneratedSessionId();
400 * Initializes queues which every node needs
404 protected function initGenericQueues () {
406 $this->debugOutput('BOOTSTRAP: Initialize queues: START');
408 // Set the query connector instance
409 $this->setQueryConnectorInstance(ObjectFactory::createObjectByConfiguredName('query_connector_class', array($this)));
412 $this->getQueryConnectorInstance()->doTestQuery();
414 // Set the queue connector instance
415 $this->setQueueConnectorInstance(ObjectFactory::createObjectByConfiguredName('queue_connector_class', array($this)));
418 $this->getQueueConnectorInstance()->doTestQueue();
421 $this->debugOutput('BOOTSTRAP: Initialize queues: FINISHED');
425 * Adds hub data elements to a given dataset instance
427 * @param $criteriaInstance An instance of a storeable criteria
428 * @param $requestInstance An instance of a Requestable class
431 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance) {
432 // Add node number and type
433 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
434 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_TYPE, $requestInstance->getRequestElement('mode'));
437 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_ID, $this->getNodeId());
439 // Add the session id if acquired
440 if ($this->getSessionId() != '') {
441 $criteriaInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, $this->getSessionId());
446 * Updates a given field with new value
448 * @param $fieldName Field to update
449 * @param $fieldValue New value to store
451 * @throws DatabaseUpdateSupportException If this class does not support database updates
452 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
454 public function updateDatabaseField ($fieldName, $fieldValue) {
456 $this->partialStub('Unfinished!');
459 // Get a critieria instance
460 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
462 // Add search criteria
463 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
464 $searchInstance->setLimit(1);
466 // Now get another criteria
467 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
469 // Add criteria entry which we shall update
470 $updateInstance->addCriteria($fieldName, $fieldValue);
472 // Add the search criteria for searching for the right entry
473 $updateInstance->setSearchInstance($searchInstance);
475 // Set wrapper class name
476 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
478 // Remember the update in database result
479 $this->getResultInstance()->add2UpdateQueue($updateInstance);
483 * Announces this hub to the upper (bootstrap or list) hubs. After this is
484 * successfully done the given task is unregistered from the handler. This
485 * might look a bit overloaded here but the announcement phase isn't a
486 * simple "Hello there" message, it may later on also contain more
487 * informations like the object list.
489 * @param $taskInstance The task instance running this announcement
491 * @throws HubAlreadyAnnouncedException If this hub is already announced
492 * @todo Change the first if() block to check for a specific state
494 public function announceSelfToUpperNodes (Taskable $taskInstance) {
495 // Is this hub node announced?
496 if ($this->hubIsAnnounced === true) {
497 // Already announced!
498 throw new HubAlreadyAnnouncedException($this, self::EXCEPTION_HUB_ALREADY_ANNOUNCED);
502 $this->debugOutput('HUB: Announcement: START (taskInstance=' . $taskInstance->__toString(). ')');
504 // Get a helper instance
505 $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_announcement_helper_class', array($this));
507 // Load the announcement descriptor
508 $helperInstance->loadDescriptorXml();
510 // Set some dummy configuration entries, e.g. node_status
511 $this->getConfigInstance()->setConfigEntry('node_status', $this->getStateInstance()->getStateName());
513 // Compile all variables
514 $helperInstance->getTemplateInstance()->compileConfigInVariables();
516 // "Publish" the descriptor by sending it to the bootstrap/list nodes
517 $helperInstance->sendPackage();
519 // Change the state, this should be the last line except debug output
520 $this->getStateInstance()->nodeAnnouncedToUpperHubs();
523 $this->debugOutput('HUB: Announcement: FINISHED');
527 * Does a self-connect attempt on the public IP address. This should make
528 * it sure, we are reachable from outside world. For this kind of package we
529 * don't need that overload we have in the announcement phase.
531 * @param $taskInstance The task instance running this announcement
534 public function doSelfConnection (Taskable $taskInstance) {
536 $this->debugOutput('HUB: Self Connection: START (taskInstance=' . $taskInstance->__toString(). ')');
538 // Get a helper instance
539 $helperInstance = ObjectFactory::createObjectByConfiguredName('hub_self_connect_helper_class', array($this));
541 // Load the descriptor (XML) file
542 $helperInstance->loadDescriptorXml();
544 // And send the package away
545 $helperInstance->sendPackage();
548 $this->debugOutput('HUB: Self Connection: FINISHED');
552 * Activates the hub by doing some final preparation and setting
553 * $hubIsActive to true
555 * @param $requestInstance A Requestable class
556 * @param $responseInstance A Responseable class
559 public function activateNode (Requestable $requestInstance, Responseable $responseInstance) {
560 // Checks wether a listener is still active and shuts it down if one
561 // is still listening.
562 if (($this->determineIfListenerIsActive()) && ($this->getIsActive())) {
563 // Shutdown them down before they can hurt anything
564 $this->shutdownListenerPool();
567 // Get the controller here
568 $controllerInstance = Registry::getRegistry()->getInstance('controller');
570 // Run all filters for the hub activation
571 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
573 // ----------------------- Last step from here ------------------------
574 // Activate the hub. This is ALWAYS the last step in this method
575 $this->getStateInstance()->nodeIsActivated();
576 // ---------------------- Last step until here ------------------------
580 * Initializes the listener pool (class)
584 public function initializeListenerPool () {
586 $this->debugOutput('HUB: Initialize listener: START');
588 // Get a new pool instance
589 $this->setListenerPoolInstance(ObjectFactory::createObjectByConfiguredName('listener_pool_class', array($this)));
591 // Get an instance of the low-level listener
592 $listenerInstance = ObjectFactory::createObjectByConfiguredName('tcp_listener_class', array($this));
594 // Setup address and port
595 $listenerInstance->setListenAddressByConfiguration('node_listen_addr');
596 if ($this instanceof HubBootNode) {
597 // Bootstrap have different listening port
598 $listenerInstance->setListenPortByConfiguration('boot_node_tcp_listen_port');
600 // All other nodes use the default port
601 $listenerInstance->setListenPortByConfiguration('node_tcp_listen_port');
604 // Initialize the listener
605 $listenerInstance->initListener();
607 // Get a decorator class
608 $decoratorInstance = ObjectFactory::createObjectByConfiguredName('hub_tcp_listener_class', array($listenerInstance));
610 // Add this listener to the pool
611 $this->getListenerPoolInstance()->addListener($decoratorInstance);
613 // Get a decorator class
614 $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_tcp_listener_class', array($listenerInstance));
616 // Add this listener to the pool
617 $this->getListenerPoolInstance()->addListener($decoratorInstance);
619 // Get an instance of the low-level listener
620 $listenerInstance = ObjectFactory::createObjectByConfiguredName('udp_listener_class', array($this));
622 // Setup address and port
623 $listenerInstance->setListenAddressByConfiguration('node_listen_addr');
624 if ($this instanceof HubBootNode) {
625 // Bootstrap have different listening port
626 $listenerInstance->setListenPortByConfiguration('boot_node_udp_listen_port');
628 // All other nodes use the default port
629 $listenerInstance->setListenPortByConfiguration('node_udp_listen_port');
632 // Initialize the listener
633 $listenerInstance->initListener();
635 // Get a decorator class
636 $decoratorInstance = ObjectFactory::createObjectByConfiguredName('hub_udp_listener_class', array($listenerInstance));
638 // Add this listener to the pool
639 $this->getListenerPoolInstance()->addListener($decoratorInstance);
641 // Get a decorator class
642 $decoratorInstance = ObjectFactory::createObjectByConfiguredName('peer_udp_listener_class', array($listenerInstance));
644 // Add this listener to the pool
645 $this->getListenerPoolInstance()->addListener($decoratorInstance);
648 $this->debugOutput('HUB: Initialize listener: FINISHED.');
652 * Restores a previously stored node list from database
656 public function bootstrapRestoreNodeList () {
658 $this->debugOutput('HUB: Restore node list: START');
660 // Get a wrapper instance
661 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class');
663 // Now get a search criteria instance
664 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
666 // Search for the node number zero which is hard-coded the default
667 // @TODO Add some criteria, e.g. if the node is active or so
668 //$searchInstance->addCriteria(NodeListDatabaseWrapper::DB_COLUMN_NODE_NR, 1);
669 //$searchInstance->addCriteria(NodeListDatabaseWrapper::DB_COLUMN_NODE_TYPE, $this->getRequestInstance()->getRequestElement('mode'));
670 //$searchInstance->setLimit(1);
673 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
676 if ($resultInstance->next()) {
677 $this->partialStub('Do something for restoring the list.');
679 //$this->debugOutput('HUB: ');
681 // No previously saved node list found!
682 $this->debugOutput('HUB: No previously saved node list found. This is fine.');
686 $this->debugOutput('HUB: Restore node list: FINISHED.');
690 * Getter for isActive attribute
692 * @return $isActive Wether the hub is active
694 public final function getIsActive () {
695 return $this->isActive;
699 * Enables (default) or disables isActive flag
701 * @param $isActive Wether the hub is active
704 public final function enableIsActive ($isActive = true) {
705 $this->isActive = (bool) $isActive;
709 * "Getter for address:port combination
711 * @param $handlerInstance A valid Networkable instance
712 * @return $addressPort A address:port combination for this node
714 public final function getAddressPort (Networkable $handlerInstance) {
715 // Construct config entry
716 $configEntry = 'node_' . $handlerInstance->getHandlerName() . '_listen_port';
719 $addressPort = $this->getConfigInstance()->detectServerAddress() . ':' . $this->getConfigInstance()->getConfigEntry($configEntry);