swift
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
6
7#include <stdio.h>
8
9#include <QCoreApplication>
10#include <QDBusServer>
11#include <QDebug>
12#include <QProcess>
13#include <QRegExp>
14#include <QString>
15#include <QStringList>
16#include <QTextStream>
17#include <Qt>
18#include <QtDebug>
19
20#include "servicetool.h"
21
22#include "misc/dbusserver.h"
23#include "misc/dbusutils.h"
24#include "misc/directoryutils.h"
26#include "misc/processctrl.h"
28
29using namespace swift::misc;
30
32int main(int argc, char *argv[])
33{
34 // Of course the code here is containing too many lines, but as it
35 // is just for testing, I did not split it up
36
38 QCoreApplication a(argc, argv);
40 QTextStream qtin(stdin);
41 const bool verbose = false;
42
43 // trying to get the arguments into a list
44 const QStringList cmdlineArgs = QCoreApplication::arguments();
45 if (cmdlineArgs.length() < 1)
46 {
47 qFatal("Missing name of executable");
48 return EXIT_FAILURE;
49 }
50
51 // some runtime settings
53 QString ip = ipV4Addresses.isEmpty() ? "192.168.0.125" : ipV4Addresses.last();
54 QString port = "45000";
55 const QString executable = QString(cmdlineArgs.at(0)); // used as command to fork myself
56 const bool clientFlag = cmdlineArgs.contains("client", Qt::CaseInsensitive);
57 bool useSessionBusForServer = false;
58 if (cmdlineArgs.contains("session", Qt::CaseInsensitive))
59 {
60 // session mode
61 useSessionBusForServer = true;
62 }
63 else
64 {
65 // TCP/IP mode
66 useSessionBusForServer = false;
67 if (cmdlineArgs.length() > 2)
68 {
69 ip = cmdlineArgs.at(cmdlineArgs.length() - 2);
70 port = cmdlineArgs.at(cmdlineArgs.length() - 1);
71 }
72 }
73 QString addressTcp = QStringLiteral("tcp:host=%1,port=%2").arg(ip, port);
74 QString address(useSessionBusForServer ? "session" : addressTcp); // testing with real transfer
75
76 // Create a Testservice instance and register it with the session bus only if
77 // the service isn't already available.
78 if (clientFlag)
79 {
80 // 2nd Process !!! Running on the client's side
81 // This runs in a second process, hence cannot be directly debugged within Qt Creator
82 out << "Running client side " << QCoreApplication::applicationPid() << Qt::endl;
83
84 // run tests
85 if (cmdlineArgs.contains("testservice", Qt::CaseInsensitive))
86 {
88 }
89
90 // loop
91 return a.exec();
92 }
93 else
94 {
95 Menu:
96 out << "Pid: " << QCoreApplication::applicationPid() << Qt::endl;
97 out << "1 .. Run testservice to test data transfer" << addressTcp << Qt::endl;
98 out << "1sb. Run testservice via session bus" << Qt::endl;
99 out << "2 .. Show signatures" << Qt::endl;
100 out << "----- Change address / port (no validation, do before starting server)" << Qt::endl;
101 out << "loop Address to loopback, 127.0.0.1" << Qt::endl;
102 out << "ip some IP address, e.g " << ip << Qt::endl;
103 out << "port some port, e.g 12345" << Qt::endl;
104 out << "-----" << Qt::endl;
105 out << "x .. Bye" << Qt::endl;
106 QString mode = qtin.readLine().toLower().trimmed();
107
108 if (mode.startsWith("l"))
109 {
110 ip = "127.0.0.1";
111 addressTcp = QStringLiteral("tcp:host=%1,port=%2").arg(ip, port);
112 goto Menu;
113 }
114 if (mode.startsWith("i"))
115 {
116 const QStringList p = mode.split(QRegExp("\\s"));
117 if (p.length() > 1)
118 {
119 ip = p.at(1);
120 addressTcp = QStringLiteral("tcp:host=%1,port=%2").arg(ip, port);
121 }
122 goto Menu;
123 }
124 if (mode.startsWith("p"))
125 {
126 const QStringList p = mode.split(QRegExp("\\s"));
127 if (p.length() > 1)
128 {
129 port = p.at(1);
130 addressTcp = QStringLiteral("tcp:host=%1,port=%2").arg(ip, port);
131 }
132 goto Menu;
133 }
134 if (mode.startsWith("2"))
135 {
136 out << "---------------------------------" << Qt::endl;
137 // swift::misc::CDBusUtils::showDBusSignatures(out);
138 out << "---------------------------------" << Qt::endl;
139 goto Menu;
140 }
141
142 // start DBus
143 address = QString(useSessionBusForServer ? "session" : addressTcp); // testing with real transfer
144 if (mode.contains("sb", Qt::CaseInsensitive)) address = "session";
145 if (mode.startsWith("1"))
146 {
147 mode = "testservice";
148 }
149 else
150 {
151 return 0;
152 }
153
154 // I know I am in the "server process here", so I can safely create a CDBusServer
155 // this runs in the original process and can be directly debugged
156 out << "--------------------------------------------------------" << Qt::endl;
157
158 CDBusServer *dBusServer = new CDBusServer(useSessionBusForServer ? "session" : address);
159 if (dBusServer->hasQDBusServer())
160 {
161 out << "server" << dBusServer->qDBusServer()->address()
162 << " connected:" << dBusServer->qDBusServer()->isConnected() << Qt::endl;
163 }
164 // start client process
165 QStringList args;
166 args << "client";
167 args << mode;
168 if (address == "session")
169 {
170 args << "session"; // set session as cmd arg
171 }
172 else
173 {
174 args << ip;
175 args << port;
176 }
177
178 // run tests
179 if (mode == "testservice")
180 {
182 }
183
184 // testing in new process
185 CProcessCtrl::startDetached(executable, args, true);
186
187 // testing in same process
188 // swift::sample::ServiceTool::dataTransferTestClient(address);
189
190 // loop
191 return a.exec();
192 }
193}
Custom DBusServer.
Definition dbusserver.h:34
bool hasQDBusServer() const
True if using P2P.
const QDBusServer * qDBusServer() const
DBus server (if using P2P).
static bool startDetached(const QString &program, const QStringList &arguments, bool withConsoleWindow)
Start a programm detached and without any console window.
static QStringList getKnownLocalIpV4Addresses()
Find out my IPv4 addresses including loopback, empty if not possible.
static void dataTransferTestClient(const QString &address)
Client side of data transfer test.
static void dataTransferTestServer(swift::misc::CDBusServer *dBusServer, bool verbose)
Server side of data transfer test.
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
qint64 applicationPid()
QStringList arguments()
QString address() const const
bool isConnected() const const
QList< T >::const_reference at(qsizetype i) const const
bool isEmpty() const const
T & last()
qsizetype length() const const
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
CaseInsensitive
QTextStream & endl(QTextStream &stream)
QString readLine(qint64 maxlen)
int main(int argc, char *argv[])
main
Definition main.cpp:32