-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrxprotocol.cpp
More file actions
277 lines (218 loc) · 5.86 KB
/
rxprotocol.cpp
File metadata and controls
277 lines (218 loc) · 5.86 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
270
271
272
273
274
275
276
277
#include "rxprotocol.hpp"
#include "logger.hpp"
#include "config.hpp"
#include "task.hpp"
#include "utils.hpp"
#include "workspace.hpp"
#include "regex/regex.hpp"
#include "pstream.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <ctime>
#include <cmath>
namespace remexec {
using namespace std;
using namespace sinlib;
using namespace redi;
#ifdef REGEX_BOOST
using boost::regex_replace;
#endif
RXProtocol::RXProtocol(Config &conf) : in(nullptr), out(nullptr), config(conf) {}
RXProtocol::~RXProtocol(){}
void RXProtocol::response(string status, string info, unordered_map<string, string> params){
*out << status;
if (!info.empty())
*out << " " << info;
*out << endl;
for (auto &p : params){
*out << p.first << ": " << encodeParameterValue(p.second) << endl;
}
*out << endl;
}
void RXProtocol::error(int code, string info){
response("ERROR " + to_string(code), info);
}
void RXProtocol::process(istream &in, ostream &out){
this->in = ∈
this->out = &out;
Workspace workspace(config);
string line;
while (getline(in, line)){
if (line.empty())
continue;
vector<string> args = regex_split(line, regex("\\s+"), 2);
unordered_map<string, string> params;
//command and argument (CMD arg)
string cmd = args[0], arg = args.size() > 1 ? args[1] : "";
//read parameters loop
while (getline(in, line)){
if (line.empty())
break;
vector<string> pars = regex_split(line, regex(":\\s*"), 2);
if (pars.size() < 2){
Log::warn("Non-parameter line, skipping: ", line);
continue;
}
Log::debug("Parameter: ", pars[0], ": ", pars[1]);
params[pars[0]] = pars[1];
}
if (cmd == "EXIT"){
Log::debug("Client exit by protocol");
return;
}
else if (cmd == "FILE"){
auto pname = params.find("Name");
auto psize = params.find("Size");
if (pname != params.end() && psize != params.end()){
string tmpath = workspace.getWorkdir();
string name = regex_replace(regex_replace(decodeParameterValue(pname->second), regex("\\\\"), "/"), regex("\\.{1,2}/"), "");
size_t size = stoull(psize->second);
Log::debug("Appended file: ", name, " ", size);
ofstream of(tmpath + name, ios::binary);
size_t copyed = 0;
if (of.good()){
copyed = bscopy(of, in, size);
}
of.close();
if (copyed == size){
in.get(); in.get(); // '\n\n'
response("OK");
} else {
//TODO: implement in sockets seekoff and seekpos
class : public streambuf {
protected:
virtual int overflow(int c){ return c; }
} nullbuf;
ostream null(&nullbuf);
bscopy(null, in, size - copyed + 2);
error(5, "Can't create file: " + name);
}
} else {
error(3, "Name or Size parameter not set");
}
}
else if (cmd == "EXEC"){
arg = decodeParameterValue(arg);
string task = realpath(config.getString(Config::TASK_DIR)) + "/" + arg;
string taskconf = realpath(config.getString(Config::TASKCONF_DIR)) + "/" + arg + ".conf";
string tasktmp = workspace.getWorkdir();
Config tconf(config);
if (!tconf.loadFromFile(taskconf)){
Log::debug("No config file for task ", arg);
}
size_t ttimeout = tconf.getInteger(Config::TASK_TIMEOUT);
Log::debug("Task binary: ", task);
Log::debug("Task tmp: ", tasktmp);
Log::debug("Task timeout: ", ttimeout);
vector<string> targs;
auto pargs = params.find("Arguments");
if (pargs != params.end()){
targs = regex_split(pargs->second, regex("\\s*;\\s*"));
for (auto &targ : targs){
targ = decodeParameterValue(targ);
}
}
if (path_exists(task)){
OutPackerBuf obuf(1, 128, out), ebuf(2, 128, out);
ostream osbuf(&obuf), esbuf(&ebuf);
response("OK");
Task t(arg, targs, task, tasktmp, ttimeout);
bool ok = t.run(osbuf, esbuf);
osbuf.flush();
esbuf.flush();
if (ok){
response("END");
} else {
error(6, "Task execution timeout exceeded");
}
} else {
error(2, "Task not found: " + arg);
}
}
else if (cmd == "FETCH"){
arg = decodeParameterValue(arg);
string tmpath = workspace.getWorkdir();
string path = tmpath + "/" + regex_replace(arg, regex("\\.{1,2}/"), "");
ifstream file(path);
if (file.good()){
string name = basename(path);
size_t sz;
file.seekg(0, ios::end);
sz = file.tellg();
file.seekg(0, ios::beg);
Log::debug("Fetched file (size: ", sz, "): ", path);
response("FILE", "", {
{"Name", name},
{"Size", to_string(sz)},
});
bscopy(out, file, sz);
out << endl << endl;
} else {
error(4, "File not found");
}
}
else {
error(1, "Unknown command: " + cmd);
}
}
Log::debug("Client exit by end of stream");
}
string RXProtocol::encodeParameterValue(const string &val){
string res;
res.reserve(val.size());
for (auto &c : val){
if ("%; \t\n\r"s.find(c) != string::npos){
res += "%";
res += stohex(string(1, c));
} else {
res += c;
}
}
return move(res);
}
string RXProtocol::decodeParameterValue(const string &val){
string res;
res.reserve(val.size());
for (size_t i = 0; i < val.size(); ++i){
if (val[i] == '%' && i+2 < val.size()){
res += hextos(val.substr(i+1, 2));
i += 2;
} else {
res += val[i];
}
}
return move(res);
}
RXProtocol::OutPackerBuf::OutPackerBuf(int f, size_t bsize, ostream& o){
fd = f;
bufsize = bsize;
out = &o;
buf = new char_type[bsize];
setp(buf, buf + (bsize - 1));
}
RXProtocol::OutPackerBuf::~OutPackerBuf(){
delete[] buf;
}
int RXProtocol::OutPackerBuf::overflow(int ch){
if (out->good() && ch != EOF){
*pptr() = ch;
pbump(1);
return sync() == 0 ? ch : EOF;
}
return EOF;
}
int RXProtocol::OutPackerBuf::sync(){
if (pptr() == pbase())
return 0;
auto sz = (size_t) (pptr() - pbase());
*out << "STREAM " << fd << endl
<< "Size: " << sz << endl
<< endl;
out->write(pbase(), sz);
*out << endl << endl;
pbump(-sz);
return out->good() ? 0 : -1;
}
}