• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Main.cpp

Go to the documentation of this file.
00001 /*
00002 Beesnest - Presonal Application Server (beesnest-server.org)
00003 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00004 This file is part of Beesnest.
00005 
00006 Beesnest is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 Beesnest is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with Beesnest; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 */
00020 
00021 
00022 /* Main.cpp: Main file for the Beesnest Server. */
00023 
00024 #include "DcGlobals.h"
00025 #include "DcParameters.h"
00026 #include "DcConnectionThread.h"
00027 #include "DcLoadController.h"
00028 #include "DcEnginesBank.h"
00029 #include "DcLogger.h"
00030 #include "Interfaces/DcAdapter.h"
00031 #include _GET_INCLUDE_PATH (DcSocket.h)
00032 #include _GET_INCLUDE_PATH (DcSocketServer.h)
00033 #include _GET_INCLUDE_PATH (DcDemon.h)
00034 
00035 
00036 /* Global Variables. */
00037 /** True when the program runs as a regular application. */
00038 bool gbRunAsApp;
00039 /** The Logger object (just one for the entire application). */
00040 CdcLogger oLogger;
00041 
00042 /* Just make the logger available to all. */
00043 void PrintString (CdcString str, int level)
00044 { oLogger.PrintString (str, level, gbRunAsApp); }
00045 
00046 int ProgramMain ()
00047 {
00048     if (gbRunAsApp == false)
00049         if (CdcDemon::StartPending () == false)
00050         {
00051             CdcDemon::Stop (-1);
00052             return -1;
00053         }
00054 
00055     CdcSocketServer* pserver = NULL;
00056     CdcSocket* psock = NULL;
00057     int ret_val = -1;
00058     try
00059     {   /* Initialize. */
00060         InitProgram();
00061         int port = CdcParameters::ToInt (
00062                 CdcParameters::GetParam ("Server->Port"));
00063         pserver = CdcSocketServer::GetSocketServer (port);
00064         PrintString ("Server started", PS_GEN);
00065         if (gbRunAsApp == false)
00066             CdcDemon::MarkRun ();
00067         /* Listen and create thread to each request. */
00068         while (CdcDemon::IsRunning () || gbRunAsApp)
00069         {
00070             psock = pserver->Listen ();
00071             if (CdcLoadController::AddThread (psock) == true)
00072             {
00073                 CdcConnectionThread* pconnection = new
00074                     CdcConnectionThread (psock);
00075             }
00076         }
00077     }
00078     catch (CdcException& e)
00079     {
00080         PrintString (e.ToStr (), PS_GEN);
00081         ret_val = -1;
00082         if (gbRunAsApp == false)
00083             CdcDemon::Stop (-1);
00084     }
00085     PrintString ("Server closed", PS_GEN);
00086     if(pserver != NULL)
00087         delete pserver;
00088     CdcSocketServer::Cleanup ();
00089     CdcCrypto::Destroy ();
00090     CdcEnginesBank::Term ();
00091     return ret_val;
00092 }
00093 
00094 
00095 void InitProgram()
00096 {
00097     CdcHttpTime::Init ();
00098     CdcCrypto::Init ();
00099     bool is_ok = CdcParameters::ReadParams (PARM_FILE_EXT);
00100     int reqlen = CdcParameters::ToInt (CdcParameters::GetParam
00101         ("Server->Max-Req-Leng"));
00102     reqlen = reqlen < 1024 ? 1024 : reqlen;
00103     int reqtime = CdcParameters::ToInt (CdcParameters::GetParam
00104         ("Server->Rec-Timeout"));
00105     CdcSocketServer::Startup (reqlen, reqtime);
00106     CdcString file = CdcFileUtil::GetWorkPath (
00107         CdcParameters::GetParam ("Login-Manager->Users-File"));
00108     CdcLoginManager::Load (file.GetBuffer ());
00109     CdcAdaptersBank::Init ();
00110     CdcLoadController::Init ();
00111     CdcRetrieveFile::Init ();
00112     CdcEnginesBank::Init ();
00113     oLogger.Init ();
00114     if (is_ok == false)
00115         PrintString ("Warning - XML file is not Well-Formed!", PS_GEN);
00116     /* Set high priority for the listening (main) thread. */
00117     SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);
00118 }
00119 
00120 
00121 void ControlUsers (int argc, char* argv[])
00122 {
00123     int is_ok = false;
00124     if (argc < 5)
00125     {
00126         cout << "usage: beesnest logins (add <username> <password> | " << endl;
00127         cout << "\t\t\tchange <username> <oldpassword> <newpassword> | " << endl;
00128         cout << "\t\t\tremove <username> <password>)" << endl;
00129         return;
00130     }
00131 
00132     try
00133     {   /* Some code from InitProgram is copied here. */
00134         bool is_ok = CdcParameters::ReadParams (PARM_FILE_EXT);
00135         CdcString file = CdcFileUtil::GetWorkPath (
00136             CdcParameters::GetParam ("Login-Manager->Users-File"));
00137         CdcLoginManager::Load (file.GetBuffer ());
00138         if (is_ok == false)
00139             PrintString ("Warning - XML file is not Well-Formed!", PS_GEN);
00140     }
00141     catch (CdcException& e)
00142     { PrintString (e.GetExpMessage (), PS_GEN); return; }
00143 
00144     if (strcmp (argv[2], "add") == 0)
00145         is_ok = CdcLoginManager::AddUser (argv[3], argv[4]);
00146     else if (strcmp (argv[2], "change") == 0 && argc > 5)
00147         is_ok = CdcLoginManager::ChangePassword (argv[3], argv[4], argv[5]);
00148     else if (strcmp (argv[2], "remove") == 0)
00149         is_ok = CdcLoginManager::RemoveUser (argv[3], argv[4]);
00150 
00151     if (is_ok == false)
00152         PrintString ("Cannot execute the request!", PS_GEN);
00153     else
00154         PrintString ("Request executed - OK!", PS_GEN);
00155 
00156     CdcString file = CdcFileUtil::GetWorkPath (
00157         CdcParameters::GetParam ("Login-Manager->Users-File"));
00158     CdcLoginManager::Save (file.GetBuffer ());
00159 }
00160 
00161 void main (int argc, char* argv[])
00162 {
00163     gbRunAsApp = true;
00164     if (argc > 1)
00165     {
00166         if (strcmp (argv[1], "install") == 0)
00167             CdcDemon::Install ();
00168         else if (strcmp (argv[1], "uninstall") == 0)
00169             CdcDemon::Uninstall ();
00170         else if (strcmp (argv[1], "run") == 0)
00171             ProgramMain ();
00172         else if (strcmp (argv[1], "logins") == 0)
00173             ControlUsers (argc, argv);
00174         else
00175             cout << "usage: beesnest [run | install | uninstall | logins...]" << endl;
00176     }
00177     else
00178     {
00179         gbRunAsApp = false;
00180         CdcDemon::RunMain ();
00181     }
00182 }
00183 

Generated on Mon Oct 11 2010 16:23:23 for Beesnest by  doxygen 1.7.2