this is what i need to implement, but my approach is too complicatedInput files
Each client will have an input file named in.txt (where R is the task rank), with the following format:
number_files_owned
name_fisier_detinut_1 number_segments_fisier_detinut_1
segment1_fisier_detinut_1
segment2_fisier_detinut_1
...
name_fisier_detinut_2 number_segments_fisier_detinut_2
segment1_fisier_detinut_2
segment2_fisier_detinut_2
...
number_fisiere_dorite
name_fisier_dorit_1
name_fisier_dorit_2
...
Thus, the first row contains the number of files owned by this client at the beginning of the program, followed by details about each file. For a file, the first row of details contains the name and number
of segments, and the following rows represent hashes (32 characters) of the file segments, in order (thus, the first row after the file name and size will contain the hash of the first segment of the file, and so on).
After the information about the files owned by a client, there is a row with the number of files that the client wants to download using BitTorrent, followed by the name of the desired files (one at a time).
Clients
As previously mentioned, the client, are all MPI tasks with rank higher than 0. As can be seen in the skeleton of the theme repository, a client has two threads of execution (implemented with
Pthreads), as follows:
• → thread this thread is used by the client to implement file download logic from seeds or peers and to achieve communication with the tracker
• → upload thread this execution thread is used to respond to segment requests that this customer owns, coming from other customers.
Initialising. At initialization, a client performs the following steps:
- read the input file
- tells the tracker what files it has (in other words, what files it is seed for)
• for each file, the client sends the hash to each segment in order
• so the tracker will know which files are in the network, how many segments they have, in what order they are, what hashes they have, and who owns them initially
- Expects a response from the tracker that can start searching and downloading files that is interested.
Downloading. Once it has received confirmation from the tracker that it can continue, the client realizes the following steps:
- asks the tracker for the list of seeds/peers for the files he wants (in other words, file swarms)
- Look at the segments missing from the files you want, search the swarm from the tracker, and start sending requests to peers/seeds
- For each segment of a desired file that it does not own, a client performs the following steps:
(a) look for a seed/peer that owns the segment sought
(b) send that seed/peer a request for the segment
(c) expect to receive the requested segment from seed/peer
(d) mark the segment as received
Attentiont! In order to receive the 20 points on efficiency (see the scoring section below), a customer must vary as much as possible the seed/node nodes/peer from which download the searched segments. With other
words, you do not want the download of a file to be done entirely from the same seed/peer.
Attentiont! Because it does not work with files themselves (but only with hashes of segments in a file), it will simulate downloading a segment of file from a seed/peer. This is enough as a customer
send a message to the seed/peer with the hash of the desired segment, and the/seed/peer to reply with a simple message like “ACK” or “OK” (it is at your discretion exactly how you approach this).
Updating. While a client is downloading a file, it is possible that another, and the client, is entering the swarm of that file, so it is necessary that occasionally, the tracker is consulted to update the client list, and from which a file can be downloaded. Thus, for this theme, a client performs the following steps after every 10 segments downloaded from seeds/peers:
- asks the tracker for the updated list of seeds/peers for the files they want
- resumes from step 2 of the Download section.
Receiving segment requests from other customers. When receiving a segment request from a peer, a customer first checks whether they own that segment. If he owns it, he's “send” to the peer the requested segment
(as specified above, this can be simulated by sending a message of type “ACK” or “OK”). If he does not own it, he sends the peer a negative answer (and he will look for the file elsewhere).
Completion download file. When you finish downloading all the segments of a file, a customer takes the following steps:
- inform the tracker that it has all the file
- Save the file (list of hashes in order) in an output file called client
(where R is the rank of the client, and NUMEFISIER is the original name of the downloaded file); an example of the name for the file1 file downloaded by the client with rank 2 is client2 file1.
Finish download all files. When you finish downloading all the files you wanted, a customer realizes the following steps:
- Sends the tracker a message informing him that he has completed all downloads
- close the download execution thread (download thread), but leave it open on the upload one (upload thread), because it will still be able to act as a seed for the files it had and for the ones it downloaded.
Completion for all customers. When a client receives a message from the tracker that all customers have finished downloading their files, realize the following steps:
- close the upload execution thread (upload thread)
- closes altogether.
Tracker
The tracker maintains a list of files and the swarm associated with each of them. The swarm of a file contains the list of clients (seeds or peers) that have segments of that file, but not what segments each client has.
A segment is defined by a hash and its position in the file.
Initialising. At initialization, the tracker performs the following steps:
- Wait for the initial message of each client, which will contain the list of owned files
- For each file owned by a client, passes the respective client as seed in the file swarm
- When received from all customers, respond to each with a “ACK” (sign that can start communication between customers to download the desired files).
Receiving messages from customers. When receiving a message from a customer, the tracker takes the following steps:
- Look what kind of message he received
- Depending on the type of message, perform one of the following actions:
• if the received message is a request from a client for a file (or an update request),
the tracker gives it the list of seeds/peers clients from swarm, and marks the client as a peer for that file (so it also adds it implicitly to the swarm of the file)
• if the received message is to complete a file download, the tracker marks that client as seed
• if the message received is to complete the download of all files by a client, the tracker marks the client as finished, but keeps it in the swarm of the files he owns (i.e
the ones he had originally or downloaded from other clients)
• if the message received means that all customers have finished downloading all the desired files, the tracker sends a message of completion to each client to tell them to stop,
and then he stops too.#include <mpi.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define TRACKER_RANK 0
#define MAX_FILES 10
#define MAX_FILENAME 15
#define HASH_SIZE 32
#define MAX_CHUNKS 100
#define MSG_INIT_FILES 1
#define MSG_ACK 2
#define MSG_REQUEST_SWARM 3
#define MSG_SWARM_RESPONSE 4
#define MSG_CHUNK_REQUEST 5
#define MSG_CHUNK_RESPONSE 6
#define MSG_FILE_COMPLETE 7
#define MSG_ALL_COMPLETE 8
#define MSG_SHUTDOWN 9
#define MSG_CHUNK_MISSING 10
typedef struct {
char hash[HASH_SIZE + 1];
bool downloaded;
int position;
} Chunk;
typedef struct{
char filename[MAX_FILENAME];
int num_chunks;
Chunk chunks[MAX_CHUNKS];
} File;
typedef struct{
int rank;
File owned_files[MAX_FILES];
int num_owned_files;
char wanted_files[MAX_FILES][MAX_FILENAME];
int num_wanted_files;
File downloading_files[MAX_FILES];
int num_downloading_files;
bool should_stop;
int chunks_downloaded;
} ClientData;
typedef struct{
int client_rank;
bool is_seed;
bool is_complete;
} PeerInfo;
typedef struct{
char filename[MAX_FILENAME];
int num_chunks;
char chunks_hash[MAX_CHUNKS][HASH_SIZE + 1];
PeerInfo peers[MAX_FILES];
int num_peers;
} SwarmInfo;
ClientData *client_data;
void initialize_client_data() {
client_data = (ClientData *)malloc(sizeof(ClientData));
if (!client_data) {
fprintf(stderr, "Failed to allocate memory for client_data\n");
exit(-1);
}
memset(client_data, 0, sizeof(ClientData));
}
void cleanup_client_data() {
if (client_data) {
free(client_data);
client_data = NULL;
}
}
void read_input_file(int rank, ClientData *client_data) {
char filename[20];
sprintf(filename, "in%d.txt", rank);
FILE *input = fopen(filename, "r");
if (!input) {
printf("Error opening input file for rank %d\n", rank);
exit(1);
}
fscanf(input, "%d", &client_data->num_owned_files);
for (int i = 0; i < client_data->num_owned_files; i++) {
fscanf(input, "%s %d", client_data->owned_files[i].filename,
&client_data->owned_files[i].num_chunks);
for (int j = 0; j < client_data->owned_files[i].num_chunks; j++) {
fscanf(input, "%s", client_data->owned_files[i].chunks[j].hash);
client_data->owned_files[i].chunks[j].downloaded = true;
client_data->owned_files[i].chunks[j].position = j;
}
}
fscanf(input, "%d", &client_data->num_wanted_files);
for (int i = 0; i < client_data->num_wanted_files; i++) {
fscanf(input, "%s", client_data->wanted_files[i]);
}
fclose(input);
}
void save_downloaded_file(int rank, const char* filename, File* file) {
char output_filename[MAX_FILENAME];
sprintf(output_filename, "client%d_%s", rank, filename);
FILE *output = fopen(output_filename, "w");
if (!output) {
printf("Error creating output file %s\n", output_filename);
return;
}
for (int i = 0; i < file->num_chunks; i++) {
fprintf(output, "%s\n", file->chunks[i].hash);
}
fclose(output);
}
void* download_thread_func(void* arg) {
int rank = (int)arg;
client_data->chunks_downloaded = 0;
// Send owned files to tracker
MPI_Send(&client_data->num_owned_files, 1, MPI_INT, TRACKER_RANK, MSG_INIT_FILES, MPI_COMM_WORLD);
for (int i = 0; i < client_data->num_owned_files; i++) {
MPI_Send(&client_data->owned_files[i], sizeof(File), MPI_BYTE, TRACKER_RANK, MSG_INIT_FILES, MPI_COMM_WORLD);
}
// Wait for confirmation to start downloading from tracker
int ack;
MPI_Recv(&ack, 1, MPI_INT, TRACKER_RANK, MSG_ACK, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (int i = 0; i < client_data->num_wanted_files && !client_data->should_stop; i++) {
strcpy(client_data->downloading_files[i].filename, client_data->wanted_files[i]);
MPI_Send(client_data->wanted_files[i], MAX_FILENAME, MPI_CHAR, TRACKER_RANK, MSG_REQUEST_SWARM, MPI_COMM_WORLD);
SwarmInfo swarm;
MPI_Recv(&swarm, sizeof(SwarmInfo), MPI_BYTE, TRACKER_RANK, MSG_SWARM_RESPONSE, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
client_data->downloading_files[i].num_chunks = swarm.num_chunks;
for (int j = 0; j < swarm.num_chunks; j++) {
strcpy(client_data->downloading_files[i].chunks[j].hash, swarm.chunks_hash[j]);
client_data->downloading_files[i].chunks[j].downloaded = false;
client_data->downloading_files[i].chunks[j].position = j;
}
// Initialize round-robin index
int peer_index = 0;
for (int j = 0; j < swarm.num_chunks; j++) {
if (client_data->should_stop) break;
// Select peer using round-robin
int peer_rank = swarm.peers[peer_index].client_rank;
peer_index = (peer_index + 1) % swarm.num_peers;
MPI_Send(&client_data->downloading_files[i].chunks[j], sizeof(Chunk), MPI_BYTE, peer_rank, MSG_CHUNK_REQUEST, MPI_COMM_WORLD);
char response[10];
MPI_Recv(response, 10, MPI_CHAR, peer_rank, MSG_CHUNK_RESPONSE, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (strcmp(response, "OK") == 0) {
client_data->downloading_files[i].chunks[j].downloaded = true;
client_data->chunks_downloaded++;
if (client_data->chunks_downloaded % 10 == 0) {
MPI_Send(client_data->wanted_files[i], MAX_FILENAME, MPI_CHAR, TRACKER_RANK, MSG_REQUEST_SWARM, MPI_COMM_WORLD);
MPI_Recv(&swarm, sizeof(SwarmInfo), MPI_BYTE, TRACKER_RANK, MSG_SWARM_RESPONSE, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
peer_index = 0;
}
}
}
save_downloaded_file(rank, client_data->wanted_files[i], &client_data->downloading_files[i]);
MPI_Send(client_data->wanted_files[i], MAX_FILENAME, MPI_CHAR, TRACKER_RANK, MSG_FILE_COMPLETE, MPI_COMM_WORLD);
}
MPI_Send(&rank, 1, MPI_INT, TRACKER_RANK, MSG_ALL_COMPLETE, MPI_COMM_WORLD);
return NULL;
}
void* upload_thread_func(void* arg) {
int rank = (int)arg;
while (!client_data->should_stop) {
MPI_Status status;
Chunk requested_chunk;
// Wait for chunk request
MPI_Recv(&requested_chunk, sizeof(Chunk), MPI_BYTE, MPI_ANY_SOURCE, MSG_CHUNK_REQUEST, MPI_COMM_WORLD, &status);
bool chunk_found = false;
// Search for chunk in owned files
for (int i = 0; i < client_data->num_owned_files; i++) {
for (int j = 0; j < client_data->owned_files[i].num_chunks; j++) {
if (strcmp(client_data->owned_files[i].chunks[j].hash, requested_chunk.hash) == 0) {
MPI_Send("OK", 3, MPI_CHAR, status.MPI_SOURCE, MSG_CHUNK_RESPONSE, MPI_COMM_WORLD);
chunk_found = true;
break;
}
}
if (chunk_found) break;
}
if (!chunk_found) {
MPI_Send("NO", 3, MPI_CHAR, status.MPI_SOURCE, MSG_CHUNK_RESPONSE, MPI_COMM_WORLD);
}
}
return NULL;
}
void tracker(int numtasks, int rank) {
SwarmInfo swarms[MAX_FILES];
int num_swarms = 0;
int completed_clients = 0;
bool* client_completed = calloc(numtasks, sizeof(bool));
// Wait for all clients to connect
for (int i = 1; i < numtasks; i++) {
int num_files;
MPI_Recv(&num_files, 1, MPI_INT, i, MSG_INIT_FILES, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (int j = 0; j < num_files; j++) {
File received_file;
MPI_Recv(&received_file, sizeof(File), MPI_BYTE, i, MSG_INIT_FILES, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Add client as seed for each file
bool found = false;
for (int k = 0; k < num_swarms; k++) {
if (strcmp(swarms[k].filename, received_file.filename) == 0) {
swarms[k].peers[swarms[k].num_peers].client_rank = i;
swarms[k].peers[swarms[k].num_peers].is_seed = true;
swarms[k].num_peers++;
found = true;
break;
}
}
if (!found) {
strcpy(swarms[num_swarms].filename, received_file.filename);
swarms[num_swarms].num_chunks = received_file.num_chunks;
for (int k = 0; k < received_file.num_chunks; k++) {
strcpy(swarms[num_swarms].chunks_hash[k], received_file.chunks[k].hash);
}
swarms[num_swarms].peers[0].client_rank = i;
swarms[num_swarms].peers[0].is_seed = true;
swarms[num_swarms].num_peers = 1;
num_swarms++;
}
}
}
// Send ack to all clients
for (int i = 1; i < numtasks; i++) {
int ack = 1;
MPI_Send(&ack, 1, MPI_INT, i, MSG_ACK, MPI_COMM_WORLD);
}
while (completed_clients < numtasks - 1) {
MPI_Status status;
char filename[MAX_FILENAME];
int client_rank;
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
switch (status.MPI_TAG) {
case MSG_REQUEST_SWARM:
MPI_Recv(filename, MAX_FILENAME, MPI_CHAR, status.MPI_SOURCE, MSG_REQUEST_SWARM, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Send swarm info
for (int i = 0; i < num_swarms; i++) {
if (strcmp(swarms[i].filename, filename) == 0) {
MPI_Send(&swarms[i], sizeof(SwarmInfo), MPI_BYTE, status.MPI_SOURCE, MSG_SWARM_RESPONSE, MPI_COMM_WORLD);
// Add client to swarm if not already present
bool found = false;
for (int j = 0; j < swarms[i].num_peers; j++) {
if (swarms[i].peers[j].client_rank == status.MPI_SOURCE) {
found = true;
break;
}
}
if (!found) {
swarms[i].peers[swarms[i].num_peers].client_rank = status.MPI_SOURCE;
swarms[i].peers[swarms[i].num_peers].is_seed = false;
swarms[i].num_peers++;
}
break;
}
}
break;
case MSG_FILE_COMPLETE:
MPI_Recv(filename, MAX_FILENAME, MPI_CHAR, status.MPI_SOURCE, MSG_FILE_COMPLETE, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Add client as seed for downloaded file
for (int i = 0; i < num_swarms; i++) {
if (strcmp(swarms[i].filename, filename) == 0) {
for (int j = 0; j < swarms[i].num_peers; j++) {
if (swarms[i].peers[j].client_rank == status.MPI_SOURCE) {
swarms[i].peers[j].is_seed = true;
break;
}
}
break;
}
}
break;
case MSG_ALL_COMPLETE:
MPI_Recv(&client_rank, 1, MPI_INT, status.MPI_SOURCE, MSG_ALL_COMPLETE, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
client_completed[client_rank] = true;
completed_clients++;
// Send shutdown message to all clients
if (completed_clients == numtasks - 1) {
for (int i = 1; i < numtasks; i++) {
MPI_Send(&i, 1, MPI_INT, i, MSG_SHUTDOWN, MPI_COMM_WORLD);
}
}
break;
}
}
free(client_completed);
}
void peer(int numtasks, int rank) {
pthread_t download_thread;
pthread_t upload_thread;
void *status;
int r;
initialize_client_data();
client_data->rank = rank;
client_data->should_stop = false;
client_data->chunks_downloaded = 0;
read_input_file(rank, client_data);
r = pthread_create(&download_thread, NULL, download_thread_func, (void *)&rank);
if (r) {
printf("Error creating download thread\n");
cleanup_client_data();
exit(-1);
}
r = pthread_create(&upload_thread, NULL, upload_thread_func, (void *)&rank);
if (r) {
printf("Error creating upload thread\n");
cleanup_client_data();
exit(-1);
}
int shutdown_msg;
MPI_Recv(&shutdown_msg, 1, MPI_INT, TRACKER_RANK, MSG_SHUTDOWN, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
client_data->should_stop = true;
r = pthread_join(download_thread, &status);
if (r) {
printf("Error joining download thread\n");
cleanup_client_data();
exit(-1);
}
r = pthread_join(upload_thread, &status);
if (r) {
printf("Error joining upload thread\n");
cleanup_client_data();
exit(-1);
}
cleanup_client_data();
}
int main(int argc, char *argv[]) {
int numtasks, rank;
int provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
if (provided < MPI_THREAD_MULTIPLE) {
fprintf(stderr, "MPI nu are suport pentru multi-threading\n");
exit(-1);
}
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == TRACKER_RANK) {
tracker(numtasks, rank);
} else {
peer(numtasks, rank);
}
MPI_Finalize();
}