]> git.mxchange.org Git - hub.git/blob - application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
Introduced and implemented findNodeBySessionId()
[hub.git] / application / hub / main / database / wrapper / node / class_NodeDistributedHashTableDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for distributed hash tables
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 class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implements NodeDhtWrapper, Registerable {
25         // Constants for database table names
26         const DB_TABLE_NODE_DHT = 'node_dht';
27
28         // Constants for database column names
29         const DB_COLUMN_NODE_ID          = 'node_id';
30         const DB_COLUMN_SESSION_ID       = 'session_id';
31         const DB_COLUMN_IP_PORT          = 'ip_port';
32         const DB_COLUMN_PRIVATE_KEY      = 'private_key';
33         const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
34         const DB_COLUMN_NODE_TYPE        = 'node_type';
35
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates an instance of this database wrapper by a provided user class
48          *
49          * @return      $wrapperInstance        An instance of the created wrapper class
50          */
51         public static final function createNodeDistributedHashTableDatabaseWrapper () {
52                 // Get a new instance
53                 $wrapperInstance = new NodeDistributedHashTableDatabaseWrapper();
54
55                 // Set (primary!) table name
56                 $wrapperInstance->setTableName(self::DB_TABLE_NODE_DHT);
57
58                 // Return the instance
59                 return $wrapperInstance;
60         }
61
62         /**
63          * Prepares an instance of a StoreableCriteria class with all node data for
64          * insert/update queries.
65          *
66          * @return      $dataSetInstance        An instance of a StoreableCriteria class
67          */
68         private function prepareDataSetInstance () {
69                 // Get node/request instances
70                 $nodeInstance = Registry::getRegistry()->getInstance('node');
71                 $requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance();
72
73                 // Get a dataset instance
74                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
75
76                 // Set the primary key
77                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
78
79                 // Add public node data
80                 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_TYPE       , $requestInstance->getRequestElement('mode'));
81                 $dataSetInstance->addCriteria(self::DB_COLUMN_IP_PORT         , $nodeInstance->getAddressPort());
82                 $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID         , $nodeInstance->getNodeId());
83                 $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID      , $nodeInstance->getSessionId());
84                 $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $nodeInstance->getPrivateKeyHash());
85
86                 // Return it
87                 return $dataSetInstance;
88         }
89
90         /**
91          * Checks whether the local (*this*) node is registered in the DHT by
92          * checking if the external ip/port is found.
93          *
94          * @return      $isRegistered   Whether *this* node is registered in the DHT
95          */
96         public function isLocalNodeRegistered () {
97                 // Is there cache?
98                 if (!isset($GLOBALS[__METHOD__])) {
99                         // Get a search criteria instance
100                         $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
101
102                         // Get node instance
103                         $nodeInstance = Registry::getRegistry()->getInstance('node');
104
105                         // Add ip:port as criteria
106                         $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
107                         $searchInstance->setLimit(1);
108
109                         // Query database and get a result instance back
110                         $resultInstance = $this->doSelectByCriteria($searchInstance);
111
112                         // Cache result of if there is an entry, next() will tell us if the next entry is valid
113                         $GLOBALS[__METHOD__] = $resultInstance->next();
114                 } // END - if
115
116                 // Return result
117                 return $GLOBALS[__METHOD__];
118         }
119
120         /**
121          * Registeres the local (*this*) node with its data in the DHT.
122          *
123          * @return      void
124          */
125         public function registerLocalNode () {
126                 // Assert to make sure this method is called with no record in DB (the actual backend of the DHT)
127                 assert(!$this->isLocalNodeRegistered());
128
129                 // Get prepared data set instance
130                 $dataSetInstance = $this->prepareDataSetInstance();
131
132                 // "Insert" this dataset instance completely into the database
133                 $this->queryInsertDataSet($dataSetInstance);
134         }
135
136         /**
137          * Updates local (*this*) node data in DHT, this is but not limited to the
138          * session id, ip number (and/or hostname) and port number.
139          *
140          * @return      void
141          */
142         public function updateLocalNode () {
143                 // Assert to make sure this method is called with one record in DB (the actual backend of the DHT)
144                 assert($this->isLocalNodeRegistered());
145
146                 // Get node instance
147                 $nodeInstance = Registry::getRegistry()->getInstance('node');
148
149                 // Get search criteria
150                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
151
152                 // Search for node id and limit it to one entry
153                 $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeInstance->getNodeId());
154                 $searchInstance->setLimit(1);
155
156                 // Get a prepared dataset instance
157                 $dataSetInstance = $this->prepareDataSetInstance();
158
159                 // Set search instance
160                 $dataSetInstance->setSearchInstance($searchInstance);
161
162                 // Update DHT database record
163                 $this->queryUpdateDataSet($dataSetInstance);
164         }
165
166         /**
167          * Finds a node by given session id
168          *
169          * @param       $sessionId      Session id to lookup
170          * @return      $nodeData       Node data array
171          */
172         public function findNodeBySessionId ($sessionId) {
173                 // Get search criteria
174                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
175
176                 // Get node instance
177                 $nodeInstance = Registry::getRegistry()->getInstance('node');
178
179                 // Search for session id and limit it to one entry
180                 $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID, $nodeInstance->getSessionId());
181                 $searchInstance->setLimit(1);
182
183                 // Query database and get a result instance back
184                 $resultInstance = $this->doSelectByCriteria($searchInstance);
185
186                 // Return result instance
187                 return $resultInstance;
188         }
189 }
190
191 // [EOF]
192 ?>