File indexing completed on 2024-07-21 12:04:58

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef TIREXBACKEND_H
0008 #define TIREXBACKEND_H
0009 
0010 #include <QElapsedTimer>
0011 #include <QNetworkDatagram>
0012 #include <QObject>
0013 #include <QTimer>
0014 
0015 #include <netinet/in.h>
0016 
0017 class QSocketNotifier;
0018 
0019 /** A Tirex meta tile. */
0020 class TirexMetatile
0021 {
0022 public:
0023     int x = -1;
0024     int y = -1;
0025     int z = -1;
0026 };
0027 
0028 /** A render request for a meta-tile from Tirex. */
0029 class TirexMetatileRequest
0030 {
0031 public:
0032     TirexMetatile tile;
0033     QByteArray id;
0034     QByteArray map;
0035     sockaddr_in addr;
0036     socklen_t  addrSize;
0037 };
0038 
0039 
0040 /** Interface to Tirex.
0041  *  @see https://wiki.openstreetmap.org/wiki/Tirex/Backends
0042  */
0043 class TirexBackend : public QObject
0044 {
0045     Q_OBJECT
0046 public:
0047     explicit TirexBackend(QObject *parent = nullptr);
0048     ~TirexBackend();
0049 
0050     /** Report a rendered request as done to Tirex. */
0051     void tileDone(const TirexMetatileRequest &req);
0052     /** Indicate a render request failed. */
0053     void tileError(const TirexMetatileRequest &req, const QString &errMsg);
0054 
0055     /** Returns the value of the entry @p key from the corresponding map configuration file of Tirex. */
0056     QVariant configValue(const QString &key) const;
0057     /** Returns the full file name of the requested meta tile. */
0058     QString metatileFileName(const TirexMetatileRequest &req);
0059 
0060     /** Amount of rows in a single metatile. */
0061     int metatileRows() const;
0062     /** Amount of columns in a single metatile. */
0063     int metatileColumns() const;
0064 
0065     /** Writes the meta tile header structures.
0066      *  QIODevice is positioned at the end for writing the first content tile afterwards.
0067      */
0068     void writeMetatileHeader(QIODevice *io, const TirexMetatile &tile) const;
0069     /** Updates the tile offset and size at the given index.
0070      * The QIODevice seek position will not change by this.
0071      */
0072     void writeMetatileEntry(QIODevice *io, int entryIdx, int offset, int size) const;
0073 
0074 Q_SIGNALS:
0075     /** Emitted when a new tile is requested.
0076      *  Respond to this by calling tileDone or tileError.
0077      */
0078     void tileRequested(const TirexMetatileRequest &req);
0079 
0080 private:
0081     void commandReadyRead();
0082 
0083     int m_commandSocketFd = -1;
0084     QSocketNotifier *m_socketNotifier = nullptr;
0085     QTimer m_heartbeatTimer;
0086     QString m_tileDir;
0087     QElapsedTimer m_renderTime;
0088     int m_heartbeatFd = -1;
0089     bool m_recursionLock = false;
0090     static constexpr const int m_metatileRows = 8; // TODO read from config eventually
0091     static constexpr const int m_metatileCols = 8;
0092 };
0093 
0094 #endif // TIREXBACKEND_H