Patch level : 10.0

Files correlati     : lurch.exe authoriz.exe
Ricompilazione Demo : [ ]
Commento            :
Resa piu' robusta la procedura di chiusura dei servizi


git-svn-id: svn://10.65.10.50/trunk@20134 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
guy 2010-02-17 11:23:55 +00:00
parent 38b45187af
commit 5304308581
4 changed files with 84 additions and 59 deletions

View File

@ -810,7 +810,7 @@ void TAuthorizationServer::ProcessUserLogin(wxString cmd, wxSocketBase& sock)
int session; int session;
cmd.Replace(",", " "); cmd.Replace(")", " "); cmd.Replace(",", " "); cmd.Replace(")", " ");
const int nStr = sscanf(cmd, "UserLogin(%s %s %s %d)", strUser, strPassword, strProgram, &session); const int nStr = wxSscanf(cmd, "UserLogin(%s %s %s %d)", strUser, strPassword, strProgram, &session);
if (nStr < 4) if (nStr < 4)
session = 0; session = 0;
@ -827,7 +827,7 @@ void TAuthorizationServer::ProcessUserLogin(wxString cmd, wxSocketBase& sock)
{ {
if (m_Users.GetLicenses() >= m_Dongle.MaxUsers() && m_Users.Find(sock, NULL, session) == NULL) if (m_Users.GetLicenses() >= m_Dongle.MaxUsers() && m_Users.Find(sock, NULL, session) == NULL)
{ {
WriteLog("*** Maximum users exceeded"); WriteLog(_("*** Maximum number of users exceeded"));
num = 0; num = 0;
} }
else else
@ -839,12 +839,12 @@ void TAuthorizationServer::ProcessUserLogin(wxString cmd, wxSocketBase& sock)
if (num > 0) if (num > 0)
m_Users.AddConnection(sock, strUser, session); m_Users.AddConnection(sock, strUser, session);
else else
WriteLog("*** Bad password"); WriteLog(_("*** Bad password"));
} }
} }
} }
else else
WriteLog("*** Dongle not responding"); WriteLog(_("*** Dongle not responding"));
ReturnInt(sock, num); ReturnInt(sock, num);
} }
@ -877,8 +877,7 @@ bool TAuthorizationServer::ReturnInt(wxSocketBase& outs, unsigned int i)
bool TAuthorizationServer::ReturnBool(wxSocketBase& outs, bool b) bool TAuthorizationServer::ReturnBool(wxSocketBase& outs, bool b)
{ {
ReturnInt(outs, b ? 1 : 0); return ReturnInt(outs, b ? 1 : 0);
return outs.IsOk();
} }
bool TAuthorizationServer::ProcessCommand(wxString cmd, wxSocketBase& outs) bool TAuthorizationServer::ProcessCommand(wxString cmd, wxSocketBase& outs)

View File

