fix transfer
This commit is contained in:
@@ -1,7 +1,4 @@
|
|||||||
#ifdef __SWITCH__
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
#else
|
using path = fs::path;
|
||||||
namespace fs = std::__fs::filesystem;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int transfer_files(fs::path directory, char** filenames, int file_count);
|
int transfer_files(path directory);
|
||||||
|
|||||||
124
server.cpp
124
server.cpp
@@ -1,15 +1,16 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <cstring>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <ostream>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <filesystem>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef __SWITCH__
|
#ifdef __SWITCH__
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
@@ -20,6 +21,8 @@
|
|||||||
#define MULTICAST_PORT 8081
|
#define MULTICAST_PORT 8081
|
||||||
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
void sendAck(int sock) {
|
void sendAck(int sock) {
|
||||||
const char *ack = "ACK";
|
const char *ack = "ACK";
|
||||||
std::cout << "Sending ACK " << std::endl;
|
std::cout << "Sending ACK " << std::endl;
|
||||||
@@ -27,51 +30,34 @@ void sendAck(int sock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Функция для получения файла
|
// Функция для получения файла
|
||||||
void receive_file(std::string dirname, int client_socket) {
|
void receive_file(int sock, const std::string &relative_path,
|
||||||
|
size_t file_size) {
|
||||||
|
fs::path filepath(relative_path);
|
||||||
|
// Create parent directories if they do not exist
|
||||||
|
|
||||||
|
std::cout << "relative_path is: " << relative_path << std::endl;
|
||||||
|
|
||||||
|
fs::create_directories(filepath.parent_path());
|
||||||
|
|
||||||
|
std::ofstream outfile(filepath, std::ios::binary);
|
||||||
char buffer[BUFFER_SIZE] = {0};
|
char buffer[BUFFER_SIZE] = {0};
|
||||||
// Receive filename length
|
|
||||||
uint32_t filename_len;
|
|
||||||
ssize_t bytes_read = read(client_socket, &filename_len, sizeof(filename_len));
|
|
||||||
|
|
||||||
std::cout << "Filename length: " << filename_len << std::endl;
|
|
||||||
|
|
||||||
// Check for end-of-transmission signal
|
|
||||||
if (bytes_read <= 0 || filename_len == 0) {
|
|
||||||
std::cout << "End of transmission detected." << std::endl;
|
|
||||||
pthread_exit(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Receive filename
|
|
||||||
char *filename = new char[filename_len + 1];
|
|
||||||
read(client_socket, filename, filename_len);
|
|
||||||
filename[filename_len] = '\0';
|
|
||||||
|
|
||||||
std::cout << "Receiving file: " << filename << std::endl;
|
|
||||||
|
|
||||||
// Receive file size
|
|
||||||
size_t file_size;
|
|
||||||
read(client_socket, &file_size, sizeof(file_size));
|
|
||||||
|
|
||||||
std::cout << "With size: " << file_size << std::endl;
|
|
||||||
// Open a file to write the received data
|
|
||||||
std::ofstream outfile(dirname + "/" + filename, std::ios::binary);
|
|
||||||
delete[] filename; // Clean up filename buffer
|
|
||||||
|
|
||||||
size_t total_bytes_received = 0;
|
size_t total_bytes_received = 0;
|
||||||
while (total_bytes_received < file_size) {
|
while (total_bytes_received < file_size) {
|
||||||
ssize_t bytes_received = read(client_socket, buffer, BUFFER_SIZE);
|
ssize_t bytes_received = read(sock, buffer, BUFFER_SIZE);
|
||||||
std::cout << "bytes received: " << bytes_received << std::endl;
|
std::cout << "Bytes received: " << bytes_received << std::endl;
|
||||||
if (bytes_received <= 0) {
|
if (bytes_received <= 0) {
|
||||||
|
std::cerr << "Error reading file data from socket." << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
outfile.write(buffer, bytes_received);
|
outfile.write(buffer, bytes_received);
|
||||||
total_bytes_received += bytes_received;
|
total_bytes_received += bytes_received;
|
||||||
|
|
||||||
// Send acknowledgment for each chunk received
|
// Send acknowledgment for each chunk received
|
||||||
sendAck(client_socket);
|
sendAck(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "File received successfully." << std::endl;
|
std::cout << "File received successfully: " << relative_path << std::endl;
|
||||||
outfile.close();
|
outfile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,31 +67,36 @@ void *handle_client(void *socket_desc) {
|
|||||||
|
|
||||||
std::cout << "Обработка нового клиента в потоке " << pthread_self() << "\n";
|
std::cout << "Обработка нового клиента в потоке " << pthread_self() << "\n";
|
||||||
|
|
||||||
// Receive directory length
|
while (true) {
|
||||||
uint32_t directory_len;
|
|
||||||
ssize_t bytes_read = read(client_socket, &directory_len, sizeof(directory_len));
|
uint32_t filename_len;
|
||||||
|
ssize_t bytes_read =
|
||||||
|
read(client_socket, &filename_len, sizeof(filename_len));
|
||||||
|
|
||||||
// Check for end-of-transmission signal
|
// Check for end-of-transmission signal
|
||||||
if (bytes_read <= 0 || directory_len == 0) {
|
if (bytes_read <= 0 || filename_len == 0) {
|
||||||
std::cout << "End of transmission detected." << std::endl;
|
std::cout << "End of transmission detected." << std::endl;
|
||||||
|
break;
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive filename
|
// Receive filename or directory name
|
||||||
char *dirname = new char[directory_len + 1];
|
char *filename = new char[filename_len + 1];
|
||||||
read(client_socket, dirname, directory_len);
|
read(client_socket, filename, filename_len);
|
||||||
dirname[directory_len] = '\0';
|
|
||||||
|
|
||||||
std::cout << "Directory name is " << dirname << std::endl;
|
filename[filename_len] = '\0';
|
||||||
|
std::string filename_str(filename);
|
||||||
|
std::cout << "Receive filename: " << filename_str << std::endl;
|
||||||
|
delete[] filename; // Clean up filename buffer
|
||||||
|
|
||||||
if (!std::filesystem::create_directory(dirname)) {
|
std::cout << "Received filename_str is " << filename_str << std::endl;
|
||||||
std::cerr << "Unable to create directory" << std::endl;
|
auto is_directory = filename_str.back() == '/';
|
||||||
pthread_exit(nullptr);
|
std::cout << "Is directory: " << is_directory << std::endl;
|
||||||
}
|
|
||||||
|
|
||||||
// Получаем файлы до тех пор, пока клиент не закроет соединение
|
size_t file_size;
|
||||||
while (true) {
|
read(client_socket, &file_size, sizeof(file_size));
|
||||||
receive_file(dirname, client_socket);
|
std::cout << "file size is: " << file_size << std::endl;
|
||||||
|
receive_file(client_socket, filename_str, file_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(client_socket);
|
close(client_socket);
|
||||||
@@ -137,7 +128,8 @@ void *broadcast_listener(void *) {
|
|||||||
group.imr_multiaddr.s_addr = inet_addr(MULTICAST_GROUP);
|
group.imr_multiaddr.s_addr = inet_addr(MULTICAST_GROUP);
|
||||||
group.imr_interface.s_addr = htonl(INADDR_ANY);
|
group.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||||
|
|
||||||
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
|
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group,
|
||||||
|
sizeof(group)) < 0) {
|
||||||
perror("setsockopt failed");
|
perror("setsockopt failed");
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
@@ -147,7 +139,8 @@ void *broadcast_listener(void *) {
|
|||||||
struct sockaddr_in client_addr;
|
struct sockaddr_in client_addr;
|
||||||
socklen_t addr_len = sizeof(client_addr);
|
socklen_t addr_len = sizeof(client_addr);
|
||||||
while (true) {
|
while (true) {
|
||||||
int n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &addr_len);
|
int n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0,
|
||||||
|
(struct sockaddr *)&client_addr, &addr_len);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
perror("recvfrom failed");
|
perror("recvfrom failed");
|
||||||
continue;
|
continue;
|
||||||
@@ -156,8 +149,10 @@ void *broadcast_listener(void *) {
|
|||||||
buffer[n] = '\0';
|
buffer[n] = '\0';
|
||||||
if (strcmp(buffer, "DISCOVER_SERVER") == 0) {
|
if (strcmp(buffer, "DISCOVER_SERVER") == 0) {
|
||||||
const char *message = "SERVER_HERE";
|
const char *message = "SERVER_HERE";
|
||||||
sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&client_addr, addr_len);
|
sendto(sockfd, message, strlen(message), 0,
|
||||||
std::cout << "Server discovery response sent to multicast group" << std::endl;
|
(const struct sockaddr *)&client_addr, addr_len);
|
||||||
|
std::cout << "Server discovery response sent to multicast group"
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +166,8 @@ int main() {
|
|||||||
nxlinkStdio();
|
nxlinkStdio();
|
||||||
#endif
|
#endif
|
||||||
pthread_t broadcast_thread;
|
pthread_t broadcast_thread;
|
||||||
if (pthread_create(&broadcast_thread, nullptr, broadcast_listener, nullptr) < 0) {
|
if (pthread_create(&broadcast_thread, nullptr, broadcast_listener, nullptr) <
|
||||||
|
0) {
|
||||||
perror("Thread creation failed");
|
perror("Thread creation failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -206,7 +202,8 @@ int main() {
|
|||||||
while (true) {
|
while (true) {
|
||||||
sockaddr_in client_address;
|
sockaddr_in client_address;
|
||||||
socklen_t client_len = sizeof(client_address);
|
socklen_t client_len = sizeof(client_address);
|
||||||
int client_socket = accept(server_fd, (sockaddr *)&client_address, &client_len);
|
int client_socket =
|
||||||
|
accept(server_fd, (sockaddr *)&client_address, &client_len);
|
||||||
|
|
||||||
if (client_socket < 0) {
|
if (client_socket < 0) {
|
||||||
std::cerr << "Ошибка принятия подключения\n";
|
std::cerr << "Ошибка принятия подключения\n";
|
||||||
@@ -225,7 +222,8 @@ int main() {
|
|||||||
std::cerr << "Ошибка создания потока\n";
|
std::cerr << "Ошибка создания потока\n";
|
||||||
delete pclient; // Освобождаем память при ошибке
|
delete pclient; // Освобождаем память при ошибке
|
||||||
} else {
|
} else {
|
||||||
pthread_detach(thread_id); // Отсоединяем поток, чтобы он мог завершиться самостоятельно
|
pthread_detach(thread_id); // Отсоединяем поток, чтобы он мог завершиться
|
||||||
|
// самостоятельно
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,17 +44,8 @@ namespace ui {
|
|||||||
if (std::get<0>(result)) {
|
if (std::get<0>(result)) {
|
||||||
printf("path is %s\n", std::get<2>(result).c_str());
|
printf("path is %s\n", std::get<2>(result).c_str());
|
||||||
std::string path = std::get<2>(result);
|
std::string path = std::get<2>(result);
|
||||||
std::vector<std::string> files;
|
|
||||||
std::vector<char*> cstrings{};
|
|
||||||
auto directory = std::filesystem::path(path);
|
auto directory = std::filesystem::path(path);
|
||||||
for (const auto & entry : std::filesystem::directory_iterator(path)) {
|
transfer_files(directory);
|
||||||
std::cout << entry.path() << std::endl;
|
|
||||||
files.push_back(entry.path().string());
|
|
||||||
}
|
|
||||||
for (auto& file : files) {
|
|
||||||
cstrings.push_back(&file.front());
|
|
||||||
}
|
|
||||||
transfer_files(directory, cstrings.data(), cstrings.size());
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,139 +1,141 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <netinet/in.h>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <string>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <sys/types.h>
|
||||||
#include <netinet/in.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <logger.hpp>
|
|
||||||
#ifdef __SWITCH__
|
#ifdef __SWITCH__
|
||||||
#include <switch.h>
|
|
||||||
#include <client.hpp>
|
#include <client.hpp>
|
||||||
namespace fs = std::filesystem;
|
#include <switch.h>
|
||||||
#else
|
|
||||||
namespace fs = std::__fs::filesystem;
|
|
||||||
#endif
|
#endif
|
||||||
#define PORT 8080
|
#define PORT 8080
|
||||||
#define BUFFER_SIZE 1024
|
#define BUFFER_SIZE 1024
|
||||||
#define MULTICAST_PORT 8081
|
#define MULTICAST_PORT 8081
|
||||||
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
||||||
|
|
||||||
struct ThreadArgs
|
namespace fs = std::filesystem;
|
||||||
{
|
using path = fs::path;
|
||||||
char **filenames;
|
|
||||||
int file_count;
|
class Logger {
|
||||||
|
public:
|
||||||
|
inline static const std::string INFO = "[INFO]";
|
||||||
|
inline static const std::string DEBUG = "[DEBUG]";
|
||||||
|
inline static const std::string ERROR = "[ERROR]";
|
||||||
|
inline static const std::string WARN = "[WARN]";
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ThreadArgs {
|
||||||
int sock;
|
int sock;
|
||||||
fs::path directory;
|
fs::path directory;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool receiveAck(int sock)
|
bool receiveAck(int sock) {
|
||||||
{
|
|
||||||
char ack[4] = {0};
|
char ack[4] = {0};
|
||||||
int bytes_received = read(sock, ack, 3);
|
int bytes_received = read(sock, ack, 3);
|
||||||
|
std::cout << "receiveAck bytes_received: " << bytes_received << std::endl;
|
||||||
return bytes_received > 0 && std::string(ack) == "ACK";
|
return bytes_received > 0 && std::string(ack) == "ACK";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Отправка строки с учетом её длины
|
// Отправка строки с учетом её длины
|
||||||
void send_string(int sock, const std::string &str)
|
void send_string(int sock, const std::string &str) {
|
||||||
{
|
|
||||||
size_t length = str.size();
|
size_t length = str.size();
|
||||||
send(sock, &length, sizeof(length), 0);
|
send(sock, &length, sizeof(length), 0);
|
||||||
send(sock, str.c_str(), length, 0);
|
send(sock, str.c_str(), length, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *send_files_thread(void *args)
|
void sendFile(int sock, const fs::path &base_path, const fs::path &filepath) {
|
||||||
{
|
std::ifstream infile(filepath, std::ios::binary | std::ios::ate);
|
||||||
ThreadArgs *thread_args = static_cast<ThreadArgs *>(args);
|
if (!infile.is_open()) {
|
||||||
char **filenames = thread_args->filenames;
|
std::cerr << "File not found: " << filepath << std::endl;
|
||||||
int file_count = thread_args->file_count;
|
return;
|
||||||
int sock = thread_args->sock;
|
}
|
||||||
fs::path cwd = thread_args->directory;
|
// std::string relative_path = fs::relative(filepath, base_path).string();
|
||||||
delete thread_args;
|
uint32_t filename_len = filepath.string().size();
|
||||||
|
|
||||||
char buffer[BUFFER_SIZE];
|
std::cout << "send filepath length: " << filename_len << std::endl;
|
||||||
|
|
||||||
// Send the directory length
|
if (send(sock, &filename_len, sizeof(filename_len), 0) == -1) {
|
||||||
|
std::cerr << "Failed to send filename length" << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "send filepath: " << filepath.string() << std::endl;
|
||||||
|
|
||||||
std::cout << Logger::INFO << "cwd.filename is: " << cwd.filename().c_str() << std::endl; // Get the parent directory
|
// Send the filename
|
||||||
std::string dirname = cwd.parent_path().filename();
|
if (send(sock, filepath.c_str(), filename_len, 0) ==
|
||||||
uint32_t directory_len = dirname.size();
|
-1) { // Send the filename length
|
||||||
send(sock, &directory_len, sizeof(directory_len), 0);
|
std::cerr << "Failed to send filename" << std::endl;
|
||||||
|
|
||||||
// Send the dirname
|
|
||||||
send(sock, dirname.c_str(), directory_len, 0);
|
|
||||||
|
|
||||||
for (int i = 0; i < file_count; ++i)
|
|
||||||
{
|
|
||||||
std::string path = filenames[i];
|
|
||||||
std::size_t found = path.find_last_of("/\\");
|
|
||||||
std::string filename = path.substr(found+1);
|
|
||||||
std::ifstream infile(path, std::ios::binary | std::ios::ate);
|
|
||||||
if (!infile.is_open())
|
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "File not found: " << filename.c_str() << std::endl;
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the size of the file
|
// Get the size of the file
|
||||||
std::streamsize file_size = infile.tellg();
|
std::streamsize file_size = infile.tellg();
|
||||||
infile.seekg(0, std::ios::beg);
|
infile.seekg(0, std::ios::beg);
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
// Send the filename length
|
std::cout << "send filesize: " << file_size << std::endl;
|
||||||
uint32_t filename_len = filename.size();
|
|
||||||
std::cout << Logger::INFO << "Send filename length: " << filename_len << std::endl;
|
|
||||||
send(sock, &filename_len, sizeof(filename_len), 0);
|
|
||||||
|
|
||||||
// Send the filename
|
|
||||||
std::cout << Logger::INFO << "Send file name: " << filename.c_str() << std::endl;
|
|
||||||
send(sock, filename.c_str(), filename_len, 0);
|
|
||||||
|
|
||||||
// Send the file size
|
// Send the file size
|
||||||
std::cout << Logger::INFO << "Send file size: " << file_size << std::endl;
|
if (send(sock, &file_size, sizeof(file_size), 0) == -1) {
|
||||||
send(sock, &file_size, sizeof(file_size), 0);
|
std::cerr << "Failed to send file size" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
char buffer[BUFFER_SIZE];
|
while (file_size > 0) {
|
||||||
// Send the file data
|
|
||||||
while (file_size > 0)
|
|
||||||
{
|
|
||||||
infile.read(buffer, BUFFER_SIZE);
|
infile.read(buffer, BUFFER_SIZE);
|
||||||
send(sock, buffer, infile.gcount(), 0);
|
ssize_t bytes_sent = send(sock, buffer, infile.gcount(), 0);
|
||||||
file_size -= infile.gcount();
|
if (bytes_sent == -1) {
|
||||||
std::cout << Logger::INFO << "wait for ACK" << std::endl;
|
std::cerr << "Failed to send file data" << std::endl;
|
||||||
|
pthread_exit(nullptr);
|
||||||
|
}
|
||||||
|
file_size -= bytes_sent;
|
||||||
|
|
||||||
// Wait for acknowledgment after each chunk
|
// Wait for acknowledgment after each chunk
|
||||||
if (!receiveAck(sock))
|
if (!receiveAck(sock)) {
|
||||||
{
|
std::cerr << "Failed to receive acknowledgment" << std::endl;
|
||||||
std::cout << Logger::ERROR << "Failed to receive acknowledgment" << std::endl;;
|
pthread_exit(nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
send(sock, 0, 0, 0);
|
std::cout << "File sent successfully: " << filepath << std::endl;
|
||||||
|
|
||||||
std::cout << Logger::INFO << "File sent successfully: " << filename.c_str() << std::endl;
|
|
||||||
|
|
||||||
infile.close();
|
infile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *send_files_thread(void *args) {
|
||||||
|
ThreadArgs *thread_args = static_cast<ThreadArgs *>(args);
|
||||||
|
int sock = thread_args->sock;
|
||||||
|
fs::path cwd = thread_args->directory;
|
||||||
|
delete thread_args;
|
||||||
|
std::cout << "cwd is: " << cwd << std::endl;
|
||||||
|
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
|
for (const auto &entry : fs::recursive_directory_iterator(cwd)) {
|
||||||
|
path path = entry.path();
|
||||||
|
std::cout << "path is " << path << std::endl;
|
||||||
|
if (fs::is_regular_file(path)) {
|
||||||
|
std::cout << "regular file | path is: " << path << std::endl;
|
||||||
|
sendFile(sock, path.parent_path(), path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
close(sock);
|
close(sock);
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int find_server(char *server_ip)
|
int find_server(char *server_ip) {
|
||||||
{
|
std::cout << Logger::INFO << "Init find_server" << std::endl;
|
||||||
std::cout << Logger::INFO << "Init find_server" << std::endl;;
|
|
||||||
int sockfd;
|
int sockfd;
|
||||||
struct sockaddr_in multicast_addr;
|
struct sockaddr_in multicast_addr;
|
||||||
|
|
||||||
std::cout << Logger::INFO << "Create socket" << std::endl;
|
std::cout << Logger::INFO << "Create socket" << std::endl;
|
||||||
|
|
||||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "Socket creation error" << std::endl;
|
std::cout << Logger::ERROR << "Socket creation error" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -147,14 +149,12 @@ int find_server(char *server_ip)
|
|||||||
std::cout << Logger::INFO << "Send DISCOVER_SERVER" << std::endl;
|
std::cout << Logger::INFO << "Send DISCOVER_SERVER" << std::endl;
|
||||||
|
|
||||||
const char *multicast_message = "DISCOVER_SERVER";
|
const char *multicast_message = "DISCOVER_SERVER";
|
||||||
if (sendto(sockfd, multicast_message, strlen(multicast_message), 0, (struct sockaddr *)&multicast_addr, sizeof(multicast_addr)) < 0)
|
if (sendto(sockfd, multicast_message, strlen(multicast_message), 0,
|
||||||
{
|
(struct sockaddr *)&multicast_addr, sizeof(multicast_addr)) < 0) {
|
||||||
std::cout << Logger::ERROR << "sendto failed" << std::endl;
|
std::cout << Logger::ERROR << "sendto failed" << std::endl;
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << Logger::INFO << "send multicast message success" << std::endl;
|
std::cout << Logger::INFO << "send multicast message success" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,24 +162,23 @@ int find_server(char *server_ip)
|
|||||||
socklen_t len = sizeof(cliaddr);
|
socklen_t len = sizeof(cliaddr);
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
ssize_t n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&cliaddr, &len);
|
ssize_t n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0,
|
||||||
|
(struct sockaddr *)&cliaddr, &len);
|
||||||
std::cout << Logger::INFO << "recvfrom n: %i" << n << std::endl;
|
std::cout << Logger::INFO << "recvfrom n: %i" << n << std::endl;
|
||||||
if (n < 0)
|
if (n < 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "recvfrom failed" << std::endl;
|
std::cout << Logger::ERROR << "recvfrom failed" << std::endl;
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
std::cout << Logger::INFO << "buffer: " << buffer << std::endl;
|
std::cout << Logger::INFO << "buffer: " << buffer << std::endl;
|
||||||
buffer[n] = '\0';
|
buffer[n] = '\0';
|
||||||
if (strcmp(buffer, "SERVER_HERE") == 0)
|
if (strcmp(buffer, "SERVER_HERE") == 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::INFO << "Server found" << std::endl;
|
std::cout << Logger::INFO << "Server found" << std::endl;
|
||||||
inet_ntop(AF_INET, &cliaddr.sin_addr, server_ip, INET_ADDRSTRLEN);
|
inet_ntop(AF_INET, &cliaddr.sin_addr, server_ip, INET_ADDRSTRLEN);
|
||||||
}
|
} else {
|
||||||
else
|
std::cout << Logger::ERROR << "Unable to find server, close socket"
|
||||||
{
|
<< std::endl;
|
||||||
std::cout << Logger::ERROR << "Unable to find server, close socket" << std::endl;;
|
;
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -188,11 +187,10 @@ int find_server(char *server_ip)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int transfer_files(fs::path directory, char **filenames, int file_count) {
|
int transfer_files(fs::path directory) {
|
||||||
std::cout << Logger::INFO << "Init transfer_files" << std::endl;
|
std::cout << Logger::INFO << "Init transfer_files" << std::endl;
|
||||||
char server_ip[INET_ADDRSTRLEN];
|
char server_ip[INET_ADDRSTRLEN];
|
||||||
if (find_server(server_ip) != 0)
|
if (find_server(server_ip) != 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "Failed to find server" << std::endl;
|
std::cout << Logger::ERROR << "Failed to find server" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -200,8 +198,7 @@ int transfer_files(fs::path directory, char **filenames, int file_count) {
|
|||||||
int sock = 0;
|
int sock = 0;
|
||||||
struct sockaddr_in serv_addr;
|
struct sockaddr_in serv_addr;
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "Socket creation error" << std::endl;
|
std::cout << Logger::ERROR << "Socket creation error" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -209,30 +206,26 @@ int transfer_files(fs::path directory, char **filenames, int file_count) {
|
|||||||
serv_addr.sin_family = AF_INET;
|
serv_addr.sin_family = AF_INET;
|
||||||
serv_addr.sin_port = htons(PORT);
|
serv_addr.sin_port = htons(PORT);
|
||||||
|
|
||||||
if (inet_pton(AF_INET, server_ip, &serv_addr.sin_addr) <= 0)
|
if (inet_pton(AF_INET, server_ip, &serv_addr.sin_addr) <= 0) {
|
||||||
{
|
std::cout << Logger::ERROR << "Invalid address / Address not supported"
|
||||||
std::cout << Logger::ERROR << "Invalid address / Address not supported" << std::endl;
|
<< std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
|
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "Connection failed" << std::endl;
|
std::cout << Logger::ERROR << "Connection failed" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_t file_thread;
|
pthread_t file_thread;
|
||||||
ThreadArgs *thread_args = new ThreadArgs{filenames, file_count, sock, directory};
|
ThreadArgs *thread_args = new ThreadArgs{sock, directory};
|
||||||
if (pthread_create(&file_thread, nullptr, send_files_thread,
|
if (pthread_create(&file_thread, nullptr, send_files_thread,
|
||||||
(void *)thread_args) < 0)
|
(void *)thread_args) < 0) {
|
||||||
{
|
|
||||||
std::cout << Logger::ERROR << "Thread creation failed" << std::endl;
|
std::cout << Logger::ERROR << "Thread creation failed" << std::endl;
|
||||||
close(sock);
|
close(sock);
|
||||||
delete thread_args;
|
delete thread_args;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << Logger::INFO << "Wait for file_thread" << std::endl;
|
std::cout << Logger::INFO << "Wait for file_thread" << std::endl;
|
||||||
pthread_join(file_thread, nullptr);
|
pthread_join(file_thread, nullptr);
|
||||||
}
|
}
|
||||||
@@ -240,18 +233,15 @@ int transfer_files(fs::path directory, char **filenames, int file_count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef __SWITCH__ // for desktop
|
#ifndef __SWITCH__ // for desktop
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[]) {
|
||||||
{
|
if (argc < 1) {
|
||||||
if (argc < 2)
|
std::cerr << "Usage: " << argv[0] << " <path> " << std::endl;
|
||||||
{
|
|
||||||
std::cerr << "Usage: " << argv[0] << " <filename1> <filename2> ..." << std::endl;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char **filenames = const_cast<char **>(&argv[1]);
|
fs::path directory = fs::path(argv[1]);
|
||||||
int file_count = argc - 1;
|
std::cout << "directory is " << directory << std::endl;
|
||||||
|
transfer_files(directory);
|
||||||
transfer_files(filenames, file_count);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user