00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef RPC_MANAGER_HPP
00034 #define RPC_MANAGER_HPP
00035
00036 #include <vector>
00037 #include <map>
00038 #include <boost/function.hpp>
00039 #include <boost/date_time/posix_time/posix_time.hpp>
00040 #include <boost/shared_ptr.hpp>
00041 #include <boost/noncopyable.hpp>
00042 #include <boost/cstdint.hpp>
00043 #include <boost/array.hpp>
00044
00045 #include <libtorrent/socket.hpp>
00046 #include <libtorrent/entry.hpp>
00047 #include <libtorrent/kademlia/packet_iterator.hpp>
00048 #include <libtorrent/kademlia/node_id.hpp>
00049 #include <libtorrent/kademlia/logging.hpp>
00050 #include <libtorrent/kademlia/node_entry.hpp>
00051
00052 namespace libtorrent { namespace dht
00053 {
00054
00055 using asio::ip::udp;
00056 #ifdef TORRENT_DHT_VERBOSE_LOGGING
00057 TORRENT_DECLARE_LOG(rpc);
00058 #endif
00059
00060 typedef std::vector<char> packet_t;
00061
00062 namespace messages
00063 {
00064 enum { ping = 0, find_node = 1, get_peers = 2, announce_peer = 3, error = 4 };
00065 char const* const ids[] = { "ping", "find_node", "get_peers", "announce_peer", "error" };
00066 }
00067
00068 struct msg
00069 {
00070 msg() : reply(false), piggy_backed_ping(false)
00071 , port(0) {}
00072
00073
00074 bool reply;
00075
00076 bool piggy_backed_ping;
00077
00078 int message_id;
00079
00080
00081
00082 std::string transaction_id;
00083
00084
00085 std::string ping_transaction_id;
00086
00087 node_id id;
00088
00089
00090 udp::endpoint addr;
00091
00092 typedef std::vector<node_entry> nodes_t;
00093 nodes_t nodes;
00094
00095 typedef std::vector<tcp::endpoint> peers_t;
00096 peers_t peers;
00097
00098
00099 entry write_token;
00100
00101
00102
00103 node_id info_hash;
00104
00105
00106 int port;
00107
00108
00109 int error_code;
00110 std::string error_msg;
00111 };
00112
00113 struct observer : boost::noncopyable
00114 {
00115 observer()
00116 : sent(boost::posix_time::microsec_clock::universal_time())
00117 {}
00118
00119 virtual ~observer() {}
00120
00121
00122
00123 virtual void send(msg& m) = 0;
00124
00125
00126 virtual void reply(msg const& m) = 0;
00127
00128
00129
00130 virtual void timeout() = 0;
00131
00132
00133
00134
00135
00136 virtual void abort() = 0;
00137
00138 udp::endpoint target_addr;
00139 boost::posix_time::ptime sent;
00140 };
00141
00142 class routing_table;
00143
00144 class rpc_manager
00145 {
00146 public:
00147 typedef boost::function1<void, msg const&> fun;
00148 typedef boost::function1<void, msg const&> send_fun;
00149
00150 rpc_manager(fun const& incoming_fun, node_id const& our_id
00151 , routing_table& table, send_fun const& sf);
00152 ~rpc_manager();
00153
00154
00155 bool incoming(msg const&);
00156 boost::posix_time::time_duration tick();
00157
00158 void invoke(int message_id, udp::endpoint target
00159 , boost::shared_ptr<observer> o);
00160
00161 void reply(msg& m, msg const& reply_to);
00162 void reply_with_ping(msg& m, msg const& reply_to);
00163
00164 #ifndef NDEBUG
00165 void check_invariant() const;
00166 #endif
00167
00168 private:
00169
00170 enum { max_transactions = 2048 };
00171
00172 unsigned int new_transaction_id(boost::shared_ptr<observer> o);
00173 void update_oldest_transaction_id();
00174
00175 boost::uint32_t calc_connection_id(udp::endpoint addr);
00176
00177 typedef boost::array<boost::shared_ptr<observer>, max_transactions>
00178 transactions_t;
00179 transactions_t m_transactions;
00180 std::vector<boost::shared_ptr<observer> > m_aborted_transactions;
00181
00182
00183 int m_next_transaction_id;
00184
00185
00186
00187
00188 int m_oldest_transaction_id;
00189
00190 fun m_incoming;
00191 send_fun m_send;
00192 node_id m_our_id;
00193 routing_table& m_table;
00194 boost::posix_time::ptime m_timer;
00195 node_id m_random_number;
00196 bool m_destructing;
00197 };
00198
00199 } }
00200
00201 #endif
00202
00203