39
39
#include <rak/error_number.h>
40
40
#include <rak/path.h>
41
#include <rak/socket_address.h>
42
#include <rak/string_manip.h>
43
#include <torrent/bitfield.h>
44
#include <torrent/rate.h>
41
45
#include <torrent/peer/peer.h>
46
#include <torrent/peer/peer_info.h>
43
48
#include "core/manager.h"
49
#include "display/utils.h"
45
51
#include "globals.h"
46
52
#include "control.h"
47
53
#include "command_helpers.h"
50
// apply_f_set_priority(torrent::File* file, uint32_t value) {
51
// if (value > torrent::PRIORITY_HIGH)
52
// throw torrent::input_error("Invalid value.");
54
// file->set_priority((torrent::priority_t)value);
58
// apply_f_path(torrent::File* file) {
59
// if (file->path()->empty())
60
// return std::string();
62
// torrent::Object resultRaw(*file->path()->begin());
63
// torrent::Object::string_type& result = resultRaw.as_string();
65
// for (torrent::Path::const_iterator itr = ++file->path()->begin(), last = file->path()->end(); itr != last; itr++)
66
// result += '/' + *itr;
72
// apply_f_path_components(torrent::File* file) {
73
// torrent::Object resultRaw(torrent::Object::TYPE_LIST);
74
// torrent::Object::list_type& result = resultRaw.as_list();
76
// for (torrent::Path::const_iterator itr = file->path()->begin(), last = file->path()->end(); itr != last; itr++)
77
// result.push_back(*itr);
83
// apply_f_path_depth(torrent::File* file) {
84
// return (int64_t)file->path()->size();
56
retrieve_p_id(torrent::Peer* peer) {
57
const torrent::HashString* hashString = &peer->id();
59
return rak::transform_hex(hashString->begin(), hashString->end());
63
retrieve_p_id_html(torrent::Peer* peer) {
64
const torrent::HashString* hashString = &peer->id();
66
return rak::copy_escape_html(hashString->begin(), hashString->end());
70
retrieve_p_address(torrent::Peer* peer) {
71
return rak::socket_address::cast_from(peer->info()->socket_address())->address_str();
75
retrieve_p_port(torrent::Peer* peer) {
76
return rak::socket_address::cast_from(peer->info()->socket_address())->port();
80
retrieve_p_client_version(torrent::Peer* peer) {
82
display::print_client_version(buf, buf + 128, peer->info()->client_info());
84
return std::string(buf);
88
retrieve_p_options_str(torrent::Peer* peer) {
89
return rak::transform_hex(peer->info()->options(), peer->info()->options() + 8);
93
retrieve_p_completed_percent(torrent::Peer* peer) {
94
return (100 * peer->bitfield()->size_set()) / peer->bitfield()->size_bits();
87
97
#define ADD_CP_SLOT(key, function, slot, parm, doc) \
88
98
commandPeerSlotsItr->set_slot(slot); \
95
105
#define ADD_CP_VOID(key, slot) \
96
106
ADD_CP_SLOT_PUBLIC("p.get_" key, call_unknown, rpc::object_fn(slot), "i:", "")
108
#define ADD_CP_VALUE(key, get) \
109
ADD_CP_SLOT_PUBLIC("p." key, call_unknown, rpc::object_void_fn<torrent::Peer*>(get), "i:", "")
98
111
#define ADD_CP_VALUE_UNI(key, get) \
99
112
ADD_CP_SLOT_PUBLIC("p.get_" key, call_unknown, rpc::object_void_fn<torrent::Peer*>(get), "i:", "")
102
115
ADD_CP_SLOT_PUBLIC("p.set_" key, call_value, rpc::object_value_fn<torrent::Peer*>(set), "i:i", "") \
103
116
ADD_CP_SLOT_PUBLIC("p.get_" key, call_unknown, rpc::object_void_fn<torrent::Peer*>(get), "i:", "")
118
#define ADD_CP_VALUE_MEM_UNI(key, target, get) \
119
ADD_CP_SLOT_PUBLIC("p.get_" key, call_unknown, rpc::object_void_fn<torrent::Peer*>(rak::on(std::mem_fun(target), std::mem_fun(get))), "i:", "");
105
121
#define ADD_CP_STRING_UNI(key, get) \
106
122
ADD_CP_SLOT_PUBLIC("p.get_" key, call_unknown, rpc::object_void_fn<torrent::Peer*>(get), "s:", "")
109
125
initialize_command_peer() {
110
// ADD_CP_VALUE_UNI("is_created", std::mem_fun(&torrent::Peer::is_created));
111
// ADD_CP_VALUE_UNI("is_open", std::mem_fun(&torrent::Peer::is_open));
113
// ADD_CP_VALUE_UNI("size_bytes", std::mem_fun(&torrent::Peer::size_bytes));
114
// ADD_CP_VALUE_UNI("size_chunks", std::mem_fun(&torrent::Peer::size_chunks));
115
// ADD_CP_VALUE_UNI("completed_chunks", std::mem_fun(&torrent::Peer::completed_chunks));
117
// ADD_CP_VALUE_UNI("offset", std::mem_fun(&torrent::Peer::offset));
118
// ADD_CP_VALUE_UNI("range_first", std::mem_fun(&torrent::Peer::range_first));
119
// ADD_CP_VALUE_UNI("range_second", std::mem_fun(&torrent::Peer::range_second));
121
// ADD_CP_VALUE_BI("priority", std::ptr_fun(&apply_f_set_priority), std::mem_fun(&torrent::Peer::priority));
123
// ADD_CP_STRING_UNI("path", std::ptr_fun(&apply_f_path));
124
// ADD_CP_STRING_UNI("path_components", std::ptr_fun(&apply_f_path_components));
125
// ADD_CP_STRING_UNI("path_depth", std::ptr_fun(&apply_f_path_depth));
126
// ADD_CP_STRING_UNI("frozen_path", std::mem_fun(&torrent::Peer::frozen_path));
128
// ADD_CP_VALUE_UNI("match_depth_prev", std::mem_fun(&torrent::Peer::match_depth_prev));
129
// ADD_CP_VALUE_UNI("match_depth_next", std::mem_fun(&torrent::Peer::match_depth_next));
131
// ADD_CP_VALUE_UNI("last_touched", std::mem_fun(&torrent::Peer::last_touched));
126
ADD_CP_STRING_UNI("id", std::ptr_fun(&retrieve_p_id));
127
ADD_CP_STRING_UNI("id_html", std::ptr_fun(&retrieve_p_id_html));
128
ADD_CP_STRING_UNI("client_version", std::ptr_fun(&retrieve_p_client_version));
130
ADD_CP_STRING_UNI("options_str", std::ptr_fun(&retrieve_p_options_str));
132
ADD_CP_VALUE("is_encrypted", std::mem_fun(&torrent::Peer::is_encrypted));
133
ADD_CP_VALUE("is_incoming", std::mem_fun(&torrent::Peer::is_incoming));
134
ADD_CP_VALUE("is_obfuscated", std::mem_fun(&torrent::Peer::is_obfuscated));
135
ADD_CP_VALUE("is_snubbed", std::mem_fun(&torrent::Peer::is_snubbed));
137
ADD_CP_STRING_UNI("address", std::ptr_fun(&retrieve_p_address));
138
ADD_CP_VALUE_UNI("port", std::ptr_fun(&retrieve_p_port));
140
ADD_CP_VALUE_UNI("completed_percent", std::ptr_fun(&retrieve_p_completed_percent));
142
ADD_CP_VALUE_MEM_UNI("up_rate", &torrent::Peer::up_rate, &torrent::Rate::rate);
143
ADD_CP_VALUE_MEM_UNI("up_total", &torrent::Peer::up_rate, &torrent::Rate::total);
144
ADD_CP_VALUE_MEM_UNI("down_rate", &torrent::Peer::down_rate, &torrent::Rate::rate);
145
ADD_CP_VALUE_MEM_UNI("down_total", &torrent::Peer::down_rate, &torrent::Rate::total);
146
ADD_CP_VALUE_MEM_UNI("peer_rate", &torrent::Peer::peer_rate, &torrent::Rate::rate);
147
ADD_CP_VALUE_MEM_UNI("peer_total", &torrent::Peer::peer_rate, &torrent::Rate::total);