Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Database refactoring #125

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ matrix:
#- 7z.exe e sqlite-amalgamation-3320300.zip -oc:\sqlite\include
#- wget -q https://www.sqlite.org/2020/sqlite-dll-win64-x64-3320300.zip
#- 7z.exe e sqlite-dll-win64-x64-3320300.zip -oc:\sqlite\lib
- choco install doxygen.install
- choco install graphviz
#- choco install doxygen.install
#- choco install graphviz
cache:
directories:
- boost_1_73_0/
script:
- mkdir ${BUILD_DIR} && cd ${BUILD_DIR}
- cmake .. -G "MinGW Makefiles"
- mingw32-make
- cmake.exe .. -G "MinGW Makefiles"
- mingw32-make.exe install

- os: linux
addons:
Expand Down
29 changes: 29 additions & 0 deletions cmake/DeployQt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function(windeployqt target)
find_package(Qt5Core REQUIRED)

find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINT ${_qt_bin_dir})

if (NOT WINDEPLOYQT_EXECUTABLE)
message(FATAL_ERROR "windeployqt not found")
endif()

add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
env PATH="${_qt_bin_dir}" "${WINDEPLOYQT_EXECUTABLE}"
--verbose 0
--no-compiler-runtime
--no-translations
--no-angle
--no-opengl-sw
--dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/windeployqt"
"$<TARGET_FILE:${target}>"
COMMENT "Deploying Qt . . ."
)

if (MINGW)
get_filename_component(mingw_path ${CMAKE_CXX_COMPILER} PATH)
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS ${mingw_path}/libgcc_s_seh-1.dll ${mingw_path}/libstdc++-6.dll)
endif()

include(InstallRequiredSystemLibraries)
endfunction()
22 changes: 12 additions & 10 deletions scripts/install_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,21 @@ def MakeBoostInstaller(platform):
print("Platform: " + platform)

boost_filename = "boost_{}_{}_{}.tar.gz".format(boost_version_major, boost_version_minor, boost_version_patch)
boost_url = "https://sourceforge.net/projects/boost/files/boost/{}.{}.{}/{}/download".format(boost_version_major, boost_version_minor, boost_version_patch, boost_filename)

print("Download boost . . .")
if not os.path.isdir('boost_{}_{}_{}'.format(boost_version_major, boost_version_minor, boost_version_patch)):
boost_url = "https://sourceforge.net/projects/boost/files/boost/{}.{}.{}/{}/download".format(boost_version_major, boost_version_minor, boost_version_patch, boost_filename)

r = requests.get(boost_url, allow_redirects=True)
print("Download boost . . .")

open(boost_filename, "wb+").write(r.content)

print("Unzip boost . . .")

tar = tarfile.open(boost_filename, "r:gz")
tar.extractall()
tar.close()
r = requests.get(boost_url, allow_redirects=True)

open(boost_filename, "wb+").write(r.content)

print("Unzip boost . . .")

tar = tarfile.open(boost_filename, "r:gz")
tar.extractall()
tar.close()

print("Install boost . . .")

Expand Down
9 changes: 8 additions & 1 deletion src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ target_link_libraries (${CLIENT_NAME}
if (WIN32)
add_definitions(-DWIN32_LEAN_AND_MEAN)
target_link_libraries(${CLIENT_NAME} -lws2_32)
endif (WIN32)
set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ${CMAKE_INSTALL_PREFIX})

include(${CMAKE_SOURCE_DIR}/cmake/DeployQt.cmake)
windeployqt(${CLIENT_NAME})

