/*MT*

    MediaTomb - http://www.mediatomb.cc/

    process_io_handler.h - this file is part of MediaTomb.

    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>

    Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>,
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
                            Leonhard Wimmer <leo@mediatomb.cc>

    Copyright (C) 2016-2026 Gerbera Contributors

    MediaTomb is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    MediaTomb is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    version 2 along with MediaTomb; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

    $Id$
*/

/// @file iohandler/process_io_handler.h
/// @brief Definition of the ProcessIOHandler class.
#ifndef __PROCESS_IO_HANDLER_H__
#define __PROCESS_IO_HANDLER_H__

#include "io_handler.h"
#include "util/executor.h"
#include "util/grb_fs.h"

#include <mutex>

// forward declaration
class Content;

class ProcListItem {
public:
    explicit ProcListItem(std::shared_ptr<Executor> exec, bool abortOnDeath = false);

    ~ProcListItem() = default;
    ProcListItem(const ProcListItem&) = delete;
    ProcListItem& operator=(const ProcListItem&) = delete;
    ProcListItem(ProcListItem&&) = default;
    ProcListItem& operator=(ProcListItem&&) = default;

    std::shared_ptr<Executor> getExecutor() const;
    bool abortOnDeath() const;

protected:
    std::shared_ptr<Executor> executor;
    bool abort;
};

/// @brief Allows the web server to read from a fifo.
class ProcessIOHandler : public IOHandler {
public:
    /// @brief Sets the filename to work with.
    /// @param content content handler instance
    /// @param filename to read the data from
    /// @param mainProc main process to observe
    /// @param timeout number of seconds to wait for data
    /// @param retryCount number of retries after timeout
    /// @param procList associated processes that will be terminated once
    /// they are no longer needed
    /// @param ignoreSeek don't throw exception when seek is called
    ProcessIOHandler(
        const std::shared_ptr<Content>& content,
        fs::path filename,
        std::shared_ptr<Executor> mainProc,
        std::chrono::seconds timeout,
        unsigned int retryCount,
        std::vector<ProcListItem> procList = {},
        bool ignoreSeek = false);
    ~ProcessIOHandler() override;

    ProcessIOHandler(const ProcessIOHandler&) = delete;
    ProcessIOHandler& operator=(const ProcessIOHandler&) = delete;

    /// @brief Opens file for reading (writing is not supported)
    void open(enum UpnpOpenFileMode mode) override;

    /// @brief Reads a previously opened file sequentially.
    /// @param buf Data from the file will be copied into this buffer.
    /// @param length Number of bytes to be copied into the buffer.
    grb_read_t read(std::byte* buf, std::size_t length) override;

    /// @brief Writes to a previously opened file.
    /// @param buf Data from the buffer will be written to the file.
    /// @param length Number of bytes to be written from the buffer.
    /// @return number of bytes written.
    std::size_t write(std::byte* buf, std::size_t length) override;

    /// @brief Performs seek on an open file.
    /// @param offset Number of bytes to move in the file. For seeking forwards
    /// positive values are used, for seeking backwards - negative. Offset must
    /// be positive if origin is set to SEEK_SET
    /// @param whence The position to move relative to. SEEK_CUR to move relative
    /// to current position, SEEK_END to move relative to the end of file,
    /// SEEK_SET to specify an absolute offset.
    void seek(off_t offset, int whence) override;

    /// @brief Close a previously opened file and kills the kill_pid process
    void close() override;

protected:
    std::shared_ptr<Content> content;

    /// @brief List of associated processes.
    std::vector<ProcListItem> procList;

    /// @brief Main process used for reading
    std::shared_ptr<Executor> mainProc;

    /// @brief name of the file or fifo to read the data from
    fs::path path;

    /// @brief file descriptor
    int fd {};

    /// @brief if this flag is set seek on a fifo will not return an error
    bool ignoreSeek;

    /// @brief number of seconds to wait for data
    std::chrono::seconds timeout { 2 };

    /// @brief number of retries after timeout
    unsigned int retryCount;

    std::mutex mutex;

    bool abort() const;
    void killAll() const;
    void registerAll();
    void unregisterAll();
};

#endif // __PROCESS_IO_HANDLER_H__
