Skip to content

Commit

Permalink
Explicit usage of std types for more safety
Browse files Browse the repository at this point in the history
  • Loading branch information
SemaiCZE committed May 29, 2018
1 parent 0b37a8f commit 583b30c
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 54 deletions.
28 changes: 14 additions & 14 deletions src/config/broker_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ broker_config::broker_config(const YAML::Node &config)
client_address_ = config["clients"]["address"].as<std::string>();
} // no throw... can be omitted
if (config["clients"]["port"] && config["clients"]["port"].IsScalar()) {
client_port_ = config["clients"]["port"].as<uint16_t>();
client_port_ = config["clients"]["port"].as<std::uint16_t>();
} // no throw... can be omitted
}

Expand All @@ -24,16 +24,16 @@ broker_config::broker_config(const YAML::Node &config)
worker_address_ = config["workers"]["address"].as<std::string>();
} // no throw... can be omitted
if (config["workers"]["port"] && config["workers"]["port"].IsScalar()) {
worker_port_ = config["workers"]["port"].as<uint16_t>();
worker_port_ = config["workers"]["port"].as<std::uint16_t>();
} // no throw... can be omitted
if (config["workers"]["max_liveness"] && config["workers"]["max_liveness"].IsScalar()) {
max_worker_liveness_ = config["workers"]["max_liveness"].as<size_t>();
max_worker_liveness_ = config["workers"]["max_liveness"].as<std::size_t>();
} // no throw... can be omitted
if (config["workers"]["max_request_failures"] && config["workers"]["max_request_failures"].IsScalar()) {
max_request_failures_ = config["workers"]["max_request_failures"].as<size_t>();
max_request_failures_ = config["workers"]["max_request_failures"].as<std::size_t>();
} // no throw... can be omitted
if (config["workers"]["ping_interval"] && config["workers"]["ping_interval"].IsScalar()) {
worker_ping_interval_ = std::chrono::milliseconds(config["workers"]["ping_interval"].as<size_t>());
worker_ping_interval_ = std::chrono::milliseconds(config["workers"]["ping_interval"].as<std::size_t>());
} // no throw... can be omitted
}

Expand All @@ -43,7 +43,7 @@ broker_config::broker_config(const YAML::Node &config)
monitor_address_ = config["monitor"]["address"].as<std::string>();
} // no throw... can be omitted
if (config["monitor"]["port"] && config["monitor"]["port"].IsScalar()) {
monitor_port_ = config["monitor"]["port"].as<uint16_t>();
monitor_port_ = config["monitor"]["port"].as<std::uint16_t>();
} // no throw... can be omitted
}

Expand All @@ -53,7 +53,7 @@ broker_config::broker_config(const YAML::Node &config)
notifier_config_.address = config["notifier"]["address"].as<std::string>();
} // no throw... can be omitted
if (config["notifier"]["port"] && config["notifier"]["port"].IsScalar()) {
notifier_config_.port = config["notifier"]["port"].as<uint16_t>();
notifier_config_.port = config["notifier"]["port"].as<std::uint16_t>();
} // no throw... can be omitted
if (config["notifier"]["username"] && config["notifier"]["username"].IsScalar()) {
notifier_config_.username = config["notifier"]["username"].as<std::string>();
Expand All @@ -74,10 +74,10 @@ broker_config::broker_config(const YAML::Node &config)
log_config_.log_level = config["logger"]["level"].as<std::string>();
} // no throw... can be omitted
if (config["logger"]["max-size"] && config["logger"]["max-size"].IsScalar()) {
log_config_.log_file_size = config["logger"]["max-size"].as<size_t>();
log_config_.log_file_size = config["logger"]["max-size"].as<std::size_t>();
} // no throw... can be omitted
if (config["logger"]["rotations"] && config["logger"]["rotations"].IsScalar()) {
log_config_.log_files_count = config["logger"]["rotations"].as<size_t>();
log_config_.log_files_count = config["logger"]["rotations"].as<std::size_t>();
} // no throw... can be omitted
} // no throw... can be omitted
} catch (YAML::Exception &ex) {
Expand All @@ -90,7 +90,7 @@ const std::string &broker_config::get_client_address() const
return client_address_;
}

