]> git.stg.codes - stg.git/commitdiff
Simplified module interfaces.
authorMaxim Mamontov <faust.madf@gmail.com>
Tue, 27 May 2014 18:19:56 +0000 (21:19 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Tue, 27 May 2014 18:19:56 +0000 (21:19 +0300)
18 files changed:
projects/sgconf/Makefile
projects/sgconf/action.h
projects/sgconf/admins.cpp
projects/sgconf/admins.h
projects/sgconf/api_action.cpp [new file with mode: 0644]
projects/sgconf/api_action.h [new file with mode: 0644]
projects/sgconf/corps.cpp
projects/sgconf/corps.h
projects/sgconf/main.cpp
projects/sgconf/options.h
projects/sgconf/services.cpp
projects/sgconf/services.h
projects/sgconf/tariffs.cpp
projects/sgconf/tariffs.h
projects/sgconf/users.cpp
projects/sgconf/users.h
projects/sgconf/xml.cpp
projects/sgconf/xml.h

index fd012fc40f1033e325b90ea30f4a6abae3ec4ade..52a70ae8fdc49d569985f02500cc188c2d36c2e0 100644 (file)
@@ -8,6 +8,7 @@ PROG = sgconf
 
 SRCS = ./main.cpp \
        ./options.cpp \
+       ./api_action.cpp \
        ./actions.cpp \
        ./admins.cpp \
        ./tariffs.cpp \
index 0c1b4f92a57aed3e70ad0acf663c22463a25c281..b45ac4703f91867039f77f3dc8e67e67cf8b1555 100644 (file)
@@ -22,6 +22,7 @@
 #define __STG_SGCONF_ACTION_H__
 
 #include <string>
+#include <map>
 #include <stdexcept>
 
 namespace SGCONF
@@ -29,6 +30,7 @@ namespace SGCONF
 
 class OPTION_BLOCK;
 struct PARSER_STATE;
+struct CONFIG;
 
 class ACTION
 {
index da404bf320b073aa382125e6d7372e1966f53abe..e756618ba9fd19b9ab3261c667bef036cbda373f 100644 (file)
@@ -1,5 +1,7 @@
 #include "admins.h"
 
+#include "api_action.h"
+#include "options.h"
 #include "config.h"
 
 #include "stg/servconf.h"
@@ -7,6 +9,8 @@
 #include "stg/os_int.h"
 
 #include <iostream>
+#include <string>
+#include <map>
 #include <cassert>
 
 namespace
@@ -83,12 +87,10 @@ for (size_t i = 0; i < info.size(); ++i)
         PrintAdmin(info[i]);
 }
 
-} // namespace anonymous
-
 
-bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config,
-                               const std::string & /*arg*/,
-                               const std::map<std::string, std::string> & /*options*/)
+bool GetAdminsFunction(const SGCONF::CONFIG & config,
+                       const std::string & /*arg*/,
+                       const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -97,9 +99,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config,
-                              const std::string & arg,
-                              const std::map<std::string, std::string> & /*options*/)
+bool GetAdminFunction(const SGCONF::CONFIG & config,
+                      const std::string & arg,
+                      const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -111,9 +113,9 @@ std::string login(arg);
 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
 }
 
-bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config,
-                              const std::string & arg,
-                              const std::map<std::string, std::string> & /*options*/)
+bool DelAdminFunction(const SGCONF::CONFIG & config,
+                      const std::string & arg,
+                      const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -122,20 +124,32 @@ STG::SERVCONF proto(config.server.data(),
 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config,
-                              const std::string & arg,
-                              const std::map<std::string, std::string> & /*options*/)
+bool AddAdminFunction(const SGCONF::CONFIG & config,
+                      const std::string & arg,
+                      const std::map<std::string, std::string> & /*options*/)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config,
-                              const std::string & arg,
-                              const std::map<std::string, std::string> & options)
+bool ChgAdminFunction(const SGCONF::CONFIG & config,
+                      const std::string & arg,
+                      const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
+
+} // namespace anonymous
+
+void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
+{
+blocks.Add("Admin management options")
+      .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
+      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, GetAdminFunction), "get admin")
+      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, AddAdminFunction), "add admin")
+      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, DelAdminFunction), "del admin")
+      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, ChgAdminFunction), "change admin");
+}
index d3271724b29344772d377ce645850eb5281b320b..6bc47380976c98528afb0850462107e38ff16f64 100644 (file)
@@ -1,33 +1,13 @@
 #ifndef __STG_SGCONF_ADMINS_H__
 #define __STG_SGCONF_ADMINS_H__
 
