]> git.stg.codes - stg.git/commitdiff
Implemented some service and corporation management functions.
authorMaxim Mamontov <faust.madf@gmail.com>
Tue, 27 May 2014 14:55:56 +0000 (17:55 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Tue, 27 May 2014 14:55:56 +0000 (17:55 +0300)
projects/sgconf/Makefile
projects/sgconf/corps.cpp [new file with mode: 0644]
projects/sgconf/corps.h [new file with mode: 0644]
projects/sgconf/main.cpp
projects/sgconf/services.cpp [new file with mode: 0644]
projects/sgconf/services.h [new file with mode: 0644]
projects/sgconf/users.cpp
projects/sgconf/users.h

index 8117bea60fe7fd2ae3f4e0ae3fbb125ad822ea72..fd012fc40f1033e325b90ea30f4a6abae3ec4ade 100644 (file)
@@ -12,6 +12,8 @@ SRCS = ./main.cpp \
        ./admins.cpp \
        ./tariffs.cpp \
        ./users.cpp \
+       ./services.cpp \
+       ./corps.cpp \
        ./xml.cpp
 
 STGLIBS = srvconf \
@@ -61,11 +63,7 @@ $(PROG): $(OBJS)
        $(CXX) $^ $(LDFLAGS) $(LIBS) -o $(PROG)
 
 clean:
-       rm -f deps $(PROG) *.o tags *.*~ .OS
-       rm -f .OS
-       rm -f .store
-       rm -f .db.sql
-       rm -f core*
+       rm -f deps $(PROG) *.o
        $(MAKE) -C $(DIR_LIBSRC) clean
 
 distclean: clean
diff --git a/projects/sgconf/corps.cpp b/projects/sgconf/corps.cpp
new file mode 100644 (file)
index 0000000..56293ef
--- /dev/null
@@ -0,0 +1,119 @@
+#include "corps.h"
+
+#include "config.h"
+
+#include "stg/servconf.h"
+#include "stg/servconf_types.h"
+#include "stg/corp_conf.h"
+#include "stg/common.h"
+
+#include <iostream>
+
+namespace
+{
+
+std::string Indent(size_t level, bool dash = false)
+{
+if (level == 0)
+    return "";
+return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
+}
+
+void PrintCorp(const STG::GET_CORP::INFO & info, size_t level = 0)
+{
+std::cout << Indent(level, true) << "name: " << info.name << "\n"
+          << Indent(level)       << "cash: " << info.cash << "\n";
+}
+
+void SimpleCallback(bool result,
+                    const std::string & reason,
+                    void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+std::cout << "Success.\n";
+}
+
+void GetCorpsCallback(bool result,
+                      const std::string & reason,
+                      const std::vector<STG::GET_CORP::INFO> & info,
+                      void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Failed to get corp list. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+std::cout << "Corps:\n";
+for (size_t i = 0; i < info.size(); ++i)
+    PrintCorp(info[i], 1);
+}
+
+void GetCorpCallback(bool result,
+                     const std::string & reason,
+                     const STG::GET_CORP::INFO & info,
+                     void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Failed to get corp. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+PrintCorp(info);
+}
+
+} // namespace anonymous
+
+bool SGCONF::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(),
+                    config.userName.data(),
+                    config.userPass.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*/)
+{
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.userName.data(),
+                    config.userPass.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*/)
+{
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.userName.data(),
+                    config.userPass.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*/)
+{
+// 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)
+{
+// TODO
+std::cerr << "Unimplemented.\n";
+return false;
+}
diff --git a/projects/sgconf/corps.h b/projects/sgconf/corps.h
new file mode 100644 (file)
index 0000000..58a6516
--- /dev/null
@@ -0,0 +1,34 @@
+#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);
+
+bool ChgCorpFunction(const CONFIG & config,
+                     const std::string & arg,
+                     const std::map<std::string, std::string> & options);
+
+} // namespace SGCONF
+
+#endif
index 135cd3c02656f826a8122ae69566ed1dd805da48..08199cfcc5b74b1a2835c029f907e181d1d58c1e 100644 (file)
@@ -23,6 +23,9 @@
 #include "admins.h"
 #include "tariffs.h"
 #include "users.h"
+#include "services.h"
+#include "corps.h"
+
 #include "options.h"
 #include "actions.h"
 #include "config.h"
@@ -433,10 +436,24 @@ blocks.Add("Tariff management options")
       .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgTariffFunction), "\tchange tariff");
 blocks.Add("User management options")
       .Add("get-users", SGCONF::MakeAPIAction(commands, SGCONF::GetUsersFunction), "\tget user list")
