Skip to content

Commit

Permalink
Replaced calls to native Windows threading and synchronizaton APIs wi…
Browse files Browse the repository at this point in the history
…th boost::thread functionality.
  • Loading branch information
martinknafve committed Jul 14, 2014
1 parent faccaeb commit d8fd9dc
Show file tree
Hide file tree
Showing 86 changed files with 653 additions and 1,537 deletions.
2 changes: 1 addition & 1 deletion hmailserver/source/Server/COM/InterfaceMessageIndexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ STDMETHODIMP InterfaceMessageIndexing::Index()
if (!GetIsServerAdmin())
return false;

HM::MessageIndexer::IndexNow();
HM::MessageIndexer::Instance()->IndexNow();

return S_OK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ namespace HM
bool useSSL,
boost::asio::io_service& io_service,
boost::asio::ssl::context& context,
shared_ptr<Event> disconnected,
String &message,
bool &testCompleted) :
AnsiStringConnection(useSSL, io_service, context),
AnsiStringConnection(useSSL, io_service, context, disconnected),
m_sMessage(message),
m_TestCompleted(testCompleted)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace HM
SpamAssassinClient(const String &sFile, bool useSSL,
boost::asio::io_service& io_service,
boost::asio::ssl::context& context,
shared_ptr<Event> disconnected,
String &message,
bool &testCompleted);
~SpamAssassinClient(void);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,20 @@ namespace HM

bool testCompleted;

shared_ptr<SpamAssassinClient> pSAClient = shared_ptr<SpamAssassinClient>(new SpamAssassinClient(tempFile, false, pIOCPServer->GetIOService(), ctx, message, testCompleted));
shared_ptr<Event> disconnectEvent = shared_ptr<Event>(new Event());
shared_ptr<SpamAssassinClient> pSAClient = shared_ptr<SpamAssassinClient>(new SpamAssassinClient(tempFile, false, pIOCPServer->GetIOService(), ctx, disconnectEvent, message, testCompleted));

pSAClient->Start();

// Copy the event so that we know when we've disconnected.
Event disconnectEvent(pSAClient->GetConnectionTerminationEvent());



// Here we handle of the ownership to the TCPIP-connection layer.
if (pSAClient->Connect(hostName, port, IPAddress()))
{
// Make sure we keep no references to the TCP connection so that it
// can be terminated whenever. We're longer own the connection.
pSAClient.reset();

disconnectEvent.Wait();
disconnectEvent->Wait();
}

return testCompleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ namespace HM
String message;
bool testCompleted;

shared_ptr<SpamAssassinClient> pSAClient = shared_ptr<SpamAssassinClient>(new SpamAssassinClient(sFilename, false, pIOCPServer->GetIOService(), ctx, message, testCompleted));
shared_ptr<Event> disconnectEvent = shared_ptr<Event>(new Event());
shared_ptr<SpamAssassinClient> pSAClient = shared_ptr<SpamAssassinClient>(new SpamAssassinClient(sFilename, false, pIOCPServer->GetIOService(), ctx, disconnectEvent, message, testCompleted));
pSAClient->Start();

String sHost = config.GetSpamAssassinHost();
int iPort = config.GetSpamAssassinPort();
// Copy the event so that we know when we've disconnected.
Event disconnectEvent(pSAClient->GetConnectionTerminationEvent());

// Here we handle of the ownership to the TCPIP-connection layer.
if (pSAClient->Connect(sHost, iPort, IPAddress()))
Expand All @@ -74,7 +73,7 @@ namespace HM
// can be terminated whenever. We're longer own the connection.
pSAClient.reset();

disconnectEvent.Wait();
disconnectEvent->Wait();
}

