Закончена основа библиотеки для работы с Redis
This commit is contained in:
@ -30,7 +30,7 @@ if(MYXLIB_BUILD_EXAMPLES)
|
||||
add_pvs_check(${TRGT})
|
||||
|
||||
# Создание цели для автоматического форматирования кода
|
||||
add_format_sources(${TRGT} ${TRGT_cpp})
|
||||
add_format_sources(${TRGT} ${TRGT_cpp} ${TRGT_moc_hpp})
|
||||
|
||||
# Qt5
|
||||
target_include_directories(${TRGT} PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
@ -60,11 +60,8 @@ endif()
|
||||
if(MYXLIB_BUILD_EXAMPLES_HO)
|
||||
set(REDIS_LIB_DIR ${CMAKE_SOURCE_DIR}/src/myx/redis)
|
||||
|
||||
set(REDIS_moc_hpp
|
||||
${REDIS_LIB_DIR}/client.hpp
|
||||
${REDIS_LIB_DIR}/lexer.hpp
|
||||
${REDIS_LIB_DIR}/parser.hpp
|
||||
${REDIS_LIB_DIR}/request.hpp)
|
||||
set(REDIS_moc_hpp ${REDIS_LIB_DIR}/client.hpp ${REDIS_LIB_DIR}/lexer.hpp ${REDIS_LIB_DIR}/parser.hpp
|
||||
${REDIS_LIB_DIR}/request.hpp)
|
||||
|
||||
qt5_wrap_cpp(REDIS_moc_cpp ${REDIS_moc_hpp})
|
||||
|
||||
|
@ -7,63 +7,83 @@ namespace MR = myx::redis;
|
||||
|
||||
class RedisClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_OBJECT
|
||||
|
||||
MR::Client m_client;
|
||||
MR::Request* m_request;
|
||||
MR::Client m_client;
|
||||
MR::Client m_subscribe;
|
||||
MR::Request* m_request;
|
||||
MR::Request* m_channel;
|
||||
|
||||
public:
|
||||
RedisClient(QObject* parent = nullptr) : QObject(parent)
|
||||
{
|
||||
connect(&m_client, &MR::Client::connected, this, &RedisClient::slotConnected);
|
||||
m_client.connectToHost("127.0.0.1");
|
||||
}
|
||||
virtual ~RedisClient() {}
|
||||
|
||||
Q_SLOT void slotConnected()
|
||||
{
|
||||
m_request = m_client.sendCommand("PING");
|
||||
connect(m_request, &MR::Request::reply, this, &RedisClient::slotPong);
|
||||
}
|
||||
|
||||
Q_SLOT void slotPong( const MR::Reply& reply)
|
||||
{
|
||||
qDebug() << static_cast<int>(reply.type());
|
||||
qDebug() << const_cast<MR::Reply&>(reply).value().toString();
|
||||
m_request->disconnect();
|
||||
m_request->deleteLater();
|
||||
|
||||
m_request = m_client.sendCommand( "SET value 10" );
|
||||
connect(m_request, &MR::Request::reply, this, &RedisClient::slotSetValue);
|
||||
}
|
||||
RedisClient( QObject* parent = nullptr ) :
|
||||
QObject( parent )
|
||||
{
|
||||
connect( &m_client, &MR::Client::connected, this, &RedisClient::slotConnected );
|
||||
connect( &m_subscribe, &MR::Client::connected, this, &RedisClient::slotStartSubscribe );
|
||||
m_client.connectToHost( "127.0.0.1" );
|
||||
m_subscribe.connectToHost( "127.0.0.1" );
|
||||
}
|
||||
|
||||
|
||||
Q_SLOT void slotSetValue( const MR::Reply& reply)
|
||||
{
|
||||
qDebug() << static_cast<int>(reply.type());
|
||||
qDebug() << const_cast<MR::Reply&>(reply).value().toString();
|
||||
m_request->disconnect();
|
||||
m_request->deleteLater();
|
||||
virtual ~RedisClient() {}
|
||||
|
||||
m_request = m_client.sendCommand( "GET value" );
|
||||
connect(m_request, &MR::Request::reply, this, &RedisClient::slotGetValue);
|
||||
}
|
||||
Q_SLOT void slotStartSubscribe()
|
||||
{
|
||||
m_channel = m_subscribe.subscribeToChannel( "test" );
|
||||
connect( m_channel, &MR::Request::reply, this, &RedisClient::slotSubscribeTest );
|
||||
}
|
||||
|
||||
|
||||
Q_SLOT void slotGetValue( const MR::Reply& reply)
|
||||
{
|
||||
qDebug() << static_cast<int>(reply.type());
|
||||
qDebug() << const_cast<MR::Reply&>(reply).value().toByteArray();
|
||||
m_request->disconnect();
|
||||
m_request->deleteLater();
|
||||
Q_SLOT void slotSubscribeTest( MR::Reply reply )
|
||||
{
|
||||
qDebug() << static_cast< int >( reply.type() );
|
||||
auto& v = reply.value();
|
||||
if ( !v.canConvert< QVariantList >() ) { return; }
|
||||
|
||||
m_request = m_client.sendCommand( "SUBSCRIBE test" );
|
||||
connect(m_request, &MR::Request::reply, this, &RedisClient::slotSubscribeTest);
|
||||
}
|
||||
auto l = v.toList();
|
||||
for ( auto& a: l )
|
||||
{
|
||||
qDebug() << a.value< MR::Reply >().value().toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
Q_SLOT void slotSubscribeTest( const MR::Reply& reply)
|
||||
{
|
||||
qDebug() << static_cast<int>(reply.type());
|
||||
qDebug() << const_cast<MR::Reply&>(reply).value();
|
||||
}
|
||||
};
|
||||
|
||||
Q_SLOT void slotConnected()
|
||||
{
|
||||
m_request = m_client.sendCommand( "PING" );
|
||||
connect( m_request, &MR::Request::reply, this, &RedisClient::slotPong );
|
||||
}
|
||||
|
||||
|
||||
Q_SLOT void slotPong( MR::Reply reply )
|
||||
{
|
||||
qDebug() << static_cast< int >( reply.type() );
|
||||
qDebug() << reply.value().toString();
|
||||
m_request->disconnect();
|
||||
m_request->deleteLater();
|
||||
|
||||
m_request = m_client.sendCommand( "SET value 10" );
|
||||
connect( m_request, &MR::Request::reply, this, &RedisClient::slotSetValue );
|
||||
}
|
||||
|
||||
|
||||
Q_SLOT void slotSetValue( MR::Reply reply )
|
||||
{
|
||||
qDebug() << static_cast< int >( reply.type() );
|
||||
qDebug() << reply.value().toString();
|
||||
m_request->disconnect();
|
||||
m_request->deleteLater();
|
||||
|
||||
m_request = m_client.sendCommand( "GET value" );
|
||||
connect( m_request, &MR::Request::reply, this, &RedisClient::slotGetValue );
|
||||
}
|
||||
|
||||
|
||||
Q_SLOT void slotGetValue( MR::Reply reply )
|
||||
{
|
||||
qDebug() << static_cast< int >( reply.type() );
|
||||
qDebug() << reply.value().toByteArray();
|
||||
m_request->disconnect();
|
||||
m_request->deleteLater();
|
||||
}
|
||||
}; // class RedisClient
|
||||
|
Reference in New Issue
Block a user