]> git.stg.codes - stg.git/commitdiff
Implemented services parser.
authorMaxim Mamontov <faust.madf@gmail.com>
Sun, 28 Sep 2014 15:35:56 +0000 (18:35 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Sun, 28 Sep 2014 15:35:56 +0000 (18:35 +0300)
include/stg/service_conf.h
include/stg/services.h
projects/stargazer/plugins/configuration/sgconfig/Makefile
projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_services.h [new file with mode: 0644]
projects/stargazer/services_impl.cpp
projects/stargazer/services_impl.h

index 473830e44393636b301efdb16cc97a1a926acf99..84f052a557b55f0befe40b09b24cb50547135b82 100644 (file)
@@ -58,6 +58,11 @@ SERVICE_CONF_RES()
       cost(), payDay()
 {}
 
+SERVICE_CONF_RES(const SERVICE_CONF & rhs)
+    : name(rhs.name), comment(rhs.comment),
+      cost(rhs.cost), payDay(rhs.payDay)
+{}
+
 SERVICE_CONF_RES & operator=(const SERVICE_CONF & conf)
 {
 name = conf.name;
@@ -77,6 +82,14 @@ sc.payDay = payDay.data();
 return sc;
 }
 
+void Splice(const SERVICE_CONF_RES & rhs)
+{
+name.splice(rhs.name);
+comment.splice(rhs.comment);
+cost.splice(rhs.cost);
+payDay.splice(rhs.payDay);
+}
+
 RESETABLE<std::string> name;
 RESETABLE<std::string> comment;
 RESETABLE<double>      cost;
index 8c49f7813b913cfe855526fadefc5e767f851596..1fe4dca1910ba71a22118f7db3363ea8d375919c 100644 (file)
@@ -33,7 +33,7 @@ public:
     virtual int Add(const SERVICE_CONF & service, const ADMIN * admin) = 0;
     virtual int Del(const std::string & name, const ADMIN * admin) = 0;
     virtual int Change(const SERVICE_CONF & service, const ADMIN * admin) = 0;
-    virtual bool Find(const std::string & name, SERVICE_CONF * service) = 0;
+    virtual bool Find(const std::string & name, SERVICE_CONF * service) const = 0;
     virtual bool Exists(const std::string & name) const = 0;
     virtual const std::string & GetStrError() const = 0;
     virtual size_t Count() const = 0;
index 40a5c534c9e933a5b44eb1c11628eff82642e011..a83bb86c7a2d85a4465e2c6fe302e306eb45d96f 100644 (file)
@@ -13,6 +13,7 @@ SRCS = ./stgconfig.cpp \
        ./parser_tariffs.cpp \
        ./parser_admins.cpp \
        ./parser_users.cpp \
+       ./parser_services.cpp \
        ./parser_message.cpp \
        ./parser_auth_by.cpp \
        ./parser_user_info.cpp \
diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_services.cpp
new file mode 100644 (file)
index 0000000..59e7f03
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ *    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#include "parser_services.h"
+
+#include "stg/services.h"
+
+#include <strings.h> // strcasecmp
+
+using STG::PARSER::GET_SERVICES;
+using STG::PARSER::GET_SERVICE;
+using STG::PARSER::ADD_SERVICE;
+using STG::PARSER::DEL_SERVICE;
+using STG::PARSER::CHG_SERVICE;
+
+const char * GET_SERVICES::tag = "GetServices";
+const char * GET_SERVICE::tag  = "AddService";
+const char * ADD_SERVICE::tag  = "AddService";
+const char * DEL_SERVICE::tag  = "DelService";
+const char * CHG_SERVICE::tag  = "ChgService";
+
+void GET_SERVICES::CreateAnswer()
+{
+    // TODO: no priviledges implemented yet
+    /*const PRIV * priv = m_currAdmin.GetPriv();
+    if (!priv->serviceChg)
+    {
+        m_answer = "<Error Result=\"Error. Access denied.\"/>";
+        return;
+    }*/
+
+    m_answer = "<Services>";
+    SERVICE_CONF conf;
+    int h = m_services.OpenSearch();
+    while (m_services.SearchNext(h, &conf) == 0)
+    {
+        m_answer += "<Service name=\"" + conf.name +
+                    "\" comment=\"" + Encode12str(conf.comment) +
+                    "\" cost=\"" + x2str(conf.cost) +
+                    "\" payDay=\"" + x2str(conf.payDay) + "\"/>";
+    }
+    m_services.CloseSearch(h);
+    m_answer += "</Services>";
+}
+
+int GET_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+    if (strcasecmp(el, m_tag.c_str()) == 0)
+    {
+        m_name = attr[1];
+        return 0;
+    }
+    return -1;
+}
+
+void GET_SERVICE::CreateAnswer()
+{
+    // TODO: no priviledges implemented yet
+    /*const PRIV * priv = m_currAdmin.GetPriv();
+    if (!priv->serviceChg)
+    {
+        m_answer = "<Error Result=\"Error. Access denied.\"/>";
+        return;
+    }*/
+
+    SERVICE_CONF conf;
+    if (!m_services.Find(m_name, &conf))
+        m_answer = "<Error result=\"Service '" + m_name + "' does not exist.\"/>";
+    else
+        m_answer += "<" + m_tag + " name=\"" + conf.name +
+                    "\" comment=\"" + Encode12str(conf.comment) +
+                    "\" cost=\"" + x2str(conf.cost) +
+                    "\" payDay=\"" + x2str(conf.payDay) + "\"/>";
+}
+
+int ADD_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+    if (strcasecmp(el, m_tag.c_str()) == 0)
+    {
+        m_name = attr[1];
+        return 0;
+    }
+    return -1;
+}
+
+void ADD_SERVICE::CreateAnswer()
+{
+    SERVICE_CONF conf(m_name);
+    if (m_services.Add(conf, &m_currAdmin) == 0)
+        m_answer = "<" + m_tag + " result=\"Ok\"/>";
+    else
+        m_answer = "<" + m_tag + " result=\"Error. " + m_services.GetStrError() + "\"/>";
+}
+
+int DEL_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+    if (strcasecmp(el, m_tag.c_str()) == 0)
+    {
+        m_name = attr[1];
+        return 0;
+    }
+    return -1;
+}
+
+void DEL_SERVICE::CreateAnswer()
+{
+    if (m_services.Del(m_name, &m_currAdmin) == 0)
+        m_answer = "<" + m_tag + " result=\"Ok\"/>";
+    else
+        m_answer = "<" + m_tag + " result=\"Error. " + m_services.GetStrError() + "\"/>";
+}
+
+int CHG_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+    if (strcasecmp(el, m_tag.c_str()) == 0)
+    {
+        for (size_t i = 0; i < 8; i += 2)
+        {
+            printfd(__FILE__, "PARSER_CHG_SERVICE::attr[%d] = %s\n", i, attr[i]);
+            if (attr[i] == NULL)
+                break;
+
+            if (strcasecmp(attr[i], "name") == 0)
+            {
+                m_service.name = attr[i + 1];
+                continue;
+            }
+
+            if (strcasecmp(attr[i], "comment") == 0)
+            {
+                m_service.comment = Decode21str(attr[i + 1]);
+                continue;
+            }
+
+            if (strcasecmp(attr[i], "cost") == 0)
+            {
+                double cost = 0;
+                if (str2x(attr[i + 1], cost) == 0)
+                    m_service.cost = cost;
+                // TODO: log it
+                continue;
+            }
+
+            if (strcasecmp(attr[i], "payDay") == 0)
+            {
+                unsigned payDay;
+                if (str2x(attr[i + 1], payDay) == 0)
+                    m_service.payDay = payDay;
+                // TODO: log it
+                continue;
+            }
+        }
+
+        return 0;
+    }
+    return -1;
+}
+
+void CHG_SERVICE::CreateAnswer()
+{
+    if (m_service.name.empty())
+    {
+        m_answer = "<" + m_tag + " result=\"Empty service name.\"/>";
+        return;
+    }
+
+    if (!m_services.Exists(m_service.name.const_data()))
+    {
+        m_answer = "<" + m_tag + " result = \"Service '" + m_service.name.const_data() + "' does not exist.\"/>";
+        return;
+    }
+
+    SERVICE_CONF orig;
+    m_services.Find(m_service.name.const_data(), &orig);
+
+    m_service.Splice(orig);
+
+    if (m_services.Change(m_service.GetData(), &m_currAdmin) != 0)
+        m_answer = "<" + m_tag + " result = \"" + m_services.GetStrError() + "\"/>";
+    else
+        m_answer = "<" + m_tag + " result = \"Ok\"/>";
+}
diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_services.h b/projects/stargazer/plugins/configuration/sgconfig/parser_services.h
new file mode 100644 (file)
index 0000000..f4d87fe
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ *    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONFIG_PARSER_SERVICES_H__
+#define __STG_SGCONFIG_PARSER_SERVICES_H__
+
+#include "parser.h"
+
+#include "stg/service_conf.h"
+
+#include "stg/common.h"
+
+#include <string>
+
+class SERVICES;
+
+namespace STG
+{
+namespace PARSER
+{
+
+class GET_SERVICES: public BASE_PARSER
+{
+    public:
+        class FACTORY : public BASE_PARSER::FACTORY
+        {
+            public:
+                FACTORY(const SERVICES & services) : m_services(services) {}
+                virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVICES(admin, m_services); }
+                static void Register(REGISTRY & registry, const SERVICES & services)
+                { registry[ToLower(tag)] = new FACTORY(services); }
+            private:
+                const SERVICES & m_services;
+        };
+
+        static const char * tag;
+
+        GET_SERVICES(const ADMIN & admin, const SERVICES & services)
+            : BASE_PARSER(admin, tag), m_services(services) {}
+
+    private:
+        const SERVICES & m_services;
+
+        void CreateAnswer();
+};
+
+class GET_SERVICE: public BASE_PARSER
+{
+    public:
+        class FACTORY : public BASE_PARSER::FACTORY
+        {
+            public:
+                FACTORY(const SERVICES & services) : m_services(services) {}
+                virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVICE(admin, m_services); }
+                static void Register(REGISTRY & registry, SERVICES & services)
+                { registry[ToLower(tag)] = new FACTORY(services); }
+            private:
+                const SERVICES & m_services;
+        };
+
+        static const char * tag;
+
+        GET_SERVICE(const ADMIN & admin, const SERVICES & services)
+            : BASE_PARSER(admin, tag), m_services(services) {}
+        int Start(void * data, const char * el, const char ** attr);
+
+    private:
+        std::string m_name;
+        const SERVICES & m_services;
+
+        void CreateAnswer();
+};
+
+class ADD_SERVICE: public BASE_PARSER
+{
+    public:
+        class FACTORY : public BASE_PARSER::FACTORY
+        {
+            public:
+                FACTORY(SERVICES & services) : m_services(services) {}
+                virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_SERVICE(admin, m_services); }
+                static void Register(REGISTRY & registry, SERVICES & services)
+                { registry[ToLower(tag)] = new FACTORY(services); }
+            private:
+                SERVICES & m_services;
+        };
+
+        static const char * tag;
+
+        ADD_SERVICE(const ADMIN & admin, SERVICES & services)
+            : BASE_PARSER(admin, tag), m_services(services) {}
+        int Start(void * data, const char * el, const char ** attr);
+
+    private:
+        std::string m_name;
+        SERVICES & m_services;
+
+        void CreateAnswer();
+};
+
+class DEL_SERVICE: public BASE_PARSER
+{
+    public:
+        class FACTORY : public BASE_PARSER::FACTORY
+        {
+            public:
+                FACTORY(SERVICES & services) : m_services(services) {}
+                virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_SERVICE(admin, m_services); }
+                static void Register(REGISTRY & registry, SERVICES & services)
+                { registry[ToLower(tag)] = new FACTORY(services); }
+            private:
+                SERVICES & m_services;
+        };
+
+        static const char * tag;
+
+        DEL_SERVICE(const ADMIN & admin, SERVICES & services)
+            : BASE_PARSER(admin, tag), m_services(services) {}
+        int Start(void * data, const char * el, const char ** attr);
+
+    private:
+        std::string m_name;
+        SERVICES & m_services;
+
+        void CreateAnswer();
+};
+
+class CHG_SERVICE: public BASE_PARSER
+{
+    public:
+        class FACTORY : public BASE_PARSER::FACTORY
+        {
+            public:
+                FACTORY(SERVICES & services) : m_services(services) {}
+                virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_SERVICE(admin, m_services); }
+                static void Register(REGISTRY & registry, SERVICES & services)
+                { registry[ToLower(tag)] = new FACTORY(services); }
+            private:
+                SERVICES & m_services;
+        };
+
+        static const char * tag;
+
+        CHG_SERVICE(const ADMIN & admin, SERVICES & services)
+            : BASE_PARSER(admin, tag), m_services(services) {}
+        int Start(void * data, const char * el, const char ** attr);
+
+    private:
+        SERVICE_CONF_RES m_service;
+        SERVICES & m_services;
+
+        void CreateAnswer();
+};
+
+} // namespace PARSER
+} // namespace STG
+
+#endif
index aebb98fb84cf0653765b27d645cca8d46dccd968..ea231615f205794f6767071febf3be5763c6ecf2 100644 (file)
@@ -184,7 +184,7 @@ for (size_t i = 0; i < servicesList.size(); i++)
 return false;
 }
 //-----------------------------------------------------------------------------
-bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * service)
+bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * service) const
 {
 assert(service != NULL && "Pointer to service is not null");
 
@@ -192,7 +192,7 @@ STG_LOCKER lock(&mutex);
 if (data.empty())
     return false;
 
-srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
+const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
 
 if (si != data.end())
     {
index 31aa72b2ed612e43e1bb8aeb383233d5bc873811..254df8a100d5a702a414017cbdd9d7e7672da7d1 100644 (file)
@@ -44,7 +44,7 @@ public:
     int Add(const SERVICE_CONF & service, const ADMIN * admin);
     int Del(const std::string & name, const ADMIN * admin);
     int Change(const SERVICE_CONF & service, const ADMIN * admin);
-    bool Find(const std::string & name, SERVICE_CONF * service);
+    bool Find(const std::string & name, SERVICE_CONF * service) const;
     bool Exists(const std::string & name) const;
     const std::string & GetStrError() const { return strError; }