-#include <string>
-#include <map>
-
 namespace SGCONF
 {
 
-struct CONFIG;
-
-bool GetAdminsFunction(const CONFIG & config,
-                       const std::string & /*arg*/,
-                       const std::map<std::string, std::string> & /*options*/);
-
-bool GetAdminFunction(const CONFIG & config,
-                      const std::string & arg,
-                      const std::map<std::string, std::string> & /*options*/);
-
-bool DelAdminFunction(const CONFIG & config,
-                      const std::string & arg,
-                      const std::map<std::string, std::string> & /*options*/);
-
-bool AddAdminFunction(const CONFIG & config,
-                      const std::string & arg,
-                      const std::map<std::string, std::string> & options);
+class OPTION_BLOCKS;
+class COMMANDS;
 
-bool ChgAdminFunction(const CONFIG & config,
-                      const std::string & arg,
-                      const std::map<std::string, std::string> & options);
+void AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks);
 
 } // namespace SGCONF
 
diff --git a/projects/sgconf/api_action.cpp b/projects/sgconf/api_action.cpp
new file mode 100644 (file)
index 0000000..1ff0e59
--- /dev/null
@@ -0,0 +1,21 @@
+#include "api_action.h"
+
+#include "parser_state.h"
+
+SGCONF::PARSER_STATE SGCONF::API_ACTION::Parse(int argc, char ** argv)
+{
+PARSER_STATE state(false, argc, argv);
+if (!m_argument.empty())
+    {
+    if (argc == 0 ||
+        argv == NULL ||
+        *argv == NULL)
+        throw ERROR("Missing argument.");
+    m_argument = *argv;
+    --state.argc;
+    ++state.argv;
+    }
+m_suboptions.Parse(state.argc, state.argv);
+m_commands.Add(m_funPtr, m_argument, m_params);
+return state;
+}
diff --git a/projects/sgconf/api_action.h b/projects/sgconf/api_action.h
new file mode 100644 (file)
index 0000000..55b3844
--- /dev/null
@@ -0,0 +1,119 @@
+#ifndef __STG_SGCONF_API_ACTION_H__
+#define __STG_SGCONF_API_ACTION_H__
+
+#include "action.h"
+
+#include "options.h"
+
+#include <string>
+#include <map>
+#include <vector>
+
+namespace SGCONF
+{
+
+typedef bool (* API_FUNCTION) (const CONFIG &,
+                               const std::string &,
+                               const std::map<std::string, std::string> &);
+
+class COMMAND
+{
+    public:
+        COMMAND(API_FUNCTION funPtr,
+                const std::string & arg,
+                const std::map<std::string, std::string> & options)
+            : m_funPtr(funPtr),
+              m_arg(arg),
+              m_options(options)
+        {}
+        bool Execute(const SGCONF::CONFIG & config) const
+        {
+            return m_funPtr(config, m_arg, m_options);
+        }
+
+    private:
+        API_FUNCTION m_funPtr;
+        std::string m_arg;
+        std::map<std::string, std::string> m_options;
+};
+
+class COMMANDS
+{
+    public:
+        void Add(API_FUNCTION funPtr,
+                 const std::string & arg,
+                 const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
+        bool Execute(const SGCONF::CONFIG & config) const
+        {
+            std::vector<COMMAND>::const_iterator it(m_commands.begin());
+            bool res = true;
+            while (it != m_commands.end() && res)
+            {
+                res = res && it->Execute(config);
+                ++it;
+            }
+            return res;
+        }
+    private:
+        std::vector<COMMAND> m_commands;
+};
+
+class API_ACTION : public ACTION
+{
+    public:
+        API_ACTION(COMMANDS & commands,
+                   const std::string & paramDescription,
+                   bool needArgument,
+                   const OPTION_BLOCK& suboptions,
+                   API_FUNCTION funPtr)
+            : m_commands(commands),
+              m_description(paramDescription),
+              m_argument(needArgument ? "1" : ""), // Hack
+              m_suboptions(suboptions),
+              m_funPtr(funPtr)
+        {}
+        API_ACTION(COMMANDS & commands,
+                   const std::string & paramDescription,
+                   bool needArgument,
+                   API_FUNCTION funPtr)
+            : m_commands(commands),
+              m_description(paramDescription),
+              m_argument(needArgument ? "1" : ""), // Hack
+              m_funPtr(funPtr)
+        {}
+
+        virtual ACTION * Clone() const { return new API_ACTION(*this); }
+
+        virtual std::string ParamDescription() const { return m_description; }
+        virtual std::string DefaultDescription() const { return ""; }
+        virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
+        virtual PARSER_STATE Parse(int argc, char ** argv);
+
+    private:
+        COMMANDS & m_commands;
+        std::string m_description;
+        std::string m_argument;
+        OPTION_BLOCK m_suboptions;
+        std::map<std::string, std::string> m_params;
+        API_FUNCTION m_funPtr;
+};
+
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+                       const std::string & paramDescription,
+                       bool needArgument,
+                       API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, paramDescription, needArgument, funPtr);
+}
+
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+                       API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, "", false, funPtr);
+}
+
+}
+
+#endif
index 56293efd7e720c6dd3a45375983fba8415ef6b88..b48a7c71352bc769a065945224448ec3109a7ffb 100644 (file)
@@ -1,5 +1,7 @@
 #include "corps.h"
 
