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.
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 General Public License for more details.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
25 #include "stg/admin.h"
26 #include "stg/common.h"
27 #include "services_impl.h"
29 //-----------------------------------------------------------------------------
30 SERVICES_IMPL::SERVICES_IMPL(STORE * st)
34 WriteServLog(GetStgLogger()),
38 pthread_mutex_init(&mutex, NULL);
41 //-----------------------------------------------------------------------------
42 int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin)
44 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
45 const PRIV * priv = admin->GetPriv();
47 if (!priv->serviceChg)
49 string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied.";
50 strError = "Access denied.";
51 WriteServLog(s.c_str());
55 srv_iter si(find(data.begin(), data.end(), service));
59 strError = "Service \'" + service.name + "\' cannot not be added. Service already exist.";
60 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
65 data.push_back(service);
67 if (store->AddService(service.name) == 0)
69 WriteServLog("%s Service \'%s\' added.",
70 admin->GetLogStr().c_str(), service.name.c_str());
74 strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError();
75 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
79 //-----------------------------------------------------------------------------
80 int SERVICES_IMPL::Del(const string & name, const ADMIN * admin)
82 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
83 const PRIV * priv = admin->GetPriv();
85 if (!priv->serviceChg)
87 string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied.";
88 strError = "Access denied.";
89 WriteServLog(s.c_str());
93 srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
97 strError = "Service \'" + name + "\' cannot be deleted. Service does not exist.";
98 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
102 map<int, const_srv_iter>::iterator csi;
103 csi = searchDescriptors.begin();
104 while (csi != searchDescriptors.end())
106 if (csi->second == si)
112 if (store->DelService(name) < 0)
114 strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError();
115 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
120 WriteServLog("%s Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
123 //-----------------------------------------------------------------------------
124 int SERVICES_IMPL::Change(const SERVICE_CONF & service, const ADMIN * admin)
126 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
127 const PRIV * priv = admin->GetPriv();
129 if (!priv->serviceChg)
131 string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied.";
132 strError = "Access denied.";
133 WriteServLog(s.c_str());
137 srv_iter si(find(data.begin(), data.end(), service));
139 if (si == data.end())
141 strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist.";
142 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
147 if (store->SaveService(service))
149 WriteServLog("Cannot write service %s.", service.name.c_str());
150 WriteServLog("%s", store->GetStrError().c_str());
154 WriteServLog("%s Service \'%s\' changed.",
155 admin->GetLogStr().c_str(), service.name.c_str());
159 //-----------------------------------------------------------------------------
160 bool SERVICES_IMPL::Read()
162 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
163 vector<string> servicesList;
164 if (store->GetServicesList(&servicesList) < 0)
166 WriteServLog(store->GetStrError().c_str());
170 for (size_t i = 0; i < servicesList.size(); i++)
172 SERVICE_CONF service;
174 if (store->RestoreService(&service, servicesList[i]))
176 WriteServLog(store->GetStrError().c_str());
180 data.push_back(service);
184 //-----------------------------------------------------------------------------
185 bool SERVICES_IMPL::Find(const string & name, SERVICE_CONF * service)
187 assert(service != NULL && "Pointer to service is not null");
189 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
193 srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
195 if (si != data.end())
203 //-----------------------------------------------------------------------------
204 bool SERVICES_IMPL::Exists(const string & name) const
206 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
209 printfd(__FILE__, "no admin in system!\n");
213 const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
215 if (si != data.end())
220 //-----------------------------------------------------------------------------
221 int SERVICES_IMPL::OpenSearch() const
223 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
225 searchDescriptors[handle] = data.begin();
228 //-----------------------------------------------------------------------------
229 int SERVICES_IMPL::SearchNext(int h, SERVICE_CONF * service) const
231 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
232 if (searchDescriptors.find(h) == searchDescriptors.end())
234 WriteServLog("SERVICES. Incorrect search handle.");
238 if (searchDescriptors[h] == data.end())
241 *service = *searchDescriptors[h]++;
245 //-----------------------------------------------------------------------------
246 int SERVICES_IMPL::CloseSearch(int h) const
248 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
249 if (searchDescriptors.find(h) != searchDescriptors.end())
251 searchDescriptors.erase(searchDescriptors.find(h));
255 WriteServLog("SERVICES. Incorrect search handle.");
258 //-----------------------------------------------------------------------------