imqttclient.h 5.38 KB
/* Copyright (C) 2019
 *
 * This file is part of the osdev components suite
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */
#ifndef OSDEV_COMPONENTS_MQTT_IMQTTCLIENT_H
#define OSDEV_COMPONENTS_MQTT_IMQTTCLIENT_H

// std
#include <chrono>
#include <functional>
#include <set>
#include <string>

// boost
#include <boost/optional.hpp>

// mlogic::client
#include "istatecallback.h"

// mlogic::mqtt
#include "connectionstatus.h"
#include "credentials.h"
#include "mqttmessage.h"
#include "token.h"

namespace osdev {
namespace components {
namespace mqtt {

/**
 * @brief Interface that describes the minimal interface that a wrapper implementation needs to have in
 * order to coorporate with the MqttClient class.
 */
class IMqttClient : public virtual IStateCallback
{
public:
    virtual ~IMqttClient() {}

    /**
     * @brief Connect to the endpoint
     * @param host The host name or ip address.
     * @param port The port to use.
     * @param credentials The credentials to use.
     */
    virtual void connect(const std::string& host, int port, const Credentials& credentials) = 0;

    /**
     * @brief Connect to the endpoint
     * @param endpoint an uri endpoint description.
     */
    virtual void connect(const std::string& endpoint) = 0;

    /**
     * @brief Disconnect the client from the broker
     */
    virtual void disconnect() = 0;

    /**
     * @brief Publish a message with a given quality of service (0, 1 or 2).
     * @param message The message to publish.
     * @param qos The quality of service to use.
     * @return The token that identifies the publication (used in the deliveryCompleteCallback).
     */
    virtual Token publish(const MqttMessage& message, int qos) = 0;

    /**
     * @brief Subscribe to a topic(filter).
     * The combination of topic and qos makes a subscription unique on a wrapper client. When a topic has overlap
     * with an existing subscription it is guaranteed that the given callback is only called once.
     * @param topic The topic to subscribe to (can have wildcards). The topic can have overlap with existing topic subscriptions.
     * @param qos The quality of service to use (0, 1 or 2).
     * @param cb The callback that is called when messages are received for this subscription.
     * @return A token that identifies the subscribe command.
     */
    virtual Token subscribe(const std::string& topic, int qos, const std::function<void(MqttMessage)>& cb) = 0;

    /**
     * @brief Unsubscribe an existing subscription.
     * The combination of topic and qos can occur on multiple wrapper clients. All subscriptions that match are unsubscribed.
     * @param topic The topic to unsubscribe.
     * @param qos The quality of service of the subscription (0, 1 or 2).
     * @return A set of unsubscribe tokens identifying the unsubscribe commands. Usually the set will contain only one item.
     */
    virtual std::set<Token> unsubscribe(const std::string& topic, int qos) = 0;

    /**
     * @brief Wait for all commands to complete.
     * @param waitFor The number of milliseconds to wait for completetion of all commands.
     * @return True when all commands have completed in time or false if not.
     */
    virtual bool waitForCompletion(std::chrono::milliseconds waitFor) const = 0;

    /**
     * @brief Wait for a single command to complete.
     * @param waitFor The number of milliseconds to wait for completetion of the command.
     * @param token The token to wait for.
     * @return True when the command has completed in time or false if not.
     * @note Non existent tokens are also reported as completed.
     */
    virtual bool waitForCompletion(std::chrono::milliseconds waitFor, const Token& token) const = 0;

    /**
     * @brief Wait for commands to complete.
     * This method enables the user to wait for a set of commands. An empty set means to wait for all commands to complete.
     * @param waitFor The number of milliseconds to wait for completetion of all commands.
     * @param tokens The tokens to wait for. An empty set means to wait for all commands to complete.
     * @return True when the commands have completed in time or false if not.
     * @note Non existent tokens are also reported as completed.
     */
    virtual bool waitForCompletion(std::chrono::milliseconds waitFor, const std::set<Token>& tokens) const = 0;

    /**
     * @brief Get the result of a command.
     * @param token The token identifying the result.
     * @return The command result when available.
     */
    virtual boost::optional<bool> commandResult(const Token& token) const = 0;

    /**
     * @return The endpoint uri.
     */
    virtual std::string endpoint() const = 0;
};

}       // End namespace mqtt
}       // End namespace components
}       // End namespace osdev

#endif  // OSDEV_COMPONENTS_MQTT_IMQTTCLIENT_H