Refactored the Main() function into several sub-functions + some improvements of the IPC code.

This commit is contained in:
LoRd_MuldeR 2014-12-14 19:33:28 +01:00
parent 53b12027e6
commit d3917b5233
10 changed files with 212 additions and 113 deletions

View File

@ -406,6 +406,7 @@ copy /Y "$(SolutionDir)\..\Prerequisites\VisualLeakDetector\bin\Win32\*.manifest
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\Config.h" /> <ClInclude Include="src\Config.h" />
<ClInclude Include="src\IPCCommands.h" />
<ClInclude Include="tmp\LameXP\UIC_AboutDialog.h" /> <ClInclude Include="tmp\LameXP\UIC_AboutDialog.h" />
<ClInclude Include="tmp\LameXP\UIC_CueSheetImport.h" /> <ClInclude Include="tmp\LameXP\UIC_CueSheetImport.h" />
<ClInclude Include="tmp\LameXP\UIC_DropBox.h" /> <ClInclude Include="tmp\LameXP\UIC_DropBox.h" />

View File

@ -540,6 +540,9 @@
<ClInclude Include="tmp\LameXP\UIC_AboutDialog.h"> <ClInclude Include="tmp\LameXP\UIC_AboutDialog.h">
<Filter>Generated Files\UIC</Filter> <Filter>Generated Files\UIC</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\IPCCommands.h">
<Filter>Header Files\Misc</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="gui\DropBox.ui"> <CustomBuild Include="gui\DropBox.ui">

View File

