-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmapreduce-worker.cc
66 lines (59 loc) · 1.95 KB
/
mapreduce-worker.cc
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
/**
* File: mapreduce-worker.cc
* -------------------------
* Presents the implementation of the MapReduceWorker class.
*/
#include "mapreduce-worker.h"
#include <cassert>
#include <sstream>
#include "mr-messages.h"
#include "string-utils.h"
#include "client-socket.h"
#include "socket++/sockstream.h"
using namespace std;
MapReduceWorker::MapReduceWorker(const string& serverHost, unsigned short serverPort,
const string& cwd, const string& executable,
const string& outputPath) :
serverHost(serverHost), serverPort(serverPort), cwd(cwd),
executable(executable), outputPath(outputPath) {}
bool MapReduceWorker::requestInput(string& name) const {
int clientSocket = getClientSocket();
sockbuf sb(clientSocket);
iosockstream ss(&sb);
sendWorkerReady(ss);
MRMessage message;
string payload;
receiveMessage(ss, message, payload);
if (message == kServerDone) return false;
name = trim(payload);
return true;
}
bool MapReduceWorker::processInput(const string& name, const string& output) const {
string executable = cwd + "/" + this->executable;
ostringstream oss;
oss << executable << " " << name << " " << output;
string command = oss.str();
return system(command.c_str()) == 0;
}
void MapReduceWorker::notifyServer(const string& name, bool success) const {
int clientSocket = getClientSocket();
sockbuf sb(clientSocket);
iosockstream ss(&sb);
if (success) {
sendJobSucceeded(ss, name);
} else {
sendJobFailed(ss, name);
}
}
void MapReduceWorker::alertServerOfProgress(const string& info) const {
int clientSocket = getClientSocket();
sockbuf sb(clientSocket);
iosockstream ss(&sb);
sendJobInfo(ss, info);
}
static const int kServerInaccessible = 2;
int MapReduceWorker::getClientSocket() const {
int clientSocket = createClientSocket(serverHost, serverPort);
if (clientSocket == kClientSocketError) exit(kServerInaccessible);
return clientSocket;
}