+#include "api_action.h"
+#include "options.h"
 #include "config.h"
 
 #include "stg/servconf.h"
@@ -8,6 +10,8 @@
 #include "stg/common.h"
 
 #include <iostream>
+#include <string>
+#include <map>
 
 namespace
 {
@@ -65,11 +69,9 @@ if (!result)
 PrintCorp(info);
 }
 
-} // namespace anonymous
-
-bool SGCONF::GetCorpsFunction(const SGCONF::CONFIG & config,
-                              const std::string & /*arg*/,
-                              const std::map<std::string, std::string> & /*options*/)
+bool GetCorpsFunction(const SGCONF::CONFIG & config,
+                      const std::string & /*arg*/,
+                      const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -78,9 +80,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetCorporations(GetCorpsCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::GetCorpFunction(const SGCONF::CONFIG & config,
-                             const std::string & arg,
-                             const std::map<std::string, std::string> & /*options*/)
+bool GetCorpFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -89,9 +91,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetCorp(arg, GetCorpCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::DelCorpFunction(const SGCONF::CONFIG & config,
-                             const std::string & arg,
-                             const std::map<std::string, std::string> & /*options*/)
+bool DelCorpFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -100,20 +102,32 @@ STG::SERVCONF proto(config.server.data(),
 return proto.DelCorp(arg, SimpleCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::AddCorpFunction(const SGCONF::CONFIG & config,
-                             const std::string & arg,
-                             const std::map<std::string, std::string> & /*options*/)
+bool AddCorpFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & /*options*/)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::ChgCorpFunction(const SGCONF::CONFIG & config,
-                             const std::string & arg,
-                             const std::map<std::string, std::string> & options)
+bool ChgCorpFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
+
+} // namespace anonymous
+
+void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
+{
+blocks.Add("Corporation management options")
+      .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list")
+      .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", true, GetCorpFunction), "get corporation")
+      .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", true, AddCorpFunction), "add corporation")
+      .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", true, DelCorpFunction), "del corporation")
+      .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", true, ChgCorpFunction), "change corporation");
+}
index 58a65161b668426e1f717802362665cfefcdcbd7..de823b7b43c9c6bfa180c2d112cd8c6f24966657 100644 (file)
@@ -1,33 +1,13 @@
 #ifndef __STG_SGCONF_CORPS_H__
 #define __STG_SGCONF_CORPS_H__
 
-#include <string>
-#include <map>
-
 namespace SGCONF
 {
 
-struct CONFIG;
-
-bool GetCorpsFunction(const CONFIG & config,
-                      const std::string & /*arg*/,
-                      const std::map<std::string, std::string> & /*options*/);
-
-bool GetCorpFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & /*options*/);
-
-bool DelCorpFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & /*options*/);
-
-bool AddCorpFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & options);
+class OPTION_BLOCKS;
+class COMMANDS;
 
-bool ChgCorpFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & options);
+void AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks);
 
 } // namespace SGCONF
 
