]> git.stg.codes - stg.git/blobdiff - projects/sgauthstress/main.cpp
TEST_SETTINGS_LOCAL moved outside the namespace, more assertions added
[stg.git] / projects / sgauthstress / main.cpp
index 96c58203ec78c5d5463cae2a52eada20232ef723..2388254dab91257ae235d096dff7f2922717f547 100644 (file)
  */
 
 #include <unistd.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include <csignal>
-#include <cstdio>
-#include <cstring>
 #include <iostream>
 #include <vector>
+#include <list>
 
-#include "stg/ia.h"
 #include "stg/common.h"
-#include "web.h"
-#include "settings_impl.h"
+#include "stg/store.h"
+#include "stg/user_conf.h"
 
-int mes;
-char infoText[256];
-char messageText[256];
-
-const int winKOI = 0;
-
-IA_CLIENT_PROT * clnp;
-WEB * web = NULL;
-
-using namespace std;
+#include "settings.h"
+#include "store_loader.h"
+#include "proto.h"
+#include "user.h"
 
 time_t stgTime;
+bool running;
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void Usage()
 {
-printf("sgauth <path_to_config>\n");
-}
-//-----------------------------------------------------------------------------
-void SetDirName(const vector<string> & dn, void *)
-{
-for (int j = 0; j < DIR_NUM; j++)
-    {
-    if (winKOI)
-        {
-        string dir;
-        KOIToWin(dn[j], &dir);
-        if (web)
-            web->SetDirName(dir, j);
-        }
-    else
-        {
-        if (web)
-            web->SetDirName(dn[j], j);
-        }
-    }
-}
-//-----------------------------------------------------------------------------
-void StatUpdate(const LOADSTAT & ls, void *)
-{
-if (web)
-    web->UpdateStat(ls);
-}
-//-----------------------------------------------------------------------------
-void StatusChanged(int, void *)
-{
-}
-//-----------------------------------------------------------------------------
-void ShowMessage(const string & message, int i, int, int, void *)
-{
-if (web)
-    web->AddMessage(message, i);
-}
-//-----------------------------------------------------------------------------
-void ShowError(const string & message, int, void *)
-{
-if (web)
-     web->AddMessage(message, 0);
-}
-//-----------------------------------------------------------------------------
-void CatchUSR1(int)
-{
-if (clnp->GetAuthorized())
-    {
-    cout << "Connect" << endl;
-    clnp->Connect();
-    }
-}
-//-----------------------------------------------------------------------------
-void CatchUSR2(int)
-{
-cout << "Disconnect" << endl;
-clnp->Disconnect();
+std::cout << "Usage:\n\nsgauth <path_to_config>" << std::endl;
 }
 //-----------------------------------------------------------------------------
 void CatchTERM(int)
 {
-cout << "Terminated" << endl;
-clnp->Disconnect();
-sleep(2);
-exit(0);
+running = false;
 }
 //-----------------------------------------------------------------------------
 static void SetSignalHandlers()
