-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
269 lines (248 loc) · 9.78 KB
/
client.cpp
File metadata and controls
269 lines (248 loc) · 9.78 KB
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
#include "common.h"
#include "BoundedBuffer.h"
#include "Histogram.h"
#include "common.h"
#include "HistogramCollection.h"
#include "NetworkRequestChannel.h"
using namespace std;
void *patient_function(BoundedBuffer &buff, mutex &m, int patient, int requests) {
/* What will the patient threads do? */
//going to request first {requests} datapoints of {patient} and default {ecg 1}
double val = requests*.004;
patient += 1; //try this for patient being 0
datamsg datmsg(patient,val,1);
for(int i=0; i<requests; i+=1){//potential no like increment by .004
datmsg.seconds = i*.004;
//datmsg is updated now just push into buff
char* ms = new char[sizeof(datamsg)];
*(datamsg*)ms = datmsg;
char* msC = reinterpret_cast<char*>(ms);
vector<char> msV(msC,msC+sizeof(datamsg));
buff.push(msV);
delete[] ms;
//just example of how i pushed msg into buff previously to copy
//MESSAGE_TYPE quit_msg = QUIT_MSG;
//char *quitbuff = new char[sizeof(datamsg)];
//*(MESSAGE_TYPE *) quitbuff = quit_msg; //MESSAGE TYPE TO DATAMSG
//char *quitbuffc = reinterpret_cast<char *>(quitbuff);
//vector<char> quitbuffv(quitbuffc, quitbuffc + sizeof(MESSAGE_TYPE));
//request_buffer.push(quitbuffv);
}
return nullptr;
}
void *worker_function(BoundedBuffer &buff, bool isFileReq, NRC *chan, mutex &m,HistogramCollection& hc,string hostname,string port) {
/*MESSAGE_TYPE c = NEWCHANNEL_MSG;
m.lock();
chan->cwrite(&c, sizeof(MESSAGE_TYPE));
char *buf = chan->cread();
m.unlock();*/
NRC wChan(hostname, port);
//cout<<buf<<endl;
bool cond = true;
while (cond) {
vector<char> a = buff.pop();
char *a1 = reinterpret_cast<char *>(a.data());
MESSAGE_TYPE msg = *(MESSAGE_TYPE *) a1;
char* readStuff;
string filename;
if(msg==QUIT_MSG) {//quitmsg
buff.push(a);
cond = false;
}
else if(msg==FILE_MSG) {//filemsg
filemsg fm = *(filemsg*)a1;
wChan.cwrite(a1, a.size());
readStuff = wChan.cread();
//now write to file here
filename = "./received/y";
filename += (a1 + sizeof(filemsg));
//use special mutex?
m.lock();
int fd = open(filename.c_str(), O_WRONLY | O_CREAT, 0644);
lseek(fd,fm.offset,SEEK_SET);
write(fd,readStuff,fm.length);
close(fd);
m.unlock();
}
else if(msg==DATA_MSG) {//datamsg
datamsg dm = *(datamsg*)a1;
wChan.cwrite(&dm, sizeof(datamsg));
readStuff = wChan.cread();
//cout<<*(double*)readStuff<<endl;
// m.lock();
hc.update(dm.person-1,*(double*)(readStuff));
// m.unlock();
}
}
MESSAGE_TYPE q = QUIT_MSG;
wChan.cwrite((char *) &q, sizeof(MESSAGE_TYPE));
return nullptr;
}
int main(int argc, char *argv[]) {
mutex mu;
int n = 100; //default number of requests per "patient"
int p = 10; // number of patients [1,15]
int w = 100; //default number of worker threads
int b = 20; // default capacity of the request buffer, you should change this default
int m = MAX_MESSAGE; // default capacity of the file buffer
string port = "";
string hostname = "";
string filename;
srand(time_t(NULL));
//parse command line args
int opt;
while ((opt = getopt(argc, argv, "n:p:w:b:m:f:h:r:")) != -1) {
switch (opt) {
case 'n':
n = atoi(optarg);
break;
case 'p':
p = atoi(optarg);
break;
case 'w':
w = atoi(optarg);
break;
case 'b':
b = atoi(optarg);
break;
case 'm':
m = atoi(optarg);
break;
case 'f':
filename = optarg;
break;
case 'h':
hostname = optarg;
break;
case 'r':
port = (optarg);
default:
//cout << "invalid option please use command line parameters to specify what action to take" << endl;
break;
}
}
/*int pid = fork();
if (pid == 0) {
// modified to pass along m
string sm = to_string(m);
execl("dataserver", "dataserver", sm.c_str(), (char *) NULL);
}*/
NRC *chan = new NRC(hostname, port);
BoundedBuffer request_buffer(b);
HistogramCollection hc;
bool isFileReq = (!filename.empty());
struct timeval start, end;
gettimeofday(&start, 0);
/* Start all threads here */
if (filename.empty()) {
//making worker and patient threads
hc.fillHist(p);
vector<thread> workerVect;
workerVect.reserve(w);
for (int i = 0; i < w; i++) {
workerVect.emplace_back(worker_function, ref(request_buffer), isFileReq, chan,
ref(mu),ref(hc),hostname,port);//worker_functions,args <- add args//give channel
}
vector<thread> patientVect;
patientVect.reserve(p);
for (int i = 0; i < p; i++) {
patientVect.emplace_back(patient_function, ref(request_buffer),ref(mu),i,n);//patient_functions,args <- add args
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//makes sure all patient threads have finished their jobs loading all commands
for (int i = 0; i < p; i++) {
if(patientVect[i].joinable())
patientVect[i].join();
}
//then sends quit message to worker threads that will be placed in queue after all patient commands done
//QUIT STUFF HERE TO STOP WORKER THREADS WITH INFINITE LOOP
MESSAGE_TYPE quit_msg = QUIT_MSG;
char *quitbuff = new char[sizeof(datamsg)];
*(MESSAGE_TYPE *) quitbuff = quit_msg; //MESSAGE TYPE TO DATAMSG
char *quitbuffc = reinterpret_cast<char *>(quitbuff);
vector<char> quitbuffv(quitbuffc, quitbuffc + sizeof(MESSAGE_TYPE));
request_buffer.push(quitbuffv);
delete[] quitbuff;
//request_buffer.push(vector<char>((char*)(quit_msg),(char*)(quit_msg) + sizeof(MESSAGE_TYPE)));
/////////////////////////`//////////////////////////////////////////////////////////////////////////////////////////
for (int i = 0; i < w; i++) {
// if(workerVect[i].joinable())
workerVect[i].join();
}
} else {
//just making worker threads
vector<thread> workerVect;
workerVect.reserve(w);
for (int i = 0; i < w; i++) {
workerVect.emplace_back(worker_function, ref(request_buffer), isFileReq, chan,
ref(mu),ref(hc),hostname,port);//worker_functions,args <- add args//give channel
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__int64_t size;
filemsg fmsg(0, 0);
//gets file size;
//need to append string to end of fmsg
int total_size = sizeof(filemsg) + filename.size() + 1;
char buff[total_size];
memcpy(buff, &fmsg, sizeof(filemsg));
strcpy(buff + sizeof(filemsg), filename.c_str());
mu.lock();
chan->cwrite(buff, total_size);
char *buf = chan->cread();
mu.unlock();
//cast buf which will be special __int64_t
size = *(__int64_t *) (buf); // now holds full file size
delete buf;
cout << "file size is: " << size << endl;
/////////////////////////////////////
int length = 0;
int offset = 0;
if (size >= MAX_MESSAGE) {
length = MAX_MESSAGE;
} else {
length = size;
}
while (size > 0) {
fmsg.offset = offset;
fmsg.length = length;
//cout << offset << " " << length << endl;
//cout << buff << endl;
memcpy(buff, &fmsg, sizeof(filemsg));
//now move buffer to vector<char>
vector<char> pushme(buff,buff+total_size);
request_buffer.push(pushme);
size -= length;
offset += length;
if (size >= MAX_MESSAGE) {
length = MAX_MESSAGE;
} else {//case where size is smaller than max
length = size;
}
}
/////////////////////////////////////
//QUIT STUFF HERE TO STOP WORKER THREADS WITH INFINITE LOOP
MESSAGE_TYPE quit_msg = QUIT_MSG;
char *quitbuff = new char[sizeof(datamsg)];
*(MESSAGE_TYPE *) quitbuff = quit_msg; //MESSAGE TYPE TO DATAMSG
char *quitbuffc = reinterpret_cast<char *>(quitbuff);
vector<char> quitbuffv(quitbuffc, quitbuffc + sizeof(MESSAGE_TYPE));
request_buffer.push(quitbuffv);
delete[] quitbuff;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//end join threads
for (int i = 0; i < w; i++) {
// if(workerVect[i].joinable());
workerVect[i].join();
}
}
/* Join all threads at end of each if statement*/
gettimeofday(&end, 0);
hc.print();
int secs = (end.tv_sec * 1e6 + end.tv_usec - start.tv_sec * 1e6 - start.tv_usec) / (int) 1e6;
int usecs = (int) (end.tv_sec * 1e6 + end.tv_usec - start.tv_sec * 1e6 - start.tv_usec) % ((int) 1e6);
cout << "Took " << secs << " seconds and " << usecs << " micor seconds" << endl;
MESSAGE_TYPE q = QUIT_MSG;
chan->cwrite((char *) &q, sizeof(MESSAGE_TYPE));
cout << "All Done!!!" << endl;
delete chan;
}