index 6a71f652ac399d8e9255b8bf7807f1d956aaba49..73917852462d27aecbd6c78310ba37c68885baef 100644 (file)
@@ -26,6 +26,7 @@
 #include "services.h"
 #include "corps.h"
 
+#include "api_action.h"
 #include "options.h"
 #include "actions.h"
 #include "config.h"
@@ -173,107 +174,6 @@ class CONFIG_ACTION : public ACTION
         void ParseHostAndPort(const std::string & hostAndPort);
 };
 
-typedef bool (* API_FUNCTION) (const SGCONF::CONFIG &,
-                               const std::string &,
-                               const std::map<std::string, std::string> &);
-
-class COMMAND
-{
-    public:
-        COMMAND(API_FUNCTION funPtr,
-                const std::string & arg,
-                const std::map<std::string, std::string> & options)
-            : m_funPtr(funPtr),
-              m_arg(arg),
-              m_options(options)
-        {}
-        bool Execute(const SGCONF::CONFIG & config) const
-        {
-            return m_funPtr(config, m_arg, m_options);
-        }
-
-    private:
-        API_FUNCTION m_funPtr;
-        std::string m_arg;
-        std::map<std::string, std::string> m_options;
-};
-
-class COMMANDS
-{
-    public:
-        void Add(API_FUNCTION funPtr,
-                 const std::string & arg,
-                 const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
-        bool Execute(const SGCONF::CONFIG & config) const
-        {
-            std::list<COMMAND>::const_iterator it(m_commands.begin());
-            bool res = true;
-            while (it != m_commands.end() && res)
-            {
-                res = res && it->Execute(config);
-                ++it;
-            }
-            return res;
-        }
-    private:
-        std::list<COMMAND> m_commands;
-};
-
-class API_ACTION : public ACTION
-{
-    public:
-        API_ACTION(COMMANDS & commands,
-                   const std::string & paramDescription,
-                   bool needArgument,
-                   const OPTION_BLOCK& suboptions,
-                   API_FUNCTION funPtr)
-            : m_commands(commands),
-              m_description(paramDescription),
-              m_argument(needArgument ? "1" : ""), // Hack
-              m_suboptions(suboptions),
-              m_funPtr(funPtr)
-        {}
-        API_ACTION(COMMANDS & commands,
-                   const std::string & paramDescription,
-                   bool needArgument,
-                   API_FUNCTION funPtr)
-            : m_commands(commands),
-              m_description(paramDescription),
-              m_argument(needArgument ? "1" : ""), // Hack
-              m_funPtr(funPtr)
-        {}
-
-        virtual ACTION * Clone() const { return new API_ACTION(*this); }
-
-        virtual std::string ParamDescription() const { return m_description; }
-        virtual std::string DefaultDescription() const { return ""; }
-        virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
-        virtual PARSER_STATE Parse(int argc, char ** argv)
-        {
-        PARSER_STATE state(false, argc, argv);
-        if (!m_argument.empty())
-            {
-            if (argc == 0 ||
-                argv == NULL ||
-                *argv == NULL)
-                throw ERROR("Missing argument.");
-            m_argument = *argv;
-            --state.argc;
-            ++state.argv;
-            }
-        m_suboptions.Parse(state.argc, state.argv);
-        m_commands.Add(m_funPtr, m_argument, m_params);
-        return state;
-        }
-
-    private:
-        COMMANDS & m_commands;
-        std::string m_description;
-        std::string m_argument;
-        OPTION_BLOCK m_suboptions;
-        std::map<std::string, std::string> m_params;
-        API_FUNCTION m_funPtr;
-};
 
 PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv)
 {
@@ -332,26 +232,8 @@ CONFIG_ACTION * MakeParamAction(SGCONF::CONFIG & config,
 return new CONFIG_ACTION(config, paramDescription);
 }
 
-inline
-ACTION * MakeAPIAction(COMMANDS & commands,
-                       const std::string & paramDescription,
-                       bool needArgument,
-                       API_FUNCTION funPtr)
-{
-return new API_ACTION(commands, paramDescription, needArgument, funPtr);
-}
-
-inline
-ACTION * MakeAPIAction(COMMANDS & commands,
-                       API_FUNCTION funPtr)
-{
-return new API_ACTION(commands, "", false, funPtr);
-}
-
 } // namespace SGCONF
 
