-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpw-cp.cpp
356 lines (276 loc) · 8.73 KB
/
mpw-cp.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**************************************************************
* This file is part of the MPWide communication library
*
* Written by Derek Groen with thanks going out to Steven Rieder,
* Simon Portegies Zwart, Joris Borgdorff, Hans Blom and Tomoaki Ishiyama.
* for questions, please send an e-mail to:
* MPWide is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* MPWide is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MPWide. If not, see <http://www.gnu.org/licenses/>.
* **************************************************************/
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <string>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
#include "MPWide.h"
#include "mpwide-macros.h"
#define min(X,Y) ((X) < (Y) ? (X) : (Y))
#define CLIENT_BINDING 0
char prefix[256] = "test";
bool add_suffix = false;
char local_dir[256] = "/data/GreeM/Snapshot";
long buffer_size = MpwCpReadBufferSize; //obtained from mpwide-macros.h
static vector <string> filelist;
static vector <string> allfiles;
/* Keeps a timestamp to track which files are older than the last check time. */
static time_t timestamp = 0;
int findfile(char* pattern) {
for(int i=0;i<allfiles.size();i++) {
if(allfiles[i].compare(pattern) == 0) { return i; }
}
return -1;
}
void add_file_to_list(string filename) {
filelist.push_back(filename);
allfiles.push_back(filename);
}
int list_size() {
return filelist.size();
}
void remove_file_from_list() {
filelist.erase(filelist.begin());
}
char *fname_from_path (char *pathname)
{
char *fname = NULL;
if (pathname) {
fname = strrchr (pathname, '/') + 1;
}
return fname;
}
int check_new_files()
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(local_dir)) == NULL) {
cout << "Error(" << errno << ") opening " << local_dir << endl;
return errno;
}
while (dirp = readdir(dp)) {
if(dirp->d_name[0] != '.' && strstr(dirp->d_name,"writing") == NULL) {
char* writefname = (char*) malloc(sizeof(dirp->d_name)+sizeof(local_dir)+8);
sprintf(writefname,"%s/%s.writing",local_dir,dirp->d_name);
if(!access(writefname,W_OK)) {
cout << "Writefile " << writefname << " has been found. " << dirp->d_name << " will not be copied yet." << endl;
sleep(1);
}
else if(findfile(dirp->d_name)==-1) {
int dup_found = 0;
for(int i=0; i<filelist.size();i++) {
if((filelist.at(0)).c_str()==dirp->d_name)
dup_found++;
}
if(!dup_found) {
add_file_to_list(string(dirp->d_name));
cout << "Added " << string(dirp->d_name) << " to the list." << endl;
cout << "List size = " << filelist.size() << endl;
}
}
free(writefname);
}
}
closedir(dp);
return 0;
}
/* Get the size of a file. */
long get_size(FILE* f) {
fseek (f , 0 , SEEK_END);
long size = ftell (f);
rewind (f);
return size;
}
void make_fname(char* a) {
memcpy(a,local_dir,strlen(local_dir));
memcpy(a+strlen(local_dir), prefix, strlen(prefix)+1);
}
void reconfig(int size, long long int pacing_rate, long long int winsize) {
for(int i=0; i<size; i++) {
MPW_setWin(i, winsize);
}
MPW_setPacingRate(pacing_rate);
cout << "Pacing rate set to: " << pacing_rate << ", window size set to: " << winsize << "." << endl;
}
int main(int argc, char** argv){
string a;
/* Initialize */
int flag = atoi(argv[2]);
int baseport = 16256; //atoi(argv[3]);
string host = (string) argv[1];
if(argc>3) {
sprintf(local_dir, "%s", argv[3]);
}
/* Define 8 different streams */
int size = 96;
if(argc>4) {
size = atoi(argv[4]);
}
/* string *hosts = new string[size]; // = {host,host,host,host,host,host,host,host};
int sports[size]; // = {6000,6001,6002,6003,6004,6005,6006,6007};
int cports[size];
int channel[size];
for(int i=0; i<size; i++) {
hosts[i] = host;
sports[i] = baseport+i;
cports[i] = baseport+size+i;
channel[i] = i;
}*/
// cout << "s:" << sports[0] << ", c:" << cports[0] << ", endpoint: " << hosts[0] << endl;
int path_id = 0;
#if CLIENT_BINDING > 0
path_id = MPW_CreatePath(host, baseport, size);
//MPW_Init(hosts,sports,cports,size);
#else
path_id = MPW_CreatePath(host, baseport, size);
//MPW_Init(hosts,sports,size);
#endif
if(argc>6) {
long long int pr = atoi(argv[5]) * 1024*1024;
long long int win = atoi(argv[6]) * 1024;
reconfig(size,pr,win);
}
cout << "FLAG = " << flag << endl;
if(flag) { //client mode
//while(true) {
cout << "Client." << endl;
char fname[256];
int status;
struct stat st_buf;
status = stat (local_dir, &st_buf);
if (status != 0) {
printf ("Error, local file or directory not accessible. Errno = %d\n", errno);
return 1;
}
int is_file = 0;
if (S_ISREG (st_buf.st_mode)) {
add_file_to_list(local_dir);
is_file = 1;
}
if (S_ISDIR (st_buf.st_mode)) {
check_new_files();
}
int wait_cnt = 0;
/*while(!filelist.size()){
usleep(1000);
check_new_files();
wait_cnt++;
if(wait_cnt>9) { wait_cnt = 0; cout << "."; }
}*/
int a[1];
a[0] = filelist.size();
int *ch = {0};
MPW_Send((char*) a, 4, path_id);
while(filelist.size()) {
cout << "no. of files = " << filelist.size() << endl;
// cout << local_dir << endl;
// cout << (filelist.at(0)).c_str() << endl;
if(is_file == 1) { sprintf(fname,"%s", local_dir); }
else {
sprintf(fname,"%s/%s",local_dir,(filelist.at(0)).c_str());
}
cout << "Trying to open the file: " << fname << endl;
FILE* f = fopen(fname,"r");
char* buf = (char*) malloc(buffer_size);
uint32_t nfsize = htonl((uint32_t) get_size(f));
long fsize = get_size(f);
cout << "fsize = " << fsize << endl;
char fn[256];
if(is_file == 1) {
char* t = fname_from_path(local_dir);
strcpy(fn,t);
} else {
strcpy(fn,filelist.at(0).c_str());
}
MPW_Send(fn, 256, path_id);
MPW_Send((char*) &nfsize, 4, path_id);
cout << "Starting main loop."<< endl;
for(long i = 0; i<fsize;i+=buffer_size) {
cout << i << " bytes sent." << endl;
long read_len = min(fsize-i,buffer_size);
long bytes_read = fread(buf,1,read_len,f);
if(bytes_read != read_len) {
cout << "Read error: Number of bytes read does not match expected amount." << endl;
cout << "Read: " << bytes_read << " bytes. Expected: " << read_len << endl;
}
// MPW_Send(buf,read_len, path_id);
char dummysend[100];
MPW_SendRecv(buf, read_len, dummysend, 100, path_id);
}
remove_file_from_list();
free(buf);
fclose(f);
cout << "done." << endl;
}
}
else { //server mode
cout << "Server." << endl;
int listsize = 0;
MPW_Recv((char*)(&listsize), 4, path_id);
cout << "no. of files = " << listsize << endl;
while(listsize) {
//cout << "Server." << endl;
char fname_temp[256];
char* fname;
char dir_fname[256];
uint32_t rs_n = 0;
long recvsize = 0;
//cout << "Receiving file header info." << endl;
MPW_Recv(fname_temp, 256, path_id);
MPW_Recv((char*) &rs_n, 4, path_id);
fname = argv[3];
if(fname[strlen(fname)-1] == '/')
{
fname = dir_fname;
sprintf(fname,"%s/%s",local_dir,fname_temp);
}
cout << "Receiving file: " << fname << endl;
FILE* f = fopen(fname,"w");
recvsize = (long) ntohl(rs_n);
char* buf = (char*) malloc(buffer_size);
cout << "size = " << recvsize << "." << endl;
char dummysend[100];
for(long i = 0; i<recvsize; i += buffer_size) {
long read_len = min(recvsize-i,buffer_size);
//cout << "read_len = " << read_len << endl;
//MPW_Recv(buf,read_len,0);
MPW_SendRecv( dummysend, 100, buf, read_len, path_id);
long bytes_written = fwrite(buf,1,read_len,f);
}
listsize--;
free(buf);
fclose(f);
}
}
MPW_Finalize();
return 1;
}