uint16_t broker_config::get_client_port() const
std::uint16_t broker_config::get_client_port() const
{
return client_port_;
}
Expand All @@ -100,7 +100,7 @@ const std::string &broker_config::get_worker_address() const
return worker_address_;
}

uint16_t broker_config::get_worker_port() const
std::uint16_t broker_config::get_worker_port() const
{
return worker_port_;
}
Expand All @@ -110,12 +110,12 @@ const std::string &broker_config::get_monitor_address() const
return monitor_address_;
}

uint16_t broker_config::get_monitor_port() const
std::uint16_t broker_config::get_monitor_port() const
{
return monitor_port_;
}

size_t broker_config::get_max_worker_liveness() const
std::size_t broker_config::get_max_worker_liveness() const
{
return max_worker_liveness_;
}
Expand All @@ -135,7 +135,7 @@ const notifier_config &broker_config::get_notifier_config() const
return notifier_config_;
}

size_t broker_config::get_max_request_failures() const
std::size_t broker_config::get_max_request_failures() const
{
return max_request_failures_;
}
Expand Down
21 changes: 11 additions & 10 deletions src/config/broker_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RECODEX_BROKER_CONFIG_H
#define RECODEX_BROKER_CONFIG_H

#include <cstdint>
#include <iostream>
#include <map>
#include <string>
Expand Down Expand Up @@ -41,7 +42,7 @@ class broker_config
* Get the port to listen for incoming tasks.
* @return Broker's port for client connections.
*/
virtual uint16_t get_client_port() const;
virtual std::uint16_t get_client_port() const;
/**
* Get IP address for worker connections.
* @return Broker's IP address for worker connections.
Expand All @@ -51,7 +52,7 @@ class broker_config
* Get the port for communication with workers.
* @return Broker's port for worker connections.
*/
virtual uint16_t get_worker_port() const;
virtual std::uint16_t get_worker_port() const;
/**
* Get IP address for monitor connections.
* @return Broker's IP address for monitor connections.
Expand All @@ -61,17 +62,17 @@ class broker_config
* Get the port for communication with monitor.
* @return Broker's port for monitor connections.
*/
virtual uint16_t get_monitor_port() const;
virtual std::uint16_t get_monitor_port() const;
/**
* Get the maximum (i.e. initial) liveness of a worker.
* @return Maximum liveness of worker.
*/
virtual size_t get_max_worker_liveness() const;
virtual std::size_t get_max_worker_liveness() const;
/**
* Get the amount of times a request can fail before it's cancelled
* @return Maximum request failure count
*/
virtual size_t get_max_request_failures() const;
virtual std::size_t get_max_request_failures() const;
/**
* Get the time (in milliseconds) expected to pass between pings from the worker.
* @return Interval between two concurrent pings.
Expand All @@ -96,18 +97,18 @@ class broker_config
/** Monitor socket address */
std::string monitor_address_ = "127.0.0.1";
/** Client socket port (from frontend) */
uint16_t client_port_ = 0;
std::uint16_t client_port_ = 0;
/** Server socket port (to workers) */
uint16_t worker_port_ = 0;
std::uint16_t worker_port_ = 0;
/** Monitor socket port */
uint16_t monitor_port_ = 7894;
std::uint16_t monitor_port_ = 7894;
/**
* Maximum (initial) liveness of a worker
* (the amount of pings the worker can miss before it's considered dead)
*/
size_t max_worker_liveness_ = 4;
std::size_t max_worker_liveness_ = 4;
/** The amount of times a request can fail before it's cancelled */
size_t max_request_failures_ = 3;
std::size_t max_request_failures_ = 3;
/** Time (in milliseconds) expected to pass between pings from the worker */
std::chrono::milliseconds worker_ping_interval_ = std::chrono::milliseconds(1000);
/** Configuration of logger */
Expand Down
4 changes: 2 additions & 2 deletions src/config/log_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ struct log_config {
/**
* File size of one rotation of log file.
*/
size_t log_file_size = 1024 * 1024;
std::size_t log_file_size = 1024 * 1024;
/**
* Number of rotations which will be kept saved.
*/
size_t log_files_count = 3;
std::size_t log_files_count = 3;

/**
* Equality operator on @ref log_config structures.
Expand Down
3 changes: 2 additions & 1 deletion src/config/notifier_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RECODEX_NOTIFIER_CONFIG_H
#define RECODEX_NOTIFIER_CONFIG_H

#include <cstdint>
#include <string>


Expand All @@ -16,7 +17,7 @@ struct notifier_config {
/**
* Port on which frontend runs.
*/
uint16_t port;
std::uint16_t port;

/**
* Username which is used in HTTP authentication.
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/broker_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ void broker_handler::process_client_eval(
}

// Parse header, save it and continue
size_t pos = it->find('=');
size_t value_size = it->size() - (pos + 1);
std::size_t pos = it->find('=');
std::size_t value_size = it->size() - (pos + 1);

headers.emplace(it->substr(0, pos), it->substr(pos + 1, value_size));
++it;
Expand Down Expand Up @@ -183,8 +183,8 @@ void broker_handler::process_worker_init(
break;
}

size_t pos = header.find('=');
size_t value_size = header.size() - (pos + 1);
std::size_t pos = header.find('=');
std::size_t value_size = header.size() - (pos + 1);

headers.emplace(header.substr(0, pos), header.substr(pos + 1, value_size));
}
Expand All @@ -211,8 +211,8 @@ void broker_handler::process_worker_init(
for (; message_it != std::end(message); ++message_it) {
auto &header = *message_it;

size_t pos = header.find('=');
size_t value_size = header.size() - (pos + 1);
std::size_t pos = header.find('=');
std::size_t value_size = header.size() - (pos + 1);
auto key = header.substr(0, pos);
auto value = header.substr(pos + 1, value_size);

Expand Down
5 changes: 3 additions & 2 deletions src/helpers/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ std::string helpers::get_http_query(const curl_params &params)
* @param str result string in which return body will be stored
* @return length of data which were written into str
*/
static size_t string_write_wrapper(void *ptr, size_t size, size_t nmemb, std::string *str)
static std::size_t string_write_wrapper(void *ptr, std::size_t size, std::size_t nmemb, std::string *str)
{
size_t length = size * nmemb;
std::size_t length = size * nmemb;

std::copy((char *) ptr, (char *) ptr + length, std::back_inserter(*str));

Expand All @@ -51,6 +51,7 @@ std::string helpers::curl_get(const std::string &url,
CURLcode res;

// get curl handle
// std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl = {curl_easy_init(), curl_easy_cleanup};
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl = {curl_easy_init(), curl_easy_cleanup};
if (curl.get()) {
// destination address
Expand Down
4 changes: 2 additions & 2 deletions src/queuing/multi_queue_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ request_ptr multi_queue_manager::worker_cancelled(worker_ptr worker)
return request;
}

size_t multi_queue_manager::get_queued_request_count()
std::size_t multi_queue_manager::get_queued_request_count()
{
size_t result = 0;
std::size_t result = 0;

for (auto &pair : queues_) {
result += pair.second.size();
Expand Down
2 changes: 1 addition & 1 deletion src/queuing/multi_queue_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class multi_queue_manager : public queue_manager_interface
request_ptr assign_request(worker_ptr worker) override;
std::shared_ptr<std::vector<request_ptr>> worker_terminated(worker_ptr) override;
enqueue_result enqueue_request(request_ptr request) override;
size_t get_queued_request_count() override;
std::size_t get_queued_request_count() override;
request_ptr get_current_request(worker_ptr worker) override;
request_ptr worker_finished(worker_ptr worker) override;
request_ptr worker_cancelled(worker_ptr worker) override;
Expand Down
2 changes: 1 addition & 1 deletion src/queuing/queue_manager_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class queue_manager_interface
/**
* Get the total amount of queued requests
*/
virtual size_t get_queued_request_count() = 0;
virtual std::size_t get_queued_request_count() = 0;

/**
* Get the request currently being processed by given worker
Expand Down
2 changes: 1 addition & 1 deletion src/reactor/reactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void reactor::start_loop()

auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(time_after_poll - time_before_poll);

size_t i = 0;
std::size_t i = 0;
for (auto item : pollitems) {
if (item.revents & ZMQ_POLLIN) {
message_container received_msg;
Expand Down
11 changes: 6 additions & 5 deletions src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class multiple_string_matcher : public header_matcher

bool multiple_string_matcher::match(const std::string &value)
{
size_t offset = 0;
std::size_t offset = 0;

while (offset < value.size()) {
size_t end = value.find(delimiter, offset);
std::size_t end = value.find(delimiter, offset);

if (end == std::string::npos) {
end = value.size();
Expand All @@ -64,12 +64,12 @@ class count_matcher : public header_matcher
{
private:
/** Preset number to be base for comparison. */
size_t my_count_;
std::size_t my_count_;

public:
/**
* Constructor.
* @param my_value Base value for comparison. Will be converted to @a size_t type using std::stoul.
* @param my_value Base value for comparison. Will be converted to @a std::size_t type using std::stoul.
*/
count_matcher(std::string my_value) : header_matcher(my_value), my_count_(std::stoul(my_value))
{
Expand All @@ -80,7 +80,8 @@ class count_matcher : public header_matcher

/**
* Check if given value is >= to inner preset value.
* @param value Value to be checked. Before comparison, the value is converted to @a size_t type using std::stoul.
* @param value Value to be checked. Before comparison, the value is converted to @a std::size_t type using
* std::stoul.
* @return @a true if value matches, @a false otherwise.
*/
bool match(const std::string &value) override;
Expand Down
4 changes: 2 additions & 2 deletions src/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct request {
const job_request_data data;

/** The amount of failed attempts at processing this request. */
size_t failure_count = 0;
std::size_t failure_count = 0;

/**
* Constructor with initialization.
Expand Down Expand Up @@ -163,7 +163,7 @@ class worker
const std::string hwgroup;

/** The amount of pings the worker can miss before it's considered dead. */
size_t liveness;
std::size_t liveness;

/**
* @param id Worker unique identifier.
Expand Down
10 changes: 6 additions & 4 deletions tests/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#ifndef RECODEX_BROKER_TESTS_MOCKS_H
#define RECODEX_BROKER_TESTS_MOCKS_H

#include <cstdint>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>

#include "../src/broker_connect.h"
#include "../src/worker.h"
Expand Down Expand Up @@ -40,13 +42,13 @@ class mock_broker_config : public broker_config
}

MOCK_CONST_METHOD0(get_client_address, const std::string &());
MOCK_CONST_METHOD0(get_client_port, uint16_t());
MOCK_CONST_METHOD0(get_client_port, std::uint16_t());
MOCK_CONST_METHOD0(get_worker_address, const std::string &());
MOCK_CONST_METHOD0(get_worker_port, uint16_t());
MOCK_CONST_METHOD0(get_worker_port, std::uint16_t());
MOCK_CONST_METHOD0(get_monitor_address, const std::string &());
MOCK_CONST_METHOD0(get_monitor_port, uint16_t());
MOCK_CONST_METHOD0(get_monitor_port, std::uint16_t());
MOCK_CONST_METHOD0(get_worker_ping_interval, std::chrono::milliseconds());
MOCK_CONST_METHOD0(get_max_request_failures, size_t());
MOCK_CONST_METHOD0(get_max_request_failures, std::size_t());
};

class mock_worker_registry : public worker_registry
Expand Down
Loading

0 comments on commit 583b30c

Please sign in to comment.