@@ -144,135 +75,94 @@ newsa.sa_mask = sigmask;
 newsa.sa_flags = 0;
 sigaction(SIGINT, &newsa, &oldsa);
 
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGUSR1);
-newsa.sa_handler = CatchUSR1;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGUSR1, &newsa, &oldsa);
-
-sigemptyset(&sigmask);
-sigaddset(&sigmask, SIGUSR2);
-newsa.sa_handler = CatchUSR2;
-newsa.sa_mask = sigmask;
-newsa.sa_flags = 0;
-sigaction(SIGUSR2, &newsa, &oldsa);
-
 return;
 }
 //-----------------------------------------------------------------------------
 int main(int argc, char *argv[])
 {
-SETTINGS_IMPL settings;
+SETTINGS settings;
 
 if (argc == 2)
     {
     settings.SetConfFile(argv[1]);
     }
-else
-    {
-    // Usage
-    }
 
 if (settings.ReadSettings())
     {
-    printf("ReadSettingsError\n");
-    printf("%s\n", settings.GetStrError().c_str());
-    exit(-1);
+    std::cerr << "Failed to read settings: '"
+              << settings.GetStrError() << "'" << std::endl;
+    Usage();
+    return -1;
     }
-settings.Print();
 
-if (settings.GetDaemon())
-    {
-    switch (fork())
-        {
-        case -1:
-            exit(1);
-            break;
+SetSignalHandlers();
 
-        case 0:
-            setsid();
-            break;
+PROTO proto(settings.GetServerName(),
+            settings.GetServerPort(),
+            settings.GetLocalPort(),
+            10);
 
-        default:
-            exit(0);
-            break;
-        }
+STORE_LOADER storeLoader(settings.GetModulesPath(), settings.GetStoreModuleSettings());
+if (storeLoader.Load())
+    {
+    std::cerr << "Failed to load storage plugin: '" << storeLoader.GetStrError() << "'" << std::endl;
+    return -1;
     }
 
-clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
+STORE * dataStore = storeLoader.GetStore();
 
-if (!settings.GetNoWeb())
+std::vector<std::string> userList;
+if (dataStore->GetUsersList(&userList))
     {
-    web = new WEB();
-    web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
-    web->SetListenAddr(settings.GetListenWebIP());
-    web->Start();
+    std::cerr << "Failed to get user list: '" << dataStore->GetStrError() << "'" << std::endl;
+    return -1;
     }
 
-clnp->SetLogin(settings.GetLogin());
-clnp->SetPassword(settings.GetPassword());
-
-clnp->SetStatusChangedCb(StatusChanged, NULL);
-clnp->SetInfoCb(ShowMessage, NULL);
-clnp->SetErrorCb(ShowError, NULL);
-clnp->SetDirNameCb(SetDirName, NULL);
-clnp->SetStatChangedCb(StatUpdate, NULL);
-clnp->SetReconnect(settings.GetReconnect());
-
-clnp->Start();
-
-SetSignalHandlers();
+std::list<uint32_t> ips;
+    {
+    std::vector<std::string>::const_iterator it;
+    for (it = userList.begin(); it != userList.end(); ++it)
+        {
+        USER_CONF userConf;
+        if (dataStore->RestoreUserConf(&userConf, *it))
+            {
+            std::cerr << "Failed to read user conf: '" << dataStore->GetStrError() << "'" << std::endl;
+            return -1;
+            }
+        proto.AddUser(
+                USER(
+                    *it,
+                    userConf.password,
+                    userConf.ips[0].ip
+                )
+        );
+        ips.push_back(userConf.ips[0].ip);
+        }
+    }
 
-#ifdef LINUX
-for (int i = 1; i < argc; i++)
-    memset(argv[i], 0, strlen(argv[i]));
+if (!proto.Start())
+    {
+    std::cerr << "Failed to start listening thread: '" << proto.GetStrError() << "'" << std::endl;
+    return -1;
+    }
 
-if(argc > 1)
-    strcpy(argv[1], "Connecting...");
-#endif
+std::list<uint32_t>::const_iterator it;
+for (it = ips.begin(); it != ips.end(); ++it)
+    {
+    proto.Connect(*it);
+    }
 
-#ifdef FREEBSD
-setproctitle("Connecting...");
-#endif
-clnp->Connect();
+std::cout << "Successfully loaded " << proto.UserCount() << " users" << std::endl;
 
-while (1)
+running = true;
+while (running)
     {
     usleep(200000);
+    }
 
-    char state[20];
-
-    if (clnp->GetAuthorized())
-        {
-        if (settings.GetShowPid())
-            sprintf(state, "On %d", getpid());
-        else
-            strcpy(state, "Online");
-        }
-    else
-        {
-        if (settings.GetShowPid())
-            sprintf(state, "Off %d", getpid());
-        else
-            strcpy(state, "Offline");
-        }
-
-    #ifdef LINUX
-    for (int i = 1; i < argc; i++)
-        memset(argv[i], 0, strlen(argv[i]));
-    if(argc > 1)
-        strcpy(argv[1], state);
-    #endif
-
-    #ifdef FREEBSD
-    setproctitle(state);
-    #endif
+proto.Stop();
 
-    #ifdef FREEBSD_5
-    setproctitle(state);
-    #endif
-    }
+storeLoader.Unload();
 
 return 0;
 }