swift
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"
27 #include "misc/registermetadata.h"
28 
29 using namespace swift::misc;
30 
32 int 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);
39  QTextStream out(stdout, QIODevice::WriteOnly);
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
52  const QStringList ipV4Addresses = swift::misc::network::CNetworkUtils::getKnownLocalIpV4Addresses();
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")) { mode = "testservice"; }
146  else { return 0; }
147 
148  // I know I am in the "server process here", so I can safely create a CDBusServer
149  // this runs in the original process and can be directly debugged
150  out << "--------------------------------------------------------" << Qt::endl;
151 
152  CDBusServer *dBusServer = new CDBusServer(useSessionBusForServer ? "session" : address);
153  if (dBusServer->hasQDBusServer())
154  {
155  out << "server" << dBusServer->qDBusServer()->address()
156  << " connected:" << dBusServer->qDBusServer()->isConnected() << Qt::endl;
157  }
158  // start client process
159  QStringList args;
160  args << "client";
161  args << mode;
162  if (address == "session")
163  {
164  args << "session"; // set session as cmd arg
165  }
166  else
167  {
168  args << ip;
169  args << port;
170  }
171 
172  // run tests
173  if (mode == "testservice") { swift::sample::ServiceTool::dataTransferTestServer(dBusServer, verbose); }
174 
175  // testing in new process
176  CProcessCtrl::startDetached(executable, args, true);
177 
178  // testing in same process
179  // swift::sample::ServiceTool::dataTransferTestClient(address);
180 
181  // loop
182  return a.exec();
183  }
184 }
Custom DBusServer.
Definition: dbusserver.h:34
bool hasQDBusServer() const
True if using P2P.
Definition: dbusserver.cpp:295
const QDBusServer * qDBusServer() const
DBus server (if using P2P)
Definition: dbusserver.cpp:293
static bool startDetached(const QString &program, const QStringList &arguments, bool withConsoleWindow)
Start a programm detached and without any console window.
Definition: processctrl.cpp:66
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.
Definition: servicetool.cpp:69
static void dataTransferTestServer(swift::misc::CDBusServer *dBusServer, bool verbose)
Server side of data transfer test.
Definition: servicetool.cpp:55
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
int main(int argc, char *argv[])
main
Definition: main.cpp:20