@ -98,7 +98,7 @@ THashTable::THashTable(size_t size) : m_Hash(wxKEY_STRING, size)
m_Hash.DeleteContents(true); m_Hash.DeleteContents(true);
} }
#ifdef WIN32 #ifdef wxHAS_TASK_BAR_ICON
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// TTaskbarIcon // TTaskbarIcon
@ -110,13 +110,12 @@ END_EVENT_TABLE()
void TTaskBarIcon::OnTaskBarClick(wxTaskBarIconEvent& WXUNUSED(e)) void TTaskBarIcon::OnTaskBarClick(wxTaskBarIconEvent& WXUNUSED(e))
{ {
wxString url; wxString strURL;
// wxIPV4address addr; addr.AnyAddress(); // wxIPV4address addr; addr.AnyAddress();
// addr.Service(GetServerApp().GetDefaultPort()); // addr.Service(GetServerApp().GetDefaultPort());
// url << addr.Hostname() << ":" << addr.Service(); // url << addr.Hostname() << ":" << addr.Service();
url << "http://127.0.0.1:" << GetServerApp().GetDefaultPort(); strURL << "http://127.0.0.1:" << GetServerApp().GetDefaultPort();
wxLaunchDefaultBrowser(strURL);
::ShellExecute(0, "open", url, NULL, NULL, SW_SHOWNORMAL);
} }
void TTaskBarIcon::Init() void TTaskBarIcon::Init()
@ -223,20 +222,20 @@ TXmlItem& TBaseServerApp::CreatePageBody(TXmlItem& html, wxString header) const
TXmlItem& body = html.AddChild("body"); TXmlItem& body = html.AddChild("body");
body.SetAttr("bgcolor", "#EFCEAD"); body.SetAttr("bgcolor", "#EFCEAD");
if (m_bRunning) if (IsRunning())
body.SetAttr("background", "back.gif"); body.SetAttr("background", "back.gif");
TXmlItem& title = body.AddChild("table"); TXmlItem& title = body.AddChild("table");
title.SetAttr("border", "5"); title.SetAttr("width", "100%"); title.SetAttr("border", "5"); title.SetAttr("width", "100%");
TXmlItem& title_tr = title.AddChild("tr"); TXmlItem& title_tr = title.AddChild("tr");
if (m_bRunning) if (IsRunning())
AddLogo(title_tr); AddLogo(title_tr);
TXmlItem& td1 = title_tr.AddChild("td"); TXmlItem& td1 = title_tr.AddChild("td");
td1.AddChild("h1").AddChild("center") << header; td1.AddChild("h1").AddChild("center") << header;
if (m_bRunning) if (IsRunning())
AddLogo(title_tr); AddLogo(title_tr);
body.AddChild("br"); body.AddChild("br");
@ -279,7 +278,7 @@ void TBaseServerApp::MessageBox(const wxChar* caption, const wxChar* msg, wxSock
bool TBaseServerApp::CanProcessCommand(wxString& cmd, wxSocketBase& outs) bool TBaseServerApp::CanProcessCommand(wxString& cmd, wxSocketBase& outs)
{ {
if (cmd.Find("stop.cgi") > 0) if (cmd.Find("stop.cgi") >= 0)
{ {
m_bRunning = false; // Disable gif on title m_bRunning = false; // Disable gif on title
if (outs.IsOk()) if (outs.IsOk())
@ -651,10 +650,10 @@ int TBaseServerApp::GetDefaultPort() const
int TBaseServerApp::GetOemInt(const wxChar* key, int def) const int TBaseServerApp::GetOemInt(const wxChar* key, int def) const
{ {
static int _oem = -1; static int _oem = -883;
#ifdef __WXMSW__ #ifdef __WXMSW__
if (_oem < 0) if (_oem == -883)
_oem = ::GetPrivateProfileInt("MAIN", "OEM", -1, OEM_INI); _oem = ::GetPrivateProfileInt("MAIN", "OEM", -1, OEM_INI);
if (_oem >= 0) if (_oem >= 0)
{ {
@ -693,7 +692,9 @@ wxString TBaseServerApp::GetOemString(const wxChar* key, const wxChar* def) cons
} }
bool TBaseServerApp::IsAdvanced() const bool TBaseServerApp::IsAdvanced() const
{ return GetOemInt(wxT("OEM"), -1) != 2; } {
const int nOEM = GetOemInt(wxT("OEM"), -1);
return nOEM==0 || nOEM>2; }
wxString TBaseServerApp::GetLogFileName() const wxString TBaseServerApp::GetLogFileName() const
{ return GetConfigString("LogFile"); } { return GetConfigString("LogFile"); }
@ -701,14 +702,19 @@ wxString TBaseServerApp::GetLogFileName() const
wxString TBaseServerApp::GetDocumentRoot() const wxString TBaseServerApp::GetDocumentRoot() const
{ return GetConfigString("DocumentRoot", m_strPath); } { return GetConfigString("DocumentRoot", m_strPath); }
bool TBaseServerApp::IsRunning() const
{ return m_bRunning && IsMainLoopRunning() && Ok(); }
bool TBaseServerApp::OnInit() bool TBaseServerApp::OnInit()
{ {
m_server = NULL; m_server = NULL;
m_log = NULL; m_log = NULL;
m_bRunning = false;
m_SingleInstance = new wxSingleInstanceChecker(GetAppName()); m_SingleInstance = new wxSingleInstanceChecker(GetAppName());
if (m_SingleInstance->IsAnotherRunning()) if (m_SingleInstance->IsAnotherRunning())
{ {
wxMessageBox(_("Another instance is already running"), GetAppName(), wxICON_HAND);
delete m_SingleInstance; delete m_SingleInstance;
m_SingleInstance = NULL; m_SingleInstance = NULL;
return false; return false;
@ -730,13 +736,12 @@ bool TBaseServerApp::OnInit()
m_server = new wxSocketServer(addr); m_server = new wxSocketServer(addr);
// Create the Log file // Create the Log file
wxString str;
//Prima di creare il nuovo log file (vuoto) salva quello vecchio aggiungendo al nome data e ora di ripartenza.. //Prima di creare il nuovo log file (vuoto) salva quello vecchio aggiungendo al nome data e ora di ripartenza..
//..Serve per monitorare i defungimenti dei server //..Serve per monitorare i defungimenti dei server
str = GetLogFileName(); const wxString strLog = GetLogFileName();
if (!str.IsEmpty()) if (!strLog.IsEmpty())
{ {
wxFileName fnLogFileName(str); wxFileName fnLogFileName(strLog);
if (fnLogFileName.FileExists()) if (fnLogFileName.FileExists())
{ {
const wxDateTime dtNow = wxDateTime::Now(); const wxDateTime dtNow = wxDateTime::Now();
@ -746,10 +751,10 @@ bool TBaseServerApp::OnInit()
dtNow.GetHour(), dtNow.GetMinute()); dtNow.GetHour(), dtNow.GetMinute());
strWorkString << fnLogFileName.GetName(); strWorkString << fnLogFileName.GetName();
fnLogFileName.SetName(strWorkString); fnLogFileName.SetName(strWorkString);
wxCopyFile(str, fnLogFileName.GetFullPath()); wxCopyFile(strLog, fnLogFileName.GetFullPath());
} }
m_log = new wxFileOutputStream(str); m_log = new wxFileOutputStream(strLog);
m_bLogVerbose = GetConfigBool("LogVerbose"); m_bLogVerbose = GetConfigBool("LogVerbose");
} }
else else
@ -759,9 +764,8 @@ bool TBaseServerApp::OnInit()
} }
m_nTmpCounter = 0; m_nTmpCounter = 0;
// We use Ok() here to see if the server is really listening // We use Ok() here to see if the server is really listening
str.Empty();
if (m_server->Ok()) if (m_server->Ok())
{ {
// Setup the event handler and subscribe to connection events // Setup the event handler and subscribe to connection events
@ -769,35 +773,36 @@ bool TBaseServerApp::OnInit()
m_server->SetNotify(wxSOCKET_CONNECTION_FLAG); m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
m_server->Notify(TRUE); m_server->Notify(TRUE);
str << GetAppName() << " listening at " << addr.Hostname() << ":" << addr.Service(); wxString str;
m_bRunning = true; str << GetAppName() << _(" listening at ") << addr.Hostname() << wxT(":") << addr.Service();
WriteLog(str);
m_bRunning = Initialization();
} }
else else
{ {
str << GetAppName() << " could not listen to port " << addr.Service(); wxString str;
str << GetAppName() << _(" could not listen to port ") << addr.Service();
WriteLog(str);
} }
WriteLog(str);
bool ok = m_server->Ok(); #ifdef wxHAS_TASK_BAR_ICON
if (ok)
ok = Initialization();
#ifdef WIN32
m_Tray = NULL; m_Tray = NULL;
if (ok) if (m_bRunning)
{ {
m_Tray = new TTaskBarIcon; m_Tray = new TTaskBarIcon;
m_Tray->Init(); m_Tray->Init();
} }
#endif #endif
return ok; return m_bRunning;
} }
int TBaseServerApp::OnExit() int TBaseServerApp::OnExit()
{ {
if (m_server != NULL) if (m_server != NULL)
{ {
m_bRunning = false;
Deinitialization(); Deinitialization();
delete m_SingleInstance; delete m_SingleInstance;
m_SingleInstance = NULL; m_SingleInstance = NULL;
@ -813,9 +818,10 @@ int TBaseServerApp::OnExit()
m_log = NULL; m_log = NULL;
} }
#ifdef WIN32 #ifdef wxHAS_TASK_BAR_ICON
if (m_Tray != NULL) if (m_Tray != NULL)
{ {
m_Tray->RemoveIcon();
delete m_Tray; delete m_Tray;
m_Tray = NULL; m_Tray = NULL;
} }

View File

@ -57,7 +57,7 @@ public:
// TTaskbarIcon // TTaskbarIcon
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
#ifdef WIN32 #ifdef wxHAS_TASK_BAR_ICON
#include <wx/taskbar.h> #include <wx/taskbar.h>
class TTaskBarIcon : public wxTaskBarIcon class TTaskBarIcon : public wxTaskBarIcon
@ -89,7 +89,7 @@ private:
int m_nTmpCounter; int m_nTmpCounter;
wxArrayPtrVoid m_Sockets; wxArrayPtrVoid m_Sockets;
#ifdef WIN32 #ifdef wxHAS_TASK_BAR_ICON
TTaskBarIcon* m_Tray; TTaskBarIcon* m_Tray;
#endif #endif
@ -141,9 +141,11 @@ public:
size_t ParseArguments(wxString args, THashTable& hashArgs) const; size_t ParseArguments(wxString args, THashTable& hashArgs) const;
virtual void WriteLog(const wxChar* str) const; // Writes on log file if present virtual void WriteLog(const wxChar* str) const; // Writes on log file if present
virtual bool Ok() const { return m_server->Ok(); } virtual bool Ok() const { return m_server != NULL && m_server->Ok(); }
virtual bool Initialization() { return true; } virtual bool Initialization() { return true; }
virtual bool Deinitialization() { return true; } virtual bool Deinitialization() { return true; }
bool IsRunning() const;
// Initialization (Do NOT use nor reimplement!) // Initialization (Do NOT use nor reimplement!)
virtual bool OnInit(); virtual bool OnInit();

View File

@ -24,6 +24,7 @@ class TLurchServer : public TBaseServerApp
protected: protected:
virtual const wxChar* GetAppName() const; virtual const wxChar* GetAppName() const;
virtual bool Initialization(); virtual bool Initialization();
virtual bool Deinitialization();
void OnTimer(wxTimerEvent& evt); void OnTimer(wxTimerEvent& evt);
void OnEndProcess(wxProcessEvent& evt); void OnEndProcess(wxProcessEvent& evt);
@ -32,7 +33,7 @@ protected:
const wxArrayString& GetServersList(); const wxArrayString& GetServersList();
const wxArrayString& GetAutoRunList(); const wxArrayString& GetAutoRunList();
const TProcessHashMap& GetRunningServers() { return m_hmProcMap; } const TProcessHashMap& GetRunningServers() const { return m_hmProcMap; }
bool PingProcess(const wxString& strApp); bool PingProcess(const wxString& strApp);
void StopProcess(const wxString& strApp); void StopProcess(const wxString& strApp);
@ -106,7 +107,7 @@ bool TLurchServer::PingProcess(const wxString& strApp)
void TLurchServer::OnTimer(wxTimerEvent& evt) void TLurchServer::OnTimer(wxTimerEvent& evt)
{ {
const wxSemaError se = m_Semaphore.TryWait(); const wxSemaError se = IsRunning() ? m_Semaphore.TryWait() : wxSEMA_MISC_ERROR;
if (se == wxSEMA_NO_ERROR) if (se == wxSEMA_NO_ERROR)
{ {
const wxArrayString& aServers = GetAutoRunList(); const wxArrayString& aServers = GetAutoRunList();
@ -446,23 +447,16 @@ void TLurchServer::ProcessFormStop(const THashTable& args, wxSocketBase& sock)
{ {
const wxString strApp = args.Get("App"); const wxString strApp = args.Get("App");
if (strApp == GetAppName() || strApp.IsEmpty()) // Stop myself! if (strApp == GetAppName() || strApp.IsEmpty()) // Stop myself!
{ {
m_PingTimer.Stop(); // Stop ping process! wxString strCmd = wxT("stop.cgi"); // Exits main loop;
// Fill a safe array of names before killing the processes stored in the hash map TBaseServerApp::CanProcessCommand(strCmd, sock);
const TProcessHashMap& hmServers = GetRunningServers(); // Hash map of the running processes }
wxArrayString aRunner; // Array of the names of the runners
TProcessHashMap::const_iterator it;
for (it = hmServers.begin(); it != hmServers.end(); ++it)
aRunner.Add(it->first);
for (size_t i = 0; i < aRunner.GetCount(); i++)
StopProcess(aRunner[i]);
ExitMainLoop();
}
else else
{
StopProcess(strApp); StopProcess(strApp);
MessageBox("Server stopped", strApp, sock); MessageBox("Server stopped", strApp, sock);
}
m_Semaphore.Post(); // Inutile, ma cosi' non rompo la simmetria m_Semaphore.Post();
} }
} }
@ -667,6 +661,30 @@ bool TLurchServer::Initialization()
return true; return true;
} }
bool TLurchServer::Deinitialization()
{
m_PingTimer.Stop(); // Stop ping process!
// kill all autorun processes even if I didn't start them
const wxArrayString& ar = GetAutoRunList();
for (size_t i = 0; i < ar.GetCount(); i++)
StopProcess(ar[i]);
// kill all still running (die hard) processes
const TProcessHashMap& hmServers = GetRunningServers();
if (!hmServers.empty())
{
// Fill a safe array of names before killing the processes stored in the hash map
wxArrayString aRunner; // Array of the names of the runners
TProcessHashMap::const_iterator it;
for (it = hmServers.begin(); it != hmServers.end(); ++it)
aRunner.Add(it->first);
for (size_t i = 0; i < aRunner.GetCount(); i++)
StopProcess(aRunner[i]);
}
return true;
}
// Istanziare l'applicazione principale // Istanziare l'applicazione principale
IMPLEMENT_APP(TLurchServer) IMPLEMENT_APP(TLurchServer)