]> git.mxchange.org Git - hub.git/blob - application/hub/main/database/wrapper/node/class_NodeInformationDatabaseWrapper.php
Expanded debug lines, renamed 'node_type' to 'node_mode' (which makes your DHT databa...
[hub.git] / application / hub / main / database / wrapper / node / class_NodeInformationDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for node informations
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 NodeInformationDatabaseWrapper extends BaseDatabaseWrapper implements NodeInformationWrapper, Registerable {
25         // Constants for database table names
26         const DB_TABLE_NODE_INFORMATION = 'node_data';
27
28         // Constants for database column names
29         const DB_COLUMN_NODE_NR          = 'node_nr';
30         const DB_COLUMN_NODE_ID          = 'node_id';
31         const DB_COLUMN_SESSION_ID       = 'session_id';
32         const DB_COLUMN_PRIVATE_KEY      = 'private_key';
33         const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
34         const DB_COLUMN_NODE_MODE        = 'node_mode';
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 createNodeInformationDatabaseWrapper () {
52                 // Get a new instance
53                 $wrapperInstance = new NodeInformationDatabaseWrapper();
54
55                 // Set (primary!) table name
56                 $wrapperInstance->setTableName(self::DB_TABLE_NODE_INFORMATION);
57
58                 // Return the instance
59                 return $wrapperInstance;
60         }
61
62         /**
63          * Checks whether there is an entry for given node instance
64          *
65          * @param       $nodeInstance   An instance of a NodeHelper class
66          * @return      $isFound                Whether a node id has been found for this node
67          */
68         public function ifNodeDataIsFound (NodeHelper $nodeInstance) {
69                 // Is there cache?
70                 if (!isset($GLOBALS[__METHOD__])) {
71                         // Now get a search criteria instance
72                         $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
73
74                         // Search for the node number one which is hard-coded the default
75                         $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR  , 1);
76                         $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_MODE, $nodeInstance->getRequestInstance()->getRequestElement('mode'));
77                         $searchInstance->setLimit(1);
78
79                         // Get a result back
80                         $resultInstance = $this->doSelectByCriteria($searchInstance);
81
82                         // Set result instance in node instance
83                         $nodeInstance->setResultInstance($resultInstance);
84
85                         // Is it valid?
86                         $GLOBALS[__METHOD__] = $resultInstance->next();
87                 } // END - if
88
89                 // Return it
90                 return $GLOBALS[__METHOD__];
91         }
92
93         /**
94          * 'Registers' a new node id along with data provided in the node instance.
95          * This may sound confusing but avoids double code very nicely...
96          *
97          * @param       $nodeInstance           A node instance
98          * @param       $requestInstance        An instance of a Requestable class
99          * @return      void
100          */
101         public function registerNodeId (BaseHubNode $nodeInstance, Requestable $requestInstance) {
102                 // Get a dataset instance
103                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
104
105                 // Set the primary key
106                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
107
108                 // Add registration elements to the dataset
109                 $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
110
111                 // "Insert" this dataset instance completely into the database
112                 $this->queryInsertDataSet($dataSetInstance);
113         }
114
115         /**
116          * 'Registers' a new session id along with data provided in the node instance.
117          * This may sound confusing but avoids double code very nicely...
118          *
119          * @param       $nodeInstance           An instance of a BaseHubNode class
120          * @param       $requestInstance        An instance of a Requestable class
121          * @param       $searchInstance         An instance of a LocalSearchCriteria class
122          * @return      void
123          */
124         public function registerSessionId (BaseHubNode $nodeInstance, Requestable $requestInstance, LocalSearchCriteria $searchInstance) {
125                 // Get a dataset instance
126                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
127
128                 // Set search instance
129                 $dataSetInstance->setSearchInstance($searchInstance);
130
131                 // Set the primary key
132                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
133
134                 // Add registration elements to the dataset
135                 $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
136
137                 // Update database record
138                 $this->queryUpdateDataSet($dataSetInstance);
139         }
140
141         /**
142          * 'Registers' a private key along with data provided in the node instance.
143          * This may sound confusing but avoids double code very nicely...
144          *
145          * @param       $nodeInstance           An instance of a BaseHubNode class
146          * @param       $requestInstance        An instance of a Requestable class
147          * @param       $searchInstance         An instance of a LocalSearchCriteria class
148          * @return      void
149          */
150         public function registerPrivateKey (BaseHubNode $nodeInstance, Requestable $requestInstance, LocalSearchCriteria $searchInstance) {
151                 // Get a dataset instance
152                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
153
154                 // Set the primary key
155                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
156
157                 // Set search instance
158                 $dataSetInstance->setSearchInstance($searchInstance);
159
160                 // Add registration elements to the dataset
161                 $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
162
163                 // Update database record
164                 $this->queryUpdateDataSet($dataSetInstance);
165         }
166 }
167
168 // [EOF]
169 ?>