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_DEFAULT = 'default';
29 const CITY_TYPE_TESTING = 'testing';
32 * Whether this City is active (default: FALSE)
34 private $isActive = FALSE;
37 * Protected constructor
39 * @param $className Name of the class
42 protected function __construct ($className) {
43 // Call parent constructor
44 parent::__construct($className);
46 // Get a wrapper instance
47 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
50 $this->setWrapperInstance($wrapperInstance);
52 // Get a crypto instance
53 $cryptoInstance = ObjectFactory::createObjectByConfiguredName('crypto_class');
56 $this->setCryptoInstance($cryptoInstance);
58 // Add own instance to registry
59 Registry::getRegistry()->addInstance('city', $this);
61 // Init state which sets the state to 'init'
66 * Initializes the City's state which sets it to 'init'
70 private function initState() {
71 // Get the state factory and create the initial state.
72 CityStateFactory::createCityStateInstanceByName('init');
76 * Outputs the console teaser. This should only be executed on startup or
77 * full restarts. This method generates some space around the teaser.
81 public function outputConsoleTeaser () {
82 // Get the app instance (for shortening our code)
83 $app = $this->getApplicationInstance();
86 self::createDebugInstance(__CLASS__)->debugOutput(' ');
87 self::createDebugInstance(__CLASS__)->debugOutput($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' daemon starting');
88 self::createDebugInstance(__CLASS__)->debugOutput('Copyright (c) 2015 City Developer Team');
89 self::createDebugInstance(__CLASS__)->debugOutput(' ');
90 self::createDebugInstance(__CLASS__)->debugOutput('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.');
91 self::createDebugInstance(__CLASS__)->debugOutput('This is free software, and you are welcome to redistribute it under certain');
92 self::createDebugInstance(__CLASS__)->debugOutput('conditions; see docs/COPYING for details.');
93 self::createDebugInstance(__CLASS__)->debugOutput(' ');
97 * Adds City data elements to a given dataset instance
99 * @param $criteriaInstance An instance of a storeable criteria
100 * @param $requestInstance An instance of a Requestable class
103 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
104 // Make sure the request instance is set as it is not optional.
105 assert($requestInstance instanceof Requestable);
107 // Add City number and type
108 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID , 1);
109 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_MODE, $requestInstance->getRequestElement('mode'));
112 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_CITY_ID, $this->getCityId());
114 // Add the session id if acquired
115 if ($this->getSessionId() != '') {
116 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_SESSION_ID, $this->getSessionId());
119 // Add the private key if acquired
120 if ($this->getPrivateKey() != '') {
121 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY, base64_encode($this->getPrivateKey()));
122 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_PRIVATE_KEY_HASH, $this->getPrivateKeyHash());
125 // Add own external and internal addresses as UNLs
126 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_INTERNAL_UNL, CityTools::determineOwnInternalAddress());
127 $criteriaInstance->addCriteria(CityInformationDatabaseWrapper::DB_COLUMN_EXTERNAL_UNL, CityTools::determineOwnExternalAddress());
131 * Updates a given field with new value
133 * @param $fieldName Field to update
134 * @param $fieldValue New value to store
136 * @throws DatabaseUpdateSupportException If this class does not support database updates
137 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
139 public function updateDatabaseField ($fieldName, $fieldValue) {
141 $this->partialStub('Unfinished!');
144 // Get a critieria instance
145 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
147 // Add search criteria
148 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
149 $searchInstance->setLimit(1);
151 // Now get another criteria
152 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
154 // Add criteria entry which we shall update
155 $updateInstance->addCriteria($fieldName, $fieldValue);
157 // Add the search criteria for searching for the right entry
158 $updateInstance->setSearchInstance($searchInstance);
160 // Set wrapper class name
161 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
163 // Remember the update in database result
164 $this->getResultInstance()->add2UpdateQueue($updateInstance);
168 * Activates the City by doing some final preparation and setting
169 * $CityIsActive to TRUE.
171 * @param $requestInstance A Requestable class
172 * @param $responseInstance A Responseable class
175 public function activateCityDaemon (Requestable $requestInstance, Responseable $responseInstance) {
176 // Get the controller here
177 $controllerInstance = Registry::getRegistry()->getInstance('controller');
179 // Run all filters for the City activation
180 $controllerInstance->executeActivationFilters($requestInstance, $responseInstance);
182 // Make sure the city's state is 'init'
183 $this->getStateInstance()->validateCityStateIsInit();
185 // ----------------------- Last step from here ------------------------
186 // Activate the city daemon. This is ALWAYS the last step in this method
187 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: state=' . $this->getStateInstance()->__toString() . ' - Activating ...');
188 $this->getStateInstance()->citySimulationIsActivated();
189 // ---------------------- Last step until here ------------------------
193 * Getter for isActive attribute
195 * @return $isActive Whether the City is active
197 public final function isCityActive () {
198 return $this->isActive;
202 * Enables (default) or disables isActive flag
204 * @param $isActive Whether the City is active
207 public final function enableIsActive ($isActive = TRUE) {
208 $this->isActive = (bool) $isActive;
212 * Updates/refreshes City data (e.g. status).
215 * @todo Find more to do here
217 public function updateCityData () {
218 // Set some dummy configuration entries, e.g. city_status
219 $this->getConfigInstance()->setConfigEntry('city_status', $this->getStateInstance()->getStateName());
223 * Adds all required elements from given array into data set instance
225 * @param $dataSetInstance An instance of a StoreableCriteria class
226 * @param $CityData An array with valid City data
229 public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $cityData) {
230 // Add all data the array provides
231 foreach (CityInformationDatabaseWrapper::getAllElements() as $element) {
232 // Is the element there?
233 if (isset($cityData[$element])) {
235 $dataSetInstance->addCriteria($element, $cityData[$element]);
237 // Output warning message
238 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CITY[' . __METHOD__ . ':' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in CityData array.');
244 * Initializes the city daemon
249 public function bootstrapInitCityDaemon () {
250 $this->partialStub('Please add something here.');
254 * Checks whether at least one map requires expansion
256 * @return $requiresExpansion Whether a map requires expansion
259 public function isMapPendingExpansion () {
261 $mapInstance = ManagerFactory::createManagerByType('city_map');
263 $this->partialStub('Please add something here.');