-
Notifications
You must be signed in to change notification settings - Fork 2
/
connector.hpp
305 lines (235 loc) · 6.99 KB
/
connector.hpp
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
#ifndef CONNECTOR
#define CONNECTOR
#include "message.hpp"
#include <iostream>
#include <sstream>
#include <vector>
#include <cstring>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
#pragma comment(lib, "AdvApi32.lib")
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#else
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#endif
namespace cpprofiler {
template <typename T>
class Option {
T value_;
bool present{false};
public:
bool valid() const { return present; }
void set(const T& t) { present = true; value_ = t; }
void unset() { present = false; }
const T& value() const { assert(present); return value_; }
T& value() { assert(present); return value_; }
};
class Connector;
class Node;
static void sendNode(Connector& c, Node& node);
class Node {
Connector& _c;
NodeUID node_;
NodeUID parent_;
int alt_;
int kids_;
NodeStatus status_;
Option<std::string> label_;
Option<std::string> nogood_;
Option<std::string> info_;
public:
Node(NodeUID node, NodeUID parent,
int alt, int kids, NodeStatus status, Connector& c)
: _c(c), node_{node}, parent_{parent},
alt_(alt), kids_(kids), status_(status) {}
Node& set_node_thread_id(int tid) {
node_.tid = tid;
return *this;
}
const Option<std::string>& label() const { return label_; }
Node& set_label(const std::string& label) {
label_.set(label);
return *this;
}
const Option<std::string>& nogood() const { return nogood_; }
Node& set_nogood(const std::string& nogood) {
nogood_.set(nogood);
return *this;
}
const Option<std::string>& info() const { return info_; }
Node& set_info(const std::string& info) {
info_.set(info);
return *this;
}
int alt() const { return alt_; }
int kids() const { return kids_; }
NodeStatus status() const { return status_; }
NodeUID nodeUID() const { return node_; }
NodeUID parentUID() const { return parent_; }
int node_id() const { return node_.nid; }
int parent_id() const { return parent_.nid; }
int node_thread_id() const { return node_.tid; }
int node_restart_id() const { return node_.rid; }
int parent_thread_id() const { return parent_.tid; }
int parent_restart_id() const { return parent_.rid; }
void send() { sendNode(_c, *this); }
};
// From http://beej.us/guide/bgnet/output/html/multipage/advanced.html#sendall
static int sendall(int s, const char* buf, int* len) {
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
ssize_t n;
while (total < *len) {
n = send(s, buf + total, static_cast<size_t>(bytesleft), 0);
if (n == -1) {
break;
}
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n == -1 ? -1 : 0; // return -1 on failure, 0 on success
}
class Connector {
private:
MessageMarshalling marshalling;
const unsigned int port;
int sockfd;
bool _connected;
void sendOverSocket() {
if (!_connected) return;
std::vector<char> buf = marshalling.serialize();
sendRawMsg(buf);
}
public:
void sendRawMsg(const std::vector<char>& buf) {
uint32_t bufSize = static_cast<uint32_t>(buf.size());
int bufSizeLen = sizeof(uint32_t);
sendall(sockfd, reinterpret_cast<char*>(&bufSize), &bufSizeLen);
int bufSizeInt = static_cast<int>(bufSize);
sendall(sockfd, reinterpret_cast<const char*>(buf.data()), &bufSizeInt);
}
Connector(unsigned int port) : port(port), _connected(false) {}
bool connected() { return _connected; }
/// connect to a socket via port specified in the construction (6565 by
/// default)
void connect() {
struct addrinfo hints, *servinfo, *p;
int rv;
#ifdef WIN32
// Initialise Winsock.
WSADATA wsaData;
int startupResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (startupResult != 0) {
printf("WSAStartup failed with error: %d\n", startupResult);
}
#endif
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo("localhost", std::to_string(port).c_str(), &hints,
&servinfo)) != 0) {
std::cerr << "getaddrinfo: " << gai_strerror(rv) << "\n";
goto giveup;
}
// loop through all the results and connect to the first we can
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
// errno is set here, but we don't examine it.
continue;
}
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
#ifdef WIN32
closesocket(sockfd);
#else
close(sockfd);
#endif
// errno is set here, but we don't examine it.
continue;
}
break;
}
// Connection failed; give up.
if (p == NULL) {
goto giveup;
}
freeaddrinfo(servinfo); // all done with this structure
_connected = true;
return;
giveup:
_connected = false;
return;
}
// sends START_SENDING message to the Profiler with a model name
void start(const std::string& file_path = "",
int execution_id = -1, bool has_restarts = false) {
/// extract fzn file name
std::string base_name(file_path);
{
size_t pos = base_name.find_last_of('/');
if (pos != static_cast<size_t>(-1)) {
base_name = base_name.substr(pos + 1, base_name.length() - pos - 1);
}
}
std::string info{""};
{
std::stringstream ss;
ss << "{";
ss << "\"has_restarts\": " << (has_restarts ? "true" : "false") << "\n";
ss << ",\"name\": " << "\"" << base_name << "\"" << "\n";
if (execution_id != -1) {
ss << ",\"execution_id\": " << execution_id;
}
ss << "}";
info = ss.str();
}
marshalling.makeStart(info);
sendOverSocket();
}
void restart(int restart_id = -1) {
std::string info{""};
{
std::stringstream ss;
ss << "{";
ss << "\"restart_id\": " << restart_id << "\n";
ss << "}";
info = ss.str();
}
marshalling.makeRestart(info);
sendOverSocket();
}
void done() {
marshalling.makeDone();
sendOverSocket();
}
/// disconnect from a socket
void disconnect() {
#ifdef WIN32
closesocket(sockfd);
#else
close(sockfd);
#endif
}
void sendNode(const Node& node) {
if (!_connected) return;
auto& msg = marshalling.makeNode(node.nodeUID(), node.parentUID(),
node.alt(), node.kids(), node.status());
if (node.label().valid()) msg.set_label(node.label().value());
if (node.nogood().valid()) msg.set_nogood(node.nogood().value());
if (node.info().valid()) msg.set_info(node.info().value());
sendOverSocket();
}
Node createNode(NodeUID node, NodeUID parent,
int alt, int kids, NodeStatus status) {
return Node(node, parent, alt, kids, status, *this);
}
};
void sendNode(Connector& c, Node& node) { c.sendNode(node); }
}
#endif