2020-05-07 15:28:15 -04:00
|
|
|
//
|
|
|
|
// Created by WolverinDEV on 28/04/2020.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2020-06-12 19:08:49 -04:00
|
|
|
#include <log/LogUtils.h>
|
2020-05-07 15:28:15 -04:00
|
|
|
#include "LocalFileProvider.h"
|
2020-09-16 08:15:06 -04:00
|
|
|
#include "LocalFileSystem.h"
|
|
|
|
#include "LocalFileTransfer.h"
|
2020-05-07 15:28:15 -04:00
|
|
|
|
|
|
|
using namespace ts::server;
|
|
|
|
using LocalFileServer = file::LocalFileProvider;
|
2020-05-13 05:32:08 -04:00
|
|
|
using LocalVirtualFileServer = file::LocalVirtualFileServer;
|
2020-05-07 15:28:15 -04:00
|
|
|
|
|
|
|
std::shared_ptr<LocalFileServer> server_instance{};
|
2020-06-12 19:08:49 -04:00
|
|
|
bool file::initialize(std::string &error, const std::string& hostnames, uint16_t port) {
|
2020-05-07 15:28:15 -04:00
|
|
|
server_instance = std::make_shared<LocalFileProvider>();
|
2020-05-10 10:23:02 -04:00
|
|
|
|
2020-06-10 12:13:14 -04:00
|
|
|
if(!server_instance->initialize(error)) {
|
2020-05-07 15:28:15 -04:00
|
|
|
server_instance = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-12 19:08:49 -04:00
|
|
|
|
|
|
|
bool any_bind{false};
|
|
|
|
for(const auto& binding : net::resolve_bindings(hostnames, port)) {
|
2020-11-26 04:35:01 -05:00
|
|
|
if(!std::get<2>(binding).empty()) {
|
|
|
|
logError(LOG_FT, "Failed to resolve binding for {}: {}", std::get<0>(binding), std::get<2>(binding));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-26 04:35:01 -05:00
|
|
|
auto result = dynamic_cast<transfer::LocalFileTransfer&>(server_instance->file_transfer()).add_network_binding({ std::get<0>(binding), std::get<1>(binding) });
|
2020-06-12 19:08:49 -04:00
|
|
|
switch (result) {
|
|
|
|
case transfer::NetworkingBindResult::SUCCESS:
|
|
|
|
any_bind = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case transfer::NetworkingBindResult::OUT_OF_MEMORY:
|
2020-11-26 04:35:01 -05:00
|
|
|
logWarning(LOG_FT, "Failed to listen to address {}: Out of memory", std::get<0>(binding));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case transfer::NetworkingBindResult::FAILED_TO_LISTEN:
|
2020-11-26 04:35:01 -05:00
|
|
|
logWarning(LOG_FT, "Failed to listen on {}: {}/{}", std::get<0>(binding), errno, strerror(errno));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case transfer::NetworkingBindResult::FAILED_TO_BIND:
|
2020-11-26 04:35:01 -05:00
|
|
|
logWarning(LOG_FT, "Failed to bind on {}: {}/{}", std::get<0>(binding), errno, strerror(errno));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case transfer::NetworkingBindResult::BINDING_ALREADY_EXISTS:
|
2020-11-26 04:35:01 -05:00
|
|
|
logWarning(LOG_FT, "Failed to bind on {}: binding already exists", std::get<0>(binding));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case transfer::NetworkingBindResult::NETWORKING_NOT_INITIALIZED:
|
2020-11-26 04:35:01 -05:00
|
|
|
logWarning(LOG_FT, "Failed to bind on {}: networking not initialized", std::get<0>(binding));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case transfer::NetworkingBindResult::FAILED_TO_ALLOCATE_SOCKET:
|
2020-11-26 04:35:01 -05:00
|
|
|
logWarning(LOG_FT, "Failed to allocate a socket for {}: {}/{}", std::get<0>(binding), errno, strerror(errno));
|
2020-06-12 19:08:49 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 11:20:43 -05:00
|
|
|
return any_bind;
|
2020-05-07 15:28:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void file::finalize() {
|
|
|
|
auto server = std::exchange(server_instance, nullptr);
|
|
|
|
if(!server) return;
|
|
|
|
|
|
|
|
server->finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<file::AbstractFileServer> file::server() {
|
|
|
|
return server_instance;
|
|
|
|
}
|
|
|
|
|
2020-09-16 08:15:06 -04:00
|
|
|
LocalFileServer::LocalFileProvider() {
|
|
|
|
this->file_system_ = new filesystem::LocalFileSystem();
|
|
|
|
this->file_transfer_ = new transfer::LocalFileTransfer(this->file_system_);
|
|
|
|
}
|
|
|
|
LocalFileServer::~LocalFileProvider() {
|
|
|
|
delete this->file_transfer_;
|
|
|
|
delete this->file_system_;
|
|
|
|
};
|
2020-05-07 15:28:15 -04:00
|
|
|
|
2020-06-10 12:13:14 -04:00
|
|
|
bool LocalFileServer::initialize(std::string &error) {
|
2020-09-16 08:15:06 -04:00
|
|
|
if(!this->file_system_->initialize(error, "files/"))
|
2020-05-07 15:28:15 -04:00
|
|
|
return false;
|
|
|
|
|
2020-09-16 08:15:06 -04:00
|
|
|
if(!this->file_transfer_->start()) {
|
2020-05-07 15:28:15 -04:00
|
|
|
error = "transfer server startup failed";
|
2020-09-16 08:15:06 -04:00
|
|
|
this->file_system_->finalize();
|
2020-05-07 15:28:15 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalFileServer::finalize() {
|
2020-09-16 08:15:06 -04:00
|
|
|
this->file_transfer_->stop();
|
|
|
|
this->file_system_->finalize();
|
2020-05-07 15:28:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
file::filesystem::AbstractProvider &LocalFileServer::file_system() {
|
2020-09-16 08:15:06 -04:00
|
|
|
return *this->file_system_;
|
2020-05-07 15:28:15 -04:00
|
|
|
}
|
|
|
|
|
2020-05-13 05:32:08 -04:00
|
|
|
file::transfer::AbstractProvider &LocalFileServer::file_transfer() {
|
2020-09-16 08:15:06 -04:00
|
|
|
return *this->file_transfer_;
|
2020-05-13 05:32:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string file::LocalFileProvider::file_base_path() const {
|
2020-09-16 08:15:06 -04:00
|
|
|
return this->file_system_->root_path();
|
2020-05-13 05:32:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<file::VirtualFileServer> LocalFileServer::register_server(ServerId server_id) {
|
|
|
|
auto server = this->find_virtual_server(server_id);
|
|
|
|
if(server) return server;
|
|
|
|
|
|
|
|
server = std::make_shared<file::LocalVirtualFileServer>(server_id, std::to_string(server_id));
|
|
|
|
{
|
|
|
|
std::lock_guard slock{this->servers_mutex};
|
|
|
|
this->servers_.push_back(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalFileServer::unregister_server(ServerId server_id) {
|
|
|
|
auto server_unique_id = std::to_string(server_id);
|
|
|
|
|
|
|
|
std::lock_guard slock{this->servers_mutex};
|
|
|
|
auto it = std::find_if(this->servers_.begin(), this->servers_.end(), [&](const std::shared_ptr<VirtualFileServer>& server) {
|
|
|
|
return server->unique_id() == server_unique_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(it == this->servers_.end()) return;
|
|
|
|
this->servers_.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalVirtualFileServer::max_networking_upload_bandwidth(int64_t value) {
|
|
|
|
VirtualFileServer::max_networking_upload_bandwidth(value);
|
|
|
|
this->upload_throttle.set_max_bandwidth(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalVirtualFileServer::max_networking_download_bandwidth(int64_t value) {
|
|
|
|
VirtualFileServer::max_networking_download_bandwidth(value);
|
|
|
|
this->download_throttle.set_max_bandwidth(value);
|
2020-05-07 15:28:15 -04:00
|
|
|
}
|