Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SemaiCZE committed May 31, 2018
1 parent 6672ffd commit 7c7f0b6
Show file tree
Hide file tree
Showing 40 changed files with 139 additions and 395 deletions.
56 changes: 14 additions & 42 deletions src/archives/archivator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ void archivator::compress(const std::string &dir, const std::string &destination
}

a = archive_write_new();
if (a == NULL) {
throw archive_exception("Cannot create destination archive.");
}
if (a == NULL) { throw archive_exception("Cannot create destination archive."); }
if (archive_write_set_format_zip(a) != ARCHIVE_OK) {
throw archive_exception("Cannot set ZIP format on destination archive.");
}
Expand All @@ -63,9 +61,7 @@ void archivator::compress(const std::string &dir, const std::string &destination
archive_entry_set_perm(entry, 0644);

r = archive_write_header(a, entry);
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(a));
}
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); }

std::ifstream ifs((file.first).string(), std::ios::in | std::ios::binary);
if (ifs.is_open()) {
Expand All @@ -83,9 +79,7 @@ void archivator::compress(const std::string &dir, const std::string &destination
}

r = archive_write_data(a, buff, static_cast<size_t>(ifs.gcount()));
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(a));
}
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); }
}
} else {
throw archive_exception("Cannot open file " + (file.first).string() + " for reading.");
Expand Down Expand Up @@ -119,19 +113,15 @@ void archivator::decompress(const std::string &filename, const std::string &dest
flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT;

a = archive_read_new();
if (a == NULL) {
throw archive_exception("Cannot create source archive.");
}
if (a == NULL) { throw archive_exception("Cannot create source archive."); }
if (archive_read_support_format_all(a) != ARCHIVE_OK) {
throw archive_exception("Cannot set formats for source archive.");
}
if (archive_read_support_compression_all(a) != ARCHIVE_OK) {
throw archive_exception("Cannot set compression methods for source archive.");
}
ext = archive_write_disk_new();
if (ext == NULL) {
throw archive_exception("Cannot allocate archive entry.");
}
if (ext == NULL) { throw archive_exception("Cannot allocate archive entry."); }
if (archive_write_disk_set_options(ext, flags) != ARCHIVE_OK) {
throw archive_exception("Cannot set options for writing to disk.");
}
Expand All @@ -140,18 +130,12 @@ void archivator::decompress(const std::string &filename, const std::string &dest
}

r = archive_read_open_filename(a, filename.c_str(), 10240);
if (r < ARCHIVE_OK) {
throw archive_exception("Cannot open source archive.");
}
if (r < ARCHIVE_OK) { throw archive_exception("Cannot open source archive."); }

while (true) {
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF) {
break;
}
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(a));
}
if (r == ARCHIVE_EOF) { break; }
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(a)); }

const char *current_file = archive_entry_pathname(entry);
const std::string full_path = (fs::path(destination) / current_file).string();
Expand All @@ -166,18 +150,12 @@ void archivator::decompress(const std::string &filename, const std::string &dest
}

r = archive_write_header(ext, entry);
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(ext));
}
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ext)); }

if (archive_entry_size(entry) > 0) {
copy_data(a, ext);
}
if (archive_entry_size(entry) > 0) { copy_data(a, ext); }

r = archive_write_finish_entry(ext);
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(ext));
}
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ext)); }
}

archive_read_close(a);
Expand All @@ -196,15 +174,9 @@ void archivator::copy_data(archive *ar, archive *aw)

while (true) {
r = archive_read_data_block(ar, &buff, &size, &offset);
if (r == ARCHIVE_EOF) {
break;
}
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(ar));
}
if (r == ARCHIVE_EOF) { break; }
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(ar)); }
r = archive_write_data_block(aw, buff, size, offset);
if (r < ARCHIVE_OK) {
throw archive_exception(archive_error_string(aw));
}
if (r < ARCHIVE_OK) { throw archive_exception(archive_error_string(aw)); }
}
}
40 changes: 10 additions & 30 deletions src/broker_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ template <typename proxy> class broker_connection
const worker_config::header_map_t &headers = config_->get_headers();
std::vector<std::string> msg = {"init", config_->get_hwgroup()};

for (auto &it : headers) {
msg.push_back(it.first + "=" + it.second);
}
for (auto &it : headers) { msg.push_back(it.first + "=" + it.second); }
msg.push_back("");
msg.push_back("description=" + config_->get_worker_description());
if (!current_job_.empty()) {
msg.push_back("current_job=" + current_job_);
}
if (!current_job_.empty()) { msg.push_back("current_job=" + current_job_); }

