Skip to content

Commit

Permalink
Merge pull request #252 from vktr/f/refactor-configuration
Browse files Browse the repository at this point in the history
Refactor configuration
  • Loading branch information
vktr committed Apr 27, 2016
2 parents b050277 + 367a5dc commit 0a89cec
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 121 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ set(PICOTORRENT_SOURCES
src/client/application
src/client/application_initializer
src/client/command_line

# Configuration w/ parts
src/client/configuration
src/client/configuration_part
src/client/configuration_session_part
src/client/configuration_websocket_part

src/client/environment
src/client/message_loop
src/client/string_operations
Expand Down
70 changes: 70 additions & 0 deletions docs/src/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Configuration
=============

PicoTorrent is stores its configuration in a JSON file and depending on if
PicoTorrent was installed or not the file will reside at different locations.

* If installed, the file can be found at
:file:`%APPDATA%/PicoTorrent/PicoTorrent.json`.
* If not installed, the file will be placed next to :file:`PicoTorrent.exe`.


Advanced settings
-----------------

The following is an annotated example file with all settings that are not
available for configuration from the GUI (ie. you need to edit the JSON file
directly).

.. code-block:: javascript
:linenos:
{
// If a user chooses to ignore an update, that version number is stored
// in this field.
"ignored_update": "",
// The API URL where PicoTorrent will check for the latest release.
"update_url": "https://api.github.com/repos/picotorrent/picotorrent/releases/latest",
"session": {
// The max number of simultaneous torrents to allow in 'checking'.
"active_checking": 1,
// The max number of torrents to announce to the DHT.
"active_dht_limit": 88,
// The max number of active downloads.
"active_downloads": 3,
// The max number of active torrents across states.
"active_limit": 15,
// The max number of torrents that are allowed to be *loaded* at
// any given time. PicoTorrent will at times load and unload inactive
// torrents from memory to conserve memory usage.
"active_loaded_limit": 100,
// The max number of torrents to announce to the local network over the
// local service discovery protocol.
"active_lsd_limit": 60,
// The max number of active seeds.
"active_seeds": 5,
// The max number of torrents to announce to their trackers.
"active_tracker_limit": 1600
},
"websocket": {
// The access token to use when connecting to the WebSocket API.
"access_token": "random characters",
// The full path to an SSL certificate which secures the WebSocket API.
"certificate_file": "PicoTorrent_generated.pem",
// The password for the SSL certificate (or empty string if no password).
"certificate_password": "secret"
}
}
1 change: 1 addition & 0 deletions docs/src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Rasterbar-libtorrent and the raw Win32 API.
.. toctree::
:maxdepth: 2

configuration
websocket-api/index
88 changes: 68 additions & 20 deletions include/picotorrent/client/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,78 @@ namespace client
uint32_t show;
};

class part
{
friend class configuration;

protected:
part(const std::shared_ptr<picojson::object> &cfg);

bool get_part_key_or_default(const char *part, const char* key, bool default_value);
int get_part_key_or_default(const char *part, const char* key, int default_value);
std::string get_part_key_or_default(const char *part, const char* key, const std::string &default_value);

void set_part_key(const char *part, const char* key, bool value);
void set_part_key(const char *part, const char* key, int value);
void set_part_key(const char *part, const char* key, const std::string &value);

private:
std::shared_ptr<picojson::object> cfg_;
};

struct session_part : public part
{
friend class configuration;

int active_checking();
int active_dht_limit();
int active_downloads();
int active_limit();
int active_loaded_limit();
int active_lsd_limit();
int active_seeds();
int active_tracker_limit();

int download_rate_limit();
void download_rate_limit(int limit);

int stop_tracker_timeout();

int upload_rate_limit();
void upload_rate_limit(int limit);

protected:
using part::part;
};

struct websocket_part : public part
{
friend class configuration;

std::string access_token();
void access_token(const std::string &token);

std::string certificate_file();
std::string certificate_password();
std::string cipher_list();

bool enabled();
void enabled(bool enable);

int listen_port();
void listen_port(int port);

protected:
using part::part;
};

configuration();
~configuration();

static configuration &instance();

int alert_queue_size();
std::shared_ptr<session_part> session();
std::shared_ptr<websocket_part> websocket();

close_action_t close_action();
void set_close_action(close_action_t action);
Expand All @@ -79,9 +145,6 @@ namespace client
std::string default_save_path();
void set_default_save_path(const std::string &path);

int download_rate_limit();
void set_download_rate_limit(int dl_rate);

std::string ignored_update();
void set_ignored_update(const std::string &version);

Expand Down Expand Up @@ -126,23 +189,8 @@ namespace client
start_position_t start_position();
void set_start_position(start_position_t pos);

int stop_tracker_timeout();
std::string update_url();

int upload_rate_limit();
void set_upload_rate_limit(int ul_rate);

std::string websocket_access_token();
void set_websocket_access_token(const std::string &token);

std::string websocket_certificate_file();
std::string websocket_certificate_password();
std::string websocket_cipher_list();
bool websocket_enabled();
void set_websocket_enabled(bool value);
int websocket_listen_port();
void set_websocket_listen_port(int port);

std::shared_ptr<placement> window_placement(const std::string &name);
void set_window_placement(const std::string &name, const placement &wnd);

Expand All @@ -162,7 +210,7 @@ namespace client
void load();
void save();

std::unique_ptr<picojson::object> value_;
std::shared_ptr<picojson::object> value_;
};
}
}
10 changes: 10 additions & 0 deletions include/picotorrent/core/session_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ namespace core
{
}

// Limits
int active_checking;
int active_dht_limit;
int active_downloads;
int active_limit;
int active_loaded_limit;
int active_lsd_limit;
int active_seeds;
int active_tracker_limit;

int alert_queue_size;
std::string default_save_path;
int download_rate_limit;
Expand Down
2 changes: 1 addition & 1 deletion src/client/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ int application::run(const std::wstring &args)
ShowWindow(main_window_->handle(), pos);
}

if (cfg.websocket_enabled())
if (cfg.websocket()->enabled())
{
ws_server_->start();
}
Expand Down
6 changes: 3 additions & 3 deletions src/client/application_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ void application_initializer::create_application_paths()
void application_initializer::generate_websocket_access_token()
{
configuration &cfg = configuration::instance();
std::string access_token = cfg.websocket_access_token();
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);
cfg.websocket()->access_token(random_token);
}
}

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

if (!pal::file_exists(certificate_file))
{
Expand Down

0 comments on commit 0a89cec

Please sign in to comment.