Compare commits

...

7 Commits

14 changed files with 181 additions and 202 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
CMakeLists.txt.user* CMakeLists.txt.user*
_build _build
_output _output
.cmake/
*.autosave *.autosave
files/lib/* files/lib/*
files/log/* files/log/*

View File

@ -89,10 +89,13 @@ class Paths
protected: protected:
Paths(); Paths();
~Paths() = default; ~Paths() = default;
Paths( const Paths& ) = delete; // NOLINT
Paths& operator=( const Paths& ) = delete; // NOLINT
public: public:
Paths( const Paths& ) = delete;
Paths& operator=( const Paths& ) = delete;
Paths( Paths&& ) = delete;
Paths& operator=( Paths&& ) = delete;
/** /**
* @brief getInstance * @brief getInstance
* @return Уникальный экземпляр класса Paths * @return Уникальный экземпляр класса Paths

View File

@ -30,13 +30,17 @@ class PathsMT : public Paths
{ {
PathsMT(); PathsMT();
~PathsMT() = default; ~PathsMT() = default;
PathsMT( const PathsMT& ) = delete; // NOLINT
PathsMT& operator=( const PathsMT& ) = delete; // NOLINT
static std::atomic< PathsMT* > mInstance; static std::atomic< PathsMT* > mInstance;
static std::mutex mMutex; static std::mutex mMutex;
public: public:
PathsMT( const PathsMT& ) = delete;
PathsMT& operator=( const PathsMT& ) = delete;
PathsMT( PathsMT&& ) = delete;
PathsMT& operator=( PathsMT&& ) = delete;
/** /**
* @brief getInstance * @brief getInstance
* @return Уникальный экземпляр класса PathsMT * @return Уникальный экземпляр класса PathsMT

View File

@ -7,72 +7,54 @@
#include <myx/redis/client.hpp> #include <myx/redis/client.hpp>
#endif #endif
#include <myx/redis/client_p.hpp>
namespace myx { namespace myx {
namespace redis { namespace redis {
ClientPrivate::ClientPrivate( Client* client ) : MYXLIB_INLINE Client::Client( QObject* parent ) :
lexer ( &socket ),
parser( &lexer )
{
connect( &socket, &QTcpSocket::connected, client, &Client::connected );
connect( &socket, &QTcpSocket::disconnected, client, &Client::disconnected );
connect( &parser, &Parser::reply, this, &ClientPrivate::sendReply );
}
void ClientPrivate::sendReply( const Reply& reply )
{
Q_EMIT queue.dequeue()->reply( reply );
}
Client::Client( QObject* parent ) :
QObject( parent ), QObject( parent ),
d ( new ClientPrivate( this ) ) d ( new ClientPrivate( this ) )
{ {
} }
void Client::connectToHost( const QString& hostName, quint16 port ) MYXLIB_INLINE void Client::connectToHost( const QString& hostName, quint16 port )
{ {
d->socket.connectToHost( hostName, port ); d->m_socket.connectToHost( hostName, port );
} }
void Client::disconnectFromHost() MYXLIB_INLINE void Client::disconnectFromHost()
{ {
d->socket.disconnectFromHost(); d->m_socket.disconnectFromHost();
} }
bool Client::isConnected() const MYXLIB_INLINE bool Client::isConnected() const
{ {
return( d->socket.state() == QAbstractSocket::ConnectedState ); return( d->m_socket.state() == QAbstractSocket::ConnectedState );
} }
Request* Client::sendCommand( const QByteArray& command ) MYXLIB_INLINE Request* Client::sendCommand( const QByteArray& command )
{ {
d->socket.write( command + "\r\n" ); d->m_socket.write( command + "\r\n" );
auto* request = new Request( this ); auto* request = new Request( this );
d->queue.enqueue( request ); d->m_queue.enqueue( request );
return( request ); return( request );
} }
bool Client::waitForConnected( int msecs ) MYXLIB_INLINE bool Client::waitForConnected( int msecs )
{ {
return( d->socket.waitForConnected( msecs ) ); return( d->m_socket.waitForConnected( msecs ) );
} }
bool Client::waitForDisconnected( int msecs ) MYXLIB_INLINE bool Client::waitForDisconnected( int msecs )
{ {
return( d->socket.waitForDisconnected( msecs ) ); return( d->m_socket.waitForDisconnected( msecs ) );
} }
} // namespace redis } // namespace redis

View File

@ -4,17 +4,18 @@
#pragma once #pragma once
#include <myx/base/config.hpp> #include <myx/base/config.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/parser.hpp>
#include <myx/redis/request.hpp> #include <myx/redis/request.hpp>
#include <QObject> #include <QQueue>
#include <QScopedPointer> #include <QScopedPointer>
#include <QTcpSocket>
namespace myx { namespace myx {
namespace redis { namespace redis {
QT_FORWARD_DECLARE_CLASS( ClientPrivate )
/** /**
* @brief Provides access to a Redis server * @brief Provides access to a Redis server
*/ */
@ -30,10 +31,15 @@ public:
*/ */
explicit Client( QObject* parent = nullptr ); explicit Client( QObject* parent = nullptr );
Client( const Client& ) = delete;
Client& operator=( const Client& ) = delete;
Client( Client&& ) = delete;
Client& operator=( Client&& ) = delete;
/** /**
* @brief Destroys the client * @brief Destroys the client
*/ */
virtual ~Client() = default; ~Client() override = default;
/* /*
* Note: we specifically avoid an overload of connectToHost that * Note: we specifically avoid an overload of connectToHost that
@ -105,6 +111,33 @@ public:
private: private:
class ClientPrivate : public QObject
{
Q_OBJECT
friend class Client;
explicit ClientPrivate( Client* client = nullptr ) :
m_lexer ( &m_socket ),
m_parser( &m_lexer )
{
connect( &m_socket, &QTcpSocket::connected, client, &Client::connected );
connect( &m_socket, &QTcpSocket::disconnected, client, &Client::disconnected );
connect( &m_parser, &Parser::reply, this, &ClientPrivate::sendReply );
}
QTcpSocket m_socket;
QQueue< Request* > m_queue;
Lexer m_lexer;
Parser m_parser;
Q_SLOT void sendReply( const myx::redis::Reply& reply )
{
Q_EMIT m_queue.dequeue()->reply( reply );
}
}; // class ClientPrivate
const QScopedPointer< ClientPrivate > d; const QScopedPointer< ClientPrivate > d;
}; // class Client }; // class Client
@ -117,4 +150,4 @@ private:
#endif #endif
#endif // MYX_REDIS_CLIENT_HPP_ #endif // ifndef MYX_REDIS_CLIENT_HPP_

View File

@ -1,38 +0,0 @@
#ifndef MYX_REDIS_CLIENT_P_HPP_
#define MYX_REDIS_CLIENT_P_HPP_
#include <QObject>
#include <QQueue>
#include <QTcpSocket>
#include <client.hpp>
#include <reply.hpp>
#include <request.hpp>
#include <lexer.hpp>
#include <parser.hpp>
namespace myx {
namespace redis {
class ClientPrivate : public QObject
{
Q_OBJECT
public:
ClientPrivate( Client* );
QTcpSocket socket;
QQueue< Request* > queue;
Lexer lexer;
Parser parser;
private:
Q_SLOT void sendReply(const myx::redis::Reply & );
}; // class ClientPrivate
}
}
#endif // MYX_REDIS_CLIENT_P_HPP_

View File

@ -11,10 +11,10 @@ namespace myx {
namespace redis { namespace redis {
Lexer::Lexer( QIODevice* device, QObject* parent ) : MYXLIB_INLINE Lexer::Lexer( QIODevice* device, QObject* parent ) :
QObject ( parent ), QObject ( parent ),
m_device( device ), m_device( device ),
m_state ( DoingNothing ), m_state ( kDoingNothing ),
m_crlf ( 0 ), m_crlf ( 0 ),
m_length( 0 ) m_length( 0 )
{ {
@ -22,39 +22,39 @@ Lexer::Lexer( QIODevice* device, QObject* parent ) :
} }
void Lexer::readData() MYXLIB_INLINE void Lexer::readData()
{ {
m_buffer.append( m_device->readAll() ); m_buffer.append( m_device->readAll() );
while ( true ) while ( true )
{ {
if ( ( m_state == DoingNothing ) && !readCharacter() ) if ( ( m_state == kDoingNothing ) && !readCharacter() )
{ {
break; break;
} }
switch ( m_state ) switch ( m_state )
{ {
case ReadingLength: case kReadingLength:
case ReadingUnsafeString: case kReadingUnsafeString:
if ( !readUnsafeString() ) { return; } if ( !readUnsafeString() ) { return; }
break; break;
case ReadingSafeString: case kReadingSafeString:
if ( !readSafeString() ) { return; } if ( !readSafeString() ) { return; }
break; break;
case DoingNothing: case kDoingNothing:
break; break;
} }
if ( m_state != ReadingSafeString ) if ( m_state != kReadingSafeString )
{ {
m_state = DoingNothing; m_state = kDoingNothing;
} }
} }
} // Lexer::readData } // Lexer::readData
bool Lexer::readCharacter() MYXLIB_INLINE bool Lexer::readCharacter()
{ {
if ( m_buffer.isEmpty() ) if ( m_buffer.isEmpty() )
{ {
@ -70,9 +70,9 @@ bool Lexer::readCharacter()
case '-': case '-':
case ':': case ':':
case '*': case '*':
m_state = ReadingUnsafeString; break; m_state = kReadingUnsafeString; break;
case '$': case '$':
m_state = ReadingLength; break; m_state = kReadingLength; break;
} }
Q_EMIT character( c ); Q_EMIT character( c );
@ -80,7 +80,7 @@ bool Lexer::readCharacter()
} // Lexer::readCharacter } // Lexer::readCharacter
bool Lexer::readUnsafeString() MYXLIB_INLINE bool Lexer::readUnsafeString()
{ {
m_crlf = m_buffer.indexOf( "\r\n", m_crlf ); m_crlf = m_buffer.indexOf( "\r\n", m_crlf );
if ( m_crlf == -1 ) if ( m_crlf == -1 )
@ -92,10 +92,10 @@ bool Lexer::readUnsafeString()
QString s = m_buffer.mid( 0, m_crlf ); QString s = m_buffer.mid( 0, m_crlf );
m_buffer.remove( 0, m_crlf + 2 ); m_buffer.remove( 0, m_crlf + 2 );
if ( m_state == ReadingLength ) if ( m_state == kReadingLength )
{ {
m_length = s.toInt(); m_length = s.toInt();
m_state = ReadingSafeString; m_state = kReadingSafeString;
} }
else else
{ {
@ -107,7 +107,7 @@ bool Lexer::readUnsafeString()
} // Lexer::readUnsafeString } // Lexer::readUnsafeString
bool Lexer::readSafeString() MYXLIB_INLINE bool Lexer::readSafeString()
{ {
if ( m_buffer.size() - m_length < 2 ) if ( m_buffer.size() - m_length < 2 )
{ {
@ -119,7 +119,7 @@ bool Lexer::readSafeString()
Q_EMIT safeString( d ); Q_EMIT safeString( d );
m_state = DoingNothing; m_state = kDoingNothing;
return( true ); return( true );
} }

View File

@ -17,8 +17,15 @@ class Lexer : public QObject
public: public:
Lexer( QIODevice*, QObject* = nullptr ); explicit Lexer( QIODevice*, QObject* = nullptr );
virtual ~Lexer() = default;
Lexer( const Lexer& ) = delete;
Lexer& operator=( const Lexer& ) = delete;
Lexer( Lexer&& ) = delete;
Lexer& operator=( Lexer&& ) = delete;
~Lexer() override = default;
Q_SIGNAL void character( char ); Q_SIGNAL void character( char );
Q_SIGNAL void unsafeString( const QString& ); Q_SIGNAL void unsafeString( const QString& );
@ -28,8 +35,6 @@ private:
Q_SLOT void readData(); Q_SLOT void readData();
private:
bool readCharacter(); bool readCharacter();
bool readLength(); bool readLength();
bool readUnsafeString(); bool readUnsafeString();
@ -40,10 +45,10 @@ private:
enum enum
{ {
DoingNothing, kDoingNothing,
ReadingLength, kReadingLength,
ReadingUnsafeString, kReadingUnsafeString,
ReadingSafeString kReadingSafeString
} m_state; } m_state;
int m_crlf; int m_crlf;
@ -59,4 +64,4 @@ private:
#endif #endif
#endif // MYX_REDIS_LEXER_HPP_ #endif // ifndef MYX_REDIS_LEXER_HPP_

View File

@ -11,7 +11,7 @@ namespace myx {
namespace redis { namespace redis {
Parser::Parser( Lexer* lexer, QObject* parent ) : MYXLIB_INLINE Parser::Parser( Lexer* lexer, QObject* parent ) :
QObject( parent ) QObject( parent )
{ {
connect( lexer, &Lexer::character, this, &Parser::readCharacter ); connect( lexer, &Lexer::character, this, &Parser::readCharacter );
@ -20,67 +20,67 @@ Parser::Parser( Lexer* lexer, QObject* parent ) :
} }
void Parser::readCharacter( const char c ) MYXLIB_INLINE void Parser::readCharacter( const char c )
{ {
switch ( c ) switch ( c )
{ {
case '+': case '+':
stack.append( Task( Reply::Status ) ); break; m_stack.append( Task( Reply::kStatus ) ); break;
case '-': case '-':
stack.append( Task( Reply::Error ) ); break; m_stack.append( Task( Reply::kError ) ); break;
case ':': case ':':
stack.append( Task( Reply::Integer ) ); break; m_stack.append( Task( Reply::kInteger ) ); break;
case '$': case '$':
stack.append( Task( Reply::Bulk ) ); break; m_stack.append( Task( Reply::kBulk ) ); break;
case '*': case '*':
stack.append( Task( Reply::MultiBulk ) ); break; m_stack.append( Task( Reply::kMultiBulk ) ); break;
default: default:
break; break;
} }
} }
void Parser::readUnsafeString( const QString& value ) MYXLIB_INLINE void Parser::readUnsafeString( const QString& value )
{ {
if ( tos().reply.type() == Reply::MultiBulk ) if ( tos().m_reply.type() == Reply::kMultiBulk )
{ {
tos().count = value.toInt(); tos().m_count = value.toInt();
} }
else else
{ {
tos().reply.value() = value; tos().m_reply.value() = value;
} }
descend(); descend();
} }
void Parser::readSafeString( const QByteArray& value ) MYXLIB_INLINE void Parser::readSafeString( const QByteArray& value )
{ {
tos().reply.value() = value; tos().m_reply.value() = value;
descend(); descend();
} }
void Parser::descend() MYXLIB_INLINE void Parser::descend()
{ {
while ( true ) while ( true )
{ {
if ( ( tos().reply.type() == Reply::MultiBulk ) && if ( ( tos().m_reply.type() == Reply::kMultiBulk ) &&
( tos().reply.value().toList().count() < tos().count ) ) ( tos().m_reply.value().toList().count() < tos().m_count ) )
{ {
return; return;
} }
if ( stack.count() == 1 ) if ( m_stack.count() == 1 )
{ {
auto r = stack.takeLast().reply; auto r = m_stack.takeLast().m_reply;
Q_EMIT reply( r ); Q_EMIT reply( r );
return; return;
} }
auto r = stack.takeLast().reply; auto r = m_stack.takeLast().m_reply;
tos().reply.value().toList().append( QVariant::fromValue( r ) ); tos().m_reply.value().toList().append( QVariant::fromValue( r ) );
} }
} }

View File

@ -4,8 +4,9 @@
#pragma once #pragma once
#include <myx/base/config.hpp> #include <myx/base/config.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/lexer.hpp> #include <myx/redis/lexer.hpp>
// #include <myx/redis/parser_p.hpp>
#include <myx/redis/reply.hpp>
#include <QList> #include <QList>
#include <QPair> #include <QPair>
@ -20,36 +21,41 @@ class Parser : public QObject
Q_OBJECT Q_OBJECT
public: public:
Parser( Lexer*, QObject* = nullptr ); explicit Parser( Lexer*, QObject* = nullptr );
virtual ~Parser() = default;
Parser( const Parser& ) = delete;
Parser& operator=( const Parser& ) = delete;
Parser( Parser&& ) = delete;
Parser& operator=( Parser&& ) = delete;
~Parser() override = default;
Q_SIGNAL void reply( const myx::redis::Reply& ); Q_SIGNAL void reply( const myx::redis::Reply& );
private: private:
Q_SLOT void readCharacter( const char ); Q_SLOT void readCharacter( char );
Q_SLOT void readUnsafeString( const QString& ); Q_SLOT void readUnsafeString( const QString& );
Q_SLOT void readSafeString( const QByteArray& ); Q_SLOT void readSafeString( const QByteArray& );
private:
void descend(); void descend();
class Task class Task
{ {
public: friend class Parser;
enum { Unknown = -2 }; enum { kUnknown = -2 };
Task( Reply::Type type ) : explicit Task( Reply::Type type ) :
reply( type ), m_reply( type ),
count( Unknown ) {} m_count( kUnknown ) {}
Reply reply; Reply m_reply;
int count; int m_count;
}; };
QList< Task > stack; QList< Task > m_stack;
Task& tos() { return( stack.last() ); } Task& tos() { return( m_stack.last() ); }
}; // class Parser }; // class Parser
} // namespace redis } // namespace redis
@ -60,4 +66,4 @@ public:
#include "parser-inl.hpp" #include "parser-inl.hpp"
#endif #endif
#endif // MYX_REDIS_PARSER_HPP_ #endif // ifndef MYX_REDIS_PARSER_HPP_

View File

@ -28,7 +28,7 @@ public:
* *
* This value is only set when the default constructor is used. * This value is only set when the default constructor is used.
*/ */
Invalid, kInvalid,
/** /**
* @brief A status reply * @brief A status reply
@ -36,7 +36,7 @@ public:
* The value property will contain the status message returned * The value property will contain the status message returned
* by the server as a QString. * by the server as a QString.
*/ */
Status, kStatus,
/** /**
* @brief An error reply * @brief An error reply
@ -44,7 +44,7 @@ public:
* The value property will contain the error message returned by * The value property will contain the error message returned by
* the server as a QString. * the server as a QString.
*/ */
Error, kError,
/** /**
* @brief An integer reply * @brief An integer reply
@ -52,7 +52,7 @@ public:
* The value property will contain the integer value returned by * The value property will contain the integer value returned by
* the server as a qlonglong. * the server as a qlonglong.
*/ */
Integer, kInteger,
/** /**
* @brief A bulk reply * @brief A bulk reply
@ -60,7 +60,7 @@ public:
* The value property will contain the bulk reply returned by * The value property will contain the bulk reply returned by
* the server as a QByteArray. * the server as a QByteArray.
*/ */
Bulk, kBulk,
/** /**
* @brief A multi-bulk reply * @brief A multi-bulk reply
@ -69,44 +69,43 @@ public:
* by the server as a QVariantList. Each entry in the list is of * by the server as a QVariantList. Each entry in the list is of
* type Reply. * type Reply.
*/ */
MultiBulk kMultiBulk
}; };
/** /**
* @brief Creates an empty reply * @brief Creates an empty reply
*/ */
Reply() : Reply() = default;
_type( Invalid ) {}
/** /**
* @brief Initializes the reply * @brief Initializes the reply
* @param type the type of the reply * @param type the type of the reply
*/ */
Reply( Type type ) : explicit Reply( Type type ) :
_type( type ) {} m_type( type ) {}
/** /**
* @brief Returns the type of the reply * @brief Returns the type of the reply
* @return the reply type * @return the reply type
*/ */
Type type() const { return( _type ); } Type type() const { return( m_type ); }
/** /**
* @brief Returns a reference to the value of the reply * @brief Returns a reference to the value of the reply
* @return the reply value * @return the reply value
*/ */
QVariant& value() { return( _value ); } QVariant& value() { return( m_value ); }
private: private:
Type _type; Type m_type { kInvalid };
QVariant _value; QVariant m_value;
}; // class MYX_REDIS_EXPORT Reply }; // class Reply
} // namespace redis } // namespace redis
} // namespace myx } // namespace myx
Q_DECLARE_METATYPE( myx::redis::Reply ) Q_DECLARE_METATYPE( myx::redis::Reply ) // NOLINT
#endif // MYX_REDIS_REPLY_HPP_ #endif // ifndef MYX_REDIS_REPLY_HPP_

