]> git.mxchange.org Git - hub.git/blob - application/hub/main/dht/class_BaseDht.php
Use constants TRUE/FALSE/NULL instead of keywords true/false/null
[hub.git] / application / hub / main / dht / class_BaseDht.php
1 <?php
2 /**
3  * A general DHT class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 abstract class BaseDht extends BaseHubSystem {
25         /**
26          * Stacker name for "INSERT" node data
27          */
28         const STACKER_NAME_INSERT_NODE = 'dht_insert_node';
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Name of the class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39
40                 // Get a stacker instance for this DHT
41                 $stackerInstance = ObjectFactory::createObjectByConfiguredName('dht_stacker_class');
42
43                 // Set it in this class
44                 $this->setStackerInstance($stackerInstance);
45
46                 // Init all stackers
47                 $this->initStackers();
48
49                 /*
50                  * Get the state factory and create the initial state, we don't need
51                  * the state instance here
52                  */
53                 DhtStateFactory::createDhtStateInstanceByName('init', $this);
54         }
55
56         /**
57          * Initializes all stackers
58          *
59          * @return      void
60          */
61         private function initStackers () {
62                 // Initialize all stacker
63                 $this->getStackerInstance()->initStackers(array(
64                         self::STACKER_NAME_INSERT_NODE,
65                 ));
66         }
67
68         /**
69          * Registers/updates an entry in the DHT with given data from $dhtData
70          * array. Different DHT implemtations may handle this differently as they
71          * may enrich the data with more meta data.
72          *
73          * @param       $dhtData        A valid array with DHT-related data (e.g. node/peer data)
74          * @return      void
75          */
76         protected abstract function insertDataIntoDht (array $dhtData);
77
78         /**
79          * Updates/refreshes DHT data (e.g. status).
80          *
81          * @return      void
82          * @todo        Find more to do here
83          */
84         public function updateDhtData () {
85                 // Set some dummy configuration entries, e.g. dht_status
86                 $this->getConfigInstance()->setConfigEntry('dht_status', $this->getStateInstance()->getStateName());
87         }
88
89         /**
90          * Checks whether there are entries in "INSERT" node data stack
91          *
92          * @return      $isPending      Whether there are pending entries
93          */
94         public function ifInsertNodeDataPending () {
95                 // Determine it if it is not empty
96                 $isPending = ($this->getStackerInstance()->isStackEmpty(self::STACKER_NAME_INSERT_NODE) === FALSE);
97
98                 // Return status
99                 return $isPending;
100         }
101
102         /**
103          * Inserts a single entry of node data into the DHT
104          *
105          * @return      void
106          */
107         public function insertSingleNodeData () {
108                 // Get next node data from stack
109                 $nodeData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_INSERT_NODE);
110
111                 // Make sure $nodeData is really an array and has at least one entry
112                 assert((is_array($nodeData)) && (count($nodeData) > 0));
113
114                 // Insert the data
115                 $this->insertDataIntoDht($nodeData);
116         }
117 }
118
119 // [EOF]
120 ?>