myxlib/src/myx/redis/client.hpp

114 lines
2.6 KiB
C++

#ifndef MYX_REDIS_CLIENT_HPP_
#define MYX_REDIS_CLIENT_HPP_
#include <QObject>
#include <QScopedPointer>
#include <config.hpp>
#include <request.hpp>
namespace myx {
namespace redis {
class MYX_REDIS_EXPORT ClientPrivate;
/**
* @brief Provides access to a Redis server
*/
class MYX_REDIS_EXPORT Client : public QObject
{
Q_OBJECT
public:
/**
* @brief Creates a Redis client
* @param parent the parent QObject
*/
explicit Client( QObject* parent = nullptr );
/**
* @brief Destroys the client
*/
virtual ~Client() = default;
/*
* Note: we specifically avoid an overload of connectToHost that
* uses QHostAddress since that would force anyone using the client
* library to use the QtNetwork module, which we wish to avoid.
*/
/**
* @brief Attempts to connect to the specified Redis server
* @param hostName the hostname of the Redis server
* @param port the port that the Redis server is listening on
*
* If the connection was successful, the connected() signal will be
* emitted.
*/
void connectToHost( const QString& hostName, quint16 port = 6379 );
/**
* @brief Disconnects from the Redis server
*/
void disconnectFromHost();
/**
* @brief Indicates whether the client is connected to a Redis server
* @return true if the client is connected
*/
bool isConnected() const;
/**
* @brief Sends the specified command to the Redis server
* @param command the command to execute
* @return an object representing the request
*/
Request* sendCommand( const QByteArray& command );
/**
* @brief Attempts to set the specified key to the specified value
* @param name the name of the key
* @param value the value of the key
* @return the request issued
*/
Request* set( const QByteArray& name, const QByteArray& value );
/**
* @brief Waits for the socket to finish connecting
* @param msecs the amount of time in milliseconds to wait
* @return true if the connection was completed
*
* Note: to wait indefinitely, pass a value of -1.
*/
bool waitForConnected( int msecs = 30000 );
/**
* @brief Waits for the socket to finish disconnecting
* @param msecs the amount of time in milliseconds to wait
* @return true if the disconnection was completed
*/
bool waitForDisconnected( int msecs = 30000 );
/**
* @brief Emitted when the client establishes a connection with the Redis server
*/
Q_SIGNAL void connected();
/**
* @brief Emitted when the client disconnects from the Redis server
*/
Q_SIGNAL void disconnected();
private:
const QScopedPointer< ClientPrivate > d;
}; // class MYX_REDIS_EXPORT
} // namespace redis
} // namespace myx
#endif // MYX_REDIS_CLIENT_HPP_