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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Maxim Shishmarev <maxim.shishmarev@monash.edu>
*
* Contributing authors:
* Kevin Leo <kevin.leo@monash.edu>
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Kevin Leo, 2017
* Christian Schulte, 2017
* Maxim Shishmarev, 2017
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#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 Gecode { namespace CPProfiler {
class Node {
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);
Node& set_node_thread_id(int tid);
const Option<std::string>& label() const;
Node& set_label(const std::string& label);
const Option<std::string>& nogood() const;
Node& set_nogood(const std::string& nogood);
const Option<std::string>& info() const;
Node& set_info(const std::string& info);
int alt() const;
int kids() const;
NodeStatus status() const;
NodeUID nodeUID() const;
NodeUID parentUID() const;
int node_id() const;
int parent_id() const;
int node_thread_id() const;
int node_restart_id() const;
int parent_thread_id() const;
int parent_restart_id() const;
};
class Connector {
private:
MessageMarshalling marshalling;
const unsigned int port;
int sockfd;
bool _connected;
static int sendall(int s, const char* buf, int* len);
void sendOverSocket(void);
void sendRawMsg(const std::vector<char>& buf);
public:
Connector(unsigned int port);
bool connected() const;
/// connect to a socket via port specified in the construction (6565 by
/// default)
void connect(void);
// 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);
void restart(int restart_id = -1);
void done();
/// disconnect from a socket
void disconnect(void);
void sendNode(const Node& node);
Node createNode(NodeUID node, NodeUID parent,
int alt, int kids, NodeStatus status);
};
/*
* Nodes
*/
inline
Node::Node(NodeUID node, NodeUID parent,
int alt, int kids, NodeStatus status)
: node_{node}, parent_{parent},
alt_(alt), kids_(kids), status_(status) {}
inline Node&
Node::set_node_thread_id(int tid) {
node_.tid = tid;
return *this;
}
inline const Option<std::string>&
Node::label() const { return label_; }
inline Node&
Node::set_label(const std::string& label) {
label_.set(label);
return *this;
}
inline const Option<std::string>&
Node::nogood() const {
return nogood_;
}
inline Node&
Node::set_nogood(const std::string& nogood) {
nogood_.set(nogood);
return *this;
}
inline const Option<std::string>&
Node::info() const { return info_; }
inline Node&
Node::set_info(const std::string& info) {
info_.set(info);
return *this;
}
inline int
Node::alt() const { return alt_; }
inline int
Node::kids() const { return kids_; }
inline NodeStatus
Node::status() const { return status_; }
inline NodeUID
Node::nodeUID() const { return node_; }
inline NodeUID
Node::parentUID() const { return parent_; }
inline int
Node::node_id() const { return node_.nid; }
inline int
Node::parent_id() const { return parent_.nid; }
inline int
Node::node_thread_id() const { return node_.tid; }
inline int
Node::node_restart_id() const { return node_.rid; }
inline int
Node::parent_thread_id() const { return parent_.tid; }
inline int
Node::parent_restart_id() const { return parent_.rid; }
/*
* Connector
*/
inline
Connector::Connector(unsigned int port) : port(port), _connected(false) {}
inline bool Connector::connected() const { return _connected; }
/*
* The following code is taken from:
* Beej's Guide to Network Programming
* http://beej.us/guide/bgnet/
* with the following license:
*
* Beej's Guide to Network Programming is Copyright © 2015 Brian "Beej Jorgensen" Hall.
* With specific exceptions for source code and translations, below, this work is licensed under the Creative Commons Attribution- Noncommercial- No Derivative Works 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
* One specific exception to the "No Derivative Works" portion of the license is as follows: this guide may be freely translated into any language, provided the translation is accurate, and the guide is reprinted in its entirety. The same license restrictions apply to the translation as to the original guide. The translation may also include the name and contact information for the translator.
* The C source code presented in this document is hereby granted to the public domain, and is completely free of any license restriction.
* Educators are freely encouraged to recommend or supply copies of this guide to their students.
* Contact beej@beej.us for more information.
*/
inline int
Connector::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 += static_cast<int>(n);
bytesleft -= static_cast<int>(n);
}
*len = static_cast<int>(total); // return number actually sent here
return (n == -1) ? -1 : 0; // return -1 on failure, 0 on success
}
inline void
Connector::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);
}
inline void
Connector::sendOverSocket(void) {
if (!_connected) return;
std::vector<char> buf = marshalling.serialize();
sendRawMsg(buf);
}
inline void
Connector::connect(void) {
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 != nullptr; p = p->ai_next) {
if ((sockfd = static_cast<int>(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 == nullptr) {
goto giveup;
}
freeaddrinfo(servinfo); // all done with this structure
_connected = true;
return;
giveup:
_connected = false;
return;
}
inline void
Connector::start(const std::string& file_path,
int execution_id, bool has_restarts) {
/// 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();
}
inline void
Connector::restart(int restart_id) {
std::string info{""};
{
std::stringstream ss;
ss << "{";
ss << "\"restart_id\": " << restart_id << "\n";
ss << "}";
info = ss.str();
}
marshalling.makeRestart(info);
sendOverSocket();
}
inline void
Connector::done() {
marshalling.makeDone();
sendOverSocket();
}
inline void
Connector::disconnect() {
#ifdef WIN32
closesocket(sockfd);
#else
close(sockfd);
#endif
}
inline void
Connector::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();
}
inline Node
Connector::createNode(NodeUID node, NodeUID parent,
int alt, int kids, NodeStatus status) {
return Node(node, parent, alt, kids, status);
}
}}
// STATISTICS: search-trace
|