Horizon Official Technical Documentation
Connector.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_NETWORKING_CONNECTOR_HPP
31#define HORIZON_NETWORKING_CONNECTOR_HPP
32
35
36#include <boost/asio.hpp>
37#include <boost/bind/bind.hpp>
38#include <thread>
39#include <functional>
40
41using namespace boost::asio::ip;
42
43namespace Horizon
44{
45namespace Networking
46{
53{
54public:
55 typedef std::function<void(std::string &, std::shared_ptr<tcp::socket>, uint32_t)> ConnectorCallback;
56
64 Connector(std::string const &connection_name, Server *server, std::string const &connect_ip, uint16_t port)
65 : server(server), _connection_name(connection_name), _endpoint(boost::asio::ip::address::from_string(connect_ip), port),
67 {
68 }
69
74 {
75 }
76
86 void connect_with_callback(ConnectorCallback callback, int connections = 1)
87 {
88 for (int i = 0; i < connections; i++) {
89 std::shared_ptr<tcp::socket> socket;
90 uint32_t network_thread_idx;
91 boost::system::error_code error;
92
93 // Get a new socket from a thread with the minimum connections.
94 std::tie(socket, network_thread_idx) = _socket_factory();
95
96 do {
97 HLog(info) << "Trying to establish connection for '" << _connection_name << "' at tcp://" << _endpoint.address().to_string() << ":" << _endpoint.port();
98
99 // Try connecting to the endpoint.
100 socket->connect(_endpoint, error);
101
102 if (error.value() != 0) {
103 HLog(info) << "Error connecting to '" << _connection_name << "' at tcp://" << _endpoint.address().to_string() << ":" << _endpoint.port();
104 std::this_thread::sleep_for(std::chrono::seconds(10));
105 socket->close();
106 error.clear();
107 } else {
108 callback(_connection_name, socket, network_thread_idx);
109 HLog(info) << "Successfully connected to '" << _connection_name << "' at tcp://" << _endpoint.address().to_string() << ":" << _endpoint.port();
110 }
111 } while (!socket->is_open());
112 }
113 }
114
118 void set_socket_factory(std::function<std::pair<std::shared_ptr<tcp::socket>, uint32_t>()> &&func) { _socket_factory = func; }
119
120private:
121 std::pair<std::shared_ptr<tcp::socket>, uint32_t> default_socket_factory() { return std::make_pair(nullptr, 0); }
122
124 std::string _connection_name;
125 tcp::endpoint _endpoint;
126 std::function<std::pair<std::shared_ptr<tcp::socket>, uint32_t>()> _socket_factory;
127};
128}
129}
130
131#endif //HORIZON_NETWORKCONNECTOR_HPP
#define HLog(type)
Definition: Logger.hpp:122
Connector object that allows connecting to remote endpoints.
Definition: Connector.hpp:53
Server * server
Definition: Connector.hpp:123
std::function< void(std::string &, std::shared_ptr< tcp::socket >, uint32_t)> ConnectorCallback
Definition: Connector.hpp:55
std::pair< std::shared_ptr< tcp::socket >, uint32_t > default_socket_factory()
Definition: Connector.hpp:121
void set_socket_factory(std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> &&func)
Sets a socket factory method that provides a socket for new connections.
Definition: Connector.hpp:118
Connector(std::string const &connection_name, Server *server, std::string const &connect_ip, uint16_t port)
Connector contructor.
Definition: Connector.hpp:64
std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> _socket_factory
Definition: Connector.hpp:126
Connector()
Destructor of the object that handles joining of all socket polling threads.
Definition: Connector.hpp:73
tcp::endpoint _endpoint
Definition: Connector.hpp:125
std::string _connection_name
Definition: Connector.hpp:124
void connect_with_callback(ConnectorCallback callback, int connections=1)
Attempts connections to the remote endpoint that the object was initiated with.
Definition: Connector.hpp:86
Definition: Server.hpp:554
Definition: Element.hpp:7