View File

@ -8,7 +8,6 @@
#ifndef MYXLIB_HEADER_ONLY #ifndef MYXLIB_HEADER_ONLY
#include <myx/redis/request.hpp> #include <myx/redis/request.hpp>
#endif #endif
#include <myx/redis/request_p.hpp>
#include <QTimer> #include <QTimer>
@ -16,34 +15,28 @@ namespace myx {
namespace redis { namespace redis {
void RequestPrivate::quitEventLoop() MYXLIB_INLINE Request::Request( QObject* parent ) :
{
loop.exit( 1 );
}
Request::Request( QObject* parent ) :
QObject( parent ), QObject( parent ),
d ( new RequestPrivate ) d ( new Loop )
{ {
connect( this, &Request::reply, this, &Request::deleteLater ); connect( this, &Request::reply, this, &Request::deleteLater );
} }
bool Request::waitForReply( int msecs ) MYXLIB_INLINE bool Request::waitForReply( int msecs )
{ {
QTimer timer; QTimer timer;
timer.setInterval( msecs ); timer.setInterval( msecs );
timer.setSingleShot( true ); timer.setSingleShot( true );
connect( &timer, &QTimer::timeout, &d->loop, &QEventLoop::quit ); connect( &timer, &QTimer::timeout, &d->m_loop, &QEventLoop::quit );
connect( this, &Request::reply, d.data(), &RequestPrivate::quitEventLoop ); connect( this, &Request::reply, d.data(), &Loop::quitEventLoop );
/* /*
* If the timer fires, the return value will be 0. * If the timer fires, the return value will be 0.
* Otherwise, quitEventLoop() will terminate the loop with 1. * Otherwise, quitEventLoop() will terminate the loop with 1.
*/ */
return( ( d->loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) ); return( ( d->m_loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) );
} }
} // namespace redis } // namespace redis

View File

@ -6,15 +6,14 @@
#include <myx/base/config.hpp> #include <myx/base/config.hpp>
#include <myx/redis/reply.hpp> #include <myx/redis/reply.hpp>
#include <QObject> #include <QEventLoop>
#include <QScopedPointer> #include <QScopedPointer>
namespace myx { namespace myx {
namespace redis { namespace redis {
QT_FORWARD_DECLARE_CLASS( RequestPrivate );
/** /**
* @brief Represents a Redis command and its response * @brief Represents a Redis command and its response
*/ */
@ -30,10 +29,15 @@ public:
*/ */
explicit Request( QObject* parent = nullptr ); explicit Request( QObject* parent = nullptr );
Request( const Request& ) = delete;
Request& operator=( const Request& ) = delete;
Request( Request&& ) = delete;
Request& operator=( Request&& ) = delete;
/** /**
* @brief Destroys the request * @brief Destroys the request
*/ */
virtual ~Request() = default; ~Request() override = default;
/** /**
* @brief Waits for the reply to be received * @brief Waits for the reply to be received
@ -50,7 +54,19 @@ public:
private: private:
const QScopedPointer< RequestPrivate > d; class Loop : public QObject
{
Q_OBJECT
friend class Request;
explicit Loop( QObject* parent = nullptr );
QEventLoop m_loop;
Q_SLOT void quitEventLoop() { m_loop.exit( 1 ); }
};
const QScopedPointer< Loop > d;
}; // class Request }; // class Request
} // namespace redis } // namespace redis
@ -61,4 +77,4 @@ private:
#include "request-inl.hpp" #include "request-inl.hpp"
#endif #endif
#endif // MYX_REDIS_REQUEST_HPP_ #endif // ifndef MYX_REDIS_REQUEST_HPP_

View File

@ -1,25 +0,0 @@
#ifndef MYX_REDIS_REQUEST_P_HPP_
#define MYX_REDIS_REQUEST_P_HPP_
#include <QEventLoop>
#include <QObject>
namespace myx {
namespace redis {
class RequestPrivate : public QObject
{
Q_OBJECT
public:
explicit RequestPrivate( QObject* parent = nullptr );
QEventLoop loop;
Q_SLOT void quitEventLoop();
};
}
}
#endif // MYX_REDIS_REQUEST_P_HPP_