-time_t stgTime;
-
 //-----------------------------------------------------------------------------
 int main(int argc, char **argv)
 {
@@ -371,40 +253,12 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
       .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
       .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, "<password>"), "\tpassword for the administrative login")
       .Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
-blocks.Add("Raw XML")
-      .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", true, SGCONF::RawXMLFunction), "\tmake raw XML request");
-blocks.Add("Admin management options")
-      .Add("get-admins", SGCONF::MakeAPIAction(commands, SGCONF::GetAdminsFunction), "\tget admin list")
-      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetAdminFunction), "get admin")
-      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddAdminFunction), "add admin")
-      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelAdminFunction), "del admin")
-      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgAdminFunction), "change admin");
-blocks.Add("Tariff management options")
-      .Add("get-tariffs", SGCONF::MakeAPIAction(commands, SGCONF::GetTariffsFunction), "\tget tariff list")
-      .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::GetTariffFunction), "get tariff")
-      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddTariffFunction), "add tariff")
-      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelTariffFunction), "del tariff")
-      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgTariffFunction), "change tariff");
-blocks.Add("User management options")
-      .Add("get-users", SGCONF::MakeAPIAction(commands, SGCONF::GetUsersFunction), "\tget user list")
-      .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetUserFunction), "get user")
-      .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddUserFunction), "add user")
-      .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelUserFunction), "del user")
-      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgUserFunction), "change user")
-      .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::CheckUserFunction), "check user existance and credentials")
-      .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::SendMessageFunction), "send message");
-blocks.Add("Service management options")
-      .Add("get-services", SGCONF::MakeAPIAction(commands, SGCONF::GetServicesFunction), "\tget service list")
-      .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::GetServiceFunction), "get service")
-      .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddServiceFunction), "add service")
-      .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelServiceFunction), "del service")
-      .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgServiceFunction), "change service");
-blocks.Add("Corporation management options")
-      .Add("get-corps", SGCONF::MakeAPIAction(commands, SGCONF::GetCorpsFunction), "\tget corporation list")
-      .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::GetCorpFunction), "get corporation")
-      .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddCorpFunction), "add corporation")
-      .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelCorpFunction), "del corporation")
-      .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgCorpFunction), "change corporation");
+SGCONF::AppendXMLOptionBlock(commands, blocks);
+SGCONF::AppendAdminsOptionBlock(commands, blocks);
+SGCONF::AppendTariffsOptionBlock(commands, blocks);
+SGCONF::AppendUsersOptionBlock(commands, blocks);
+SGCONF::AppendServicesOptionBlock(commands, blocks);
+SGCONF::AppendCorpsOptionBlock(commands, blocks);
 
 SGCONF::PARSER_STATE state(false, argc, argv);
 
index 6b33ee8c2ba263efaa9bb3d3474586b1feb5fd24..53875091afd57c67a024ddb903355f92b27969f1 100644 (file)
@@ -107,6 +107,7 @@ class OPTION_BLOCKS
     public:
         OPTION_BLOCK & Add(const std::string & description)
         { m_blocks.push_back(OPTION_BLOCK(description)); return m_blocks.back(); }
+        void Add(const OPTION_BLOCK & block) { m_blocks.push_back(block); }
         void Help(size_t level) const;
         PARSER_STATE Parse(int argc, char ** argv);
 
index 3609166da76fcf88814f7828c04631cd2d20fe35..ba490467932403d7fbad7d4efdae4ddb841d4cc0 100644 (file)
@@ -1,5 +1,7 @@
 #include "services.h"
 
+#include "api_action.h"
+#include "options.h"
 #include "config.h"
 
 #include "stg/servconf.h"
@@ -8,6 +10,8 @@
 #include "stg/common.h"
 
 #include <iostream>
+#include <string>
+#include <map>
 
 namespace
 {
@@ -67,11 +71,9 @@ if (!result)
 PrintService(info);
 }
 