// Check if the message is tagged as spam.
Expand Down
66 changes: 26 additions & 40 deletions hmailserver/source/Server/Common/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ namespace HM
{
Application::Application() :
m_sServerWorkQueue("Server queue"),
m_sRandomWorkQueue("Random queue"),
m_sAsynchronousTasksQueue("Aynchronous task queue"),
m_sMaintenanceQueue("Maintenance queue"),
m_sAsynchronousTasksQueue("Asynchronous task queue"),
m_iUniqueID(0)
{
m_sProdName = _T("hMailServer");
Expand Down Expand Up @@ -115,7 +115,7 @@ namespace HM

// Start a random thread queue that can run different
// types of background tasks, such as backups etc.
WorkQueueManager::Instance()->CreateWorkQueue(100, m_sRandomWorkQueue, WorkQueue::eQTRandom);
WorkQueueManager::Instance()->CreateWorkQueue(5, m_sMaintenanceQueue);

// Language needed for COM API
Languages::Instance()->Load();
Expand Down Expand Up @@ -148,7 +148,7 @@ namespace HM

// Start an asynch workqueue which processes asynchronous tasks from clients.
WorkQueueManager::Instance()->CreateWorkQueue(Configuration::Instance()->GetAsynchronousThreads(),
m_sAsynchronousTasksQueue, WorkQueue::eQTFixedSize);
m_sAsynchronousTasksQueue);

return true;
}
Expand Down Expand Up @@ -230,7 +230,7 @@ namespace HM
//---------------------------------------------------------------------------()
{
// Close work queue
WorkQueueManager::Instance()->RemoveQueue(m_sRandomWorkQueue);
WorkQueueManager::Instance()->RemoveQueue(m_sMaintenanceQueue);

WorkQueueManager::Instance()->RemoveQueue(m_sAsynchronousTasksQueue);

Expand Down Expand Up @@ -308,48 +308,42 @@ namespace HM

SpamProtection::Instance()->Load();

// Create queue for server tasks, such as responsing to SMTP and
// POP3 and IMAP connections
m_pIOCPServer = shared_ptr<IOCPServer>(new IOCPServer);

_RegisterSessionTypes();

// Create the main work queue.
int iMainServerQueue = WorkQueueManager::Instance()->CreateWorkQueue(4, m_sServerWorkQueue, WorkQueue::eQTPreLoad);
int iMainServerQueue = WorkQueueManager::Instance()->CreateWorkQueue(4, m_sServerWorkQueue);

m_pNotificationServer = shared_ptr<NotificationServer>(new NotificationServer());
_folderManager = shared_ptr<FolderManager>(new FolderManager());

// Create the scheduler. This is always in use.
m_pScheduler = shared_ptr<Scheduler>(new Scheduler);
m_pScheduler = shared_ptr<Scheduler>(new Scheduler());
WorkQueueManager::Instance()->AddTask(iMainServerQueue, m_pScheduler);

// Always run the IOCP server.
WorkQueueManager::Instance()->AddTask(iMainServerQueue, m_pIOCPServer);

// Always run delivery manager. Software useless without it.
m_pSMTPDeliveryManager = shared_ptr<SMTPDeliveryManager>(new SMTPDeliveryManager);
WorkQueueManager::Instance()->AddTask(iMainServerQueue, m_pSMTPDeliveryManager);

// ... and the external account fetch manager.
m_pExternalFetchManager = shared_ptr<ExternalFetchManager> (new ExternalFetchManager);
WorkQueueManager::Instance()->AddTask(iMainServerQueue, m_pExternalFetchManager);

_CreateScheduledTasks();

if (Configuration::Instance()->GetMessageIndexing())
{
MessageIndexer::Start();
MessageIndexer::Instance()->Start();
}

ServerStatus::Instance()->SetState(ServerStatus::StateRunning);

m_sStartTime = Time::GetCurrentDateTime();

// Wait for the IOCP server to signal start-up.
_serverStartEvent.Wait();
_serverStartEvent.Reset();
m_pScheduler->GetIsStartedEvent().Wait();
m_pSMTPDeliveryManager->GetIsStartedEvent().Wait();
m_pExternalFetchManager->GetIsStartedEvent().Wait();
m_pIOCPServer->GetIsStartedEvent().Wait();

ServerStatus::Instance()->SetState(ServerStatus::StateRunning);
LOG_APPLICATION("Servers started.")

return true;
Expand All @@ -362,7 +356,6 @@ namespace HM
// Registers the different session types...
//---------------------------------------------------------------------------()
{

// Start SMTP server and delivery threads.
if (Configuration::Instance()->GetUseSMTP())
{
Expand Down Expand Up @@ -418,18 +411,26 @@ namespace HM

ServerStatus::Instance()->SetState(ServerStatus::StateStopping);

LOG_DEBUG("Application::StopServers() - Removing server work queue");
// Then remove the main server.
WorkQueueManager::Instance()->RemoveQueue(m_sServerWorkQueue);

// Unload the message list cache.
LOG_DEBUG("Application::StopServers() - Clearing caches");
IMAPFolderContainer::Instance()->Clear();


// Deinitialize servers
if (m_pSMTPDeliveryManager) m_pSMTPDeliveryManager.reset();
LOG_DEBUG("Application::StopServers() - Destructing IOCP");
if (m_pIOCPServer) m_pIOCPServer.reset();
LOG_DEBUG("Application::StopServers() - Destructing DeliveryManager");
if (m_pSMTPDeliveryManager) m_pSMTPDeliveryManager.reset();
LOG_DEBUG("Application::StopServers() - Destructing FetchManager");
if (m_pExternalFetchManager) m_pExternalFetchManager.reset();
LOG_DEBUG("Application::StopServers() - Destructing Scheduler");
if (m_pScheduler) m_pScheduler.reset();

LOG_DEBUG("Application::StopServers() - Destructing Rest");
if (m_pNotificationServer) m_pNotificationServer.reset();
if (_folderManager) _folderManager.reset();

Expand Down Expand Up @@ -468,22 +469,13 @@ namespace HM
}

shared_ptr<WorkQueue>
Application::GetRandomWorkQueue()
Application::GetMaintenanceWorkQueue()
{
shared_ptr<WorkQueue> pWorkQueue;

for (int i = 0; i < 10; i++)
{
pWorkQueue = WorkQueueManager::Instance()->GetQueue(m_sRandomWorkQueue);

if (pWorkQueue)
break;

Sleep(2000);
}
shared_ptr<WorkQueue> pWorkQueue =
WorkQueueManager::Instance()->GetQueue(m_sMaintenanceQueue);

if (!pWorkQueue)
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5118, "Application::GetRandomWorkQueue()", "Random work queue not available.");
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5118, "Application::GetMaintenanceWorkQueue()", "Maintenance work queue not available.");

return pWorkQueue;

Expand All @@ -509,12 +501,6 @@ namespace HM
return iResult;
}

