This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
routing_api.h
188 lines (143 loc) · 7.38 KB
/
routing_api.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright 2012 MaidSafe.net limited
This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License,
version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
licence you accepted on initial access to the Software (the "Licences").
By contributing code to the MaidSafe Software, or to this project generally, you agree to be
bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root
directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also
available at: http://www.maidsafe.net/licenses
Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed
under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied.
See the Licences for the specific language governing permissions and limitations relating to
use of the MaidSafe Software. */
/*******************************************************************************
*Guarantees *
*______________________________________________________________________________*
* *
*1: Provide NAT traversal techniques where necessary. *
*2: Read and Write configuration file to allow bootstrap from known nodes. *
*3: Allow retrieval of bootstrap nodes from known location. *
*4: Remove bad nodes from all routing tables (ban from network). *
*5: Inform of changes in data range to be stored and sent to each node *
*6: Respond to every send that requires it, either with timeout or reply *
*******************************************************************************/
#ifndef MAIDSAFE_ROUTING_ROUTING_API_H_
#define MAIDSAFE_ROUTING_ROUTING_API_H_
#include <condition_variable>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "boost/asio/ip/udp.hpp"
#include "boost/date_time/posix_time/posix_time_config.hpp"
#include "maidsafe/common/node_id.h"
#include "maidsafe/common/rsa.h"
#include "maidsafe/passport/types.h"
#include "maidsafe/routing/api_config.h"
namespace maidsafe {
namespace routing {
struct NodeInfo;
namespace test { class GenericNode; }
namespace detail {
template <typename FobType>
struct is_client : public std::true_type {};
template <>
struct is_client<passport::Pmid> : public std::false_type {};
template <>
struct is_client<const passport::Pmid> : public std::false_type {};
} // namespace detail
class Routing {
public:
// create a non-mutating client
Routing();
// Providing :
// pmid as a paramater will create a non client routing object(vault).
// maid as a paramater will create a mutating client.
// Non-mutating client means that random keys will be generated by routing for this node.
template <typename FobType>
explicit Routing(const FobType& fob)
: pimpl_() {
asymm::Keys keys;
keys.private_key = fob.private_key();
keys.public_key = fob.public_key();
InitialisePimpl(detail::is_client<FobType>::value, NodeId(fob.name()->string()), keys);
}
~Routing();
// Joins the network. Valid method for requesting public key must be provided by the functor,
// otherwise no node will be added to the routing table and node will fail to join the network.
void Join(Functors functors);
// WARNING: THIS FUNCTION SHOULD BE ONLY USED TO JOIN FIRST TWO ZERO STATE NODES.
int ZeroStateJoin(Functors functors, const boost::asio::ip::udp::endpoint& local_endpoint,
const boost::asio::ip::udp::endpoint& peer_endpoint, const NodeInfo& peer_info);
// Sends message to a known destnation. (Typed Message API)
// Throws on invalid paramaters
template <typename T>
void Send(const T& message);
// Sends message to a known destnation.
// If a valid response functor is provided, it will be called when:
// a) the response is receieved or,
// b) waiting time (Parameters::default_response_timeout) for receiving the response expires
// Throws on invalid paramaters
void SendDirect(const NodeId& destination_id, // ID of final destination
const std::string& message, bool cacheable, // to cache message content
ResponseFunctor response_functor); // Called on response
// Sends message to Parameters::group_size most closest nodes to destination_id. The node
// having id equal to destination id is not considered as part of group and will not receive
// group message
// If a valid response functor is provided, it will be called when:
// a) for each response receieved (Parameters::group_size responses expected) or,
// b) waiting time (Parameters::default_response_timeout) for receiving the response expires
// Throws on invalid paramaters
void SendGroup(const NodeId& destination_id, // ID of final destination or group centre
const std::string& message, bool cacheable, // to cache message content
ResponseFunctor response_functor); // Called on each response
// Compares own closeness to target against other known nodes' closeness to the target
bool ClosestToId(const NodeId& target_id);
// Gets a random connected node from routing table (excluding closest
// Parameters::closest_nodes_size nodes).
// Shouldn't be called when routing table is likely to be smaller than closest_nodes_size.
NodeId RandomConnectedNode();
// Evaluates whether the sender_id is a legitimate source to send a request for performing
// an operation on info_id
bool EstimateInGroup(const NodeId& sender_id, const NodeId& info_id) const;
// Returns this node's id.
NodeId kNodeId() const;
// Returns a number between 0 to 100 representing % network health w.r.t. number of connections
int network_status();
// Checks if routing table contains given node id
bool IsConnectedVault(const NodeId& node_id);
// Checks if client routing table contains given node id
bool IsConnectedClient(const NodeId& node_id);
friend class test::GenericNode;
private:
Routing(const Routing&);
Routing(const Routing&&);
Routing& operator=(const Routing&);
void InitialisePimpl(bool client_mode, const NodeId& node_id, const asymm::Keys& keys);
class Impl;
std::shared_ptr<Impl> pimpl_;
};
// Locks 'mutex', sets 'current_health' to 'updated_health' then calls notify_one() on 'cond_var'.
void UpdateNetworkHealth(int updated_health, int& current_health, std::mutex& mutex,
std::condition_variable& cond_var, const NodeId& this_node_id);
template <>
Routing::Routing(const NodeId& node_id);
template <>
void Routing::Send(const SingleToSingleMessage& message);
template <>
void Routing::Send(const SingleToGroupMessage& message);
template <>
void Routing::Send(const GroupToSingleMessage& message);
template <>
void Routing::Send(const GroupToGroupMessage& message);
template <>
void Routing::Send(const GroupToSingleRelayMessage& message);
template <typename T>
void Routing::Send(const T&) {
T::message_type_must_be_one_of_the_specialisations_defined_as_typedefs_in_message_dot_h_file;
}
} // namespace routing
} // namespace maidsafe
#endif // MAIDSAFE_ROUTING_ROUTING_API_H_