user_def_ipk.cpp 4.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* 
  SZARP: SCADA software 
  

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
/* 
 * draw3
 * SZARP

 *
 * $Id$
 *
 * Contains user defined IPK manager
 */

#include "user_def_ipk.h"
#include <boost/thread/locks.hpp>

UserDefinedIPKManager::UserDefinedIPKManager(const std::wstring& szarp_data_dir, const std::wstring& szarp_system_dir, const std::wstring& language) {
	ipk = new UserDefinedIPKContainer(szarp_data_dir, szarp_system_dir, language, this);
}

bool UserDefinedIPKManager::PreloadConfig(const std::wstring &prefix) {
	boost::upgrade_lock<boost::shared_mutex> lock(mutex);
	if (configs_ready_for_load.get(prefix) != nullptr)
		return true;

	boost::upgrade_to_unique_lock<boost::shared_mutex> unique(lock);

	auto config = ipk->InitializeIPK(prefix);
	if (!config)
		return false;

	configs_ready_for_load.insert(prefix, config);
	return true;
}

TSzarpConfig* UserDefinedIPKManager::PopConfig(const std::wstring &prefix) {
52
	boost::unique_lock<boost::shared_mutex> lock(mutex);
53 54 55 56 57 58 59 60 61
	TSzarpConfig* config = configs_ready_for_load.get(prefix);
	if (config) {
		configs_ready_for_load.remove(prefix);
	}
	return config;
}

void UserDefinedIPKManager::AddUserDefined(const std::wstring& prefix, TParam *param) {
	if (ipk->AddUserDefined(prefix, param)) {
62
		boost::unique_lock<boost::shared_mutex> lock(mutex);
63 64 65 66 67 68
		m_extra_params[prefix].emplace_back(param);
	}
}

void UserDefinedIPKManager::RemoveUserDefined(const std::wstring& prefix, TParam *param) {
	if (ipk->RemoveUserDefined(prefix, param)) {
69
		boost::unique_lock<boost::shared_mutex> lock(mutex);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		auto& tp = m_extra_params[prefix];
		auto ei = std::find_if(tp.begin(), tp.end(), 
			[param] (std::shared_ptr<TParam> &_p) { return _p.get() == param; }
		);

		if (ei != tp.end()) {
			tp.erase(ei);
		}
	}
}

bool UserDefinedIPKContainer::AddUserDefined(const std::wstring& prefix, TParam *n) {
	boost::unique_lock<boost::shared_mutex> lock(ParamCachingIPKContainer::m_lock);

	TSzarpConfig* config;
	if (!(config = ParamCachingIPKContainer::configs.get(prefix))) {
		return false;
	}

	n->SetParentSzarpConfig(config);
	cacher.AddParam(n);

	/* update param's id in config cache */
	/* TODO: move this to TSzarpConfig or separate function */
	ConfigAux& aux = ParamCachingIPKContainer::config_aux[prefix];
	if (aux._freeIds.size()) {
		std::set<unsigned>::iterator i = aux._freeIds.begin();
		n->SetParamId(*i);
		aux._freeIds.erase(i);
	} else {
		n->SetParamId(aux._maxParamId++);
	}

	return true;
}

bool UserDefinedIPKContainer::RemoveUserDefined(const std::wstring& prefix, TParam *p) {
	boost::unique_lock<boost::shared_mutex> lock(ParamCachingIPKContainer::m_lock);

	if (!cacher.RemoveParam(p)) {
		return false;
	}

	/* update param's id in config cache */
	/* TODO: move this to TSzarpConfig or separate function */
	ConfigAux& aux = ParamCachingIPKContainer::config_aux[prefix];
	if (p->GetParamId() + 1 == aux._maxParamId)
		aux._maxParamId--;
	else
		aux._freeIds.insert(p->GetParamId());

	return true;
}

std::vector<std::shared_ptr<TParam>>& UserDefinedIPKManager::GetUserDefinedParams(const std::wstring& prefix) {
	return m_extra_params[prefix];
}

UserDefinedIPKContainer::UserDefinedIPKContainer(const std::wstring& szarp_data_dir, const std::wstring& szarp_system_dir, const std::wstring &language, UserDefinedIPKManager* _ipk_manager): ParamCachingIPKContainer(szarp_data_dir, szarp_system_dir, language), manager(_ipk_manager) {
	assert(IPKContainer::_object == nullptr);
Michal Glinka's avatar
Michal Glinka committed
130 131
	if( IPKContainer::_object != nullptr && IPKContainer::_object != this )
		delete IPKContainer::_object;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	IPKContainer::_object = this;
}

TSzarpConfig* UserDefinedIPKContainer::AddConfig(const std::wstring& prefix) {
	auto config = manager->PopConfig(prefix);
	if (config) {
		config = AddConfig(config, prefix);
	} else {
		config = ParamCachingIPKContainer::AddConfig(prefix);
	}
	return config;
}

TSzarpConfig* UserDefinedIPKContainer::AddConfig(TSzarpConfig* config, const std::wstring& prefix) {
	// No need to lock manager as this version of AddConfig will be called from prefix-only

	ParamCachingIPKContainer::AddConfig(config, prefix);
	// manager->UpdateConfig(ipk, prefix);

	auto& ca = config_aux[prefix];
	unsigned id = ca._maxParamId;
	for (auto p: manager->GetUserDefinedParams(prefix)) {
		p->SetParamId(id++);
		p->SetParentSzarpConfig(config);
		p->SetConfigId(ca._configId);
		cacher.AddParam(p.get());
	}

	return config;
}