Skip to content

Commit

Permalink
Merge pull request #251 from vktr/f/app-init
Browse files Browse the repository at this point in the history
Use application initializer
  • Loading branch information
vktr committed Apr 27, 2016
2 parents 5720d56 + a84b196 commit b050277
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 33 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ endif()

set(PICOTORRENT_SOURCES
src/client/application
src/client/application_initializer
src/client/command_line
src/client/configuration
src/client/environment
Expand Down
15 changes: 15 additions & 0 deletions include/picotorrent/client/application_initializer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

namespace picotorrent
{
namespace client
{
class application_initializer
{
public:
void create_application_paths();
void generate_websocket_access_token();
void generate_websocket_certificate();
};
}
}
8 changes: 8 additions & 0 deletions src/client/application.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <picotorrent/client/application.hpp>

#include <picotorrent/client/application_initializer.hpp>
#include <picotorrent/client/command_line.hpp>
#include <picotorrent/client/message_loop.hpp>
#include <picotorrent/client/controllers/add_magnet_link_controller.hpp>
Expand Down Expand Up @@ -31,6 +32,7 @@ namespace controllers = picotorrent::client::controllers;
namespace core = picotorrent::core;
namespace ui = picotorrent::client::ui;
using picotorrent::client::application;
using picotorrent::client::application_initializer;
using picotorrent::client::command_line;
using picotorrent::client::configuration;
using picotorrent::client::logging::log;
Expand Down Expand Up @@ -80,6 +82,12 @@ bool application::init()
return false;
}

// Generate required configuration
application_initializer app_init;
app_init.create_application_paths();
app_init.generate_websocket_access_token();
app_init.generate_websocket_certificate();

sess_ = std::make_shared<core::session>(configuration::instance().session_configuration());
main_window_ = std::make_shared<ui::main_window>(sess_);
ws_server_ = std::make_shared<websocket_server>(sess_);
Expand Down
60 changes: 60 additions & 0 deletions src/client/application_initializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <picotorrent/client/application_initializer.hpp>

#include <fstream>
#include <string>
#include <vector>

#include <picotorrent/client/configuration.hpp>
#include <picotorrent/client/environment.hpp>
#include <picotorrent/client/security/certificate_manager.hpp>
#include <picotorrent/client/security/random_string_generator.hpp>
#include <picotorrent/core/pal.hpp>

#define DEFAULT_ACCESS_TOKEN_SIZE 20

using picotorrent::client::application_initializer;
using picotorrent::client::configuration;
using picotorrent::client::environment;
using picotorrent::client::security::certificate_manager;
using picotorrent::client::security::random_string_generator;
using picotorrent::core::pal;

void application_initializer::create_application_paths()
{
auto create_if_not_exists = [](const std::string &path)
{
if (!pal::directory_exists(path))
{
pal::create_directories(path);
}
};

create_if_not_exists(environment::get_data_path());
create_if_not_exists(pal::combine_paths(environment::get_data_path(), "Torrents"));
}

void application_initializer::generate_websocket_access_token()
{
configuration &cfg = configuration::instance();
std::string access_token = cfg.websocket_access_token();

if (access_token.empty())
{
random_string_generator rsg;
std::string random_token = rsg.generate(DEFAULT_ACCESS_TOKEN_SIZE);
cfg.set_websocket_access_token(random_token);
}
}

void application_initializer::generate_websocket_certificate()
{
configuration &cfg = configuration::instance();
std::string certificate_file = cfg.websocket_certificate_file();

if (!pal::file_exists(certificate_file))
{
auto v = certificate_manager::generate();
std::ofstream co(certificate_file, std::ios::binary);
co.write(&v[0], v.size());
}
}
12 changes: 0 additions & 12 deletions src/client/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,6 @@ void configuration::set<int>(const char *name, int value)
void configuration::load()
{
std::string data_path = environment::get_data_path();

if (!pal::directory_exists(data_path))
{
pal::create_directories(data_path);
}

std::string config_file = pal::combine_paths(data_path, "PicoTorrent.json");

if (!pal::file_exists(config_file))
Expand All @@ -422,12 +416,6 @@ void configuration::save()
std::string json = v.serialize(true);

std::string data_path = environment::get_data_path();

if (!pal::directory_exists(data_path))
{
pal::create_directories(data_path);
}

std::string config_file = pal::combine_paths(data_path, "PicoTorrent.json");

std::ofstream output(config_file, std::ios::binary);
Expand Down
21 changes: 0 additions & 21 deletions src/client/ws/websocket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <fstream>

#include <picotorrent/client/configuration.hpp>
#include <picotorrent/client/security/certificate_manager.hpp>
#include <picotorrent/client/security/dh_params.hpp>
#include <picotorrent/client/security/random_string_generator.hpp>
#include <picotorrent/client/ws/messages/pico_state_message.hpp>
#include <picotorrent/client/ws/messages/torrent_added_message.hpp>
#include <picotorrent/client/ws/messages/torrent_finished_message.hpp>
Expand All @@ -18,15 +16,11 @@

#pragma warning(disable : 4503)

#define DEFAULT_TOKEN_SIZE 20

namespace ssl = boost::asio::ssl;

namespace msg = picotorrent::client::ws::messages;
using picotorrent::client::configuration;
using picotorrent::client::security::certificate_manager;
using picotorrent::client::security::dh_params;
using picotorrent::client::security::random_string_generator;
using picotorrent::client::ws::websocket_server;
using picotorrent::core::pal;
using picotorrent::core::session;
Expand All @@ -51,21 +45,6 @@ websocket_server::websocket_server(const std::shared_ptr<session> &session)
configuration &cfg = configuration::instance();
certificate_file_ = cfg.websocket_certificate_file();
configured_token_ = cfg.websocket_access_token();

if (configured_token_.empty())
{
random_string_generator rsg;
configured_token_ = rsg.generate(DEFAULT_TOKEN_SIZE);
cfg.set_websocket_access_token(configured_token_);
}

if (!pal::file_exists(certificate_file_))
{
// Create an automatically generated SSL certificate.
auto v = certificate_manager::generate();
std::ofstream co(certificate_file_, std::ios::binary);
co.write(&v[0], v.size());
}
}

websocket_server::~websocket_server()
Expand Down

0 comments on commit b050277

Please sign in to comment.