void
Application::SetServerStartedEvent()
{
_serverStartEvent.Set();
}

void
Application::OnPropertyChanged(shared_ptr<Property> pProperty)
{
Expand Down
8 changes: 2 additions & 6 deletions hmailserver/source/Server/Common/Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace HM

shared_ptr<BackupManager> GetBackupManager() {return m_pBackupManager; }

shared_ptr<WorkQueue> GetRandomWorkQueue();
shared_ptr<WorkQueue> GetMaintenanceWorkQueue();
shared_ptr<WorkQueue> GetAsyncWorkQueue();
shared_ptr<IOCPServer> GetIOCPServer() {return m_pIOCPServer; }
// The random work queue can run any task.
Expand All @@ -67,8 +67,6 @@ namespace HM

int GetUniqueID();

void SetServerStartedEvent();

void OnPropertyChanged(shared_ptr<Property> pProperty);

bool OnDatabaseConnected(String &sErrorMessage);
Expand Down Expand Up @@ -96,7 +94,7 @@ namespace HM
shared_ptr<IOCPServer> m_pIOCPServer;
shared_ptr<FolderManager> _folderManager;

const String m_sRandomWorkQueue;
const String m_sMaintenanceQueue;
// The random work queue can run any type of task.

const String m_sServerWorkQueue;
Expand All @@ -105,7 +103,5 @@ namespace HM
const String m_sAsynchronousTasksQueue;

long m_iUniqueID;

Event _serverStartEvent;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace HM

shared_ptr<BackupTask> pBackupTask = shared_ptr<BackupTask>(new BackupTask(true));

shared_ptr<WorkQueue> pWorkQueue = Application::Instance()->GetRandomWorkQueue();
shared_ptr<WorkQueue> pWorkQueue = Application::Instance()->GetMaintenanceWorkQueue();
if (!pWorkQueue)
{
m_bIsRunning = false;
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace HM
shared_ptr<BackupTask> pBackupTask = shared_ptr<BackupTask>(new BackupTask(false));
pBackupTask->SetBackupToRestore(pBackup);

Application::Instance()->GetRandomWorkQueue()->AddTask(pBackupTask);
Application::Instance()->GetMaintenanceWorkQueue()->AddTask(pBackupTask);

LOG_DEBUG("BackupManager::~StartRestore() - E2");
return true;
Expand Down
7 changes: 1 addition & 6 deletions hmailserver/source/Server/Common/Application/BackupTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace HM
{
BackupTask::BackupTask(bool bDoBackup) :
Task("BackupTask"),
m_bDoBackup(bDoBackup)
{
}
Expand All @@ -38,12 +39,6 @@ namespace HM
Application::Instance()->GetBackupManager()->OnThreadStopped();
}

void
BackupTask::StopWork()
{


}

void
BackupTask::SetBackupToRestore(shared_ptr<Backup> pBackup)
Expand Down
1 change: 0 additions & 1 deletion hmailserver/source/Server/Common/Application/BackupTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace HM
~BackupTask(void);

virtual void DoWork();
virtual void StopWork();

void SetBackupToRestore(shared_ptr<Backup> pBackup);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,9 @@ namespace HM
Configuration::SetMessageIndexing(bool enable)
{
if (enable)
MessageIndexer::Start();
MessageIndexer::Instance()->Start();
else
MessageIndexer::Stop();
MessageIndexer::Instance()->Stop();

_propertySet->SetBool(PROPERTY_MESSAGE_INDEXING, enable);
}
Expand Down
8 changes: 6 additions & 2 deletions hmailserver/source/Server/Common/Application/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ namespace HM

String theTime = Time::GetCurrentDate();

assert(NULL != IniFileSettings::Instance());
m_bSepSvcLogs = IniFileSettings::Instance()->GetSepSvcLogs();
IniFileSettings* pIniFileSettings = IniFileSettings::Instance();
if (!pIniFileSettings)
{
assert(0);
}
m_bSepSvcLogs = pIniFileSettings->GetSepSvcLogs();

switch (lt)
{
Expand Down
Loading

0 comments on commit d8fd9dc

Please sign in to comment.