From cad794a561868c32ca23369193a10a0311516625 Mon Sep 17 00:00:00 2001 From: Vitaliy Ivanov Date: Mon, 19 Aug 2013 18:31:54 +0300 Subject: [PATCH 1/2] connection-manager: cleanup of hardcoded connection types. Connection types and their names should be in sync and not hardcoded thru the code. On the way added better naming to connection type variables. --- src/builtin/connection-manager.cc | 31 +++++++++++++++++++------------ src/include/connection-manager.hh | 20 ++++++++++++++++---- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/builtin/connection-manager.cc b/src/builtin/connection-manager.cc index 8e2befb..188536e 100644 --- a/src/builtin/connection-manager.cc +++ b/src/builtin/connection-manager.cc @@ -49,7 +49,14 @@ namespace bassl = ::boost::asio::ssl; static Vlog_module lg("connection_manager"); -static const std::string conn_t_str[] = { "tcp", "ssl", "ptcp", "pssl" }; +const std::string Connection_manager::Connection_type_string[_AMMOUNT] = +{ + "tcp", + "ssl", + "ptcp", + "pssl", + "unknown" +}; Connection_manager::Connection_manager(const Component_context* ctxt, const std::list& interfaces) @@ -85,7 +92,7 @@ Connection_manager::install() /* Bind/listen to interfaces */ BOOST_FOREACH(const std::string& interface, interfaces) { - Conn_t type; + Connection_type type; std::string host, key, cert, cafile; uint16_t port; @@ -112,21 +119,21 @@ Connection_manager::install() if (type == TCP || type == SSL) { VLOG_DBG(lg, "connecting to %s:%s:%d:%s:%s:%s", - conn_t_str[type].c_str(), host.c_str(), port, + Connection_type_string[type].c_str(), host.c_str(), port, key.c_str(), cert.c_str(), cafile.c_str()); connect(type, host, port, key, cert, cafile); } else if (type == PTCP || type == PSSL) { VLOG_DBG(lg, "listening on %s:%s:%d:%s:%s:%s", - conn_t_str[type].c_str(), host.c_str(), port, + Connection_type_string[type].c_str(), host.c_str(), port, key.c_str(), cert.c_str(), cafile.c_str()); listen(type, host, port, key, cert, cafile); } else { throw std::runtime_error("Unsupported connection type \"" - + conn_t_str[type] + "\""); + + Connection_type_string[type] + "\""); } } @@ -137,7 +144,7 @@ Connection_manager::install() void Connection_manager::parse(const std::string& interface, - Conn_t& type, + Connection_type& type, std::string& host, uint16_t& port, std::string& key, @@ -159,13 +166,13 @@ Connection_manager::parse(const std::string& interface, size_t ntokens = tokens.size(); if (ntokens >= 1) { - if (tokens[0] == "tcp") + if (tokens[0] == Connection_type_string[TCP]) type = TCP; - else if (tokens[0] == "ssl") + else if (tokens[0] == Connection_type_string[SSL]) type = SSL; - else if (tokens[0] == "ptcp") + else if (tokens[0] == Connection_type_string[PTCP]) type = PTCP; - else if (tokens[0] == "pssl") + else if (tokens[0] == Connection_type_string[PSSL]) type = PSSL; if (ntokens == 2) @@ -245,7 +252,7 @@ Connection_manager::handle_handshake(bassl::stream_base::handshake_type type, } void -Connection_manager::connect(Conn_t type, +Connection_manager::connect(Connection_type type, const std::string& host, const uint16_t& port, const std::string& key, const std::string& cert, @@ -329,7 +336,7 @@ Connection_manager::listen(boost::shared_ptr acc } void -Connection_manager::listen(Conn_t type, +Connection_manager::listen(Connection_type type, const std::string& bind_ip, const uint16_t& port, const std::string& key, const std::string& cert, diff --git a/src/include/connection-manager.hh b/src/include/connection-manager.hh index 4255d5c..1e9705b 100644 --- a/src/include/connection-manager.hh +++ b/src/include/connection-manager.hh @@ -55,7 +55,19 @@ public: void install(); private: - enum Conn_t { TCP, SSL, PTCP, PSSL, UNKNOWN }; + + enum Connection_type + { + TCP = 0, + SSL = 1, + PTCP = 2, + PSSL = 3, + UNKNOWN = 4, + // should be last + _AMMOUNT = UNKNOWN+1 + }; + + static const std::string Connection_type_string[_AMMOUNT]; typedef boost::function Listen_callback; //typedef std::string Protocol_name; @@ -72,7 +84,7 @@ private: const std::list& interfaces); void parse(const std::string& interface, - Conn_t& type, + Connection_type& type, std::string& ip, uint16_t& port, std::string& key, @@ -88,7 +100,7 @@ private: boost::shared_ptr, Listen_callback, const boost::system::error_code&); - void connect(Conn_t type, const std::string& host, const uint16_t& port, + void connect(Connection_type type, const std::string& host, const uint16_t& port, const std::string& key, const std::string& cert, const std::string& cafile); @@ -98,7 +110,7 @@ private: const std::string& key, const std::string& cert, const std::string& cafile); - void listen(Conn_t type, const std::string& bind_ip, const uint16_t& port, + void listen(Connection_type type, const std::string& bind_ip, const uint16_t& port, const std::string& key, const std::string& cert, const std::string& cafile); From 45d5857256c75bbe26f2318e7dea537e2785617b Mon Sep 17 00:00:00 2001 From: Vitaliy Ivanov Date: Mon, 29 Dec 2014 16:08:45 +0200 Subject: [PATCH 2/2] connection-manager: s/_AMMOUNT/NR_CONNECTION_TYPES/g Signed-off-by: Vitaliy Ivanov --- src/builtin/connection-manager.cc | 2 +- src/include/connection-manager.hh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/builtin/connection-manager.cc b/src/builtin/connection-manager.cc index 188536e..5d6b187 100644 --- a/src/builtin/connection-manager.cc +++ b/src/builtin/connection-manager.cc @@ -49,7 +49,7 @@ namespace bassl = ::boost::asio::ssl; static Vlog_module lg("connection_manager"); -const std::string Connection_manager::Connection_type_string[_AMMOUNT] = +const std::string Connection_manager::Connection_type_string[NR_CONNECTION_TYPES] = { "tcp", "ssl", diff --git a/src/include/connection-manager.hh b/src/include/connection-manager.hh index 1e9705b..b47bd5f 100644 --- a/src/include/connection-manager.hh +++ b/src/include/connection-manager.hh @@ -64,10 +64,10 @@ private: PSSL = 3, UNKNOWN = 4, // should be last - _AMMOUNT = UNKNOWN+1 + NR_CONNECTION_TYPES = UNKNOWN+1 }; - static const std::string Connection_type_string[_AMMOUNT]; + static const std::string Connection_type_string[NR_CONNECTION_TYPES]; typedef boost::function Listen_callback; //typedef std::string Protocol_name;