Files
pilotclient/samples/blackmiscdbus/main.cpp
Roland Winklmeier cf2c0d0f35 Use qt.conf to override plugin path on Mac OS
Summary:
QCoreApplication::addLibraryPath is called before QCoreApplication
was constructed and this caused the returned string to be different
depending from which working directory it was called and not always
the intended binary path.
Using qt.conf has a fixed prefix relative to the binary path
inside the application bundle and therefore is easier to be used
with a relative path.

Reviewers: kbasan, msutcliffe

Reviewed By: msutcliffe

Differential Revision: https://dev.swift-project.org/D18
2017-05-05 23:08:40 +01:00

197 lines
6.5 KiB
C++

/* Copyright (C) 2013
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
//! \ingroup sampleblackmiscdbus
#include "blackmisc/dbusserver.h"
#include "blackmisc/directoryutils.h"
#include "blackmisc/registermetadata.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/dbusutils.h"
#include "blackmisc/processctrl.h"
#include "servicetool.h"
#include <stdio.h>
#include <QCoreApplication>
#include <QDBusServer>
#include <QDebug>
#include <QProcess>
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <Qt>
#include <QtDebug>
using namespace BlackMisc;
//! main
int main(int argc, char *argv[])
{
// Of course the code here is containing too many lines, but as it
// is just for testing, I did not split it up
BlackMisc::registerMetadata();
QCoreApplication a(argc, argv);
QTextStream out(stdout, QIODevice::WriteOnly);
QTextStream qtin(stdin);
const bool verbose = false;
// trying to get the arguments into a list
const QStringList cmdlineArgs = QCoreApplication::arguments();
if (cmdlineArgs.length() < 1)
{
qFatal("Missing name of executable");
return EXIT_FAILURE;
}
// some runtime settings
const QStringList ipV4Addresses = BlackMisc::Network::CNetworkUtils::getKnownLocalIpV4Addresses();
QString ip = ipV4Addresses.isEmpty() ? "192.168.0.125" : ipV4Addresses.last();
QString port = "45000";
const QString executable = QString(cmdlineArgs.at(0)); // used as command to fork myself
const bool clientFlag = cmdlineArgs.contains("client", Qt::CaseInsensitive);
bool useSessionBusForServer = false;
if (cmdlineArgs.contains("session", Qt::CaseInsensitive))
{
// session mode
useSessionBusForServer = true;
}
else
{
// TCP/IP mode
useSessionBusForServer = false;
if (cmdlineArgs.length() > 2)
{
ip = cmdlineArgs.at(cmdlineArgs.length() - 2);
port = cmdlineArgs.at(cmdlineArgs.length() - 1);
}
}
QString addressTcp = QString("tcp:host=%1,port=%2").arg(ip, port);
QString address(useSessionBusForServer ? "session" : addressTcp); // testing with real transfer
// Create a Testservice instance and register it with the session bus only if
// the service isn't already available.
if (clientFlag)
{
// 2nd Process !!! Running on the client's side
// This runs in a second process, hence cannot be directly debugged within Qt Creators
out << "Running client side " << QCoreApplication::applicationPid() << endl;
// run tests
if (cmdlineArgs.contains("testservice", Qt::CaseInsensitive))
{
BlackSample::ServiceTool::dataTransferTestClient(address);
}
// loop
return a.exec();
}
else
{
Menu:
out << "Pid: " << QCoreApplication::applicationPid() << endl;
out << "1 .. Run testservice to test data transfer" << addressTcp << endl;
out << "1sb. Run testservice via session bus" << endl;
out << "2 .. Show signatures" << endl;
out << "----- Change address / port (no validation, do before starting server)" << endl;
out << "loop Address to loopback, 127.0.0.1" << endl;
out << "ip some IP address, e.g " << ip << endl;
out << "port some port, e.g 12345" << endl;
out << "-----" << endl;
out << "x .. Bye" << endl;
QString mode = qtin.readLine().toLower().trimmed();
if (mode.startsWith("l"))
{
ip = "127.0.0.1";
addressTcp = QString("tcp:host=%1,port=%2").arg(ip, port);
goto Menu;
}
if (mode.startsWith("i"))
{
const QStringList p = mode.split(QRegExp("\\s"));
if (p.length() > 1)
{
ip = p.at(1);
addressTcp = QString("tcp:host=%1,port=%2").arg(ip, port);
}
goto Menu;
}
if (mode.startsWith("p"))
{
const QStringList p = mode.split(QRegExp("\\s"));
if (p.length() > 1)
{
port = p.at(1);
addressTcp = QString("tcp:host=%1,port=%2").arg(ip, port);
}
goto Menu;
}
if (mode.startsWith("2"))
{
out << "---------------------------------" << endl;
BlackMisc::CDBusUtils::showDBusSignatures(out);
out << "---------------------------------" << endl;
goto Menu;
}
// start DBus
address = QString(useSessionBusForServer ? "session" : addressTcp); // testing with real transfer
if (mode.contains("sb", Qt::CaseInsensitive)) address = "session";
if (mode.startsWith("1"))
{
mode = "testservice";
}
else
{
return 0;
}
// I know I am in the "server process here", so I can safely create a CDBusServer
// this runs in the original process and can be directly debugged
out << "--------------------------------------------------------" << endl;
BlackMisc::CDBusServer *dBusServer = new BlackMisc::CDBusServer(useSessionBusForServer ? "session" : address);
if (dBusServer->hasQDBusServer())
{
out << "server" << dBusServer->qDBusServer()->address() <<
" connected:" << dBusServer->qDBusServer()->isConnected() << endl;
}
// start client process
QStringList args;
args << "client";
args << mode;
if (address == "session")
{
args << "session"; // set session as cmd arg
}
else
{
args << ip;
args << port;
}
// run tests
if (mode == "testservice")
{
BlackSample::ServiceTool::dataTransferTestServer(dBusServer, verbose);
}
// testing in new process
BlackMisc::CProcessCtrl::startDetached(executable, args, true);
// testing in same process
// BlackSample::ServiceTool::dataTransferTestClient(address);
// loop
return a.exec();
}
}