@ -35,7 +35,7 @@
#define VER_LAMEXP_MINOR_LO 1 #define VER_LAMEXP_MINOR_LO 1
#define VER_LAMEXP_TYPE Beta #define VER_LAMEXP_TYPE Beta
#define VER_LAMEXP_PATCH 8 #define VER_LAMEXP_PATCH 8
#define VER_LAMEXP_BUILD 1628 #define VER_LAMEXP_BUILD 1632
#define VER_LAMEXP_CONFG 1558 #define VER_LAMEXP_CONFG 1558
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -155,7 +155,7 @@ ProcessingDialog::ProcessingDialog(FileListModel *fileListModel, const AudioFile
m_systemTray(new QSystemTrayIcon(QIcon(":/icons/cd_go.png"), this)), m_systemTray(new QSystemTrayIcon(QIcon(":/icons/cd_go.png"), this)),
m_settings(settings), m_settings(settings),
m_metaInfo(metaInfo), m_metaInfo(metaInfo),
m_shutdownFlag(shutdownFlag_None), m_shutdownFlag(SHUTDOWN_FLAG_NONE),
m_threadPool(NULL), m_threadPool(NULL),
m_diskObserver(NULL), m_diskObserver(NULL),
m_cpuObserver(NULL), m_cpuObserver(NULL),
@ -769,7 +769,7 @@ void ProcessingDialog::doneEncoding(void)
{ {
if(shutdownComputer()) if(shutdownComputer())
{ {
m_shutdownFlag = m_settings->hibernateComputer() ? shutdownFlag_Hibernate : shutdownFlag_TurnPowerOff; m_shutdownFlag = m_settings->hibernateComputer() ? SHUTDOWN_FLAG_HIBERNATE : SHUTDOWN_FLAG_POWER_OFF;
accept(); accept();
} }
} }

View File

@ -45,11 +45,11 @@ class QElapsedTimer;
class RAMObserverThread; class RAMObserverThread;
class SettingsModel; class SettingsModel;
enum shutdownFlag_t enum lamexp_shutdownFlag_t
{ {
shutdownFlag_None = 0, SHUTDOWN_FLAG_NONE = 0,
shutdownFlag_TurnPowerOff = 1, SHUTDOWN_FLAG_POWER_OFF = 1,
shutdownFlag_Hibernate = 2 SHUTDOWN_FLAG_HIBERNATE = 2
}; };
//UIC forward declartion //UIC forward declartion

41
src/IPCCommands.h Normal file
View File

@ -0,0 +1,41 @@
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version, but always including the *additional*
// restrictions defined in the "License.txt" file.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// http://www.gnu.org/licenses/gpl-2.0.txt
///////////////////////////////////////////////////////////////////////////////
#include <climits>
typedef enum
{
IPC_CMD_NOOP = 0x0000,
IPC_CMD_PING = 0x0001,
IPC_CMD_ADD_FILE = 0x0002,
IPC_CMD_ADD_FOLDER = 0x0003,
IPC_CMD_TERMINATE = 0xFFFF
}
lamexp_ipc_command_t;
typedef enum
{
IPC_FLAG_NONE = 0x0000,
IPC_FLAG_ADD_RECURSIVE = 0x0001,
IPC_FLAG_FORCE = 0x8000,
}
lamexp_ipc_flag_t;

View File

@ -50,15 +50,11 @@
#include <QDir> #include <QDir>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Main function // Helper functions
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static int lamexp_main(int &argc, char **argv) static void lamexp_print_logo(void)
{ {
int iResult = -1;
int iShutdown = shutdownFlag_None;
bool bAccepted = true;
//Print version info //Print version info
qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build()); qDebug("LameXP - Audio Encoder Front-End v%d.%02d %s (Build #%03d)", lamexp_version_major(), lamexp_version_minor(), lamexp_version_release(), lamexp_version_build());
qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(MUtils::Version::app_build_date().year(), MUtils::OS::current_date().year())); qDebug("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.", qMax(MUtils::Version::app_build_date().year(), MUtils::OS::current_date().year()));
@ -71,7 +67,7 @@ static int lamexp_main(int &argc, char **argv)
//Print library version //Print library version
qDebug("This application is powerd by MUtils library v%u.%02u (%s, %s).\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate))); qDebug("This application is powerd by MUtils library v%u.%02u (%s, %s).\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate)));
//Print warning, if this is a "debug" build //Print warning, if this is a "debug" build
if(MUTILS_DEBUG) if(MUTILS_DEBUG)
{ {
@ -79,7 +75,107 @@ static int lamexp_main(int &argc, char **argv)
qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!"); qWarning("DEBUG BUILD: DO NOT RELEASE THIS BINARY TO THE PUBLIC !!!");
qWarning("---------------------------------------------------------\n"); qWarning("---------------------------------------------------------\n");
} }
}
static int lamexp_initialize_ipc(MUtils::IPCChannel *const ipcChannel)
{
int iResult = 0;
if((iResult = ipcChannel->initialize()) != MUtils::IPCChannel::RET_SUCCESS_MASTER)
{
if(iResult == MUtils::IPCChannel::RET_SUCCESS_SLAVE)
{
qDebug("LameXP is already running, connecting to running instance...");
QScopedPointer<MessageProducerThread> messageProducerThread(new MessageProducerThread(ipcChannel));
messageProducerThread->start();
if(!messageProducerThread->wait(30000))
{
qWarning("MessageProducer thread has encountered timeout -> going to kill!");
messageProducerThread->terminate();
messageProducerThread->wait();
MUtils::OS::system_message_err(L"LameXP", L"LameXP is already running, but the running instance doesn't respond!");
return -1;
}
return 0;
}
else
{
qFatal("The IPC initialization has failed!");
return -1;
}
}
return 1;
}
static void lamexp_show_splash(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, SettingsModel *const settingsModel)
{
QScopedPointer<InitializationThread> poInitializationThread(new InitializationThread(cpuFeatures));
SplashScreen::showSplash(poInitializationThread.data());
settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
}
static int lamexp_main_loop_helper(MUtils::IPCChannel *const ipcChannel, FileListModel *const fileListModel, AudioFileModel_MetaInfo *const metaInfo, SettingsModel *const settingsModel, int &iShutdown)
{
int iResult = -1;
bool bAccepted = true;
//Create main window
QScopedPointer<MainWindow> poMainWindow(new MainWindow(ipcChannel, fileListModel, metaInfo, settingsModel));
//Main application loop
while(bAccepted && (iShutdown <= SHUTDOWN_FLAG_NONE))
{
//Show main window
poMainWindow->show();
iResult = qApp->exec();
bAccepted = poMainWindow->isAccepted();
//Sync settings
settingsModel->syncNow();
//Show processing dialog
if(bAccepted && (fileListModel->rowCount() > 0))
{
ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
processingDialog->exec();
iShutdown = processingDialog->getShutdownFlag();
MUTILS_DELETE(processingDialog);
}
}
return iResult;
}
static int lamexp_main_loop(const MUtils::CPUFetaures::cpu_info_t &cpuFeatures, MUtils::IPCChannel *const ipcChannel, int &iShutdown)
{
//Create models
QScopedPointer<FileListModel> fileListModel(new FileListModel() );
QScopedPointer<AudioFileModel_MetaInfo> metaInfo (new AudioFileModel_MetaInfo());
QScopedPointer<SettingsModel> settingsModel(new SettingsModel() );
//Show splash screen
lamexp_show_splash(cpuFeatures, settingsModel.data());
//Validate settings
settingsModel->validate();
//Main processing loop
return lamexp_main_loop_helper(ipcChannel, fileListModel.data(), metaInfo.data(), settingsModel.data(), iShutdown);
}
///////////////////////////////////////////////////////////////////////////////
// Main function
///////////////////////////////////////////////////////////////////////////////
static int lamexp_main(int &argc, char **argv)
{
int iResult = -1;
int iShutdown = SHUTDOWN_FLAG_NONE;
//Print logo
lamexp_print_logo();
//Get CLI arguments //Get CLI arguments
const QStringList &arguments = MUtils::OS::arguments(); const QStringList &arguments = MUtils::OS::arguments();
@ -125,34 +221,10 @@ static int lamexp_main(int &argc, char **argv)
} }
//Initialize IPC //Initialize IPC
MUtils::IPCChannel *ipcChannel = new MUtils::IPCChannel("LameXP", "MultiInstanceHandling"); QScopedPointer<MUtils::IPCChannel> ipcChannel(new MUtils::IPCChannel("lamexp-v4", lamexp_version_build(), "instance"));
if((iResult = ipcChannel->initialize()) != MUtils::IPC_RET_SUCCESS_MASTER) if((iResult = lamexp_initialize_ipc(ipcChannel.data())) < 1)
{ {
if(iResult == MUtils::IPC_RET_SUCCESS_SLAVE) return (iResult == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
{
qDebug("LameXP is already running, connecting to running instance...");
MessageProducerThread *messageProducerThread = new MessageProducerThread(ipcChannel);
messageProducerThread->start();
if(!messageProducerThread->wait(30000))
{
messageProducerThread->terminate();
QMessageBox messageBox(QMessageBox::Critical, "LameXP", "LameXP is already running, but the running instance doesn't respond!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
messageBox.exec();
messageProducerThread->wait();
MUTILS_DELETE(messageProducerThread);
MUTILS_DELETE(ipcChannel);
lamexp_finalization();
return EXIT_FAILURE;
}
MUTILS_DELETE(messageProducerThread);
}
else
{
qFatal("The IPC initialization has failed!");
}
MUTILS_DELETE(ipcChannel);
lamexp_finalization();
return EXIT_SUCCESS;
} }
//Kill application? //Kill application?
@ -160,7 +232,6 @@ static int lamexp_main(int &argc, char **argv)
{ {
if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive)) if(!arguments[i].compare("--kill", Qt::CaseInsensitive) || !arguments[i].compare("--force-kill", Qt::CaseInsensitive))
{ {
MUTILS_DELETE(ipcChannel);
lamexp_finalization(); lamexp_finalization();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -175,61 +246,19 @@ static int lamexp_main(int &argc, char **argv)
//Taskbar init //Taskbar init
WinSevenTaskbar::init(); WinSevenTaskbar::init();
//Create models
FileListModel *fileListModel = new FileListModel();
AudioFileModel_MetaInfo *metaInfo = new AudioFileModel_MetaInfo();
SettingsModel *settingsModel = new SettingsModel();
//Show splash screen
InitializationThread *poInitializationThread = new InitializationThread(cpuFeatures);
SplashScreen::showSplash(poInitializationThread);
settingsModel->slowStartup(poInitializationThread->getSlowIndicator());
MUTILS_DELETE(poInitializationThread);
//Validate settings
settingsModel->validate();
//Create main window
MainWindow *poMainWindow = new MainWindow(ipcChannel, fileListModel, metaInfo, settingsModel);
//Main application loop //Main application loop
while(bAccepted && (iShutdown <= shutdownFlag_None)) iResult = lamexp_main_loop(cpuFeatures, ipcChannel.data(), iShutdown);
{
//Show main window
poMainWindow->show();
iResult = qApp->exec();
bAccepted = poMainWindow->isAccepted();
//Sync settings
settingsModel->syncNow();
//Show processing dialog
if(bAccepted && (fileListModel->rowCount() > 0))
{
ProcessingDialog *processingDialog = new ProcessingDialog(fileListModel, metaInfo, settingsModel);
processingDialog->exec();
iShutdown = processingDialog->getShutdownFlag();
MUTILS_DELETE(processingDialog);
}
}
//Free models
MUTILS_DELETE(poMainWindow);
MUTILS_DELETE(fileListModel);
MUTILS_DELETE(metaInfo);
MUTILS_DELETE(settingsModel);
//Taskbar un-init //Taskbar un-init
WinSevenTaskbar::uninit(); WinSevenTaskbar::uninit();
MUTILS_DELETE(ipcChannel);
//Final clean-up //Final clean-up
qDebug("Shutting down, please wait...\n"); qDebug("Shutting down, please wait...\n");
//Shotdown computer //Shotdown computer
if(iShutdown > shutdownFlag_None) if(iShutdown > SHUTDOWN_FLAG_NONE)
{ {
if(!MUtils::OS::shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == shutdownFlag_Hibernate))) if(!MUtils::OS::shutdown_computer(QApplication::applicationFilePath(), 12, true, (iShutdown == SHUTDOWN_FLAG_HIBERNATE)))
{ {
QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint); QMessageBox messageBox(QMessageBox::Critical, "LameXP", "Sorry, LameXP was unable to shutdown your computer!", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
} }

View File

@ -24,6 +24,7 @@
//Internal //Internal
#include "Global.h" #include "Global.h"
#include "IPCCommands.h"
//MUtils //MUtils
#include <MUtils/IPCChannel.h> #include <MUtils/IPCChannel.h>
@ -36,6 +37,8 @@
//CRL //CRL
#include <limits.h> #include <limits.h>
#define TEST_FLAG(X) ((flags & (X)) == (X))
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Constructor // Constructor
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -45,7 +48,7 @@ MessageHandlerThread::MessageHandlerThread(MUtils::IPCChannel *const ipcChannel)
m_ipcChannel(ipcChannel) m_ipcChannel(ipcChannel)
{ {
m_aborted = false; m_aborted = false;
m_parameter = new char[4096]; m_parameter = new char[MUtils::IPCChannel::MAX_MESSAGE_LEN];
} }
MessageHandlerThread::~MessageHandlerThread(void) MessageHandlerThread::~MessageHandlerThread(void)
@ -60,33 +63,35 @@ void MessageHandlerThread::run()
while(!m_aborted) while(!m_aborted)
{ {
unsigned int command = 0; unsigned int command = 0, flags = 0;
m_ipcChannel->read(command, m_parameter, 4096); if(!m_ipcChannel->read(command, flags, m_parameter, MUtils::IPCChannel::MAX_MESSAGE_LEN))
if(!command) continue; {
qWarning("Failed to read next IPC message!");
break;
}
if(command == IPC_CMD_NOOP)
{
continue;
}
switch(command) switch(command)
{ {
case 1: case IPC_CMD_PING:
emit otherInstanceDetected();
break;
case IPC_CMD_ADD_FILE:
emit fileReceived(QString::fromUtf8(m_parameter)); emit fileReceived(QString::fromUtf8(m_parameter));
break; break;
case 2: case IPC_CMD_ADD_FOLDER:
emit folderReceived(QString::fromUtf8(m_parameter), false); emit folderReceived(QString::fromUtf8(m_parameter), TEST_FLAG(IPC_FLAG_ADD_RECURSIVE));
break; break;
case 3: case IPC_CMD_TERMINATE:
emit folderReceived(QString::fromUtf8(m_parameter), true); if(TEST_FLAG(IPC_FLAG_FORCE))
break;
case 666:
if(!_stricmp(m_parameter, "Force!"))
{ {
_exit(-2); _exit(-2);
} }
else emit killSignalReceived();
{
emit killSignalReceived();
}
break;
case UINT_MAX:
emit otherInstanceDetected();
break; break;
default: default:
qWarning("Received an unknown IPC message! (command=%u)", command); qWarning("Received an unknown IPC message! (command=%u)", command);
@ -100,7 +105,7 @@ void MessageHandlerThread::stop(void)
if(!m_aborted) if(!m_aborted)
{ {
m_aborted = true; m_aborted = true;
m_ipcChannel->send(0, NULL); m_ipcChannel->send(0, 0, NULL);
} }
} }

View File

@ -24,6 +24,7 @@
//Internal //Internal
#include "Global.h" #include "Global.h"
#include "IPCCommands.h"
//MUtils //MUtils
#include <MUtils/Global.h> #include <MUtils/Global.h>
@ -63,12 +64,18 @@ void MessageProducerThread::run()
{ {
if(!arguments[i].compare("--kill", Qt::CaseInsensitive)) if(!arguments[i].compare("--kill", Qt::CaseInsensitive))
{ {
m_ipcChannel->send(666, NULL); if(!m_ipcChannel->send(IPC_CMD_TERMINATE, IPC_FLAG_NONE, NULL))
{
qWarning("Failed to send IPC message!");
}
return; return;
} }
if(!arguments[i].compare("--force-kill", Qt::CaseInsensitive)) if(!arguments[i].compare("--force-kill", Qt::CaseInsensitive))
{ {
m_ipcChannel->send(666, "Force!"); if(!m_ipcChannel->send(IPC_CMD_TERMINATE, IPC_FLAG_FORCE, NULL))
{
qWarning("Failed to send IPC message!");
}
return; return;
} }
} }
@ -80,7 +87,10 @@ void MessageProducerThread::run()
QFileInfo file = QFileInfo(arguments[++i]); QFileInfo file = QFileInfo(arguments[++i]);
if(file.exists() && file.isFile()) if(file.exists() && file.isFile())
{ {
m_ipcChannel->send(1, MUTILS_UTF8(file.canonicalFilePath())); if(!m_ipcChannel->send(IPC_CMD_ADD_FILE, IPC_FLAG_NONE, MUTILS_UTF8(file.canonicalFilePath())))
{
qWarning("Failed to send IPC message!");
}
} }
bSentFiles = true; bSentFiles = true;
} }
@ -89,7 +99,10 @@ void MessageProducerThread::run()
QDir dir = QDir(arguments[++i]); QDir dir = QDir(arguments[++i]);
if(dir.exists()) if(dir.exists())
{ {
m_ipcChannel->send(2, MUTILS_UTF8(dir.canonicalPath())); if(!m_ipcChannel->send(IPC_CMD_ADD_FOLDER, IPC_FLAG_NONE, MUTILS_UTF8(dir.canonicalPath())))
{
qWarning("Failed to send IPC message!");
}
} }
bSentFiles = true; bSentFiles = true;
} }
@ -98,7 +111,10 @@ void MessageProducerThread::run()
QDir dir = QDir(arguments[++i]); QDir dir = QDir(arguments[++i]);
if(dir.exists()) if(dir.exists())
{ {
m_ipcChannel->send(3, MUTILS_UTF8(dir.canonicalPath())); if(!m_ipcChannel->send(IPC_CMD_ADD_FOLDER, IPC_FLAG_ADD_RECURSIVE, MUTILS_UTF8(dir.canonicalPath())))
{
qWarning("Failed to send IPC message!");
}
} }
bSentFiles = true; bSentFiles = true;
} }
@ -106,7 +122,10 @@ void MessageProducerThread::run()
if(!bSentFiles) if(!bSentFiles)
{ {
m_ipcChannel->send(UINT_MAX, "Use running instance!"); if(!m_ipcChannel->send(IPC_CMD_PING, IPC_FLAG_NONE, "Use running instance!"))
{
qWarning("Failed to send IPC message!");
}
} }
} }

View File

@ -36,6 +36,7 @@ class MessageProducerThread: public QThread
public: public:
MessageProducerThread(MUtils::IPCChannel *const ipcChannel); MessageProducerThread(MUtils::IPCChannel *const ipcChannel);
~MessageProducerThread(void); ~MessageProducerThread(void);
void run(); void run();
protected: protected: