| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License as published by |
| 4 |
* the Free Software Foundation; either version 2 of the License, or |
| 5 |
* (at your option) any later version. |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU Library General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 |
* |
| 16 |
* UniverseStore.cpp |
| 17 |
* The universe store class. This maintains the set of all active universes and |
| 18 |
* saves the settings. |
| 19 |
* Copyright (C) 2005 Simon Newton |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "olad/plugin_api/UniverseStore.h" |
| 23 |
|
| 24 |
#include <iostream> |
| 25 |
#include <set> |
| 26 |
#include <sstream> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "ola/ExportMap.h" |
| 32 |
#include "ola/Logging.h" |
| 33 |
#include "ola/StringUtils.h" |
| 34 |
#include "ola/stl/STLUtils.h" |
| 35 |
#include "olad/Preferences.h" |
| 36 |
#include "olad/Universe.h" |
| 37 |
|
| 38 |
namespace ola { |
| 39 |
|
| 40 |
using std::pair; |
| 41 |
using std::set; |
| 42 |
using std::string; |
| 43 |
using std::vector; |
| 44 |
|
| 45 |
const unsigned int UniverseStore::MINIMUM_RDM_DISCOVERY_INTERVAL = 30; |
| 46 |
|
| 47 |
UniverseStore::UniverseStore(Preferences *preferences, |
| 48 |
ExportMap *export_map) |
| 49 |
: m_preferences(preferences), |
| 50 |
m_export_map(export_map) { |
| 51 |
if (export_map) { |
| 52 |
export_map->GetStringMapVar(Universe::K_UNIVERSE_NAME_VAR, "universe"); |
| 53 |
export_map->GetStringMapVar(Universe::K_UNIVERSE_MODE_VAR, "universe"); |
| 54 |
|
| 55 |
const char *vars[] = { |
| 56 |
Universe::K_FPS_VAR, |
| 57 |
Universe::K_UNIVERSE_INPUT_PORT_VAR, |
| 58 |
Universe::K_UNIVERSE_OUTPUT_PORT_VAR, |
| 59 |
Universe::K_UNIVERSE_SINK_CLIENTS_VAR, |
| 60 |
Universe::K_UNIVERSE_SOURCE_CLIENTS_VAR, |
| 61 |
Universe::K_UNIVERSE_UID_COUNT_VAR, |
| 62 |
}; |
| 63 |
|
| 64 |
for (unsigned int i = 0; i < sizeof(vars) / sizeof(vars[0]); ++i) { |
| 65 |
export_map->GetUIntMapVar(string(vars[i]), "universe"); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
UniverseStore::~UniverseStore() { |
| 71 |
DeleteAll(); |
| 72 |
} |
| 73 |
|
| 74 |
Universe *UniverseStore::GetUniverse(unsigned int universe_id) const { |
| 75 |
return STLFindOrNull(m_universe_map, universe_id); |
| 76 |
} |
| 77 |
|
| 78 |
Universe *UniverseStore::GetUniverseOrCreate(unsigned int universe_id) { |
| 79 |
UniverseMap::iterator iter = STLLookupOrInsertNull( |
| 80 |
&m_universe_map, universe_id); |
| 81 |
|
| 82 |
if (!iter->second) { |
| 83 |
iter->second = new Universe(universe_id, this, m_export_map, &m_clock); |
| 84 |
|
| 85 |
if (iter->second) { |
| 86 |
if (m_preferences) { |
| 87 |
RestoreUniverseSettings(iter->second); |
| 88 |
} |
| 89 |
} else { |
| 90 |
OLA_WARN << "Failed to create universe " << universe_id; |
| 91 |
} |
| 92 |
} |
| 93 |
return iter->second; |
| 94 |
} |
| 95 |
|
| 96 |
void UniverseStore::GetList(vector<Universe*> *universes) const { |
| 97 |
STLValues(m_universe_map, universes); |
| 98 |
} |
| 99 |
|
| 100 |
void UniverseStore::DeleteAll() { |
| 101 |
UniverseMap::iterator iter; |
| 102 |
|
| 103 |
for (iter = m_universe_map.begin(); iter != m_universe_map.end(); iter++) { |
| 104 |
SaveUniverseSettings(iter->second); |
| 105 |
delete iter->second; |
| 106 |
} |
| 107 |
m_deletion_candidates.clear(); |
| 108 |
m_universe_map.clear(); |
| 109 |
} |
| 110 |
|
| 111 |
void UniverseStore::AddUniverseGarbageCollection(Universe *universe) { |
| 112 |
m_deletion_candidates.insert(universe); |
| 113 |
} |
| 114 |
|
| 115 |
void UniverseStore::GarbageCollectUniverses() { |
| 116 |
set<Universe*>::iterator iter; |
| 117 |
UniverseMap::iterator map_iter; |
| 118 |
|
| 119 |
for (iter = m_deletion_candidates.begin(); |
| 120 |
iter != m_deletion_candidates.end(); iter++) { |
| 121 |
if (!(*iter)->IsActive()) { |
| 122 |
SaveUniverseSettings(*iter); |
| 123 |
m_universe_map.erase((*iter)->UniverseId()); |
| 124 |
delete *iter; |
| 125 |
} |
| 126 |
} |
| 127 |
m_deletion_candidates.clear(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/* |
| 132 |
* Restore a universe's settings |
| 133 |
* @param uni the universe to update |
| 134 |
*/ |
| 135 |
bool UniverseStore::RestoreUniverseSettings(Universe *universe) const { |
| 136 |
string key, value; |
| 137 |
std::ostringstream oss; |
| 138 |
|
| 139 |
if (!universe) |
| 140 |
return 0; |
| 141 |
|
| 142 |
oss << std::dec << universe->UniverseId(); |
| 143 |
|
| 144 |
// load name |
| 145 |
key = "uni_" + oss.str() + "_name"; |
| 146 |
value = m_preferences->GetValue(key); |
| 147 |
|
| 148 |
if (!value.empty()) |
| 149 |
universe->SetName(value); |
| 150 |
|
| 151 |
// load merge mode |
| 152 |
key = "uni_" + oss.str() + "_merge"; |
| 153 |
value = m_preferences->GetValue(key); |
| 154 |
|
| 155 |
if (!value.empty()) { |
| 156 |
if (value == "HTP") |
| 157 |
universe->SetMergeMode(Universe::MERGE_HTP); |
| 158 |
else |
| 159 |
universe->SetMergeMode(Universe::MERGE_LTP); |
| 160 |
} |
| 161 |
|
| 162 |
// load RDM discovery interval |
| 163 |
key = "uni_" + oss.str() + "_rdm_discovery_interval"; |
| 164 |
value = m_preferences->GetValue(key); |
| 165 |
|
| 166 |
if (!value.empty()) { |
| 167 |
unsigned int interval; |
| 168 |
if (StringToInt(value, &interval, true)) { |
| 169 |
if (interval != 0 && interval < MINIMUM_RDM_DISCOVERY_INTERVAL) { |
| 170 |
OLA_WARN << "RDM Discovery interval for universe " << |
| 171 |
universe->UniverseId() << " less than the minimum of " << |
| 172 |
MINIMUM_RDM_DISCOVERY_INTERVAL; |
| 173 |
interval = MINIMUM_RDM_DISCOVERY_INTERVAL; |
| 174 |
} |
| 175 |
OLA_DEBUG << "RDM Discovery interval for " << oss.str() << " is " << |
| 176 |
interval; |
| 177 |
TimeInterval discovery_interval(interval, 0); |
| 178 |
universe->SetRDMDiscoveryInterval(discovery_interval); |
| 179 |
} else { |
| 180 |
OLA_WARN << "Invalid RDM discovery interval for universe " << |
| 181 |
universe->UniverseId() << ", value was " << value; |
| 182 |
} |
| 183 |
} |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* |
| 189 |
* Save this universe's settings. |
| 190 |
* @param universe, the universe to save |
| 191 |
*/ |
| 192 |
bool UniverseStore::SaveUniverseSettings(Universe *universe) const { |
| 193 |
string key, mode; |
| 194 |
std::ostringstream oss; |
| 195 |
|
| 196 |
if (!universe || !m_preferences) |
| 197 |
return 0; |
| 198 |
|
| 199 |
oss << std::dec << universe->UniverseId(); |
| 200 |
|
| 201 |
// save name |
| 202 |
key = "uni_" + oss.str() + "_name"; |
| 203 |
m_preferences->SetValue(key, universe->Name()); |
| 204 |
|
| 205 |
// save merge mode |
| 206 |
key = "uni_" + oss.str() + "_merge"; |
| 207 |
mode = (universe->MergeMode() == Universe::MERGE_HTP ? "HTP" : "LTP"); |
| 208 |
m_preferences->SetValue(key, mode); |
| 209 |
|
| 210 |
// We don't save the RDM Discovery interval since it can only be set in the |
| 211 |
// config files for now. |
| 212 |
|
| 213 |
m_preferences->Save(); |
| 214 |
|
| 215 |
return 0; |
| 216 |
} |
| 217 |
} // namespace ola |