-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_transfer.h
122 lines (102 loc) · 3.15 KB
/
file_transfer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
We use a Peer-2-Peer architecture for handling the file
transfers involved during synchronization.
Here each node acts both as a server,
and a client.
This is the main P2P class that further uses
Server and Client classes for each Host --->
*/
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "p2p_client.h"
#include "p2p_server.h"
#define PR(s) \
{ \
perror((s)); \
exit(EXIT_FAILURE); \
}
int serv_port, client_port1, client_port2, client_port3;
// P2P class Handles File Transfers (Exists for each host)
class p2p
{
private:
int num_foreign_hosts;
char filepath1[1024];
char save_folder[1024];
public:
server s1;
client c1[5];
// Constructor, will initialize the P2P
p2p() { ; }
p2p(int serv_port, vector<pair<char *, int>> foreign_hosts)
{
num_foreign_hosts = foreign_hosts.size(); // All the other Hosts
// Server Initialized
s1 = server(serv_port);
for (int i = 0; i < foreign_hosts.size(); i++)
{
c1[i] = client(foreign_hosts[i].first, foreign_hosts[i].second);
}
}
/////////////////////////////////////////////////////////////
// [Only for Debugging] Trash function: Used to test the working of P2P without Watchdog
void file_to_transfer(char *file1, char *file2)
{
strcpy(filepath1, "books/");
strcat(filepath1, file1);
strcpy(save_folder, file2);
}
/////////////////////////////////////////////////////////////
// To initialize the P2P instance
int initialise(int index)
{
if (c1[index].connect2server()==0) {
return 0;
}
cout << "client initialized...";
s1.accept_connection();
cout << "server accepted";
return 1; //if everything worked smoothly
}
/////////////////////////////////////////////////////////////
// To send the files in addlist to all the other hosts
void server_filelist(string rootdir, filelist addlist)
{
cout << "starting file transfer..." << endl;
for (auto itr = addlist.begin(); itr != addlist.end(); itr++)
{
s1.send_file((char *)rootdir.c_str(), (char *)(*itr).c_str());
cout << "server sent file" << endl;
}
cout << "returning from server file list..." << endl;
}
void checkonline(){
s1.checkonline();
}
~p2p() { ; }
};
// }
// }
// ;
// int main(int argc, char * argv[]){
// serv_port = atoi(argv[1]); client_port1 = atoi(argv[2]); client_port2 = atoi(argv[3]);
// vector <pair <char *, int>> foreign_hosts;
// foreign_hosts.push_back({(char *)"127.0.0.1", client_port1});
// foreign_hosts.push_back({(char *)"127.0.0.1", client_port2});
// char *file1 = argv[4];
// char *save_folder = argv[5];
// p2p p1 (serv_port, foreign_hosts);
// p1.file_to_transfer(file1, save_folder);
// p1.start();
// return 0;
// }