5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2015 City Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 BaseCityDaemon extends BaseCitySystem implements Updateable, AddableCriteria {
28 const CITY_TYPE_TESTING = 'testing';
31 * Whether this City is active (default: FALSE)
33 private $isActive = FALSE;
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
45 // Get a wrapper instance
46 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
49 $this->setWrapperInstance($wrapperInstance);
51 // Get a crypto instance
52 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
55 $this->setCryptoInstance($cryptoInstance);
57 // Add own instance to registry
58 Registry::getRegistry()->addInstance('city', $this);
60 // Init state which sets the state to 'init'
65 * Initializes the City's state which sets it to 'init'
69 private function initState() {
70 // Get the state factory and create the initial state.
71 CityStateFactory::createCityStateInstanceByName('init');
75 * Outputs the console teaser. This should only be executed on startup or
76 * full restarts. This method generates some space around the teaser.
80 public function outputConsoleTeaser () {
81 // Get the app instance (for shortening our code)
82 $app = $this->getApplicationInstance();
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(' ');
96 * Adds City data elements to a given dataset instance
98 * @param $criteriaInstance An instance of a storeable criteria
99 * @param $requestInstance An instance of a Requestable class
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);
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'));
111 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
113 // Add the session id if acquired
114 if ($this->getSessionId() != '') {
115 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, $this->getSessionId());
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());
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());
130 * Updates a given field with new value
132 * @param $fieldName Field to update
133 * @param $fieldValue New value to store
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
138 public function updateDatabaseField ($fieldName, $fieldValue) {
140 $this->partialStub('Unfinished!');
143 // Get a critieria instance
144 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
146 // Add search criteria
147 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
148 $searchInstance->setLimit(1);
150 // Now get another criteria
151 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
153 // Add criteria entry which we shall update
154 $updateInstance->addCriteria($fieldName, $fieldValue);
156 // Add the search criteria for searching for the right entry
157 $updateInstance->setSearchInstance($searchInstance);
159 // Set wrapper class name
160 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
162 // Remember the update in database result
163 $this->getResultInstance()->add2UpdateQueue($updateInstance);
167 * Activates the City by doing some final preparation and setting
168 * $CityIsActive to TRUE.
170 * @param $requestInstance A Requestable class
171 * @param $responseInstance A Responseable class
174 public function activateCity (Requestable $requestInstance, Responseable $responseInstance) {
175 // Get the controller here
176 $controllerInstance = Registry::getRegistry()->getInstance('controller');
178 // Run all filters for the City activation
179 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
181 // Make sure the city's state is 'virigin'
182 $this->getStateInstance()->validateCityStateIsVirgin();
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 ------------------------
192 * Getter for isActive attribute
194 * @return $isActive Whether the City is active
196 public final function isCityActive () {
197 return $this->isActive;
201 * Enables (default) or disables isActive flag
203 * @param $isActive Whether the City is active
206 public final function enableIsActive ($isActive = TRUE) {
207 $this->isActive = (bool) $isActive;
211 * Updates/refreshes City data (e.g. status).
214 * @todo Find more to do here
216 public function updateCityData () {
217 // Set some dummy configuration entries, e.g. city_status
218 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
222 * Adds all required elements from given array into data set instance
224 * @param $dataSetInstance An instance of a StoreableCriteria class
225 * @param $CityData An array with valid City data
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])) {
234 $dataSetInstance->addCriteria($element, $cityData[$element]);
236 // Output warning message
237 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
243 * Initializes the city daemon
247 public function bootstrapInitCityDaemon () {
248 $this->partialStub('Please add something here.');
250 // Daemon has been fully initialized, so change state to 'virgin'
251 $this->getStateInstance()->daemonHasInitialized();