socket_->send_broker(msg);
}
Expand All @@ -73,9 +69,7 @@ template <typename proxy> class broker_connection
std::this_thread::sleep_for(reconnect_delay);

std::chrono::seconds max_reconnect_delay(32);
if (reconnect_delay < max_reconnect_delay) {
reconnect_delay *= 2;
}
if (reconnect_delay < max_reconnect_delay) { reconnect_delay *= 2; }
}

/**
Expand All @@ -97,9 +91,7 @@ template <typename proxy> class broker_connection
std::shared_ptr<spdlog::logger> logger = nullptr)
: config_(config), socket_(socket), logger_(logger), current_job_("")
{
if (logger_ == nullptr) {
logger_ = helpers::create_null_logger();
}
if (logger_ == nullptr) { logger_ = helpers::create_null_logger(); }

// prepare dependent context for commands (in this class)
broker_connection_context<proxy> dependent_context = {socket_, config_, current_job_};
Expand Down Expand Up @@ -160,47 +152,35 @@ template <typename proxy> class broker_connection
poll_limit -= poll_duration;
}

if (terminate) {
break;
}
if (terminate) { break; }

if (result.test(message_origin::BROKER)) {
broker_liveness = config_->get_max_broker_liveness();
reset_reconnect_delay();

socket_->recv_broker(msg, &terminate);

if (terminate) {
break;
}
if (terminate) { break; }

if (msg.size() >= 2 && msg.at(0) == "eval") {
current_job_ = msg.at(1);
}
if (msg.size() >= 2 && msg.at(0) == "eval") { current_job_ = msg.at(1); }

broker_cmds_->call_function(msg.at(0), msg);
}

if (result.test(message_origin::JOBS)) {
socket_->recv_jobs(msg, &terminate);

if (terminate) {
break;
}
if (terminate) { break; }

if (msg.size() >= 1 && msg.at(0) == "done") {
current_job_ = "";
}
if (msg.size() >= 1 && msg.at(0) == "done") { current_job_ = ""; }

jobs_server_cmds_->call_function(msg.at(0), msg);
}

if (result.test(message_origin::PROGRESS)) {
socket_->recv_progress(msg, &terminate);

if (terminate) {
break;
}
if (terminate) { break; }

socket_->send_broker(msg);
}
Expand Down
10 changes: 3 additions & 7 deletions src/commands/broker_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ namespace broker_commands
{
std::vector<std::string> reply = {"init", context.config->get_hwgroup()};

for (auto &it : context.config->get_headers()) {
reply.push_back(it.first + "=" + it.second);
}
for (auto &it : context.config->get_headers()) { reply.push_back(it.first + "=" + it.second); }
reply.push_back("");
reply.push_back("description=" + context.config->get_worker_description());
if (!context.current_job.empty()) {
reply.push_back("current_job=" + context.current_job);
}
if (!context.current_job.empty()) { reply.push_back("current_job=" + context.current_job); }

context.sockets->send_broker(reply);
}
}
} // namespace broker_commands

#endif // RECODEX_WORKER_BROKER_COMMANDS_H
8 changes: 2 additions & 6 deletions src/commands/command_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ template <typename context_t> class command_context : public context_t
command_context(const context_t &dependent_context, std::shared_ptr<spdlog::logger> logger)
: context_t(dependent_context), logger(logger)
{
if (this->logger == nullptr) {
logger = helpers::create_null_logger();
}
if (this->logger == nullptr) { logger = helpers::create_null_logger(); }
}

/** System logger. */
Expand Down Expand Up @@ -101,9 +99,7 @@ template <typename context_t> class command_holder
void call_function(const std::string &command, const std::vector<std::string> &args)
{
auto it = functions_.find(command);
if (it != functions_.end()) {
(it->second)(args, context_);
}
if (it != functions_.end()) { (it->second)(args, context_); }
}
/**
* Register new command with a callback.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/jobs_client_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ namespace jobs_client_commands
context.logger->warn("Job-receiver: Eval command with wrong number of arguments.");
}
}
}
} // namespace jobs_client_commands

#endif // RECODEX_WORKER_JOBS_CLIENT_COMMANDS_H
2 changes: 1 addition & 1 deletion src/commands/jobs_server_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ namespace jobs_server_commands
{
context.sockets->send_broker(args);
}
}
} // namespace jobs_server_commands

#endif // RECODEX_WORKER_JOBS_SERVER_COMMANDS_H
8 changes: 2 additions & 6 deletions src/config/sandbox_limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ struct sandbox_limits {
void add_environ_vars(std::vector<std::pair<std::string, std::string>> &vars)
{
for (auto &var : vars) {
if (std::find(environ_vars.begin(), environ_vars.end(), var) != environ_vars.end()) {
continue;
}
if (std::find(environ_vars.begin(), environ_vars.end(), var) != environ_vars.end()) { continue; }
environ_vars.push_back(var);
}
}
Expand All @@ -123,9 +121,7 @@ struct sandbox_limits {
void add_bound_dirs(std::vector<std::tuple<std::string, std::string, dir_perm>> &dirs)
{
for (auto &dir : dirs) {
if (std::find(bound_dirs.begin(), bound_dirs.end(), dir) != bound_dirs.end()) {
continue;
}
if (std::find(bound_dirs.begin(), bound_dirs.end(), dir) != bound_dirs.end()) { continue; }
bound_dirs.push_back(dir);
}
}
Expand Down
12 changes: 3 additions & 9 deletions src/config/worker_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@ worker_config::worker_config(const YAML::Node &config)
max_broker_liveness_ = config["max-broker-liveness"].as<size_t>();
}

if (!config["headers"].IsMap()) {
throw config_error("Headers are not a map");
}
if (!config["headers"].IsMap()) { throw config_error("Headers are not a map"); }

for (auto entry : config["headers"]) {
if (!entry.first.IsScalar()) {
throw config_error("A header key is not scalar");
}
if (!entry.first.IsScalar()) { throw config_error("A header key is not scalar"); }

if (entry.second.IsSequence()) {
for (auto value : entry.second) {
if (!value.IsScalar()) {
throw config_error("A header value is not scalar");
}
if (!value.IsScalar()) { throw config_error("A header value is not scalar"); }

headers_.insert(std::make_pair(entry.first.as<std::string>(), value.as<std::string>()));
}
Expand Down
12 changes: 3 additions & 9 deletions src/connection_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,11 @@ class connection_proxy
return;
}

if (items_[0].revents & ZMQ_POLLIN) {
result.set(message_origin::BROKER, true);
}
if (items_[0].revents & ZMQ_POLLIN) { result.set(message_origin::BROKER, true); }

if (items_[1].revents & ZMQ_POLLIN) {
result.set(message_origin::JOBS, true);
}
if (items_[1].revents & ZMQ_POLLIN) { result.set(message_origin::JOBS, true); }

if (items_[2].revents & ZMQ_POLLIN) {
result.set(message_origin::PROGRESS, true);
}
if (items_[2].revents & ZMQ_POLLIN) { result.set(message_origin::PROGRESS, true); }
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/fileman/cache_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ cache_manager::cache_manager(std::shared_ptr<spdlog::logger> logger)

cache_manager::cache_manager(const std::string &caching_dir, std::shared_ptr<spdlog::logger> logger) : logger_(logger)
{
if (logger_ == nullptr) {
logger_ = helpers::create_null_logger();
}
if (logger_ == nullptr) { logger_ = helpers::create_null_logger(); }

fs::path cache_path(caching_dir);

try {
if (!fs::is_directory(cache_path)) {
fs::create_directories(cache_path);
}
if (!fs::is_directory(cache_path)) { fs::create_directories(cache_path); }
} catch (fs::filesystem_error &e) {
auto message = "Cannot create directory " + cache_path.string() + ". Error: " + e.what();
logger_->warn(message);
Expand Down
8 changes: 2 additions & 6 deletions src/fileman/http_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ http_manager::http_manager(std::shared_ptr<spdlog::logger> logger) : logger_(log
http_manager::http_manager(const std::vector<fileman_config> &configs, std::shared_ptr<spdlog::logger> logger)
: configs_(configs), logger_(logger)
{
if (logger_ == nullptr) {
logger_ = helpers::create_null_logger();
}
if (logger_ == nullptr) { logger_ = helpers::create_null_logger(); }
}

void http_manager::get_file(const std::string &src_name, const std::string &dst_name)
Expand Down Expand Up @@ -207,9 +205,7 @@ void http_manager::put_file(const std::string &src_name, const std::string &dst_
const fileman_config *http_manager::find_config(const std::string &url) const
{
for (const auto &item : configs_) {
if (url.compare(0, item.remote_url.size(), item.remote_url) == 0) {
return &item;
}
if (url.compare(0, item.remote_url.size(), item.remote_url) == 0) { return &item; }
}

return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace helpers
/** Textual description of error. */
std::string what_;
};
}
} // namespace helpers


#endif // RECODEX_WORKER_HELPERS_CONFIG_H
Loading

0 comments on commit 7c7f0b6

Please sign in to comment.