Fixed Tape client/server work. Disable client/server work in test mode.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-10-03 17:16:28 +03:00
parent 7a95b52cd0
commit 693dd7a06e
3 changed files with 45 additions and 46 deletions

View File

@ -49,11 +49,7 @@ int main(int argc, char *argv[])
qt_qhash_seed.store(0); // Lock producing random attribute order in XML qt_qhash_seed.store(0); // Lock producing random attribute order in XML
MApplication app(argc, argv); MApplication app(argc, argv);
if (not app.IsTheOnly())
{
return V_EX_OK;
}
app.InitOptions(); app.InitOptions();
app.ParseCommandLine(app.arguments()); app.ParseCommandLine(SocketConnection::Client, app.arguments());
return app.IsTestMode() ? V_EX_OK : app.exec(); // single return point is always better than more return app.IsTestMode() ? V_EX_OK : app.exec(); // single return point is always better than more
} }

View File

@ -198,30 +198,6 @@ MApplication::MApplication(int &argc, char **argv)
// Setting the Application version // Setting the Application version
setApplicationVersion(APP_VERSION_STR); setApplicationVersion(APP_VERSION_STR);
setWindowIcon(QIcon(":/tapeicon/64x64/logo.png")); setWindowIcon(QIcon(":/tapeicon/64x64/logo.png"));
const QString serverName = QCoreApplication::applicationName();
QLocalSocket socket;
socket.connectToServer(serverName);
if (socket.waitForConnected(500))
{
QTextStream stream(&socket);
stream << QCoreApplication::arguments().join(";;");
stream.flush();
socket.waitForBytesWritten();
return;
}
localServer = new QLocalServer(this);
connect(localServer, &QLocalServer::newConnection, this, &MApplication::NewLocalSocketConnection);
if (!localServer->listen(serverName))
{
if (localServer->serverError() == QAbstractSocket::AddressInUseError
&& QFile::exists(localServer->serverName()))
{
QFile::remove(localServer->serverName());
localServer->listen(serverName);
}
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -311,12 +287,6 @@ bool MApplication::IsTestMode() const
return testMode; return testMode;
} }
//---------------------------------------------------------------------------------------------------------------------
bool MApplication::IsTheOnly() const
{
return (localServer != 0);
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
TMainWindow *MApplication::MainWindow() TMainWindow *MApplication::MainWindow()
{ {
@ -347,12 +317,12 @@ void MApplication::InitOptions()
OpenSettings(); OpenSettings();
qDebug()<<"Version:"<<APP_VERSION_STR; qCDebug(mApp, "Version: %s", qUtf8Printable(APP_VERSION_STR));
qDebug()<<"Build revision:"<<BUILD_REVISION; qCDebug(mApp, "Build revision: %s", BUILD_REVISION);
qDebug()<<buildCompatibilityString(); qCDebug(mApp, "%s", qUtf8Printable(buildCompatibilityString()));
qDebug()<<"Built on"<<__DATE__<<"at"<<__TIME__; qCDebug(mApp, "Built on %s at %s", __DATE__, __TIME__);
qDebug()<<"Command-line arguments:"<<this->arguments(); qCDebug(mApp, "Command-line arguments: %s", qUtf8Printable(this->arguments().join(", ")));
qDebug()<<"Process ID:"<<this->applicationPid(); qCDebug(mApp, "Process ID: %s", qUtf8Printable(QString().setNum(this->applicationPid())));
LoadTranslation(QLocale::system().name());// By default the console version uses system locale LoadTranslation(QLocale::system().name());// By default the console version uses system locale
@ -526,7 +496,7 @@ void MApplication::RetranslateTables()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MApplication::ParseCommandLine(const QStringList &arguments) void MApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments)
{ {
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Valentina's measurements editor.")); parser.setApplicationDescription(QCoreApplication::translate("main", "Valentina's measurements editor."));
@ -630,8 +600,40 @@ void MApplication::ParseCommandLine(const QStringList &arguments)
testMode = parser.isSet(testOption); testMode = parser.isSet(testOption);
if (not testMode) if (not testMode && connection == SocketConnection::Client)
{ {
const QString serverName = QCoreApplication::applicationName();
QLocalSocket socket;
socket.connectToServer(serverName);
if (socket.waitForConnected(1000))
{
qCDebug(mApp, "Connected to the server '%s'", qUtf8Printable(serverName));
QTextStream stream(&socket);
stream << QCoreApplication::arguments().join(";;");
stream.flush();
socket.waitForBytesWritten();
std::exit(V_EX_OK);
}
qCDebug(mApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
localServer = new QLocalServer(this);
connect(localServer, &QLocalServer::newConnection, this, &MApplication::NewLocalSocketConnection);
if (not localServer->listen(serverName))
{
qCDebug(mApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
if (localServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer(serverName);
if (not localServer->listen(serverName))
{
qCWarning(mApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
}
}
}
LoadTranslation(TapeSettings()->GetLocale()); LoadTranslation(TapeSettings()->GetLocale());
} }
@ -732,7 +734,7 @@ void MApplication::NewLocalSocketConnection()
const QString arg = stream.readAll(); const QString arg = stream.readAll();
if (not arg.isEmpty()) if (not arg.isEmpty())
{ {
ParseCommandLine(arg.split(";;")); ParseCommandLine(SocketConnection::Server, arg.split(";;"));
} }
delete socket; delete socket;
MainWindow()->raise(); MainWindow()->raise();

View File

@ -44,6 +44,8 @@ class QLocalServer;
#endif #endif
#define qApp (static_cast<MApplication*>(VAbstractApplication::instance())) #define qApp (static_cast<MApplication*>(VAbstractApplication::instance()))
enum class SocketConnection : bool {Client = false, Server = true};
class MApplication : public VAbstractApplication class MApplication : public VAbstractApplication
{ {
Q_OBJECT Q_OBJECT
@ -55,7 +57,6 @@ public:
virtual bool notify(QObject * receiver, QEvent * event) Q_DECL_OVERRIDE; virtual bool notify(QObject * receiver, QEvent * event) Q_DECL_OVERRIDE;
bool IsTestMode() const; bool IsTestMode() const;
bool IsTheOnly() const;
TMainWindow *MainWindow(); TMainWindow *MainWindow();
QList<TMainWindow*> MainWindows(); QList<TMainWindow*> MainWindows();
@ -77,7 +78,7 @@ public:
void RetranslateGroups(); void RetranslateGroups();
void RetranslateTables(); void RetranslateTables();
void ParseCommandLine(const QStringList &arguments); void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments);
public slots: public slots:
TMainWindow *NewMainWindow(); TMainWindow *NewMainWindow();