2 changed files with 79 additions and 70 deletions
@ -0,0 +1,73 @@ |
|||||||
|
#include <session.hpp> |
||||||
|
|
||||||
|
namespace session { |
||||||
|
nlohmann::json translate::to_json(const libtorrent::session_handle &handle){ |
||||||
|
nlohmann::json session_json; |
||||||
|
return session_json; |
||||||
|
} |
||||||
|
|
||||||
|
nlohmann::json manager::get_json() const { |
||||||
|
nlohmann::json session_json; |
||||||
|
session_json["peer_id"] = handle->id().to_string(); |
||||||
|
session_json["is_paused"] = handle->is_paused(); |
||||||
|
session_json["is_listening"] = handle->is_listening(); |
||||||
|
session_json["listen_port"] = handle->listen_port(); |
||||||
|
session_json["ssl_listen_port"] = handle->ssl_listen_port(); |
||||||
|
return session_json; |
||||||
|
} |
||||||
|
|
||||||
|
bool manager::patch(const nlohmann::json &data) { |
||||||
|
int is_paused=data["is_paued"]; |
||||||
|
int listen_port=data["listen_port"]; |
||||||
|
|
||||||
|
auto pause = [this](int is_paused){ |
||||||
|
if(is_paused!=-1){ |
||||||
|
if(is_paused==handle->is_paused()){ |
||||||
|
return true; |
||||||
|
}else if(is_paused==true){ |
||||||
|
handle->resume(); |
||||||
|
return true; |
||||||
|
}else if(is_paused==false){ |
||||||
|
handle->pause(); |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
auto listen = [this](int listen_port){ |
||||||
|
if(listen_port!=-1&&listen_port>6880){ |
||||||
|
libtorrent::settings_pack sp; |
||||||
|
sp.set_str(libtorrent::settings_pack::listen_interfaces,"0.0.0.0:"+std::to_string(listen_port)); |
||||||
|
handle->apply_settings(sp); |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
return pause(is_paused) && listen(listen_port); |
||||||
|
}; |
||||||
|
void resource::patch(const nlohmann::json &data){ |
||||||
|
if(mgr && mgr->is_valid()){ |
||||||
|
if(data.is_object() && !data.is_null()){ |
||||||
|
int is_paused=util::json::get<int>("is_paused",data,-1), |
||||||
|
listen_port=util::json::get<int>("listen_port",data,-1); |
||||||
|
bool at_at_least_one_option_is_set=is_paused!=-1||listen_port!=-1; |
||||||
|
if(at_at_least_one_option_is_set){ |
||||||
|
nlohmann::json patch={{"is_paused",is_paused},{"listen_port",listen_port}}; |
||||||
|
if(mgr->patch(patch)){ |
||||||
|
return get_response().set_status(http::ok); |
||||||
|
}else{ |
||||||
|
return get_response().set_status(http::internal_server_error); |
||||||
|
} |
||||||
|
}else{ |
||||||
|
return get_response().set_status(http::bad_request); |
||||||
|
} |
||||||
|
} |
||||||
|
return get_response().set_status(http::bad_request); |
||||||
|
// auto data=data.array();
|
||||||
|
}else{ |
||||||
|
return get_response().set_status(http::service_unavailable); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue