Skip to content

Commit 12c6746

Browse files
committed
move files. Simplify. RFC 5737
1 parent 66a086f commit 12c6746

File tree

7 files changed

+58
-56
lines changed

7 files changed

+58
-56
lines changed
Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424

2525
/**
2626
* @brief Default port offset used when all parties are running locally.
27+
* @ingroup net-tcp
2728
*/
28-
#ifndef DEFAULT_PORT_OFFSET
29-
#define DEFAULT_PORT_OFFSET 9900
29+
#ifndef DEFAULT_LOCALHOST_PORT_OFFSET
30+
#define DEFAULT_LOCALHOST_PORT_OFFSET 9900
3031
#endif
3132

3233
namespace scl {
3334

3435
/**
3536
* @brief Connection information for a party.
37+
* @ingroup net-tcp
3638
*/
3739
struct ConnectionInfo {
3840
/**
@@ -53,6 +55,7 @@ struct ConnectionInfo {
5355

5456
/**
5557
* @brief Network configuration.
58+
* @ingroup net-tcp
5659
*
5760
* A NetworkConfig is needed whenever by network objects in order to establish
5861
* connections to other nodes.
@@ -61,45 +64,56 @@ class NetworkConfig {
6164
public:
6265
/**
6366
* @brief Load a network config from a file.
64-
* @param id the identity of this party
65-
* @param filename the filename
67+
*
68+
* This method creates a NetworkConfig from a file containing connection
69+
* information of the different parties. The file should be a CSV filed with
70+
* three columns. Each row in the file contains a party's IPv4 address and the
71+
* port to use, in that order. An example is given below for three parties
72+
*
73+
* \code{.unparsed}
74+
* $ cat 3_parties.txt
75+
* 192.0.2.1, 8000
76+
* 192.0.2.2, 5000
77+
* 192.0.2.3, 3000
78+
* $
79+
* \endcode{.unparsed}
80+
*
81+
* The order in which parties information occurs will correspond to their
82+
* identifier in the NetworkConfig that will be created.
83+
*
84+
* @code
85+
* auto nc = NetworkConfig::load(1, "3_parties.txt");
86+
* assert(nc.id() == 1);
87+
* assert(nc.networkSize() == 3);
88+
*
89+
* auto me = nc.party(1);
90+
* assert(me.hostname == "192.0.2.2");
91+
* assert(me.port == 5000);
92+
* @endcode
6693
*/
6794
static NetworkConfig load(std::size_t id, const std::string& filename);
6895

6996
/**
7097
* @brief Create a network config where all parties are running locally.
71-
*
72-
* Because different processes cannot reuse the same port, the port argument
73-
* denotes a base from which a party's actual port is computed. Specifically,
74-
* party <code>i</code> will listen on <code>port_base + i</code> and connect
75-
* (as a client) to party <code>j</code> on <code>port_base + j</code>.
76-
*
77-
* @param id the identity of this party
78-
* @param size the size of the network
79-
* @param port_base the base port
8098
*/
8199
static NetworkConfig localhost(std::size_t id,
82100
std::size_t size,
83101
std::size_t port_base);
84102

85103
/**
86104
* @brief Create a network config where all parties are running locally.
87-
* @param id the identity of this party
88-
* @param size the size of the network
89105
*/
90106
static NetworkConfig localhost(std::size_t id, std::size_t size) {
91-
return NetworkConfig::localhost(id, size, DEFAULT_PORT_OFFSET);
107+
return NetworkConfig::localhost(id, size, DEFAULT_LOCALHOST_PORT_OFFSET);
92108
};
93109

94110
/**
95111
* @brief Create a config from a list of parties.
96-
* @param id the id of the local party
97-
* @param parties a list of parties
98112
*/
99113
NetworkConfig(std::size_t id, const std::vector<ConnectionInfo>& parties)
100114
: m_id(id), m_parties(parties) {
101115
validate();
102-
};
116+
}
103117

104118
/**
105119
* @brief Gets the identity of this party.
Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,12 @@
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18-
#include "scl/net/config.h"
18+
#include "scl/net/tcp/config.h"
1919

2020
#include <fstream>
2121
#include <stdexcept>
2222
#include <string>
2323

24-
namespace {
25-
26-
void validateIdAndSize(std::size_t id, std::size_t n) {
27-
if (n == 0) {
28-
throw std::invalid_argument("n cannot be zero");
29-
}
30-
31-
if (n <= id) {
32-
throw std::invalid_argument("invalid id");
33-
}
34-
}
35-
36-
} // namespace
37-
3824
scl::NetworkConfig scl::NetworkConfig::load(std::size_t id,
3925
const std::string& filename) {
4026
std::ifstream file(filename);
@@ -46,32 +32,35 @@ scl::NetworkConfig scl::NetworkConfig::load(std::size_t id,
4632
std::string line;
4733
std::vector<ConnectionInfo> info;
4834

49-
while (std::getline(file, line)) {
50-
auto a_ = line.find(',');
51-
auto b_ = line.rfind(',');
35+
std::size_t i = 0;
5236

53-
if (a_ == std::string::npos || a_ == b_) {
37+
while (std::getline(file, line)) {
38+
auto split = line.find(',');
39+
40+
if (split == std::string::npos) {
5441
throw std::invalid_argument("invalid entry in config file");
5542
}
5643

57-
auto a = static_cast<std::string::difference_type>(a_);
58-
auto b = static_cast<std::string::difference_type>(b_);
44+
auto s = static_cast<std::string::difference_type>(split);
45+
auto host = std::string(line.begin(), line.begin() + s);
46+
auto port = std::stoul(std::string(line.begin() + s + 1, line.end()));
5947

60-
auto id = std::stoul(std::string(line.begin(), line.begin() + a));
61-
auto hostname = std::string(line.begin() + a + 1, line.begin() + b);
62-
auto port = std::stoul(std::string(line.begin() + b + 1, line.end()));
63-
info.emplace_back(ConnectionInfo{id, hostname, port});
48+
info.emplace_back(ConnectionInfo{i++, host, port});
6449
}
6550

66-
validateIdAndSize(id, info.size());
51+
if (id >= info.size()) {
52+
throw std::invalid_argument("invalid id");
53+
}
6754

6855
return NetworkConfig(id, info);
6956
}
7057

7158
scl::NetworkConfig scl::NetworkConfig::localhost(std::size_t id,
7259
std::size_t size,
7360
std::size_t port_base) {
74-
validateIdAndSize(id, size);
61+
if (id >= size) {
62+
throw std::invalid_argument("invalid id");
63+
}
7564

7665
std::vector<ConnectionInfo> info;
7766
for (std::size_t i = 0; i < size; ++i) {

test/data/3_parties.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
1,1.2.3.4,8000
2-
0,2.3.4.5,5000
3-
2,5.5.5.5,3000
1+
192.0.2.1, 8000
2+
192.0.2.2, 5000
3+
192.0.2.3, 3000

test/data/invalid_entry.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a,b
1+
abc
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#include <catch2/catch_test_macros.hpp>
1919
#include <catch2/matchers/catch_matchers_exception.hpp>
20-
#include <iostream>
2120
#include <stdexcept>
2221

2322
#include "scl/net/config.h"
@@ -31,17 +30,17 @@ TEST_CASE("Config read from file", "[net]") {
3130
REQUIRE(cfg.networkSize() == 3);
3231
REQUIRE(cfg.id() == 0);
3332
auto parties = cfg.parties();
34-
REQUIRE(parties[0].hostname == "1.2.3.4");
33+
REQUIRE(parties[0].hostname == "192.0.2.1");
3534
REQUIRE(parties[0].port == 8000);
36-
REQUIRE(parties[1].hostname == "2.3.4.5");
35+
REQUIRE(parties[1].hostname == "192.0.2.2");
3736
REQUIRE(parties[1].port == 5000);
38-
REQUIRE(parties[2].hostname == "5.5.5.5");
37+
REQUIRE(parties[2].hostname == "192.0.2.3");
3938
REQUIRE(parties[2].port == 3000);
4039

4140
std::string invalid_empty = SCL_TEST_DATA_DIR "invalid_no_entries.txt";
4241
REQUIRE_THROWS_MATCHES(NetworkConfig::load(0, invalid_empty),
4342
std::invalid_argument,
44-
Catch::Matchers::Message("n cannot be zero"));
43+
Catch::Matchers::Message("invalid id"));
4544

4645
std::string valid = SCL_TEST_DATA_DIR "3_parties.txt";
4746
REQUIRE_THROWS_MATCHES(NetworkConfig::load(4, valid),
@@ -54,7 +53,7 @@ TEST_CASE("Config read from file", "[net]") {
5453
std::invalid_argument,
5554
Catch::Matchers::Message("invalid entry in config file"));
5655

57-
std::string invalid_non_existing_file;
56+
std::string invalid_non_existing_file = "";
5857
REQUIRE_THROWS_MATCHES(NetworkConfig::load(0, invalid_non_existing_file),
5958
std::invalid_argument,
6059
Catch::Matchers::Message("could not open file"));

0 commit comments

Comments
 (0)