-} // namespace anonymous
-
-bool SGCONF::GetServicesFunction(const SGCONF::CONFIG & config,
-                                 const std::string & /*arg*/,
-                                 const std::map<std::string, std::string> & /*options*/)
+bool GetServicesFunction(const SGCONF::CONFIG & config,
+                         const std::string & /*arg*/,
+                         const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -80,9 +82,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::GetServiceFunction(const SGCONF::CONFIG & config,
-                                const std::string & arg,
-                                const std::map<std::string, std::string> & /*options*/)
+bool GetServiceFunction(const SGCONF::CONFIG & config,
+                        const std::string & arg,
+                        const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -91,9 +93,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::DelServiceFunction(const SGCONF::CONFIG & config,
-                                const std::string & arg,
-                                const std::map<std::string, std::string> & /*options*/)
+bool DelServiceFunction(const SGCONF::CONFIG & config,
+                        const std::string & arg,
+                        const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -102,20 +104,32 @@ STG::SERVCONF proto(config.server.data(),
 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::AddServiceFunction(const SGCONF::CONFIG & config,
-                                const std::string & arg,
-                                const std::map<std::string, std::string> & /*options*/)
+bool AddServiceFunction(const SGCONF::CONFIG & config,
+                        const std::string & arg,
+                        const std::map<std::string, std::string> & /*options*/)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::ChgServiceFunction(const SGCONF::CONFIG & config,
-                                const std::string & arg,
-                                const std::map<std::string, std::string> & options)
+bool ChgServiceFunction(const SGCONF::CONFIG & config,
+                        const std::string & arg,
+                        const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
+
+} // namespace anonymous
+
+void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
+{
+blocks.Add("Service management options")
+      .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list")
+      .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", true, GetServiceFunction), "get service")
+      .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", true, AddServiceFunction), "add service")
+      .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", true, DelServiceFunction), "del service")
+      .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", true, ChgServiceFunction), "change service");
+}
index 1b8b0c3c9bf10e11a521323aee4f2419f1c77f1c..c50452735ed483597ee7c692c03158665f671d7b 100644 (file)
@@ -1,33 +1,13 @@
 #ifndef __STG_SGCONF_SERVICES_H__
 #define __STG_SGCONF_SERVICES_H__
 
-#include <string>
-#include <map>
-
 namespace SGCONF
 {
 
-struct CONFIG;
-
-bool GetServicesFunction(const CONFIG & config,
-                         const std::string & /*arg*/,
-                         const std::map<std::string, std::string> & /*options*/);
-
-bool GetServiceFunction(const CONFIG & config,
-                        const std::string & arg,
-                        const std::map<std::string, std::string> & /*options*/);
-
-bool DelServiceFunction(const CONFIG & config,
-                        const std::string & arg,
-                        const std::map<std::string, std::string> & /*options*/);
-
-bool AddServiceFunction(const CONFIG & config,
-                        const std::string & arg,
-                        const std::map<std::string, std::string> & options);
+class OPTION_BLOCKS;
+class COMMANDS;
 
-bool ChgServiceFunction(const CONFIG & config,
-                        const std::string & arg,
-                        const std::map<std::string, std::string> & options);
+void AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks);
 
 } // namespace SGCONF
 
index 04bf68797a1481f3399eaf4c9cc3487d2fc33be4..4d4feb9a9aaeab66152d741cca9b47879d60bffa 100644 (file)
@@ -1,5 +1,7 @@
 #include "tariffs.h"
 
+#include "api_action.h"
+#include "options.h"
 #include "config.h"
 
 #include "stg/servconf.h"
@@ -9,6 +11,8 @@
 
 #include <iostream>
 #include <sstream>
+#include <string>
+#include <map>
 #include <cassert>
 
 namespace
@@ -131,11 +135,9 @@ for (size_t i = 0; i < info.size(); ++i)
         PrintTariff(info[i]);
 }
 
-} // namespace anonymous
-
-bool SGCONF::GetTariffsFunction(const SGCONF::CONFIG & config,
-                                const std::string & /*arg*/,
-                                const std::map<std::string, std::string> & /*options*/)
+bool GetTariffsFunction(const SGCONF::CONFIG & config,
+                        const std::string & /*arg*/,
+                        const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -144,9 +146,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::GetTariffFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & /*options*/)
+bool GetTariffFunction(const SGCONF::CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -158,9 +160,9 @@ std::string name(arg);
 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
 }
 
-bool SGCONF::DelTariffFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & /*options*/)
+bool DelTariffFunction(const SGCONF::CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -169,20 +171,32 @@ STG::SERVCONF proto(config.server.data(),
 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::AddTariffFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & /*options*/)
+bool AddTariffFunction(const SGCONF::CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & /*options*/)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::ChgTariffFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & options)
+bool ChgTariffFunction(const SGCONF::CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
+
+} // namespace anonymous
+
+void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
+{
+blocks.Add("Tariff management options")
+      .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
+      .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, GetTariffFunction), "get tariff")
+      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, AddTariffFunction), "add tariff")
+      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, DelTariffFunction), "del tariff")
+      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, ChgTariffFunction), "change tariff");
+}
index b3cd99e0edf9a215e10a926ee9bd439c750beea8..cde906bf9e3e045c88e7a1f00fa538d703afaa30 100644 (file)
@@ -1,33 +1,13 @@
 #ifndef __STG_SGCONF_TARIFFS_H__
 #define __STG_SGCONF_TARIFFS_H__
 
