// // Created by WolverinDEV on 29/04/2020. // #include #include #include #include #include namespace fs = std::experimental::filesystem; using namespace ts::server; struct Nothing {}; template inline void print_fs_response(const std::string& message, const std::shared_ptr, ResponseType>>& response) { if(response->status == file::ExecuteStatus::ERROR) logError(LOG_FT, "{}: {} => {}", message, (int) response->error().error_type, response->error().error_message); else if(response->status == file::ExecuteStatus::SUCCESS) logMessage(LOG_FT, "{}: success", message); else logWarning(LOG_FT, "Unknown response state ({})!", (int) response->status); } template inline void print_ft_response(const std::string& message, const std::shared_ptr>& response) { if(response->status == file::ExecuteStatus::ERROR) logError(LOG_FT, "{}: {} => {}", message, (int) response->error().error_type, response->error().error_message); else if(response->status == file::ExecuteStatus::SUCCESS) logMessage(LOG_FT, "{}: success", message); else logWarning(LOG_FT, "Unknown response state ({})!", (int) response->status); } inline void print_query(const std::string& message, const file::filesystem::AbstractProvider::directory_query_response_t& response) { if(response.status == file::ExecuteStatus::ERROR) logError(LOG_FT, "{}: {} => {}", message, (int) response.error().error_type, response.error().error_message); else if(response.status == file::ExecuteStatus::SUCCESS) { const auto& entries = response.response(); logMessage(LOG_FT, "{}: Found {} entries", message, entries.size()); for(auto& entry : entries) { if(entry.type == file::filesystem::DirectoryEntry::FILE) logMessage(LOG_FT, " - File {}", entry.name); else if(entry.type == file::filesystem::DirectoryEntry::DIRECTORY) logMessage(LOG_FT, " - Directory {}", entry.name); else logMessage(LOG_FT, " - Unknown {}", entry.name); logMessage(LOG_FT, " Write timestamp: {}", std::chrono::floor(entry.modified_at.time_since_epoch()).count()); logMessage(LOG_FT, " Size: {}", entry.size); } } else logWarning(LOG_FT, "{}: Unknown response state ({})!", message, (int) response.status); } int main() { evthread_use_pthreads(); auto log_config = std::make_shared(); log_config->terminalLevel = spdlog::level::trace; logger::setup(log_config); std::string error{}; if(!file::initialize(error)) { logError(LOG_FT, "Failed to initialize file server: {}", error); return 0; } logMessage(LOG_FT, "File server started"); auto instance = file::server(); #if 0 auto& fs = instance->file_system(); { auto response = fs.initialize_server(0); response->wait(); print_fs_response("Server init result", response); if(response->status != file::ExecuteStatus::SUCCESS) return 0; } { auto response = fs.create_channel_directory(0, 2, "/"); response->wait(); print_fs_response("Channel dir create A", response); } { auto response = fs.create_channel_directory(0, 2, "/test-folder/"); response->wait(); print_fs_response("Channel dir create B", response); } { auto response = fs.create_channel_directory(0, 2, "../test-folder/"); response->wait(); print_fs_response("Channel dir create C", response); } { auto response = fs.create_channel_directory(0, 2, "./test-folder/../test-folder-2"); response->wait(); print_fs_response("Channel dir create D", response); } { auto response = fs.query_channel_directory(0, 2, "/"); response->wait(); print_query("Channel query", *response); } { auto response = fs.query_icon_directory(0); response->wait(); print_query("Icons", *response); } { auto response = fs.query_avatar_directory(0); response->wait(); print_query("Avatars", *response); } { auto response = fs.rename_channel_file(0, 2, "./test-folder/../test-folder-2", "./test-folder/../test-folder-3"); response->wait(); print_fs_response("Folder rename A", response); } { auto response = fs.rename_channel_file(0, 2, "./test-folder/../test-folder-3", "./test-folder/../test-folder-2"); response->wait(); print_fs_response("Folder rename B", response); } #endif #if 1 auto& ft = instance->file_transfer(); ft.callback_transfer_finished = [](const std::shared_ptr& transfer) { logMessage(0, "Transfer finished"); }; ft.callback_transfer_started = [](const std::shared_ptr& transfer) { logMessage(0, "Transfer started"); }; ft.callback_transfer_aborted = [](const std::shared_ptr& transfer, const file::transfer::TransferError& error) { logMessage(0, "Transfer aborted ({}/{})", (int) error.error_type, error.error_message); }; ft.callback_transfer_statistics = [](const std::shared_ptr& transfer, const file::transfer::TransferStatistics& stats) { logMessage(0, "Transfer stats. New file bytes: {}, delta bytes send {}", stats.delta_file_bytes_transferred, stats.delta_network_bytes_send); }; { auto response = ft.initialize_channel_transfer(file::transfer::Transfer::DIRECTION_DOWNLOAD, 0, 2, { "test2.txt", false, 4, 120, 16 }); response->wait(); print_ft_response("Download test.txt", response); if(response->succeeded()) logMessage(LOG_FT, "Download key: {}", std::string{response->response()->transfer_key, TRANSFER_KEY_LENGTH}); } #endif std::this_thread::sleep_for(std::chrono::seconds{120}); //TODO: Test file locking file::finalize(); return 0; }