install(TARGETS ${CLIENT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/windeployqt/" DESTINATION ${CMAKE_INSTALL_PREFIX})
endif()

if (NOT WIN32)
install(TARGETS ${CLIENT_NAME} RUNTIME DESTINATION bin)
Expand Down
4 changes: 4 additions & 0 deletions src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ find_library(SQLITE3_LIBRARY NAMES sqlite3)
set(STORAGE_SRC
${DIR_STORAGE}/database.h
${DIR_STORAGE}/database.cpp
#${DIR_STORAGE}/database_.h
#${DIR_STORAGE}/database_.cpp
${DIR_STORAGE}/sqlitedatabase.h
${DIR_STORAGE}/sqlitedatabase.cpp
)

set(SERVER_SOURCES
Expand Down
4 changes: 2 additions & 2 deletions src/server/channel/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <mutex>
#include <deque>
#include "iroom.h"
#include "storage/database.h"
#include "storage/sqlitedatabase.h"
#include "log/logger.h"

/**
Expand All @@ -25,7 +25,7 @@ class Channel : public IRoom
*
* @param db
*/
Channel(identifier_t room, database_ptr db) : channel_id(room)
Channel(identifier_t room, Storage::database_ptr db) : channel_id(room)
{
if (db == nullptr) {
BOOST_LOG_TRIVIAL(info) << "Failed to load history. Database pointer is nullptr.";
Expand Down
2 changes: 1 addition & 1 deletion src/server/channel/channels_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ChannelsManager::ChannelsManager()
BOOST_LOG_TRIVIAL(info) << "create ChannelsManager";
}

bool ChannelsManager::join(subscriber_ptr new_sub, identifier_t new_room_id, database_ptr db) {
bool ChannelsManager::join(subscriber_ptr new_sub, identifier_t new_room_id, Storage::database_ptr db) {
bool flag_result = true;
BOOST_LOG_TRIVIAL(info) << "ChannelsManager::join";
if (auto it=channels.find(new_room_id); it!=channels.end()) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/channel/channels_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ChannelsManager
*
* @param db
*/
bool join(subscriber_ptr, identifier_t, database_ptr db);
bool join(subscriber_ptr, identifier_t, Storage::database_ptr db);

/**
* @brief Send message to specific room and specific user
Expand Down
4 changes: 2 additions & 2 deletions src/server/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void Connection::do_text_msg(Serialize::Request new_request) {

// @todo ba::post(pool), bd into ChannelsManager
ChannelsManager::Instance().send_to_channel(msg);
db->save_text_msg(msg);
db->save_text_message(msg);
} else {
BOOST_LOG_TRIVIAL(error) << "request without text_request";
}
Expand Down Expand Up @@ -470,7 +470,7 @@ void Connection::do_read_pb_text_req(boost::system::error_code error, std::size_

// @todo ba::post(pool), bd into ChannelsManager
ChannelsManager::Instance().send_to_channel(msg);
db->save_text_msg(msg);
db->save_text_message(msg);

async_read_pb_header();
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Connection : public ISubscriber, public std::enable_shared_from_this<Conne
* @param _socket Accepted client socket.
* @param _db
*/
explicit Connection(std::shared_ptr<boost::asio::thread_pool> a_thread_pool, boost::asio::ip::tcp::socket&& _socket, database_ptr _db):
explicit Connection(std::shared_ptr<boost::asio::thread_pool> a_thread_pool, boost::asio::ip::tcp::socket&& _socket, Storage::database_ptr _db):
thread_pool(a_thread_pool),
socket(std::move(_socket)),
db(_db),
Expand Down Expand Up @@ -89,7 +89,7 @@ class Connection : public ISubscriber, public std::enable_shared_from_this<Conne
std::string login;
std::string password;

database_ptr db;
Storage::database_ptr db;
std::atomic<bool> busy;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/server/connection/connection_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class ConnectionManager
{
public:
ConnectionManager(std::shared_ptr<boost::asio::thread_pool> a_thread_pool, database_ptr n_db):
ConnectionManager(std::shared_ptr<boost::asio::thread_pool> a_thread_pool, Storage::database_ptr n_db):
thread_pool(a_thread_pool),
db(n_db)
{
Expand All @@ -37,7 +37,7 @@ class ConnectionManager
private:
std::shared_ptr<boost::asio::thread_pool> thread_pool;
std::vector<connection_ptr> pool_connections;
database_ptr db;
Storage::database_ptr db;

/**
* @brief print all connections
Expand Down
4 changes: 2 additions & 2 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Server {
* @param port - number port
* @param _db - database ptr
*/
Server(unsigned short port, database_ptr _db):
Server(unsigned short port, Storage::database_ptr _db):
endpoint(boost::asio::ip::tcp::v4(), port),
acceptor(io_service, endpoint),
// @todo thread_pool
Expand Down Expand Up @@ -50,7 +50,7 @@ class Server {
boost::asio::ip::tcp::acceptor acceptor;
std::shared_ptr<boost::asio::thread_pool> thread_pool;

database_ptr db;
Storage::database_ptr db;
ConnectionManager connect_manager;

/**
Expand Down
17 changes: 15 additions & 2 deletions src/server/startup_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,21 @@ int main(int argc, char** argv) {
if (set_parametrs(argc, argv, port)) {
BOOST_LOG_TRIVIAL(info) << "starting server v.0.7";
try {
database_ptr db_sqlite = std::make_shared<Database>();
std::unique_ptr<Server> server = std::make_unique<Server>(port, db_sqlite);
Storage::DatabaseConfiguration db_config;
db_config.FolderPath = std::string{std::getenv("HOME")} + "/AppChat/";
db_config.ConnectionString = "file://" + db_config.FolderPath + "history.db";

Storage::database_ptr db = std::make_shared<Storage::SqliteDatabase>(db_config);

if (!db->open()) {
BOOST_LOG_TRIVIAL(error) << "failed open database";
return 0;
}

db->create_table_history();
db->create_table_logins();

std::unique_ptr<Server> server = std::make_unique<Server>(port, db);
server->run();
} catch (const std::exception & ex) {
BOOST_LOG_TRIVIAL(fatal) << "Exception " << ex.what();
Expand Down
Loading