-#include <string>
-#include <map>
-
 namespace SGCONF
 {
 
-struct CONFIG;
-
-bool GetTariffsFunction(const CONFIG & config,
-                        const std::string & /*arg*/,
-                        const std::map<std::string, std::string> & /*options*/);
-
-bool GetTariffFunction(const CONFIG & config,
-                       const std::string & arg,
-                       const std::map<std::string, std::string> & /*options*/);
-
-bool DelTariffFunction(const CONFIG & config,
-                       const std::string & arg,
-                       const std::map<std::string, std::string> & /*options*/);
-
-bool AddTariffFunction(const CONFIG & config,
-                       const std::string & arg,
-                       const std::map<std::string, std::string> & options);
+class OPTION_BLOCKS;
+class COMMANDS;
 
-bool ChgTariffFunction(const CONFIG & config,
-                       const std::string & arg,
-                       const std::map<std::string, std::string> & options);
+void AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks);
 
 } // namespace SGCONF
 
index 3958869c0eb6ab3ed80ac2c40f3d7d7e86d442e1..251599240ae88a06ef41abd613a128f6d9c4b33c 100644 (file)
@@ -1,5 +1,7 @@
 #include "users.h"
 
+#include "api_action.h"
+#include "options.h"
 #include "config.h"
 
 #include "stg/servconf.h"
@@ -7,6 +9,8 @@
 #include "stg/common.h"
 
 #include <iostream>
+#include <string>
+#include <map>
 
 namespace
 {
@@ -105,11 +109,9 @@ if (!result)
 PrintUser(info);
 }
 
-} // namespace anonymous
-
-bool SGCONF::GetUsersFunction(const SGCONF::CONFIG & config,
-                                const std::string & /*arg*/,
-                                const std::map<std::string, std::string> & /*options*/)
+bool GetUsersFunction(const SGCONF::CONFIG & config,
+                      const std::string & /*arg*/,
+                      const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -118,9 +120,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::GetUserFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & /*options*/)
+bool GetUserFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -129,9 +131,9 @@ STG::SERVCONF proto(config.server.data(),
 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::DelUserFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & /*options*/)
+bool DelUserFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & /*options*/)
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
@@ -140,38 +142,52 @@ STG::SERVCONF proto(config.server.data(),
 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
 }
 
-bool SGCONF::AddUserFunction(const SGCONF::CONFIG & config,
-                             const std::string & arg,
-                             const std::map<std::string, std::string> & /*options*/)
+bool AddUserFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & /*options*/)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config,
-                             const std::string & arg,
-                             const std::map<std::string, std::string> & options)
+bool ChgUserFunction(const SGCONF::CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::CheckUserFunction(const SGCONF::CONFIG & config,
-                               const std::string & arg,
-                               const std::map<std::string, std::string> & options)
+bool CheckUserFunction(const SGCONF::CONFIG & config,
+                       const std::string & arg,
+                       const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
 
-bool SGCONF::SendMessageFunction(const SGCONF::CONFIG & config,
-                                 const std::string & arg,
-                                 const std::map<std::string, std::string> & options)
+bool SendMessageFunction(const SGCONF::CONFIG & config,
+                         const std::string & arg,
+                         const std::map<std::string, std::string> & options)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
 return false;
 }
+
+} // namespace anonymous
+
+void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
+{
+blocks.Add("User management options")
+      .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list")
+      .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", true, GetUserFunction), "get user")
+      .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", true, AddUserFunction), "add user")
+      .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", true, DelUserFunction), "del user")
+      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", true, ChgUserFunction), "change user")
+      .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", true, CheckUserFunction), "check user existance and credentials")
+      .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", true, SendMessageFunction), "send message");
+}
index a7a30f079d1ccfe46e7ac15c9e8711fc3f0b9652..a757fe10fb2be2b1d730f7e9cf671ecf881584b9 100644 (file)
@@ -1,41 +1,13 @@
 #ifndef __STG_SGCONF_USERS_H__
 #define __STG_SGCONF_USERS_H__
 
