aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/QtRemoteObjects/cpp_interop/cpp_interop.cpp
blob: 8cef3c795f894e1661cf557d6783eccb922c4301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2025 Ford Motor Company
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtCore/qcoreapplication.h>
#include <QtCore/qsocketnotifier.h>
#include <QtCore/qtimer.h>

#include <QtRemoteObjects/qremoteobjectreplica.h>
#include <QtRemoteObjects/qremoteobjectnode.h>

#ifdef Q_OS_WIN
#  include <QtCore/qt_windows.h>
#  include <QtCore/qwineventnotifier.h>
#endif // Q_OS_WIN

#include <iostream>

using namespace Qt::StringLiterals;

class CommandReader : public QObject
{
    Q_OBJECT
public:
    explicit CommandReader(QObject *parent = nullptr) : QObject(parent)
    {
#ifndef Q_OS_WIN
        auto *notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read, this);
        connect(notifier, &QSocketNotifier::activated, this, &CommandReader::handleInput);
#else
        // FIXME: Does not work, signals triggers too often, the app is stuck in getline()
        auto notifier = new QWinEventNotifier(GetStdHandle(STD_INPUT_HANDLE), this);
        connect(notifier, &QWinEventNotifier::activated, this, &CommandReader::handleInput);
#endif
    }

signals:
    void started();

private slots:
    void handleInput()
    {
        std::string line;
        if (!std::getline(std::cin, line))
            return;

        if (line == "quit") {
            std::cerr << "harness: Received quit. Stopping harness event loop.\n";
            QCoreApplication::quit();
        } else if (line == "start") {
            std::cerr << "harness: Received start. Initializing harness nodes.\n";
            emit started();
        } else {
            std::cerr << "harness: Unknown command \"" << line << "\"\n";
        }
    }
};

class Runner : public QObject
{
    Q_OBJECT
public:
    Runner(const QUrl &url, const QString &repName, QObject *parent = nullptr)
        : QObject(parent)
        , m_url(url)
        , m_repName(repName)
    {
        m_host.setObjectName("cpp_host");
        if (!m_host.setHostUrl(QUrl("tcp://127.0.0.1:0"_L1))) {
            qWarning() << "harness: setHostUrl failed: " << m_host.lastError() << m_host.hostUrl();
            std::cerr << "harness: Fatal harness error.\n";
            QCoreApplication::exit(-2);
        }

        m_node.setObjectName("cpp_node");
        std::cout << "harness: Host url:" << m_host.hostUrl().toEncoded().constData() << '\n';
        std::cout.flush();
    }

public slots:
    void onStart()
    {
        m_node.connectToNode(m_url);
        m_replica.reset(m_node.acquireDynamic(m_repName));
        if (!m_replica->waitForSource(1000)) {
            std::cerr << "harness: Failed to acquire replica.\n";
            QCoreApplication::exit(-1);
        }

        m_host.enableRemoting(m_replica.get());
    }

private:
    QUrl m_url;
    QString m_repName;
    QRemoteObjectHost m_host;
    QRemoteObjectNode m_node;
    std::unique_ptr<QRemoteObjectDynamicReplica> m_replica;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if (argc < 3) {
        std::cerr << "Usage: " << argv[0] << " <url> <name of type>\n";
        return -1;
    }
    QUrl url = QUrl::fromUserInput(QString::fromUtf8(argv[1]));
    QString repName = QString::fromUtf8(argv[2]);

    if (!url.isValid()) {
        std::cerr << "Invalid URL: " << argv[1] << '\n';
        return -1;
    }

    CommandReader reader;
    Runner runner(url, repName);


    QRemoteObjectNode node;
    node.setObjectName("cpp_node");
    std::unique_ptr<QRemoteObjectDynamicReplica> replica;

    QObject::connect(&reader, &CommandReader::started, &runner, &Runner::onStart);

    return QCoreApplication::exec();
}

#include "cpp_interop.moc"