-      .Add("get-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::GetUserFunction), "\tget user")
-      .Add("add-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddUserFunction), "\tadd user")
-      .Add("del-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelUserFunction), "\tdel user")
-      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgUserFunction), "\tchange user");
+      .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetUserFunction), "\tget user")
+      .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddUserFunction), "\tadd user")
+      .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelUserFunction), "\tdel user")
+      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgUserFunction), "\tchange user")
+      .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::CheckUserFunction), "\tcheck user existance and credentials")
+      .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::SendMessageFunction), "\tsend 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), "\tget service")
+      .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddServiceFunction), "\tadd service")
+      .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelServiceFunction), "\tdel service")
+      .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgServiceFunction), "\tchange 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), "\tget corporation")
+      .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::AddCorpFunction), "\tadd corporation")
+      .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::DelCorpFunction), "\tdel corporation")
+      .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", true, SGCONF::ChgCorpFunction), "\tchange corporation");
 
 SGCONF::PARSER_STATE state(false, argc, argv);
 
diff --git a/projects/sgconf/services.cpp b/projects/sgconf/services.cpp
new file mode 100644 (file)
index 0000000..3609166
--- /dev/null
@@ -0,0 +1,121 @@
+#include "services.h"
+
+#include "config.h"
+
+#include "stg/servconf.h"
+#include "stg/servconf_types.h"
+#include "stg/service_conf.h"
+#include "stg/common.h"
+
+#include <iostream>
+
+namespace
+{
+
+std::string Indent(size_t level, bool dash = false)
+{
+if (level == 0)
+    return "";
+return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
+}
+
+void PrintService(const STG::GET_SERVICE::INFO & info, size_t level = 0)
+{
+std::cout << Indent(level, true) << "name: " << info.name << "\n"
+          << Indent(level)       << "cost: " << info.cost << "\n"
+          << Indent(level)       << "payment day: " << info.payDay << "\n"
+          << Indent(level)       << "comment: " << info.comment << "\n";
+}
+
+void SimpleCallback(bool result,
+                    const std::string & reason,
+                    void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+std::cout << "Success.\n";
+}
+
+void GetServicesCallback(bool result,
+                         const std::string & reason,
+                         const std::vector<STG::GET_SERVICE::INFO> & info,
+                         void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+std::cout << "Services:\n";
+for (size_t i = 0; i < info.size(); ++i)
+    PrintService(info[i], 1);
+}
+
+void GetServiceCallback(bool result,
+                        const std::string & reason,
+                        const STG::GET_SERVICE::INFO & info,
+                        void * /*data*/)
+{
+if (!result)
+    {
+    std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
+    return;
+    }
+PrintService(info);
+}
+
+} // namespace anonymous
+
+bool SGCONF::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(),
+                    config.userName.data(),
+                    config.userPass.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*/)
+{
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.userName.data(),
+                    config.userPass.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*/)
+{
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.userName.data(),
+                    config.userPass.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*/)
+{
+// 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)
+{
+// TODO
+std::cerr << "Unimplemented.\n";
+return false;
+}
diff --git a/projects/sgconf/services.h b/projects/sgconf/services.h
new file mode 100644 (file)
index 0000000..1b8b0c3
--- /dev/null
@@ -0,0 +1,34 @@
+#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);
+
+bool ChgServiceFunction(const CONFIG & config,
+                        const std::string & arg,
+                        const std::map<std::string, std::string> & options);
+
+} // namespace SGCONF
+
+#endif
index 37b3db5e10168c708d5902d2f94342f713f75057..3958869c0eb6ab3ed80ac2c40f3d7d7e86d442e1 100644 (file)
@@ -141,8 +141,8 @@ 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*/)
+                             const std::string & arg,
+                             const std::map<std::string, std::string> & /*options*/)
 {
 // TODO
 std::cerr << "Unimplemented.\n";
@@ -150,6 +150,15 @@ return false;
 }
 
 bool SGCONF::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)
 {
@@ -157,3 +166,12 @@ bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config,
 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)
+{
+// TODO
+std::cerr << "Unimplemented.\n";
+return false;
+}
index d6c2dd8b1a306337afda216f67aed9bd9cac5866..a7a30f079d1ccfe46e7ac15c9e8711fc3f0b9652 100644 (file)
@@ -29,6 +29,14 @@ 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);
+
+bool SendMessageFunction(const CONFIG & config,
+                         const std::string & arg,
+                         const std::map<std::string, std::string> & options);
+
 }
 
 #endif