Files
@ 0b0f016e4934
Branch filter:
Location: libtransport.git/spectrum_manager/src/methods.cpp
0b0f016e4934
13.9 KiB
text/x-c++hdr
Use win32 specific libs in SWIFTEN_LIBRARY
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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | #include "methods.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <pthread.h>
#include <boost/foreach.hpp>
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <cstdlib>
#include "signal.h"
#include "sys/wait.h"
#define WRAP(MESSAGE, TYPE) pbnetwork::WrapperMessage wrap; \
wrap.set_type(TYPE); \
wrap.set_payload(MESSAGE); \
wrap.SerializeToString(&MESSAGE);
using namespace Transport;
using namespace boost::filesystem;
using namespace boost;
std::string _data;
static std::string response;
std::string get_response() {
return response;
}
std::string searchForBinary(const std::string &binary) {
std::vector<std::string> path_list;
char * env_path = getenv("PATH");
if (env_path != NULL) {
std::string buffer = "";
for (int s = 0; s < strlen(env_path); s++) {
if (env_path[s] == ':') {
path_list.insert(path_list.end(), std::string(buffer));
buffer = "";
}
else {
buffer += env_path[s];
}
}
if (buffer != "") {
path_list.insert(path_list.end(), std::string(buffer));
buffer = "";
}
for (std::vector<std::string>::iterator dit = path_list.begin(); dit < path_list.end(); dit++) {
std::string bpath = *dit;
bpath += "/";
bpath += binary;
path p(bpath);
if (exists(p) && !is_directory(p)) {
return bpath;
}
}
}
return "";
}
// Executes new backend
unsigned long exec_(std::string path, std::string config, std::string jid) {
// fork and exec
pid_t pid = fork();
if ( pid == 0 ) {
// child process
if (jid.empty()) {
exit(execl(path.c_str(), path.c_str(), config.c_str(), NULL));
}
else {
exit(execl(path.c_str(), path.c_str(), "-j", jid.c_str(), config.c_str(), NULL));
}
} else if ( pid < 0 ) {
// fork failed
}
else {
waitpid(pid, 0, 0);
}
return (unsigned long) pid;
}
int getPort(const std::string &portfile) {
path p(portfile);
if (!exists(p) || is_directory(p)) {
return 0;
}
std::ifstream f(p.string().c_str(), std::ios_base::in);
std::string port;
f >> port;
if (port.empty())
return 0;
return boost::lexical_cast<int>(port);
}
int isRunning(const std::string &pidfile) {
path p(pidfile);
if (!exists(p) || is_directory(p)) {
return 0;
}
std::ifstream f(p.string().c_str(), std::ios_base::in);
std::string pid;
f >> pid;
if (pid.empty())
return 0;
if (kill(boost::lexical_cast<int>(pid), 0) != 0)
return 0;
return boost::lexical_cast<int>(pid);
}
void start_instances(ManagerConfig *config, const std::string &_jid) {
response = "";
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
std::string spectrum2_binary = searchForBinary("spectrum2");
if (spectrum2_binary.empty()) {
std::cerr << "spectrum2 binary not found in PATH\n";
exit(8);
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
continue;
}
std::vector<std::string> vhosts;
if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost"))
vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
BOOST_FOREACH(std::string &vhost, vhosts) {
Config vhostCfg;
if (vhostCfg.load(itr->path().string(), vhost) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
continue;
}
if (!_jid.empty() && _jid != vhost) {
continue;
}
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
if (pid == 0) {
response = "Starting " + itr->path().string() + ": OK\n";
std::cout << "Starting " << itr->path() << ": OK\n";
exec_(spectrum2_binary, itr->path().string(), vhost);
}
else {
response = "Starting " + itr->path().string() + ": Already started (PID=" + boost::lexical_cast<std::string>(pid) + ")\n";
std::cout << "Starting " << itr->path() << ": Already started (PID=" << pid << ")\n";
}
}
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
}
void stop_instances(ManagerConfig *config, const std::string &_jid) {
response = "";
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
}
std::vector<std::string> vhosts;
if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost"))
vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
BOOST_FOREACH(std::string &vhost, vhosts) {
Config vhostCfg;
if (vhostCfg.load(itr->path().string(), vhost) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
continue;
}
if (!_jid.empty() && _jid != vhost) {
continue;
}
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
if (pid) {
response ="Stopping " + itr->path().string() + ": ";
std::cout << "Stopping " << itr->path() << ": ";
kill(pid, SIGTERM);
sleep(1);
int count = 20;
while (kill(pid, 0) == 0 && count != 0) {
std::cout << ".";
sleep(1);
count--;
}
if (count == 0) {
response += "ERROR (timeout)\n";
std::cout << " ERROR (timeout)\n";
}
else {
response += "OK\n";
std::cout << " OK\n";
}
}
else {
response = "Stopping " + itr->path().string() + ": Not running\n";
std::cout << "Stopping " << itr->path() << ": Not running\n";
}
}
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
}
int show_status(ManagerConfig *config) {
int ret = 0;
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
}
std::vector<std::string> vhosts;
if (CONFIG_HAS_KEY(&cfg, "vhosts.vhost"))
vhosts = CONFIG_VECTOR(&cfg, "vhosts.vhost");
vhosts.push_back(CONFIG_STRING(&cfg, "service.jid"));
BOOST_FOREACH(std::string &vhost, vhosts) {
Config vhostCfg;
if (vhostCfg.load(itr->path().string(), vhost) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
continue;
}
int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile"));
if (pid) {
std::cout << itr->path() << ": " << vhost << " Running\n";
}
else {
ret = 3;
std::cout << itr->path() << ": " << vhost << " Stopped\n";
}
}
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
return ret;
}
static void handleDataRead(boost::shared_ptr<Swift::Connection> m_conn, boost::shared_ptr<Swift::SafeByteArray> data) {
_data += std::string(data->begin(), data->end());
// Parse data while there are some
while (_data.size() != 0) {
// expected_size of wrapper message
unsigned int expected_size;
// if data is >= 4, we have whole header and we can
// read expected_size.
if (_data.size() >= 4) {
expected_size = *((unsigned int*) &_data[0]);
expected_size = ntohl(expected_size);
// If we don't have whole wrapper message, wait for next
// handleDataRead call.
if (_data.size() - 4 < expected_size)
return;
}
else {
return;
}
// Parse wrapper message and erase it from buffer.
pbnetwork::WrapperMessage wrapper;
if (wrapper.ParseFromArray(&_data[4], expected_size) == false) {
std::cout << "PARSING ERROR " << expected_size << "\n";
_data.erase(_data.begin(), _data.begin() + 4 + expected_size);
continue;
}
_data.erase(_data.begin(), _data.begin() + 4 + expected_size);
if (wrapper.type() == pbnetwork::WrapperMessage_Type_TYPE_QUERY) {
pbnetwork::BackendConfig payload;
if (payload.ParseFromString(wrapper.payload()) == false) {
std::cout << "PARSING ERROR\n";
// TODO: ERROR
continue;
}
m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1));
m_conn->disconnect();
response = payload.config();
std::cout << payload.config() << "\n";
// exit(0);
}
}
}
static void handleConnected(boost::shared_ptr<Swift::Connection> m_conn, const std::string &msg, bool error) {
m_conn->onConnectFinished.disconnect(boost::bind(&handleConnected, m_conn, msg, _1));
if (error) {
std::cerr << "Can't connect the server\n";
response = "Can't connect the server\n";
m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1));
// exit(50);
}
else {
pbnetwork::BackendConfig m;
m.set_config(msg);
std::string message;
m.SerializeToString(&message);
WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY);
uint32_t size = htonl(message.size());
char *header = (char *) &size;
// send header together with wrapper message
m_conn->write(Swift::createSafeByteArray(std::string(header, 4) + message));
}
}
void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message) {
response = "";
path p(CONFIG_STRING(config, "service.config_directory"));
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
bool found = false;
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
continue;
}
if (CONFIG_STRING(&cfg, "service.jid") != jid) {
continue;
}
found = true;
boost::shared_ptr<Swift::Connection> m_conn;
m_conn = networkFactories.getConnectionFactory()->createConnection();
m_conn->onDataRead.connect(boost::bind(&handleDataRead, m_conn, _1));
m_conn->onConnectFinished.connect(boost::bind(&handleConnected, m_conn, message, _1));
m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(CONFIG_STRING(&cfg, "service.backend_host")), getPort(CONFIG_STRING(&cfg, "service.portfile"))));
// finished++;
// Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
// client->setAlwaysTrustCertificates();
// client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
// client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
// client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
// Swift::ClientOptions opt;
// opt.allowPLAINWithoutTLS = true;
// client->connect(opt);
}
}
if (!found) {
response = "Config file for Spectrum instance with this JID was not found\n";
std::cerr << "Config file for Spectrum instance with this JID was not found\n";
// exit(20);
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
exit(5);
}
}
std::vector<std::string> show_list(ManagerConfig *config, bool show) {
path p(CONFIG_STRING(config, "service.config_directory"));
std::vector<std::string> list;
try {
if (!exists(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(6);
}
if (!is_directory(p)) {
std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
exit(7);
}
bool found = false;
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
Config cfg;
if (cfg.load(itr->path().string()) == false) {
std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
continue;
}
if (show) {
std::cout << CONFIG_STRING(&cfg, "service.jid") << "\n";
}
list.push_back(CONFIG_STRING(&cfg, "service.jid"));
}
}
}
catch (const filesystem_error& ex) {
std::cerr << "boost filesystem error\n";
}
return list;
}
|