-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
executable file
·67 lines (57 loc) · 1.5 KB
/
client.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
#include "lib.h"
#include <iostream>
#include <string>
#include "stdlib.h"
#include "MapReduce.h"
#include "assert.h"
int main(int argc, char* argv[])
{
if(argc != 4){
return 1;
}
int sockFd = connect_to_server(SOCK_STREAM, argv[1],argv[2]);
int count;
join_server_conn(sockFd, atoi(argv[3]), JOIN);
char buffer[500];
std::string input;;
while(1)
{
count = read(sockFd, buffer, sizeof buffer);
if(count <= 0){
if(count == 0){ //server disconnected
exit(-1);
}
continue;
}
Buffer buf(buffer,500,count);
TaskControlMessage msg;
msg.decode(buf);
switch(msg.taskType){
case TASK_TYPE_MAP:{
buf.reset();
msg.outputFile = mapReduce::mapFunctions[msg.taskName](msg.inputFile,msg.startOffset, msg.endOffset);
msg.encode(buf);
count = write(sockFd, buf.getBuffer(), buf.getSize());
if(count<=0){
exit(-1);
}
}
break;
case TASK_TYPE_REDUCE:{
buf.reset();
msg.outputFile = mapReduce::reduceFunctions[msg.taskName](msg.inputFile,msg.inputFile2);
msg.encode(buf);
count = write(sockFd, buf.getBuffer(), buf.getSize());
if(count<=0){
exit(-1);
}
}
break;
default:
assert(0);
break;
}
//count = write(sockFd, input.c_str(), input.size());
}
close(sockFd); //have a signal handler for the when the term sigals comes in
}