From 45aab711c367f693ee758c1ecf806d5862bb36db Mon Sep 17 00:00:00 2001 From: luca Date: Wed, 23 Dec 2009 09:21:46 +0000 Subject: [PATCH] Patch level :10.0 Files correlati : Ricompilazione Demo : [ ] Commento :server modificati da provare su win2008 git-svn-id: svn://10.65.10.50/trunk@19835 c028cbd2-c16b-5b4b-a496-9718f37d4682 --- server/authoriz.cpp | 114 +++++++++++++------------ server/baseserv.cpp | 30 +++++-- server/baseserv.h | 1 + server/coffee.cpp | 3 + server/lurch.cpp | 199 +++++++++++++++++++++++++++++++++++++------- server/servers.ini | 10 ++- 6 files changed, 265 insertions(+), 92 deletions(-) diff --git a/server/authoriz.cpp b/server/authoriz.cpp index 1b5859e68..a4305b90c 100755 --- a/server/authoriz.cpp +++ b/server/authoriz.cpp @@ -2,6 +2,8 @@ #include "dongle.h" +#include + #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -28,36 +30,44 @@ TUserInfo::TUserInfo(const wxChar* user, const wxChar* host) { } -class TUserTable : public wxObject +WX_DECLARE_HASH_MAP( wxString, TUserInfo*, wxStringHash, wxStringEqual, TUsersHashMap ); + +class TUserTable : public TUsersHashMap { - wxHashTable m_Hash; - +protected: + wxString BuildKey(wxSocketBase& sock, const wxChar* user, int session) const; public: TUserInfo* AddConnection(wxSocketBase& sock, const wxChar* user, int session); TUserInfo* Find(wxSocketBase& sock, const wxChar* user, int session); void RemoveConnection(wxSocketBase& sock, const wxChar* user, int session); - void Kill(const wxChar* user); + void Kill(const wxChar* strUserAtHost); void KillSession(wxSocketBase& sock, int session); - size_t GetCount() const { return m_Hash.GetCount(); } - size_t GetLicenses(); - void BeginFind() { m_Hash.BeginFind(); } - TUserInfo* Next() { wxHashTable::Node* n = m_Hash.Next(); return n ? (TUserInfo*)n->GetData() : NULL; } + size_t GetCount() const { return size(); } + size_t GetLicenses() const; - TUserTable(size_t size = 13); + TUserTable(); }; -size_t TUserTable::GetLicenses() -{ - wxHashTable Hosts; - - BeginFind(); - for (TUserInfo* ui = Next(); ui; ui = Next()) - if (Hosts.Get(ui->m_strHost) == NULL) - Hosts.Put(ui->m_strHost, ui); +WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, THostSet ); - return Hosts.GetCount(); +size_t TUserTable::GetLicenses() const +{ + THostSet hsHosts; + for( TUsersHashMap::const_iterator it = begin(); it != end(); ++it ) + hsHosts.insert(it->first); + return hsHosts.size(); +} + +wxString TUserTable::BuildKey(wxSocketBase& sock, const wxChar* user, int session) const +{ + wxIPV4address peer; sock.GetPeer(peer); + wxString host; + host.Printf("%s:%d", peer.Hostname().c_str(), session); + wxString strUserAtHost; + strUserAtHost.Printf("%s@%s", user, host.c_str()); + return strUserAtHost; } TUserInfo* TUserTable::AddConnection(wxSocketBase& sock, const wxChar* user, int session) @@ -65,11 +75,9 @@ TUserInfo* TUserTable::AddConnection(wxSocketBase& sock, const wxChar* user, int TUserInfo* ui = Find(sock, user, session); if (ui == NULL) { - wxIPV4address peer; sock.GetPeer(peer); - wxString host; - host.Printf("%s:%d", (const char *) peer.Hostname(), session); - ui = new TUserInfo(user, host); - m_Hash.Put(wxString::Format("%s@%s", (const char *) user, host.c_str()), ui); + const wxString strUserAtHost = BuildKey(sock, user, session); + ui = new TUserInfo(user, strUserAtHost.After('@')); + (*this)[strUserAtHost] = ui; } ui->m_nPrograms++; @@ -78,18 +86,15 @@ TUserInfo* TUserTable::AddConnection(wxSocketBase& sock, const wxChar* user, int TUserInfo* TUserTable::Find(wxSocketBase& sock, const wxChar* user, int session) { - wxIPV4address peer; sock.GetPeer(peer); - wxString host; - - host = wxString::Format("%s:%d", (const char *) peer.Hostname(), session); - + const wxString strUserAtHost = BuildKey(sock, user, session); if (user && *user) - return (TUserInfo*)m_Hash.Get(wxString::Format("%s@%s", (const char *) user, host.c_str())); + return (*this)[strUserAtHost]; - BeginFind(); - for (TUserInfo* ui = Next(); ui; ui = Next()) - { - if (ui->m_strHost == host) + const wxString strHost = strUserAtHost.After('@'); + for( TUsersHashMap::iterator it = begin(); it != end(); ++it ) + { + TUserInfo* ui = it->second; + if (ui->m_strHost == strHost) return ui; } @@ -103,21 +108,16 @@ void TUserTable::RemoveConnection(wxSocketBase& sock, const wxChar* user, int se { ui->m_nPrograms--; if (ui->m_nPrograms <= 0) - m_Hash.Delete(wxString::Format("%s@%s", ui->m_strName.c_str(), ui->m_strHost.c_str())); + { + const wxString strUserAtHost = BuildKey(sock, user, session); + erase(strUserAtHost); + } } } -void TUserTable::Kill(const wxChar* strUser) +void TUserTable::Kill(const wxChar* strUserAtHost) { - BeginFind(); - for (TUserInfo* ui = Next(); ui; ui = Next()) - { - if (ui->m_strName == strUser) - { - m_Hash.Delete(wxString::Format("%s@%s", ui->m_strName.c_str(), ui->m_strHost.c_str())); - break; - } - } + erase(strUserAtHost); } void TUserTable::KillSession(wxSocketBase& sock, int session) @@ -126,15 +126,18 @@ void TUserTable::KillSession(wxSocketBase& sock, int session) { TUserInfo* ui = Find(sock, NULL, session); if (ui != NULL) - m_Hash.Delete(wxString::Format("%s@%s", ui->m_strName.c_str(), ui->m_strHost.c_str())); + { + const wxString strUserAtHost = BuildKey(sock, ui->m_strName.c_str(), session); + erase(strUserAtHost); + } else break; } } -TUserTable::TUserTable(size_t size) : m_Hash(wxKEY_STRING, size) +TUserTable::TUserTable() { - m_Hash.DeleteContents(true); + clear(); } /////////////////////////////////////////////////////////// @@ -246,7 +249,7 @@ bool TAuthorizationServer::IsMagicName(wxString& strFilename) const strFilename = GetLogFileName(); } else if (strName == "activate" || strName == "deactivate" || - strName == "year" || strName == "maxusers" || strName == "kill") + strName == "year" || strName == "maxusers" || strName == "kill.cgi") { return true; } @@ -425,10 +428,10 @@ void TAuthorizationServer::GenerateUsers(wxString& strFilename) th.AddChild("th").SetAttr("width", "15%") << "Date"; th.AddChild("th").SetAttr("width", "15%") << "Kill"; - m_Users.BeginFind(); int nUser = 0; - for (TUserInfo* ui = m_Users.Next(); ui; ui = m_Users.Next()) + for( TUsersHashMap::const_iterator it = m_Users.begin(); it != m_Users.end(); ++it ) { + const TUserInfo* ui = it->second; TXmlItem& tr = table.AddChild("tr"); tr.AddChild("td").SetAttr("align", "right") << wxString::Format("%d", ++nUser); tr.AddChild("td").AddChild("b") << ui->m_strName; @@ -437,7 +440,8 @@ void TAuthorizationServer::GenerateUsers(wxString& strFilename) tr.AddChild("td").SetAttr("align", "center") << ui->m_time.Format("%H:%M:%S"); tr.AddChild("td").SetAttr("align", "center") << ui->m_time.Format("%d-%m-%Y"); - wxString href = wxString::Format("kill.cgi?%s", ui->m_strName.c_str()); + wxString strKey; strKey << ui->m_strName << wxT("@") << ui->m_strHost; + wxString href = wxString::Format("kill.cgi?%s", strKey.c_str()); AddLinkButton(tr.AddChild("td"), "Kill", href).SetAttr("width", "100%"); } @@ -906,7 +910,13 @@ void TAuthorizationServer::ProcessCommand(wxString cmd, wxSocketBase& outs) if (cmd.StartsWith("DongleModules")) { outs.Write(m_Modules, sizeof(m_Modules)); - } + } else + if (cmd.StartsWith("DongleLoggedLicenses")) + { + ReturnInt(outs, m_Users.GetLicenses()); + } + else + TBaseServerApp::ProcessCommand(cmd, outs); // Gestisce PING! } #define ATOMIC_SEMAPHORE "DONGLE_SERVER_ATOM" diff --git a/server/baseserv.cpp b/server/baseserv.cpp index 7ee293031..fb6d29dd1 100755 --- a/server/baseserv.cpp +++ b/server/baseserv.cpp @@ -163,7 +163,11 @@ END_EVENT_TABLE() void TBaseServerApp::WriteLog(const wxChar* str) const { if (m_log != NULL) - *m_log << str << endl; + { + //raccatta data ed ora + const wxString strNow = wxNow(); + *m_log << strNow << " " << str << endl; + } } wxString TBaseServerApp::GetTempFilename(const wxChar * ext) @@ -416,9 +420,10 @@ void TBaseServerApp::ProcessHttpGet(wxString cmd, wxSocketBase& sock) { const int stop = cmd.Find(" HTTP"); wxString str = cmd.Mid(4, stop-4).Trim(); - if (str == "/") - str += "index.htm"; - wxString strFilename = GetDocumentRoot() + str; + + if (str == "/") + str += "index.htm"; + wxString strFilename = GetDocumentRoot() + str; SendFile(strFilename, sock); } @@ -450,7 +455,9 @@ void TBaseServerApp::ProcessCommand(wxString cmd, wxSocketBase& outs) ProcessFormCommand(cmd, outs); } else if (cmd.StartsWith("GET ")) - ProcessHttpGet(cmd, outs); + ProcessHttpGet(cmd, outs); else + if (cmd.StartsWith("PING")) + outs << "PONG\n"; } void TBaseServerApp::OnServerEvent(wxSocketEvent& e) @@ -534,7 +541,9 @@ void TBaseServerApp::OnIdle(wxIdleEvent& event) sock.SetNotify(wxSOCKET_LOST_FLAG); wxString & str = ((TCommand *) m_Sockets[0])->m_Command; - WriteLog(str); + //scrive sul log solo se chiacchierone! + if (m_bLogVerbose) + WriteLog(str); if (CanProcessCommand(str, sock)) { @@ -664,9 +673,15 @@ bool TBaseServerApp::OnInit() str = GetLogFileName(); if (!str.IsEmpty()) + { m_log = new wxFileOutputStream(str); + m_bLogVerbose = GetConfigBool("LogVerbose"); + } else + { m_log = NULL; + m_bLogVerbose = false; + } m_nTmpCounter = 0; @@ -711,6 +726,8 @@ int TBaseServerApp::OnExit() Deinitialization(); delete m_SingleInstance; delete m_server; + m_SingleInstance = NULL; + m_server = NULL; } if (m_log != NULL) { @@ -718,6 +735,7 @@ int TBaseServerApp::OnExit() str << GetAppName() << " shutting down."; WriteLog(str); delete m_log; + m_log = NULL; } #ifdef WIN32 diff --git a/server/baseserv.h b/server/baseserv.h index 2be3beb11..c2127595d 100755 --- a/server/baseserv.h +++ b/server/baseserv.h @@ -84,6 +84,7 @@ private: wxFileOutputStream* m_log; wxString m_strPath, m_strTempDir, m_strIni; bool m_bRunning; + bool m_bLogVerbose; int m_nTmpCounter; wxArrayPtrVoid m_Sockets; diff --git a/server/coffee.cpp b/server/coffee.cpp index 5916c7e91..6345278e2 100755 --- a/server/coffee.cpp +++ b/server/coffee.cpp @@ -119,6 +119,9 @@ void TCoffeeServer::ProcessCommand(wxString cmd, wxSocketBase& outs) SendFile(strFilename, outs); } + else + TBaseServerApp::ProcessCommand(cmd, outs); // Gestisce PING! + } // Istanziare l'applicazione principale diff --git a/server/lurch.cpp b/server/lurch.cpp index be3ba2178..b588f2a26 100755 --- a/server/lurch.cpp +++ b/server/lurch.cpp @@ -1,23 +1,61 @@ #include "baseserv.h" #include +#include +#include #ifdef WIN32 #include #endif #include +/////////////////////////////////////////////////////////// +// TProcess declaration +/////////////////////////////////////////////////////////// + +class TLurchServer; //segnaposto della TLurchServer che serve alla TProcess + +class TProcess : public wxProcess +{ + TLurchServer* m_pLurch; + wxString m_strApp; + +protected: + virtual void OnTerminate(int pid, int status); + +public: + void ForcePid(int pid) { SetPid(pid); } + TProcess(TLurchServer* pLurch, const wxString& strApp) : m_pLurch(pLurch), m_strApp(strApp) {} +}; + +/////////////////////////////////////////////////////////// +// TLurchServer declaration +/////////////////////////////////////////////////////////// + +//classe TProcessHashMap derivata da wxHashMap (è una specie di assocarray!) +WX_DECLARE_HASH_MAP( wxString, wxProcess*, wxStringHash, wxStringEqual, TProcessHashMap ); + + class TLurchServer : public TBaseServerApp { + TProcessHashMap m_ProcMap; + wxTimer m_Timer; + int m_nFreq; + + DECLARE_EVENT_TABLE(); protected: virtual const wxChar* GetAppName() const; virtual bool Initialization(); + void OnTimer(wxTimerEvent& evt); void AddMiniForm(TXmlItem& tr, const wxChar* action, const wxChar* app, const wxChar* prompt) const; void EnumerateVariables(const wxString& strApp, wxArrayString& arr) const; void CreateServersList(wxArrayString& arr) const; + bool PingProcess(const wxString& strApp); + void StopProcess(const wxString& strApp); void KillProcess(const wxString& strApp); + wxString StartProcess(const wxString& strApp); public: void GenerateFile(wxString& strFilename); @@ -27,13 +65,79 @@ public: void ProcessHttpGet(wxString cmd, wxSocketBase& outs); void ProcessHttpPost(wxString cmd, wxSocketBase& outs); + bool ForgetProcess(const wxString& strApp); + + //metodi riguardanti l'interfaccia html void ProcessFormStart(const THashTable& args, wxSocketBase& sock); - void ProcessFormKill(const THashTable& args, wxSocketBase& sock); + void ProcessFormStop(const THashTable& args, wxSocketBase& sock); void ProcessFormConfig(const THashTable& args, wxSocketBase& sock); void ProcessFormUpdate(THashTable& args, wxSocketBase& sock); void CallCgi(wxString& strFileName, wxSocketBase& sock); }; +/////////////////////////////////////////////////////////// +// TProcess implementation +/////////////////////////////////////////////////////////// + +void TProcess::OnTerminate(int pid, int WXUNUSED(status)) +{ + SetPid(pid); + m_pLurch->ForgetProcess(m_strApp); +} + +/////////////////////////////////////////////////////////// +// TLurchServer implementation +/////////////////////////////////////////////////////////// + +BEGIN_EVENT_TABLE(TLurchServer, TBaseServerApp) +EVT_TIMER(wxID_ANY, TLurchServer::OnTimer) +END_EVENT_TABLE() + +bool TLurchServer::PingProcess(const wxString& strApp) +{ + bool bPinged = false; + TProcess* pProcess = (TProcess*)m_ProcMap[strApp]; + if (pProcess != NULL) + { + const wxString strPort = GetConfigString("Port", "", strApp); + if (!strPort.IsEmpty()) + { + const int nTimeOut = m_nFreq > 3000 ? m_nFreq / 3000 : 1; + wxIPV4address ipAddress; + ipAddress.LocalHost(); + ipAddress.Service(strPort); + wxSocketClient sSock(wxSOCKET_NOWAIT); + sSock.Connect(ipAddress, false); + if (sSock.WaitOnConnect(nTimeOut)) + { + sSock.Write("PING\n", 5); + if (sSock.WaitForWrite(nTimeOut)) + { + char buffer[8]; memset(buffer, 0, sizeof(buffer)); + sSock.Read(buffer, 4); + if (sSock.WaitForRead(nTimeOut)) + bPinged = wxStrcmp(buffer, "PONG") == 0; + } + } + } + } + return bPinged; +} + +void TLurchServer::OnTimer(wxTimerEvent& WXUNUSED(evt)) +{ + m_Timer.Stop(); + const wxString strApp = "Authorization"; + if (!PingProcess(strApp)) + { + TProcess* pProcess = (TProcess*)m_ProcMap[strApp]; + pProcess->Kill(pProcess->GetPid()); + wxSleep(2); + StartProcess(strApp); + } + m_Timer.Start(m_nFreq); +} + const wxChar* TLurchServer::GetAppName() const { return "Lurch"; @@ -170,12 +274,13 @@ bool TLurchServer::IsCgiName(wxString strFilename) const return strExt == "cgi" || strExt == "exe"; } -void TLurchServer::ProcessFormStart(const THashTable& args, wxSocketBase& sock) -{ - bool ok = false; - const wxString strApp = args.Get("App"); - if (!strApp.IsEmpty()) // Dummy test +wxString TLurchServer::StartProcess(const wxString& strApp) +{ + bool ok = false; + + wxString strMessage; + if (!strApp.IsEmpty()) { const wxSingleInstanceChecker sic(strApp); ok = !sic.IsAnotherRunning(); @@ -183,29 +288,61 @@ void TLurchServer::ProcessFormStart(const THashTable& args, wxSocketBase& sock) if (ok) { - wxString strRun = GetConfigString("Run", "", strApp); + const wxString strRun = GetConfigString("Run", "", strApp); if (wxFileExists(strRun)) { -#ifdef LINUX - if (strRun[ 0u] != '/' && strRun[ 0u] != '.') - strRun = "./" + strRun; -#endif - const long nProc = wxExecute(strRun); + wxFileName fnPath; + fnPath.SetPath(GetServerPath()); + fnPath.SetFullName(strRun); + + TProcess* pProcess = new TProcess(this, strApp); + const long nProc = wxExecute(fnPath.GetFullPath(), wxEXEC_ASYNC, pProcess); if (nProc == 0 || nProc == -1) - MessageBox("ERROR", wxString::Format("Can't run %s executable (%s)", strApp.c_str(), - strRun.c_str()), sock); + { + strMessage.Printf("Can't run %s executable (%s)", strApp.c_str(), strRun.c_str()); + delete pProcess; + } else - MessageBox("Server Started", strApp, sock); + { + pProcess->ForcePid(nProc); + m_ProcMap[strApp] = pProcess; //memorizza il numero del processo che ha lanciato per poterlo usare in fase di controllo + strMessage = wxEmptyString; + } } else - MessageBox("ERROR", wxString::Format("Can't find %s executable (%s)", strApp.c_str(), - strRun.c_str()), sock); + strMessage.Printf("Can't find %s executable (%s)", strApp.c_str(), strRun.c_str()); } else - MessageBox("ERROR", wxString::Format("%s il already running", strApp.c_str()), sock); + strMessage.Printf("%s il already running", strApp.c_str()); + + if (strMessage.IsEmpty()) + WriteLog(strApp + " started."); + else + WriteLog(strMessage); + + return strMessage; } -void TLurchServer::KillProcess(const wxString& strApp) +void TLurchServer::ProcessFormStart(const THashTable& args, wxSocketBase& sock) +{ + const wxString strApp = args.Get("App"); + if (!strApp.IsEmpty()) // Dummy test + { + const wxString strMessage = StartProcess(strApp); + if (strMessage.IsEmpty()) + MessageBox("Server Started", strApp, sock); + else + MessageBox("ERROR", strMessage, sock); + } +} + +bool TLurchServer::ForgetProcess(const wxString& strApp) +{ + WriteLog(strApp + " terminated."); + return m_ProcMap.erase(strApp) != 0; +} + +void TLurchServer::StopProcess(const wxString& strApp) { const wxString strHost = GetConfigString("Host", "localhost", strApp); const int nPort = GetConfigInt("Port", 0, strApp); @@ -225,12 +362,13 @@ void TLurchServer::KillProcess(const wxString& strApp) const wxSingleInstanceChecker sic(strApp); for (int i = 0; i < 5 && sic.IsAnotherRunning(); i++) wxSleep(1); + ForgetProcess(strApp); } } } } -void TLurchServer::ProcessFormKill(const THashTable& args, wxSocketBase& sock) +void TLurchServer::ProcessFormStop(const THashTable& args, wxSocketBase& sock) { const wxString strApp = args.Get("App"); if (strApp == GetAppName()) // Stop myself! @@ -242,11 +380,11 @@ void TLurchServer::ProcessFormKill(const THashTable& args, wxSocketBase& sock) { const wxSingleInstanceChecker sic(app[i]); if (sic.IsAnotherRunning()) - KillProcess(app[i]); + StopProcess(app[i]); } } } - KillProcess(strApp); + StopProcess(strApp); MessageBox("Server stopped", strApp, sock); } @@ -365,7 +503,7 @@ void TLurchServer::CallCgi(wxString& strFileName, wxSocketBase& sock) if (strName == "start") ProcessFormStart(hashArgs, sock); else if (strName == "kill") - ProcessFormKill(hashArgs, sock); else + ProcessFormStop(hashArgs, sock); else if (strName == "config") ProcessFormConfig(hashArgs, sock); else if (strName == "update") @@ -419,16 +557,13 @@ bool TLurchServer::Initialization() const wxString& strApp = arr[i]; const bool bAutorun = GetConfigBool("Autorun", false, strApp); if (bAutorun) - { - const wxSingleInstanceChecker sic(strApp); - if (!sic.IsAnotherRunning()) - { - wxString strRun = GetServerPath() + GetConfigString("Run", "", strApp); - if (wxFileExists(strRun)) - wxExecute(strRun); - } - } + StartProcess(strApp); } + + m_nFreq = GetConfigInt("PingFreq") * 1000; + if (m_nFreq > 0) + m_Timer.Start(m_nFreq); + return true; } diff --git a/server/servers.ini b/server/servers.ini index afd5fa132..e4f290bff 100755 --- a/server/servers.ini +++ b/server/servers.ini @@ -1,7 +1,8 @@ [Authorization] Autorun=1 Icon=autho.gif -LogFile= +LogFile=authoriz.txt +LogVerbose= Run=authoriz.exe Port=1883 Donglehw=2 @@ -16,6 +17,7 @@ Port=3885 Icon=DataBase.gif Run=DataBase.exe LogFile= +LogVerbose= Dsn= Query= @@ -25,15 +27,19 @@ Run=diction.exe Port=3883 Dictionary=english.xml LogFile= +LogVerbose= [Postman] Icon=postman.gif Run=postman.exe Port=8080 LogFile= +LogVerbose= [Lurch] Run=lurch.exe Port=10000 Icon=lurch.gif -LogFile= +LogFile=lurch.txt +LogVerbose= +PingFreq=10