Пример Redis
This commit is contained in:
parent
8744da7759
commit
71e8dd0202
@ -50,6 +50,7 @@ add_subdirectory(src/myx/redis)
|
|||||||
if(MYXLIB_BUILD_EXAMPLES OR MYXLIB_BUILD_EXAMPLES_HO)
|
if(MYXLIB_BUILD_EXAMPLES OR MYXLIB_BUILD_EXAMPLES_HO)
|
||||||
add_subdirectory(examples/filesystem)
|
add_subdirectory(examples/filesystem)
|
||||||
add_subdirectory(examples/qt)
|
add_subdirectory(examples/qt)
|
||||||
|
add_subdirectory(examples/redis)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Документация
|
# Документация
|
||||||
|
82
examples/redis/01_client/CMakeLists.txt
Normal file
82
examples/redis/01_client/CMakeLists.txt
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
# Название основной цели в текущем каталоге
|
||||||
|
set(TRGT example-redis-client)
|
||||||
|
|
||||||
|
# Список файлов исходных текстов
|
||||||
|
set(TRGT_cpp ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
|
||||||
|
|
||||||
|
set(TRGT_moc_hpp ${CMAKE_CURRENT_SOURCE_DIR}/client.hpp)
|
||||||
|
|
||||||
|
qt5_wrap_cpp(TRGT_moc_cpp ${TRGT_moc_hpp})
|
||||||
|
|
||||||
|
if(MYXLIB_BUILD_EXAMPLES)
|
||||||
|
|
||||||
|
# Путь поиска библиотек внутри проекта
|
||||||
|
link_directories(${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
|
||||||
|
|
||||||
|
# Цель для создания исполняемого файла
|
||||||
|
add_executable(${TRGT} ${TRGT_cpp} ${TRGT_moc_cpp} ${TRGT_moc_hpp})
|
||||||
|
common_target_properties(${TRGT})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой clang-tidy
|
||||||
|
add_clang_tidy_check(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой clang-analyze
|
||||||
|
add_clang_analyze_check(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой clazy
|
||||||
|
add_clazy_check(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Создание цели для проверки утилитой pvs-studio
|
||||||
|
add_pvs_check(${TRGT})
|
||||||
|
|
||||||
|
# Создание цели для автоматического форматирования кода
|
||||||
|
add_format_sources(${TRGT} ${TRGT_cpp})
|
||||||
|
|
||||||
|
# Qt5
|
||||||
|
target_include_directories(${TRGT} PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||||
|
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
|
||||||
|
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Network_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
target_compile_definitions(${TRGT} PUBLIC MYXLIB_HEADER_ONLY)
|
||||||
|
add_dependencies(${TRGT} base_static redis_static)
|
||||||
|
|
||||||
|
target_link_libraries(${TRGT} myx-redis)
|
||||||
|
target_link_libraries(${TRGT} Qt5::Core Qt5::Network)
|
||||||
|
|
||||||
|
# Имя выходного файла для цели
|
||||||
|
set_target_properties(${TRGT} PROPERTIES OUTPUT_NAME redis-client)
|
||||||
|
|
||||||
|
add_sanitizers(${TRGT})
|
||||||
|
|
||||||
|
cotire(${TRGT})
|
||||||
|
|
||||||
|
add_dependencies(${TRGT} create_auxilary_symlinks)
|
||||||
|
|
||||||
|
# Правила для установки
|
||||||
|
install(TARGETS ${TRGT} COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(MYXLIB_BUILD_EXAMPLES_HO)
|
||||||
|
# Цель для создания исполняемого файла
|
||||||
|
add_executable(${TRGT}-ho ${TRGT_cpp} ${TRGT_qrc})
|
||||||
|
common_target_properties(${TRGT}-ho)
|
||||||
|
|
||||||
|
target_include_directories(${TRGT}-ho PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||||
|
target_include_directories(${TRGT}-ho SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
add_dependencies(${TRGT}-ho base-header-only qt-header-only)
|
||||||
|
|
||||||
|
target_link_libraries(${TRGT}-ho Qt5::Core)
|
||||||
|
target_link_libraries(${TRGT}-ho Threads::Threads)
|
||||||
|
|
||||||
|
# Имя выходного файла для цели
|
||||||
|
set_target_properties(${TRGT}-ho PROPERTIES OUTPUT_NAME qt-translators-ho)
|
||||||
|
|
||||||
|
add_sanitizers(${TRGT}-ho)
|
||||||
|
cotire(${TRGT}-ho)
|
||||||
|
|
||||||
|
add_dependencies(${TRGT}-ho create_auxilary_symlinks)
|
||||||
|
|
||||||
|
# Правила для установки
|
||||||
|
install(TARGETS ${TRGT}-ho COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
endif()
|
67
examples/redis/01_client/client.hpp
Normal file
67
examples/redis/01_client/client.hpp
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <QObject>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include <myx/redis/client.hpp>
|
||||||
|
|
||||||
|
namespace MR = myx::redis;
|
||||||
|
|
||||||
|
class RedisClient : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
MR::Client m_client;
|
||||||
|
MR::Request* m_request;
|
||||||
|
|
||||||
|
public:
|
||||||
|
RedisClient()
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
m_request = m_client.sendCommand( "GET value" );
|
||||||
|
connect(m_request, &MR::Request::reply, this, &RedisClient::slotGetValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
m_request = m_client.sendCommand( "SUBSCRIBE test" );
|
||||||
|
connect(m_request, &MR::Request::reply, this, &RedisClient::slotSubscribeTest);
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_SLOT void slotSubscribeTest( const MR::Reply& reply)
|
||||||
|
{
|
||||||
|
qDebug() << static_cast<int>(reply.type());
|
||||||
|
qDebug() << const_cast<MR::Reply&>(reply).value().toByteArray();
|
||||||
|
}
|
||||||
|
};
|
14
examples/redis/01_client/main.cpp
Normal file
14
examples/redis/01_client/main.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include "client.hpp"
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
namespace MR = myx::redis;
|
||||||
|
|
||||||
|
int main( int argc, char** argv )
|
||||||
|
{
|
||||||
|
QCoreApplication app( argc, argv );
|
||||||
|
RedisClient client;
|
||||||
|
|
||||||
|
return( QCoreApplication::exec() );
|
||||||
|
} // main
|
1
examples/redis/CMakeLists.txt
Normal file
1
examples/redis/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
add_subdirectory(01_client)
|
Loading…
Reference in New Issue
Block a user