]> git.mxchange.org Git - city.git/blob - application/city/main/city_daemon/class_BaseCityDaemon.php
661c6138b631840a5061c4592a5272620766e83e
[city.git] / application / city / main / city_daemon / class_BaseCityDaemon.php
1 <?php
2 /**
3  * A general City class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2015 City Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
25         /**
26          * City types
27          */
28         const CITY_TYPE_TESTING = 'testing';
29
30         /**
31          * Whether this City is active (default: FALSE)
32          */
33         private $isActive = FALSE;
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Get a wrapper instance
46                 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
47
48                 // Set it here
49                 $this->setWrapperInstance($wrapperInstance);
50
51                 // Get a crypto instance
52                 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
53
54                 // Set it here
55                 $this->setCryptoInstance($cryptoInstance);
56
57                 // Add own instance to registry
58                 Registry::getRegistry()->addInstance('city', $this);
59
60                 // Init state which sets the state to 'init'
61                 $this->initState();
62         }
63
64         /**
65          * Initializes the City's state which sets it to 'init'
66          *
67          * @return      void
68          */
69         private function initState() {
70                 // Get the state factory and create the initial state.
71                 CityStateFactory::createCityStateInstanceByName('init');
72         }
73
74         /**
75          * Outputs the console teaser. This should only be executed on startup or
76          * full restarts. This method generates some space around the teaser.
77          *
78          * @return      void
79          */
80         public function outputConsoleTeaser () {
81                 // Get the app instance (for shortening our code)
82                 $app = $this->getApplicationInstance();
83
84                 // Output all lines
85                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
86                 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting');
87                 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015 City Developer Team');
88                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
89                 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
90                 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
91                 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
92                 self::createDebugInstance(__CLASS__)->debugOutput(' ');
93         }
94
95         /**
96          * Adds City data elements to a given dataset instance
97          *
98          * @param       $criteriaInstance       An instance of a storeable criteria
99          * @param       $requestInstance        An instance of a Requestable class
100          * @return      void
101          */
102         public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
103                 // Make sure the request instance is set as it is not optional.
104                 assert($requestInstance instanceof Requestable);
105
106                 // Add City number and type
107                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID  , 1);
108                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
109
110                 // Add the City id
111                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
112
113                 // Add the session id if acquired
114                 if ($this->getSessionId() != '') {
115                         $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, $this->getSessionId());
116                 } // END - if
117
118                 // Add the private key if acquired
119                 if ($this->getPrivateKey() != '') {
120                         $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY, base64_encode($this->getPrivateKey()));
121                         $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, $this->getPrivateKeyHash());
122                 } // END - if
123
124                 // Add own external and internal addresses as UNLs
125                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL, CityTools::determineOwnInternalAddress());
126                 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL, CityTools::determineOwnExternalAddress());
127         }
128
129         /**
130          * Updates a given field with new value
131          *
132          * @param       $fieldName              Field to update
133          * @param       $fieldValue             New value to store
134          * @return      void
135          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
136          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
137          */
138         public function updateDatabaseField ($fieldName, $fieldValue) {
139                 // Unfinished
140                 $this->partialStub('Unfinished!');
141                 return;
142
143                 // Get a critieria instance
144                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
145
146                 // Add search criteria
147                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
148                 $searchInstance->setLimit(1);
149
150                 // Now get another criteria
151                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
152
153                 // Add criteria entry which we shall update
154                 $updateInstance->addCriteria($fieldName, $fieldValue);
155
156                 // Add the search criteria for searching for the right entry
157                 $updateInstance->setSearchInstance($searchInstance);
158
159                 // Set wrapper class name
160                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
161
162                 // Remember the update in database result
163                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
164         }
165
166         /**
167          * Activates the City by doing some final preparation and setting
168          * $CityIsActive to TRUE.
169          *
170          * @param       $requestInstance        A Requestable class
171          * @param       $responseInstance       A Responseable class
172          * @return      void
173          */
174         public function activateCity (Requestable $requestInstance, Responseable $responseInstance) {
175                 // Get the controller here
176                 $controllerInstance = Registry::getRegistry()->getInstance('controller');
177
178                 // Run all filters for the City activation
179                 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
180
181                 // Make sure the city's state is 'virigin'
182                 $this->getStateInstance()->validateCityStateIsVirgin();
183
184                 // ----------------------- Last step from here ------------------------
185                 // Activate the City. This is ALWAYS the last step in this method
186                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString());
187                 $this->getStateInstance()->citySimulationIsActivated();
188                 // ---------------------- Last step until here ------------------------
189         }
190
191         /**
192          * Getter for isActive attribute
193          *
194          * @return      $isActive       Whether the City is active
195          */
196         public final function isCityActive () {
197                 return $this->isActive;
198         }
199
200         /**
201          * Enables (default) or disables isActive flag
202          *
203          * @param       $isActive       Whether the City is active
204          * @return      void
205          */
206         public final function enableIsActive ($isActive = TRUE) {
207                 $this->isActive = (bool) $isActive;
208         }
209
210         /**
211          * Updates/refreshes City data (e.g. status).
212          *
213          * @return      void
214          * @todo        Find more to do here
215          */
216         public function updateCityData () {
217                 // Set some dummy configuration entries, e.g. city_status
218                 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
219         }
220
221         /**
222          * Adds all required elements from given array into data set instance
223          *
224          * @param       $dataSetInstance        An instance of a StoreableCriteria class
225          * @param       $CityData                       An array with valid City data
226          * @return      void
227          */
228         public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
229                 // Add all data the array provides
230                 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
231                         // Is the element there?
232                         if (isset($cityData[$element])) {
233                                 // Add it
234                                 $dataSetInstance->addCriteria($element, $cityData[$element]);
235                         } else {
236                                 // Output warning message
237                                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
238                         }
239                 } // END - foreac
240         }
241
242         /**
243          * Initializes the city daemon
244          *
245          * @return      void
246          */
247         public function bootstrapInitCityDaemon () {
248                 $this->partialStub('Please add something here.');
249
250                 // Daemon has been fully initialized, so change state to 'virgin'
251                 $this->getStateInstance()->daemonHasInitialized();
252         }
253 }
254
255 // [EOF]
256 ?>