-#include <string>
-#include <map>
-
 namespace SGCONF
 {
 
-struct CONFIG;
-
-bool GetUsersFunction(const CONFIG & config,
-                      const std::string & /*arg*/,
-                      const std::map<std::string, std::string> & /*options*/);
-
-bool GetUserFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & /*options*/);
-
-bool DelUserFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & /*options*/);
-
-bool AddUserFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & options);
-
-bool ChgUserFunction(const CONFIG & config,
-                     const std::string & arg,
-                     const std::map<std::string, std::string> & options);
-
-bool CheckUserFunction(const CONFIG & config,
-                       const std::string & arg,
-                       const std::map<std::string, std::string> & options);
+class OPTION_BLOCKS;
+class COMMANDS;
 
-bool SendMessageFunction(const CONFIG & config,
-                         const std::string & arg,
-                         const std::map<std::string, std::string> & options);
+void AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks);
 
 }
 
index ec8b9178a06305891cdca5c3ad84fa9503500d32..5a7d2afdaa513fab3b3e9df906fd3dab6d6bb697 100644 (file)
@@ -1,10 +1,14 @@
 #include "xml.h"
 
+#include "api_action.h"
+#include "options.h"
 #include "config.h"
 
 #include "stg/servconf.h"
 
 #include <iostream>
+#include <string>
+#include <map>
 
 #include <expat.h>
 
@@ -52,19 +56,7 @@ if (el != NULL)
     std::cout << Indent(state->level) << "</" << el << ">\n";
 }
 
-void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/)
-{
-if (!result)
-    {
-    std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl;
-    return;
-    }
-SGCONF::PrintXML(response);
-}
-
-}
-
-void SGCONF::PrintXML(const std::string& xml)
+void PrintXML(const std::string& xml)
 {
 ParserState state = { 0 };
 
@@ -81,13 +73,31 @@ if (XML_Parse(parser, xml.c_str(), xml.length(), true) == XML_STATUS_ERROR)
 XML_ParserFree(parser);
 }
 
-bool SGCONF::RawXMLFunction(const SGCONF::CONFIG & config,
-                            const std::string & arg,
-                            const std::map<std::string, std::string> & /*options*/)
+void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+PrintXML(response);
+}
+
+bool RawXMLFunction(const SGCONF::CONFIG & config,
+                    const std::string & arg,
+                    const std::map<std::string, std::string> & /*options*/)
+{
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.userName.data(),
+                    config.userPass.data());
+return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok;
+}
+
+}
+
+void SGCONF::AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
 {
-    STG::SERVCONF proto(config.server.data(),
-                        config.port.data(),
-                        config.userName.data(),
-                        config.userPass.data());
-    return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok;
+blocks.Add("Raw XML")
+      .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", true, RawXMLFunction), "\tmake raw XML request");
 }
index 549a69b377faee922a8dec5422dd558a91544f2b..453d5eb60bfadf4aa03ccf7c666691c88227914e 100644 (file)
 #ifndef __STG_SGCONF_XML_H__
 #define __STG_SGCONF_XML_H__
 
-#include <string>
-#include <map>
-
 namespace SGCONF
 {
 
-struct CONFIG;
-
-void PrintXML(const std::string& xml);
+class OPTION_BLOCKS;
+class COMMANDS;
 
-bool RawXMLFunction(const SGCONF::CONFIG & config,
-                    const std::string & arg,
-                    const std::map<std::string, std::string> & /*options*/);
+void AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks);
 
 }