<commit_before>/***************************************************************************
 *   Copyright (C) 2006 by FThauer FHammer   *
 *   f.thauer@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <qapplication.h>
#include <QtGui>
#include <QtCore>

#ifdef __APPLE__
	#include <QMacStyle>
#endif

#include <curl/curl.h>

#include "session.h"
#include "startwindowimpl.h"
#include "configfile.h"
#include "startsplash.h"
#include "game_defs.h"
#include <net/socket_startup.h>
#include <third_party/qtsingleapplication/qtsingleapplication.h>

#ifdef _MSC_VER
	#ifdef _DEBUG
		#define _CRTDBG_MAP_ALLOC
		#include <crtdbg.h>

		#define ENABLE_LEAK_CHECK() \
			{ \
				int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); \
				tmpFlag |= _CRTDBG_LEAK_CHECK_DF; \
				_CrtSetDbgFlag(tmpFlag); \
			}
	#endif
#endif

#ifndef ENABLE_LEAK_CHECK
	#define ENABLE_LEAK_CHECK()
#endif

//Uncomment this for RELEASE on Linux/Unix/BSD (static Qt only)
//#include <QtPlugin>
//Q_IMPORT_PLUGIN(qjpeg)
//Q_IMPORT_PLUGIN(qgif)

#ifdef _WIN32 // Always use static Qt on Windows.
#include <QtPlugin>
Q_IMPORT_PLUGIN(qjpeg)
Q_IMPORT_PLUGIN(qgif)
#endif

using namespace std;

class startWindowImpl;
class Game;

int main( int argc, char **argv )
{

	//ENABLE_LEAK_CHECK();

	//_CrtSetBreakAlloc(49937);
	socket_startup();
	curl_global_init(CURL_GLOBAL_NOTHING);

	/////// can be removed for non-qt-guis ////////////
	QtSingleApplication a( argc, argv );

	if (a.sendMessage("Wake up!")) {
		return 0;
	 }

#ifdef __APPLE__
	// The following needs to be done directly after the application is created.
	QDir dir(QApplication::applicationDirPath());
	dir.cdUp();
	dir.cd("plugins");
	QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
#endif

	//create defaultconfig
	ConfigFile *myConfig = new ConfigFile(argv[0], false);

	// set PlastiqueStyle even for mac-version to prevent artefacts on styled widgets
        a.setStyle(new QPlastiqueStyle);

	QString	myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
	//set QApplication default font

	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/n019003l.pfb");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/VeraBd.ttf");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/c059013l.pfb");

#ifdef _WIN32
        QString font1String("QApplication, QWidget, QDialog { font-family: \"Arial\"; font-size: 12px; }");
#else
        #ifdef __APPLE__
//            QString font1String("font-family: \"Lucida Grande\";");
            QString font1String("QApplication, QWidget, QDialog { font-family: \"Lucida Grande\"; font-size: 12px; }");
        #else
            QString font1String("QApplication, QWidget, QDialog { font-family: \"Nimbus Sans L\"; font-size: 12px; }");
        #endif
#endif
        a.setStyleSheet(font1String + " QDialogButtonBox, QMessageBox { dialogbuttonbox-buttons-have-icons: 1; dialog-ok-icon: url(:/gfx/dialog_ok_apply.png); dialog-cancel-icon: url(:/gfx/dialog_close.png); dialog-close-icon: url(:/gfx/dialog_close.png); dialog-yes-icon: url(:/gfx/dialog_ok_apply.png); dialog-no-icon: url(:/gfx/dialog_close.png) }");

	QPixmap *pixmap = new QPixmap(myAppDataPath + "gfx/gui/misc/welcomepokerth.png");
	StartSplash splash(*pixmap);
	if(!myConfig->readConfigInt("DisableSplashScreenOnStartup")) {
		splash.show();
		splash.showMessage(QString("Version %1").arg(POKERTH_BETA_RELEASE_STRING), 0x0042, QColor(153,213,0));
	}

	//Set translations
	QTranslator qtTranslator;
        qtTranslator.load(QString(myAppDataPath +"translations/qt_") + QString::fromStdString(myConfig->readConfigString("Language")));
        a.installTranslator(&qtTranslator);

	QTranslator translator;
	translator.load(QString(myAppDataPath +"translations/pokerth_") + QString::fromStdString(myConfig->readConfigString("Language")));
	a.installTranslator(&translator);

	qRegisterMetaType<unsigned>("unsigned");
	qRegisterMetaType<boost::shared_ptr<Game> >("boost::shared_ptr<Game>");
	qRegisterMetaType<ServerStats>("ServerStats");
        qRegisterMetaType<DenyGameInvitationReason>("DenyGameInvitationReason");
	///////////////////////////////////////////////////

        startWindowImpl mainWin(myConfig);
        a.setActivationWindow(&mainWin, true);

	int retVal = a.exec();

	curl_global_cleanup();
	socket_cleanup();

	return retVal;

}
<commit_msg>mac font fix<commit_after>/***************************************************************************
 *   Copyright (C) 2006 by FThauer FHammer   *
 *   f.thauer@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <qapplication.h>
#include <QtGui>
#include <QtCore>

#ifdef __APPLE__
	#include <QMacStyle>
#endif

#include <curl/curl.h>

#include "session.h"
#include "startwindowimpl.h"
#include "configfile.h"
#include "startsplash.h"
#include "game_defs.h"
#include <net/socket_startup.h>
#include <third_party/qtsingleapplication/qtsingleapplication.h>

#ifdef _MSC_VER
	#ifdef _DEBUG
		#define _CRTDBG_MAP_ALLOC
		#include <crtdbg.h>

		#define ENABLE_LEAK_CHECK() \
			{ \
				int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); \
				tmpFlag |= _CRTDBG_LEAK_CHECK_DF; \
				_CrtSetDbgFlag(tmpFlag); \
			}
	#endif
#endif

#ifndef ENABLE_LEAK_CHECK
	#define ENABLE_LEAK_CHECK()
#endif

//Uncomment this for RELEASE on Linux/Unix/BSD (static Qt only)
//#include <QtPlugin>
//Q_IMPORT_PLUGIN(qjpeg)
//Q_IMPORT_PLUGIN(qgif)

#ifdef _WIN32 // Always use static Qt on Windows.
#include <QtPlugin>
Q_IMPORT_PLUGIN(qjpeg)
Q_IMPORT_PLUGIN(qgif)
#endif

using namespace std;

class startWindowImpl;
class Game;

int main( int argc, char **argv )
{

	//ENABLE_LEAK_CHECK();

	//_CrtSetBreakAlloc(49937);
	socket_startup();
	curl_global_init(CURL_GLOBAL_NOTHING);

	/////// can be removed for non-qt-guis ////////////
	QtSingleApplication a( argc, argv );

	if (a.sendMessage("Wake up!")) {
		return 0;
	 }

#ifdef __APPLE__
	// The following needs to be done directly after the application is created.
	QDir dir(QApplication::applicationDirPath());
	dir.cdUp();
	dir.cd("plugins");
	QApplication::setLibraryPaths(QStringList(dir.absolutePath()));
#endif

	//create defaultconfig
	ConfigFile *myConfig = new ConfigFile(argv[0], false);

	// set PlastiqueStyle even for mac-version to prevent artefacts on styled widgets
        a.setStyle(new QPlastiqueStyle);

	QString	myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
	//set QApplication default font

	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/n019003l.pfb");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/VeraBd.ttf");
	QFontDatabase::addApplicationFont (myAppDataPath +"fonts/c059013l.pfb");

#ifdef _WIN32
        QString font1String("QApplication, QWidget, QDialog { font-family: \"Arial\"; font-size: 12px; }");
#else
        #ifdef __APPLE__
//            QString font1String("font-family: \"Lucida Grande\";");
            QString font1String("QApplication, QWidget, QDialog { font-size: 11px; }");
        #else
            QString font1String("QApplication, QWidget, QDialog { font-family: \"Nimbus Sans L\"; font-size: 12px; }");
        #endif
#endif
        a.setStyleSheet(font1String + " QDialogButtonBox, QMessageBox { dialogbuttonbox-buttons-have-icons: 1; dialog-ok-icon: url(:/gfx/dialog_ok_apply.png); dialog-cancel-icon: url(:/gfx/dialog_close.png); dialog-close-icon: url(:/gfx/dialog_close.png); dialog-yes-icon: url(:/gfx/dialog_ok_apply.png); dialog-no-icon: url(:/gfx/dialog_close.png) }");

	QPixmap *pixmap = new QPixmap(myAppDataPath + "gfx/gui/misc/welcomepokerth.png");
	StartSplash splash(*pixmap);
	if(!myConfig->readConfigInt("DisableSplashScreenOnStartup")) {
		splash.show();
		splash.showMessage(QString("Version %1").arg(POKERTH_BETA_RELEASE_STRING), 0x0042, QColor(153,213,0));
	}

	//Set translations
	QTranslator qtTranslator;
        qtTranslator.load(QString(myAppDataPath +"translations/qt_") + QString::fromStdString(myConfig->readConfigString("Language")));
        a.installTranslator(&qtTranslator);

	QTranslator translator;
	translator.load(QString(myAppDataPath +"translations/pokerth_") + QString::fromStdString(myConfig->readConfigString("Language")));
	a.installTranslator(&translator);

	qRegisterMetaType<unsigned>("unsigned");
	qRegisterMetaType<boost::shared_ptr<Game> >("boost::shared_ptr<Game>");
	qRegisterMetaType<ServerStats>("ServerStats");
        qRegisterMetaType<DenyGameInvitationReason>("DenyGameInvitationReason");
	///////////////////////////////////////////////////

        startWindowImpl mainWin(myConfig);
        a.setActivationWindow(&mainWin, true);

	int retVal = a.exec();

	curl_global_cleanup();
	socket_cleanup();

	return retVal;

}
<|endoftext|>


<commit_before>#include "container_reader_impl.hpp"

#include <boost/asio.hpp>
#include <glog/logging.h>

#include "sync_util.hpp"

#define UDP_MAX_PACKET_BYTES 65536 /* UDP size limit in IPv4 (may be larger in IPv6) */
#define RECEIVED_BYTES_STATSD_LABEL "dcos.metrics.container_received_bytes_per_sec"
#define THROTTLED_BYTES_STATSD_LABEL "dcos.metrics.container_throttled_bytes_per_sec"

typedef boost::asio::ip::udp::endpoint udp_endpoint_t;
typedef boost::asio::ip::udp::resolver resolver_t;

namespace {
  inline std::string to_statsd(const char* label, size_t value, size_t period_ms) {
    std::ostringstream oss;
    oss << label << ':' << value / (period_ms / 1000.) << "|g";
    return oss.str();
  }
}

metrics::ContainerReaderImpl::ContainerReaderImpl(
    const std::shared_ptr<boost::asio::io_service>& io_service,
    const std::vector<output_writer_ptr_t>& writers,
    const UDPEndpoint& requested_endpoint,
    size_t limit_period_ms,
    size_t limit_amount_bytes)
  : writers(writers),
    requested_endpoint(requested_endpoint),
    limit_period_ms(limit_period_ms),
    limit_amount_bytes(limit_amount_bytes),
    io_service(io_service),
    shutdown(false),
    limit_reset_timer(*io_service),
    socket(*io_service),
    socket_buffer(UDP_MAX_PACKET_BYTES, '\0'),
    received_bytes(0),
    dropped_bytes(0) {
  LOG(INFO) << "Reader constructed for " << requested_endpoint.string();
}

metrics::ContainerReaderImpl::~ContainerReaderImpl() {
  LOG(INFO) << "Triggering ContainerReader shutdown";
  shutdown = true; // avoid race resulting in segfault against entries in writers
  if (sync_util::dispatch_run("~ContainerReaderImpl:shutdown",
          *io_service, std::bind(&ContainerReaderImpl::shutdown_cb, this))) {
    LOG(INFO) << "ContainerReader shutdown succeeded";
  } else {
    LOG(ERROR) << "ContainerReader shutdown failed";
  }
}

Try<metrics::UDPEndpoint> metrics::ContainerReaderImpl::open() {
  if (actual_endpoint) {
    return *actual_endpoint;
  }

  resolver_t resolver(*io_service);
  resolver_t::query query(requested_endpoint.host, "");
  boost::system::error_code ec;
  resolver_t::iterator iter = resolver.resolve(query, ec);
  boost::asio::ip::address resolved_address;
  if (!ec && iter != resolver_t::iterator()) {
    // resolved, bind to first entry in list
    resolved_address = iter->endpoint().address();
  } else {
    // failed or no results, fall back to using the host as-is
    resolved_address = boost::asio::ip::address::from_string(requested_endpoint.host);
  }

  udp_endpoint_t bind_endpoint(resolved_address, requested_endpoint.port);
  socket.open(bind_endpoint.protocol(), ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to open reader socket at endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  // Enable SO_REUSEADDR: When mesos-agent is restarted, child processes such as
  // mesos-logrotate-logger and mesos-executor wind up taking ownership of the socket, preventing us
  // from recovering the socket after a mesos-agent restart (result: bind() => EADDRINUSE).
  // Due to this behavior, SO_REUSEADDR is required for the agent to recover its own sockets.
  // To verify that data wasn't being lost after a recovery, the author ran several 'test-sender'
  // tasks and observed that 'loop_gauge' was incrementing without any skipped values.
  socket.set_option(boost::asio::socket_base::reuse_address(true), ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to set bind reader socket at endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  socket.bind(bind_endpoint, ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to bind reader socket at endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  udp_endpoint_t bound_endpoint = socket.local_endpoint(ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to retrieve reader socket's resulting endpoint for bind at "
        << "endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  std::string bound_endpoint_address_str = bound_endpoint.address().to_string(ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to stringify reader socket's "
        << "address[" << bound_endpoint.address() << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  // Set endpoint (indicates open socket) and start listening AFTER all error conditions are clear
  actual_endpoint.reset(new UDPEndpoint(bound_endpoint_address_str, bound_endpoint.port()));
  start_recv();
  start_limit_reset_timer();

  LOG(INFO) << "Reader listening on " << actual_endpoint->string();
  return *actual_endpoint;
}

Try<metrics::UDPEndpoint> metrics::ContainerReaderImpl::endpoint() const {
  if (actual_endpoint) {
    return *actual_endpoint;
  } else {
    return Try<metrics::UDPEndpoint>(Error("Not listening on UDP"));
  }
}

void metrics::ContainerReaderImpl::register_container(
    const mesos::ContainerID& container_id,
    const mesos::ExecutorInfo& executor_info) {
  registered_containers[container_id] = executor_info;
}

void metrics::ContainerReaderImpl::unregister_container(
    const mesos::ContainerID& container_id) {
  registered_containers.erase(container_id);
}

void metrics::ContainerReaderImpl::start_limit_reset_timer() {
  limit_reset_timer.expires_from_now(boost::posix_time::milliseconds(limit_period_ms));
  limit_reset_timer.async_wait(
      std::bind(&ContainerReaderImpl::limit_reset_cb, this, std::placeholders::_1));
}

void metrics::ContainerReaderImpl::limit_reset_cb(boost::system::error_code ec) {
  if (ec) {
    if (boost::asio::error::operation_aborted) {
      // We're being destroyed. Don't look at local state, it may be destroyed already.
      LOG(WARNING) << "Limit timer aborted: Exiting loop immediately";
      return;
    } else {
      LOG(ERROR) << "Limit timer returned error. "
                 << "err='" << ec.message() << "'(" << ec << ")";
    }
  }

  // Also produce throughput stats while we're here.
  if (actual_endpoint) {
    LOG(INFO) << "Container Throughput (port " << actual_endpoint->port <<"): "
              << "received=" << received_bytes << "b, " << "dropped=" << dropped_bytes << "b";
  } else {
    LOG(INFO) << "Container Throughput (port ?): "
              << "received=" << received_bytes << "b, " << "dropped=" << dropped_bytes << "b";
  }

  // Send our own metrics on the data we received and/or dropped
  // Always emit, even if values are zero, just to let upstream know we're listening
  std::string msg = to_statsd(RECEIVED_BYTES_STATSD_LABEL, received_bytes, limit_period_ms);
  write_message(msg.data(), msg.size());
  msg = to_statsd(THROTTLED_BYTES_STATSD_LABEL, dropped_bytes, limit_period_ms);
  write_message(msg.data(), msg.size());

  received_bytes = 0;
  dropped_bytes = 0;
  if (!shutdown) {
    start_limit_reset_timer();
  }
}

void metrics::ContainerReaderImpl::start_recv() {
  socket.async_receive_from(boost::asio::buffer(socket_buffer.data(), socket_buffer.size()),
      sender_endpoint,
      std::bind(&ContainerReaderImpl::recv_cb, this,
          std::placeholders::_1, std::placeholders::_2));
}

void metrics::ContainerReaderImpl::recv_cb(
    boost::system::error_code ec, size_t bytes_transferred) {
  if (ec) {
    // FIXME handle certain errors here, eg boost::asio::error::message_size.
    if (boost::asio::error::operation_aborted) {
      // We're being destroyed. Don't look at local state, it may be destroyed already.
      LOG(WARNING) << "Aborted: Exiting read loop immediately";
    } else {
      if (actual_endpoint) {
        LOG(WARNING) << "Error when receiving data from reader socket at "
                     << "dest[" << actual_endpoint->host << ":" << actual_endpoint->port << "] "
                     << "from source[" << sender_endpoint << "]: " << ec;
      } else {
        LOG(WARNING) << "Error when receiving data from reader socket at "
                     << "dest[???] from source[" << sender_endpoint << "]: " << ec;
      }
      start_recv();
    }
    return;
  }

  if (received_bytes >= limit_amount_bytes) {
    // We've hit the limit, drop data and continue.
    dropped_bytes += bytes_transferred;
  } else {
    // Search for newline chars, which indicate multiple statsd entries in a single packet
    char* next_newline = (char*) memchr(socket_buffer.data(), '\n', bytes_transferred);
    if (next_newline == NULL) {
      // Single entry. Pass buffer directly.
      write_message(socket_buffer.data(), bytes_transferred);
    } else {
      // Multiple newline-separated entries. Pass each from buffer as separate messages.
      size_t start_index = 0;
      for (;;) {
        size_t newline_offset = (next_newline != NULL)
          ? next_newline - socket_buffer.data()
          : bytes_transferred; // no more newlines, use end of buffer
        //DLOG(INFO) << "newline_offset=" << newline_offset << ", start_index=" << start_index;
        size_t entry_size = newline_offset - start_index;
        //DLOG(INFO) << "entry_size " << entry_size << " => copy "
        //           << "[" << start_index << "," << start_index+entry_size << ") to front of scratch";
        if (entry_size > 0) { // check/skip empty rows ("\n\n", or "\n" at start/end of pkt)
          write_message(socket_buffer.data() + start_index, entry_size);
        }
        start_index = start_index + entry_size + 1; // pass over newline itself
        if (start_index >= bytes_transferred) {
          break; // seeked to end of received data
        }
        next_newline =
          (char*) memchr(socket_buffer.data() + start_index, '\n', bytes_transferred - start_index);
      }
    }
  }

  received_bytes += bytes_transferred;
  if (!shutdown) {
    start_recv();
  }
}

void metrics::ContainerReaderImpl::write_message(const char* data, size_t size) {
  DLOG(INFO) << "Received " << size << " byte entry from "
             << "endpoint[" << sender_endpoint << "] => "
             << registered_containers.size() << " containers";
  switch (registered_containers.size()) {
    case 0:
      // No containers assigned to this reader, nothing to pair the data with.
      for (output_writer_ptr_t writer : writers) {
        writer->write_container_statsd(NULL, NULL, data, size);
      }
      break;
    case 1:
      // Typical/expected case: One container per UDP port.
      {
        auto container_entry = *registered_containers.cbegin();
        for (output_writer_ptr_t writer : writers) {
          writer->write_container_statsd(
              &container_entry.first, &container_entry.second, data, size);
        }
      }
      break;
    default:
      // Multiple containers assigned to this port. Unable to determine which container this
      // data came from.
      // FIXME: This is where ip-per-container support would be added, using an ip provided
      // by the caller.
      for (output_writer_ptr_t writer : writers) {
        writer->write_container_statsd(NULL, NULL, data, size);
      }
      break;
  }
}

void metrics::ContainerReaderImpl::shutdown_cb() {
  boost::system::error_code ec;
  udp_endpoint_t bound_endpoint = socket.local_endpoint(ec);
  if (ec) {
    LOG(INFO) << "Destroying reader for requested[" << requested_endpoint.string() << "] -> "
              << "actual[???], " << socket.available() << " socket bytes dropped";
  } else {
    LOG(INFO) << "Destroying reader for requested[" << requested_endpoint.string() << "] -> "
              << "actual[" << bound_endpoint << "], " << socket.available() << " socket bytes dropped";
  }

  // Shut down the throttle timer
  limit_reset_timer.cancel(ec);
  if (ec) {
    LOG(ERROR) << "Resolve timer cancellation returned error. "
               << "err='" << ec.message() << "'(" << ec << ")";
  }

  // Flush any remaining data queued in the socket
  while (socket.available()) {
    size_t bytes_transferred =
      socket.receive_from(boost::asio::buffer(socket_buffer.data(), socket_buffer.size()),
          sender_endpoint, 0 /* flags */, ec);
    if (ec) {
      LOG(WARNING) << "Sync receive failed, dropping " << socket.available() << " bytes: " << ec;
      break;
    } else if (bytes_transferred == 0) {
      LOG(WARNING)
        << "Sync receive had no data, dropping " << socket.available() << " bytes: " << ec;
      break;
    } else {
      recv_cb(ec, bytes_transferred);
    }
  }

  // Close the socket
  if (socket.is_open()) {
    socket.close(ec);
    if (ec) {
      LOG(ERROR) << "Error on reader socket close: " << ec;
    }
  }
}
<commit_msg>Better log message for input throughput<commit_after>#include "container_reader_impl.hpp"

#include <boost/asio.hpp>
#include <glog/logging.h>

#include "sync_util.hpp"

#define UDP_MAX_PACKET_BYTES 65536 /* UDP size limit in IPv4 (may be larger in IPv6) */
#define RECEIVED_BYTES_STATSD_LABEL "dcos.metrics.container_received_bytes_per_sec"
#define THROTTLED_BYTES_STATSD_LABEL "dcos.metrics.container_throttled_bytes_per_sec"

typedef boost::asio::ip::udp::endpoint udp_endpoint_t;
typedef boost::asio::ip::udp::resolver resolver_t;

namespace {
  inline std::string to_statsd(const char* label, size_t value, size_t period_ms) {
    std::ostringstream oss;
    oss << label << ':' << value / (period_ms / 1000.) << "|g";
    return oss.str();
  }
}

metrics::ContainerReaderImpl::ContainerReaderImpl(
    const std::shared_ptr<boost::asio::io_service>& io_service,
    const std::vector<output_writer_ptr_t>& writers,
    const UDPEndpoint& requested_endpoint,
    size_t limit_period_ms,
    size_t limit_amount_bytes)
  : writers(writers),
    requested_endpoint(requested_endpoint),
    limit_period_ms(limit_period_ms),
    limit_amount_bytes(limit_amount_bytes),
    io_service(io_service),
    shutdown(false),
    limit_reset_timer(*io_service),
    socket(*io_service),
    socket_buffer(UDP_MAX_PACKET_BYTES, '\0'),
    received_bytes(0),
    dropped_bytes(0) {
  LOG(INFO) << "Reader constructed for " << requested_endpoint.string();
}

metrics::ContainerReaderImpl::~ContainerReaderImpl() {
  LOG(INFO) << "Triggering ContainerReader shutdown";
  shutdown = true; // avoid race resulting in segfault against entries in writers
  if (sync_util::dispatch_run("~ContainerReaderImpl:shutdown",
          *io_service, std::bind(&ContainerReaderImpl::shutdown_cb, this))) {
    LOG(INFO) << "ContainerReader shutdown succeeded";
  } else {
    LOG(ERROR) << "ContainerReader shutdown failed";
  }
}

Try<metrics::UDPEndpoint> metrics::ContainerReaderImpl::open() {
  if (actual_endpoint) {
    return *actual_endpoint;
  }

  resolver_t resolver(*io_service);
  resolver_t::query query(requested_endpoint.host, "");
  boost::system::error_code ec;
  resolver_t::iterator iter = resolver.resolve(query, ec);
  boost::asio::ip::address resolved_address;
  if (!ec && iter != resolver_t::iterator()) {
    // resolved, bind to first entry in list
    resolved_address = iter->endpoint().address();
  } else {
    // failed or no results, fall back to using the host as-is
    resolved_address = boost::asio::ip::address::from_string(requested_endpoint.host);
  }

  udp_endpoint_t bind_endpoint(resolved_address, requested_endpoint.port);
  socket.open(bind_endpoint.protocol(), ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to open reader socket at endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  // Enable SO_REUSEADDR: When mesos-agent is restarted, child processes such as
  // mesos-logrotate-logger and mesos-executor wind up taking ownership of the socket, preventing us
  // from recovering the socket after a mesos-agent restart (result: bind() => EADDRINUSE).
  // Due to this behavior, SO_REUSEADDR is required for the agent to recover its own sockets.
  // To verify that data wasn't being lost after a recovery, the author ran several 'test-sender'
  // tasks and observed that 'loop_gauge' was incrementing without any skipped values.
  socket.set_option(boost::asio::socket_base::reuse_address(true), ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to set bind reader socket at endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  socket.bind(bind_endpoint, ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to bind reader socket at endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  udp_endpoint_t bound_endpoint = socket.local_endpoint(ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to retrieve reader socket's resulting endpoint for bind at "
        << "endpoint[" << bind_endpoint << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  std::string bound_endpoint_address_str = bound_endpoint.address().to_string(ec);
  if (ec) {
    std::ostringstream oss;
    oss << "Failed to stringify reader socket's "
        << "address[" << bound_endpoint.address() << "]: " << ec;
    return Try<metrics::UDPEndpoint>(Error(oss.str()));
  }

  // Set endpoint (indicates open socket) and start listening AFTER all error conditions are clear
  actual_endpoint.reset(new UDPEndpoint(bound_endpoint_address_str, bound_endpoint.port()));
  start_recv();
  start_limit_reset_timer();

  LOG(INFO) << "Reader listening on " << actual_endpoint->string();
  return *actual_endpoint;
}

Try<metrics::UDPEndpoint> metrics::ContainerReaderImpl::endpoint() const {
  if (actual_endpoint) {
    return *actual_endpoint;
  } else {
    return Try<metrics::UDPEndpoint>(Error("Not listening on UDP"));
  }
}

void metrics::ContainerReaderImpl::register_container(
    const mesos::ContainerID& container_id,
    const mesos::ExecutorInfo& executor_info) {
  registered_containers[container_id] = executor_info;
}

void metrics::ContainerReaderImpl::unregister_container(
    const mesos::ContainerID& container_id) {
  registered_containers.erase(container_id);
}

void metrics::ContainerReaderImpl::start_limit_reset_timer() {
  limit_reset_timer.expires_from_now(boost::posix_time::milliseconds(limit_period_ms));
  limit_reset_timer.async_wait(
      std::bind(&ContainerReaderImpl::limit_reset_cb, this, std::placeholders::_1));
}

void metrics::ContainerReaderImpl::limit_reset_cb(boost::system::error_code ec) {
  if (ec) {
    if (boost::asio::error::operation_aborted) {
      // We're being destroyed. Don't look at local state, it may be destroyed already.
      LOG(WARNING) << "Limit timer aborted: Exiting loop immediately";
      return;
    } else {
      LOG(ERROR) << "Limit timer returned error. "
                 << "err='" << ec.message() << "'(" << ec << ")";
    }
  }

  // Also produce throughput stats while we're here.
  if (actual_endpoint) {
    LOG(INFO) << "Throughput from container at port " << actual_endpoint->port <<" (bytes): "
              << "received=" << received_bytes << ", throttled=" << dropped_bytes;
  } else {
    LOG(INFO) << "Throughput from container at port ?UNKNOWN? (bytes): "
              << "received=" << received_bytes << ", throttled=" << dropped_bytes;
  }

  // Send our own metrics on the data we received and/or dropped
  // Always emit, even if values are zero, just to let upstream know we're listening
  std::string msg = to_statsd(RECEIVED_BYTES_STATSD_LABEL, received_bytes, limit_period_ms);
  write_message(msg.data(), msg.size());
  msg = to_statsd(THROTTLED_BYTES_STATSD_LABEL, dropped_bytes, limit_period_ms);
  write_message(msg.data(), msg.size());

  received_bytes = 0;
  dropped_bytes = 0;
  if (!shutdown) {
    start_limit_reset_timer();
  }
}

void metrics::ContainerReaderImpl::start_recv() {
  socket.async_receive_from(boost::asio::buffer(socket_buffer.data(), socket_buffer.size()),
      sender_endpoint,
      std::bind(&ContainerReaderImpl::recv_cb, this,
          std::placeholders::_1, std::placeholders::_2));
}

void metrics::ContainerReaderImpl::recv_cb(
    boost::system::error_code ec, size_t bytes_transferred) {
  if (ec) {
    // FIXME handle certain errors here, eg boost::asio::error::message_size.
    if (boost::asio::error::operation_aborted) {
      // We're being destroyed. Don't look at local state, it may be destroyed already.
      LOG(WARNING) << "Aborted: Exiting read loop immediately";
    } else {
      if (actual_endpoint) {
        LOG(WARNING) << "Error when receiving data from reader socket at "
                     << "dest[" << actual_endpoint->host << ":" << actual_endpoint->port << "] "
                     << "from source[" << sender_endpoint << "]: " << ec;
      } else {
        LOG(WARNING) << "Error when receiving data from reader socket at "
                     << "dest[???] from source[" << sender_endpoint << "]: " << ec;
      }
      start_recv();
    }
    return;
  }

  if (received_bytes >= limit_amount_bytes) {
    // We've hit the limit, drop data and continue.
    dropped_bytes += bytes_transferred;
  } else {
    // Search for newline chars, which indicate multiple statsd entries in a single packet
    char* next_newline = (char*) memchr(socket_buffer.data(), '\n', bytes_transferred);
    if (next_newline == NULL) {
      // Single entry. Pass buffer directly.
      write_message(socket_buffer.data(), bytes_transferred);
    } else {
      // Multiple newline-separated entries. Pass each from buffer as separate messages.
      size_t start_index = 0;
      for (;;) {
        size_t newline_offset = (next_newline != NULL)
          ? next_newline - socket_buffer.data()
          : bytes_transferred; // no more newlines, use end of buffer
        //DLOG(INFO) << "newline_offset=" << newline_offset << ", start_index=" << start_index;
        size_t entry_size = newline_offset - start_index;
        //DLOG(INFO) << "entry_size " << entry_size << " => copy "
        //           << "[" << start_index << "," << start_index+entry_size << ") to front of scratch";
        if (entry_size > 0) { // check/skip empty rows ("\n\n", or "\n" at start/end of pkt)
          write_message(socket_buffer.data() + start_index, entry_size);
        }
        start_index = start_index + entry_size + 1; // pass over newline itself
        if (start_index >= bytes_transferred) {
          break; // seeked to end of received data
        }
        next_newline =
          (char*) memchr(socket_buffer.data() + start_index, '\n', bytes_transferred - start_index);
      }
    }
  }

  received_bytes += bytes_transferred;
  if (!shutdown) {
    start_recv();
  }
}

void metrics::ContainerReaderImpl::write_message(const char* data, size_t size) {
  DLOG(INFO) << "Received " << size << " byte entry from "
             << "endpoint[" << sender_endpoint << "] => "
             << registered_containers.size() << " containers";
  switch (registered_containers.size()) {
    case 0:
      // No containers assigned to this reader, nothing to pair the data with.
      for (output_writer_ptr_t writer : writers) {
        writer->write_container_statsd(NULL, NULL, data, size);
      }
      break;
    case 1:
      // Typical/expected case: One container per UDP port.
      {
        auto container_entry = *registered_containers.cbegin();
        for (output_writer_ptr_t writer : writers) {
          writer->write_container_statsd(
              &container_entry.first, &container_entry.second, data, size);
        }
      }
      break;
    default:
      // Multiple containers assigned to this port. Unable to determine which container this
      // data came from.
      // FIXME: This is where ip-per-container support would be added, using an ip provided
      // by the caller.
      for (output_writer_ptr_t writer : writers) {
        writer->write_container_statsd(NULL, NULL, data, size);
      }
      break;
  }
}

void metrics::ContainerReaderImpl::shutdown_cb() {
  boost::system::error_code ec;
  udp_endpoint_t bound_endpoint = socket.local_endpoint(ec);
  if (ec) {
    LOG(INFO) << "Destroying reader for requested[" << requested_endpoint.string() << "] -> "
              << "actual[???], " << socket.available() << " socket bytes dropped";
  } else {
    LOG(INFO) << "Destroying reader for requested[" << requested_endpoint.string() << "] -> "
              << "actual[" << bound_endpoint << "], " << socket.available() << " socket bytes dropped";
  }

  // Shut down the throttle timer
  limit_reset_timer.cancel(ec);
  if (ec) {
    LOG(ERROR) << "Resolve timer cancellation returned error. "
               << "err='" << ec.message() << "'(" << ec << ")";
  }

  // Flush any remaining data queued in the socket
  while (socket.available()) {
    size_t bytes_transferred =
      socket.receive_from(boost::asio::buffer(socket_buffer.data(), socket_buffer.size()),
          sender_endpoint, 0 /* flags */, ec);
    if (ec) {
      LOG(WARNING) << "Sync receive failed, dropping " << socket.available() << " bytes: " << ec;
      break;
    } else if (bytes_transferred == 0) {
      LOG(WARNING)
        << "Sync receive had no data, dropping " << socket.available() << " bytes: " << ec;
      break;
    } else {
      recv_cb(ec, bytes_transferred);
    }
  }

  // Close the socket
  if (socket.is_open()) {
    socket.close(ec);
    if (ec) {
      LOG(ERROR) << "Error on reader socket close: " << ec;
    }
  }
}
<|endoftext|>


<commit_before>#include "mooapplication.h"

#include <QFile>
#include <QDateTime>
#include <QStandardPaths>
#include <QTextStream>
#include <QDebug>
#include <QFileInfo>
#include <QDir>
#include <QSysInfo>

#include <iostream>

#include <lua.hpp>

#include "lua_moo.h"
#include "mooapp.h"

#include "connectionmanager.h"
#include "connection.h"
#include "listenertelnet.h"
#include "listenerwebsocket.h"
#include "taskentry.h"
#include "objectmanager.h"
#include "osc.h"

#define Q(x) #x
#define QUOTE(x) Q(x)

MooApplication	*MooApplication::mInstance = nullptr;

MooApplication::MooApplication( QCoreApplication &a ) :
	QObject( &a ), mApp( a ), mMooApp( nullptr ), mOSC( nullptr ), mServer( nullptr ),
	mOptionDataDirectory( QStringList() << "d" << "dir", "data directory", "directory", QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ) ),
	mOptionServerPort( QStringList() << "p" << "port", "default server port", "port", "1123" ),
	mOptionConfiguration( QStringList() << "c" << "cfg", "path to moo.ini", "filepath", "moo.ini" )
{
	mInstance = this;

	a.setApplicationName( "ArtMOO" );
	a.setOrganizationName( "Alex May" );
	a.setOrganizationDomain( "www.bigfug.com" );

#if QT_VERSION < QT_VERSION_CHECK( 5, 4, 0 )
	QApplication::setApplicationVersion( QUOTE( MOO_VERSION ) );
#else
	a.setApplicationVersion( QString( "%1 (%2/%3)" ).arg( QUOTE( MOO_VERSION ) ).arg( QSysInfo::buildCpuArchitecture() ).arg( QSysInfo::currentCpuArchitecture() ) );
#endif

	connect( this, &MooApplication::message, this, &MooApplication::fileMessageHandler );

#if defined( QT_DEBUG )
	connect( this, &MooApplication::message, this, &MooApplication::debugMessageHandler );
#endif

	qInstallMessageHandler( &MooApplication::staticMessageHandler );

	//-------------------------------------------------------------------------
	// Command line parsing

	mParser.setApplicationDescription( "ArtMOO" );
	mParser.addHelpOption();
	mParser.addVersionOption();

	mParser.addOption( mOptionDataDirectory );
	mParser.addOption( mOptionServerPort );
	mParser.addOption( mOptionConfiguration );
}

MooApplication::~MooApplication()
{
	qInstallMessageHandler( nullptr );

	mInstance = 0;
}

void MooApplication::process( void )
{
	mParser.process( mApp );

	//-------------------------------------------------------------------------

	QFileInfo	DataDirInfo( mParser.value( mOptionDataDirectory ) );

	if( !DataDirInfo.exists() )
	{
		DataDirInfo.dir().mkpath( DataDirInfo.fileName() );
	}

	if( !QDir::setCurrent( DataDirInfo.filePath() ) )
	{
		qFatal( QObject::tr( "Can't set directory to %1" ).arg( DataDirInfo.filePath() ).toLatin1().constData() );
	}

	qInfo() << QString( "ArtMOO v%1 by Alex May - www.bigfug.com" ).arg( mApp.applicationVersion() );

	qInfo() << "Using data directory" << DataDirInfo.filePath();

	//-------------------------------------------------------------------------

	lua_moo::setSettingsFilePath( optionConfigurationFile() );

	QFileInfo	SettingsFileInfo( lua_moo::settingsFilePath() );

	if( !SettingsFileInfo.isFile() || !SettingsFileInfo.isReadable() )
	{
		SettingsFileInfo.makeAbsolute();

		qCritical() << "Can't open" << SettingsFileInfo.filePath();
	}
	else
	{
		SettingsFileInfo.makeAbsolute();

		qInfo() << "Using settings in" << SettingsFileInfo.filePath();
	}

	//-------------------------------------------------------------------------

	int			Port = optionServerPort();

	if( Port < 0 || Port > 65535 )
	{
		qFatal( "Port must be in the range 0-65535" );
	}

	if( Port < 1024 )
	{
		qWarning() << "Port" << Port << "is in the 'well known' range of TCP/IP ports (0-1023)";
	}
}

bool MooApplication::initialiseApp()
{
	mMooApp = new mooApp();

	if( !mMooApp )
	{
		return( false );
	}

	mOSC = OSC::newDevice();

	if( !mOSC )
	{
		return( false );
	}

	ConnectionManager	*CM = ConnectionManager::instance();

	if( !CM )
	{
		return( false );
	}

	mServer	= new ListenerTelnet( 0, optionServerPort(), CM );

	if( !mServer )
	{
		return( false );
	}

	CM->doConnect( 0 );

	qInfo() << "ArtMOO listening for connections on port" << optionServerPort();

	return( true );
}

void MooApplication::deinitialiseApp()
{
	qInfo() << "ArtMOO exiting\n";

	if( mServer )
	{
		delete mServer;

		mServer = nullptr;
	}

	if( mOSC )
	{
		OSC::delDevice( mOSC );

		mOSC = nullptr;
	}

	if( mMooApp )
	{
		delete mMooApp;

		mMooApp = nullptr;
	}
}

void MooApplication::staticMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
	if( mInstance )
	{
		mInstance->messageHandler( type, context, msg );
	}
}

void MooApplication::messageHandler( QtMsgType type, const QMessageLogContext &context, const QString &msg )
{
	Q_UNUSED( context )

	const QDateTime		CurDat = QDateTime::currentDateTime();
	QString				DateTime = CurDat.toString( "dd-MMM-yyyy hh:mm:ss" );
	QString				LogMsg;

	switch( type )
	{
		case QtDebugMsg:
			LogMsg = "[DBUG]";
			break;

		case QtInfoMsg:
			LogMsg = "[INFO]";
			break;

		case QtWarningMsg:
			LogMsg = "[WARN]";
			break;

		case QtCriticalMsg:
			LogMsg = "[CRIT]";
			break;

		default:
			LogMsg = "[----]";
			break;
	}

	emit message( QString( "%1 - %2: %3" ).arg( DateTime ).arg( LogMsg ).arg( msg ) );
}

void MooApplication::fileMessageHandler(QString pMessage)
{
	QFile				LogFil( "moo.log" );

	if( LogFil.open( QIODevice::Append | QIODevice::Text ) )
	{
		QTextStream		TxtOut( &LogFil );

		TxtOut << pMessage << "\n";

		LogFil.close();
	}
}

void MooApplication::debugMessageHandler(QString pMessage)
{
	std::cout << pMessage.toStdString() << std::endl;
}
<commit_msg>Switched around data directory parsing so it tries to load settings file relative to the location of the cwd at startup<commit_after>#include "mooapplication.h"

#include <QFile>
#include <QDateTime>
#include <QStandardPaths>
#include <QTextStream>
#include <QDebug>
#include <QFileInfo>
#include <QDir>
#include <QSysInfo>

#include <iostream>

#include <lua.hpp>

#include "lua_moo.h"
#include "mooapp.h"

#include "connectionmanager.h"
#include "connection.h"
#include "listenertelnet.h"
#include "listenerwebsocket.h"
#include "taskentry.h"
#include "objectmanager.h"
#include "osc.h"

#define Q(x) #x
#define QUOTE(x) Q(x)

MooApplication	*MooApplication::mInstance = nullptr;

MooApplication::MooApplication( QCoreApplication &a ) :
	QObject( &a ), mApp( a ), mMooApp( nullptr ), mOSC( nullptr ), mServer( nullptr ),
	mOptionDataDirectory( QStringList() << "d" << "dir", "data directory", "directory", QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ) ),
	mOptionServerPort( QStringList() << "p" << "port", "default server port", "port", "1123" ),
	mOptionConfiguration( QStringList() << "c" << "cfg", "path to moo.ini", "filepath", "moo.ini" )
{
	mInstance = this;

	a.setApplicationName( "ArtMOO" );
	a.setOrganizationName( "Alex May" );
	a.setOrganizationDomain( "www.bigfug.com" );

#if QT_VERSION < QT_VERSION_CHECK( 5, 4, 0 )
	QApplication::setApplicationVersion( QUOTE( MOO_VERSION ) );
#else
	a.setApplicationVersion( QString( "%1 (%2/%3)" ).arg( QUOTE( MOO_VERSION ) ).arg( QSysInfo::buildCpuArchitecture() ).arg( QSysInfo::currentCpuArchitecture() ) );
#endif

	connect( this, &MooApplication::message, this, &MooApplication::fileMessageHandler );

#if defined( QT_DEBUG )
	connect( this, &MooApplication::message, this, &MooApplication::debugMessageHandler );
#endif

	qInstallMessageHandler( &MooApplication::staticMessageHandler );

	//-------------------------------------------------------------------------
	// Command line parsing

	mParser.setApplicationDescription( "ArtMOO" );
	mParser.addHelpOption();
	mParser.addVersionOption();

	mParser.addOption( mOptionDataDirectory );
	mParser.addOption( mOptionServerPort );
	mParser.addOption( mOptionConfiguration );
}

MooApplication::~MooApplication()
{
	qInstallMessageHandler( nullptr );

	mInstance = 0;
}

void MooApplication::process( void )
{
	qInfo() << QString( "ArtMOO v%1 by Alex May - www.bigfug.com" ).arg( mApp.applicationVersion() );

	mParser.process( mApp );

	//-------------------------------------------------------------------------

	lua_moo::setSettingsFilePath( optionConfigurationFile() );

	QFileInfo	SettingsFileInfo( lua_moo::settingsFilePath() );

	if( !SettingsFileInfo.isFile() || !SettingsFileInfo.isReadable() )
	{
		SettingsFileInfo.makeAbsolute();

		qCritical() << "Can't open" << SettingsFileInfo.filePath();
	}
	else
	{
		SettingsFileInfo.makeAbsolute();

		qInfo() << "Using settings in" << SettingsFileInfo.filePath();
	}

	//-------------------------------------------------------------------------

	QFileInfo	DataDirInfo( mParser.value( mOptionDataDirectory ) );

	if( !DataDirInfo.exists() )
	{
		DataDirInfo.dir().mkpath( DataDirInfo.fileName() );
	}

	if( !QDir::setCurrent( DataDirInfo.filePath() ) )
	{
		qFatal( QObject::tr( "Can't set directory to %1" ).arg( DataDirInfo.filePath() ).toLatin1().constData() );
	}

	qInfo() << "Using data directory" << DataDirInfo.filePath();

	//-------------------------------------------------------------------------

	int			Port = optionServerPort();

	if( Port < 0 || Port > 65535 )
	{
		qFatal( "Port must be in the range 0-65535" );
	}

	if( Port < 1024 )
	{
		qWarning() << "Port" << Port << "is in the 'well known' range of TCP/IP ports (0-1023)";
	}
}

bool MooApplication::initialiseApp()
{
	mMooApp = new mooApp();

	if( !mMooApp )
	{
		return( false );
	}

	mOSC = OSC::newDevice();

	if( !mOSC )
	{
		return( false );
	}

	ConnectionManager	*CM = ConnectionManager::instance();

	if( !CM )
	{
		return( false );
	}

	mServer	= new ListenerTelnet( 0, optionServerPort(), CM );

	if( !mServer )
	{
		return( false );
	}

	CM->doConnect( 0 );

	qInfo() << "ArtMOO listening for connections on port" << optionServerPort();

	return( true );
}

void MooApplication::deinitialiseApp()
{
	qInfo() << "ArtMOO exiting\n";

	if( mServer )
	{
		delete mServer;

		mServer = nullptr;
	}

	if( mOSC )
	{
		OSC::delDevice( mOSC );

		mOSC = nullptr;
	}

	if( mMooApp )
	{
		delete mMooApp;

		mMooApp = nullptr;
	}
}

void MooApplication::staticMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
	if( mInstance )
	{
		mInstance->messageHandler( type, context, msg );
	}
}

void MooApplication::messageHandler( QtMsgType type, const QMessageLogContext &context, const QString &msg )
{
	Q_UNUSED( context )

	const QDateTime		CurDat = QDateTime::currentDateTime();
	QString				DateTime = CurDat.toString( "dd-MMM-yyyy hh:mm:ss" );
	QString				LogMsg;

	switch( type )
	{
		case QtDebugMsg:
			LogMsg = "[DBUG]";
			break;

		case QtInfoMsg:
			LogMsg = "[INFO]";
			break;

		case QtWarningMsg:
			LogMsg = "[WARN]";
			break;

		case QtCriticalMsg:
			LogMsg = "[CRIT]";
			break;

		default:
			LogMsg = "[----]";
			break;
	}

	emit message( QString( "%1 - %2: %3" ).arg( DateTime ).arg( LogMsg ).arg( msg ) );
}

void MooApplication::fileMessageHandler(QString pMessage)
{
	QFile				LogFil( "moo.log" );

	if( LogFil.open( QIODevice::Append | QIODevice::Text ) )
	{
		QTextStream		TxtOut( &LogFil );

		TxtOut << pMessage << "\n";

		LogFil.close();
	}
}

void MooApplication::debugMessageHandler(QString pMessage)
{
	std::cout << pMessage.toStdString() << std::endl;
}
<|endoftext|>


<commit_before>//This tutorial illustrates how to create an histogram with polygonal
//bins (TH2Poly), fill it and draw it. The initial data are stored
//in TMultiGraphs. They represent the european countries.
//The histogram filling is done according to a Mercator projection,
//therefore the bin contains should be proportional to the real surface
//of the countries.
//
//The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/
//This database was developed in 1991/1992 and national boundaries reflect
//political reality as of that time.
//
//Author: Olivier Couet

void th2polyEurope()
{
   Int_t i,j;
   Double_t lon1 = -25;
   Double_t lon2 =  35;
   Double_t lat1 =  34;
   Double_t lat2 =  72;
   Double_t R = (lat2-lat1)/(lon2-lon1);
   Int_t W    = 800;
   Int_t H    = (Int_t)(R*800);

   // Canvas used to draw TH2Poly (the map)
   TCanvas *ce = new TCanvas("ce", "ce",0,0,W,H);
   ce->ToggleEventStatus();
   ce->SetGridx();
   ce->SetGridy();

   // Real surfaces taken from Wikipedia.
   const Int_t nx = 36;
   char *countries[nx] = { "france", "spain", "sweden", "germany", "finland",
                           "norway", "poland", "italy", "yugoslavia",
                           "united_kingdom", "romania", "belarus", "greece",
                           "czechoslovakia", "bulgaria", "iceland", "hungary",
                           "portugal", "austria", "ireland", "lithuania",
                           "latvia", "estonia", "denmark", "netherlands",
                           "switzerland", "moldova", "belgium", "albania",
                           "cyprus", "luxembourg", "andorra", "malta",
                           "liechtenstein", "san_marino", "monaco" };
   Float_t surfaces[nx] = { 547030, 505580, 449964, 357021, 338145, 324220,
                            312685, 301230, 255438, 244820, 237500, 207600,
                            131940, 127711, 110910, 103000, 93030, 89242, 83870,
                            70280, 65200, 64589, 45226, 43094, 41526, 41290,
                            33843, 30528, 28748, 9250, 2586, 468, 316, 160, 61,
                            2};

   TH1F *h = new TH1F("h","Countries' surfaces (in km^{2})",3,0,3);
   for (i=0; i<nx; i++) h->Fill(countries[i], surfaces[i]);
   h->LabelsDeflate();

   TFile *f;
   f = TFile::Open("http://root.cern.ch/files/europe.root");

   TH2Poly *p = new TH2Poly("Europe","Europe",lon1,lon2,lat1,lat2);
   p->GetXaxis()->SetNdivisions(520);

   TMultiGraph *mg;
   TKey *key;
   TIter nextkey(gDirectory->GetListOfKeys());
   while (key = (TKey*)nextkey()) {
      obj = key->ReadObj();
      if (obj->InheritsFrom("TMultiGraph")) {
         mg = (TMultiGraph*)obj;
         p->AddBin(mg);
      }
   }

   TRandom r;
   Double_t longitude, latitude;
   Double_t x, y, dr = TMath::Pi()/180, rd = 180/TMath::Pi();

   gBenchmark->Start("Partitioning");
   p->ChangePartition(100, 100);
   gBenchmark->Show("Partitioning");

   // Fill TH2Poly according to a Mercator projection.
   gBenchmark->Start("Filling");
   for (i=0; i<500000; i++) {
      longitude = r.Uniform(dr*lon1,dr*lon2);
      latitude  = r.Uniform(-dr*90,dr*90);
      x         = rd*longitude;
      y         = 39*TMath::Log(TMath::Tan((TMath::Pi()/4)+(latitude/2)));
      p->Fill(x,y);
   }
   gBenchmark->Show("Filling");

   gStyle->SetOptStat(1111);
   gStyle->SetPalette(1);
   p->Draw("COL");

   Int_t nbins = p->GetNumberOfBins();
   Double_t maximum = p->GetMaximum();
   printf("Nbins = %d Minimum = %g Maximum = %g Integral = %f \n",
           nbins,
           p->GetMinimum(),
           maximum,
           p->Integral());

   // h2 contains the surfaces computed from TH2Poly.
   TH1F *h2 = h->Clone();
   h2->Reset();
   for (j=0; j<nx; j++) {
      for (i=0; i<nbins; i++) {
         if (strstr(countries[j],p->GetBinName(i+1))) {
            h2->Fill(countries[j],p->GetBinContent(i+1));
            h2->SetBinError(j, p->GetBinError(i+1));
         }
      }
   }

   TCanvas *c1 = new TCanvas("c1", "c1",W+10,0,W-20,H);
   c1->SetRightMargin(0.047);

   gStyle->SetTitleX(0.35);
   Double_t scale = h->GetMaximum()/h2->GetMaximum();

   h->Draw();
   h->SetStats(0);
   h->SetFillColor(38);
   h->GetXaxis()->SetLabelFont(42);
   h->GetXaxis()->SetLabelSize(0.03);
   h->GetYaxis()->SetLabelFont(42);

   h2->Scale(scale);
   h2->Draw("E3 SAME");
   h2->SetFillColor(kRed-3);
   h2->SetFillStyle(3001);

   TLegend *leg = new TLegend(0.5,0.67,0.92,0.8,NULL,"NDC");
   leg->SetTextFont(42);
   leg->SetTextSize(0.025);
   leg->AddEntry(h,"Real countries' surfaces from Wikipedia (in km^{2})","f");
   leg->AddEntry(h2,"Countries' surfaces from TH2Poly (with errors)","f");
   leg->Draw();
}
<commit_msg>- better version<commit_after>//This tutorial illustrates how to create an histogram with polygonal
//bins (TH2Poly), fill it and draw it. The initial data are stored
//in TMultiGraphs. They represent the european countries.
//The histogram filling is done according to a Mercator projection,
//therefore the bin contains should be proportional to the real surface
//of the countries.
//
//The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/
//This database was developed in 1991/1992 and national boundaries reflect
//political reality as of that time.
//
//Author: Olivier Couet

void th2polyEurope()
{
   Int_t i,j;
   Double_t lon1 = -25;
   Double_t lon2 =  35;
   Double_t lat1 =  34;
   Double_t lat2 =  72;
   Double_t R = (lat2-lat1)/(lon2-lon1);
   Int_t W    = 800;
   Int_t H    = (Int_t)(R*800);
   gStyle->SetTitleX(0.2);
   gStyle->SetStatY(0.89);
   gStyle->SetStatW(0.15);

   // Canvas used to draw TH2Poly (the map)
   TCanvas *ce = new TCanvas("ce", "ce",0,0,W,H);
   ce->ToggleEventStatus();
   ce->SetGridx();
   ce->SetGridy();

   // Real surfaces taken from Wikipedia.
   const Int_t nx = 36;
   char *countries[nx] = { "france", "spain", "sweden", "germany", "finland",
                           "norway", "poland", "italy", "yugoslavia",
                           "united_kingdom", "romania", "belarus", "greece",
                           "czechoslovakia", "bulgaria", "iceland", "hungary",
                           "portugal", "austria", "ireland", "lithuania",
                           "latvia", "estonia", "denmark", "netherlands",
                           "switzerland", "moldova", "belgium", "albania",
                           "cyprus", "luxembourg", "andorra", "malta",
                           "liechtenstein", "san_marino", "monaco" };
   Float_t surfaces[nx] = { 547030, 505580, 449964, 357021, 338145, 324220,
                            312685, 301230, 255438, 244820, 237500, 207600,
                            131940, 127711, 110910, 103000, 93030, 89242, 83870,
                            70280, 65200, 64589, 45226, 43094, 41526, 41290,
                            33843, 30528, 28748, 9250, 2586, 468, 316, 160, 61,
                            2};

   TH1F *h = new TH1F("h","Countries' surfaces (in km^{2})",3,0,3);
   for (i=0; i<nx; i++) h->Fill(countries[i], surfaces[i]);
   h->LabelsDeflate();

   TFile *f;
   f = TFile::Open("http://root.cern.ch/files/europe.root");

   TH2Poly *p = new TH2Poly(
             "Europe",
             "Europe (bins' contains are normalize to the surfaces in km^{2})",
             lon1,lon2,lat1,lat2);
   p->GetXaxis()->SetNdivisions(520);

   TMultiGraph *mg;
   TKey *key;
   TIter nextkey(gDirectory->GetListOfKeys());
   while (key = (TKey*)nextkey()) {
      obj = key->ReadObj();
      if (obj->InheritsFrom("TMultiGraph")) {
         mg = (TMultiGraph*)obj;
         p->AddBin(mg);
      }
   }

   TRandom r;
   Double_t longitude, latitude;
   Double_t x, y, dr = TMath::Pi()/180, rd = 180/TMath::Pi();

   gBenchmark->Start("Partitioning");
   p->ChangePartition(100, 100);
   gBenchmark->Show("Partitioning");

   // Fill TH2Poly according to a Mercator projection.
   gBenchmark->Start("Filling");
   for (i=0; i<500000; i++) {
      longitude = r.Uniform(dr*lon1,dr*lon2);
      latitude  = r.Uniform(-dr*90,dr*90);
      x         = rd*longitude;
      y         = 39*TMath::Log(TMath::Tan((TMath::Pi()/4)+(latitude/2)));
      p->Fill(x,y);
   }
   gBenchmark->Show("Filling");

   Int_t nbins = p->GetNumberOfBins();
   Double_t maximum = p->GetMaximum();
   printf("Nbins = %d Minimum = %g Maximum = %g Integral = %f \n",
           nbins,
           p->GetMinimum(),
           maximum,
           p->Integral());

   // h2 contains the surfaces computed from TH2Poly.
   TH1F *h2 = h->Clone();
   h2->Reset();
   for (j=0; j<nx; j++) {
      for (i=0; i<nbins; i++) {
         if (strstr(countries[j],p->GetBinName(i+1))) {
            h2->Fill(countries[j],p->GetBinContent(i+1));
            h2->SetBinError(j, p->GetBinError(i+1));
         }
      }
   }

   // Normalize the TH2Poly bin contents to the real surfaces.
   Double_t scale = surfaces[0]/maximum;
   for (i=0; i<nbins; i++) p->SetBinContent(i+1, scale*p->GetBinContent(i+1));

   gStyle->SetOptStat(1111);
   gStyle->SetPalette(1);
   p->Draw("COL");

   TCanvas *c1 = new TCanvas("c1", "c1",W+10,0,W-20,H);
   c1->SetRightMargin(0.047);

   Double_t scale = h->GetMaximum()/h2->GetMaximum();

   h->SetStats(0);
   h->Draw("L");
   h->SetLineColor(kRed-3);
   h->SetLineWidth(2);
   h->GetXaxis()->SetLabelFont(42);
   h->GetXaxis()->SetLabelSize(0.03);
   h->GetYaxis()->SetLabelFont(42);

   h2->Scale(scale);
   h2->Draw("E SAME");
   h2->SetMarkerStyle(20);
   h2->SetMarkerSize(0.8);

   TLegend *leg = new TLegend(0.5,0.67,0.92,0.8,NULL,"NDC");
   leg->SetTextFont(42);
   leg->SetTextSize(0.025);
   leg->AddEntry(h,"Real countries' surfaces from Wikipedia (in km^{2})","l");
   leg->AddEntry(h2,"Countries' surfaces from TH2Poly (with errors)","lp");
   leg->Draw();
}
<|endoftext|>


<commit_before>#include "country.hpp"

#include "../base/logging.hpp"

#include "../base/std_serialization.hpp"

#include "../coding/streams_sink.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/file_writer.hpp"

#include "../version/version.hpp"

#include "../platform/platform.hpp"

#include "../indexer/data_header.hpp"
#include "../indexer/data_header_reader.hpp"

#include "../std/fstream.hpp"

namespace storage
{
  /// Simple check - compare url size with real file size on disk
  bool IsTileDownloaded(TTile const & tile)
  {
    uint64_t size = 0;
    if (!GetPlatform().GetFileSize(GetPlatform().WritablePathForFile(tile.first), size))
      return false;
    return true;//tile.second == size;
  }

  struct CountryBoundsCalculator
  {
    m2::RectD & m_bounds;
    CountryBoundsCalculator(m2::RectD & bounds) : m_bounds(bounds) {}
    void operator()(TTile const & tile)
    {
      static feature::DataHeader header;
      if (feature::ReadDataHeader(GetPlatform().WritablePathForFile(tile.first), header))
        m_bounds.Add(header.Bounds());
    }
  };

  m2::RectD Country::Bounds() const
  {
    m2::RectD bounds;
    std::for_each(m_tiles.begin(), m_tiles.end(), CountryBoundsCalculator(bounds));
    return bounds;
  }

  struct SizeCalculator
  {
    uint64_t & m_localSize;
    uint64_t & m_remoteSize;
    SizeCalculator(uint64_t & localSize, uint64_t & remoteSize)
      : m_localSize(localSize), m_remoteSize(remoteSize) {}
    void operator()(TTile const & tile)
    {
      if (IsTileDownloaded(tile))
        m_localSize += tile.second;
      else
        m_remoteSize += tile.second;
    }
  };

  TLocalAndRemoteSize Country::Size() const
  {
    uint64_t localSize = 0;
    uint64_t remoteSize = 0;
    std::for_each(m_tiles.begin(), m_tiles.end(), SizeCalculator(localSize, remoteSize));
    return TLocalAndRemoteSize(localSize, remoteSize);
  }

  void Country::AddTile(TTile const & tile)
  {
    m_tiles.push_back(tile);
  }

  ////////////////////////////////////////////////////////////////////////

//  template <class TArchive> TArchive & operator << (TArchive & ar, storage::Country const & country)
//  {
//    ar << country.m_name;
//    ar << country.m_tiles;
//    return ar;
//  }

  inline bool IsCellId(string const & cellId)
  {
    size_t const size = cellId.size();
    if (size == 0)
      return false;
    for (size_t i = 0; i < size; ++i)
    {
      if (cellId[i] < '0' || cellId[i] > '3')
        return false;
    }
    return true;
  }

  bool LoadCountries(string const & countriesFile, TTilesContainer const & sortedTiles,
                     TCountriesContainer & countries)
  {
    countries.Clear();
    ifstream stream(countriesFile.c_str());
    std::string line;
    Country * currentCountry = &countries.Value();
    while (stream.good())
    {
      std::getline(stream, line);
      if (line.empty())
        continue;

      // calculate spaces - depth inside the tree
      int spaces = 0;
      for (size_t i = 0; i < line.size(); ++i)
      {
        if (line[i] == ' ')
          ++spaces;
        else
          break;
      }
      switch (spaces)
      {
      case 0: // this is value for current tree node
        {
          TTilesContainer::const_iterator const first = sortedTiles.begin();
          TTilesContainer::const_iterator const last = sortedTiles.end();
          TTilesContainer::const_iterator found = lower_bound(first, last, TTile(line, 0));
          // @TODO current implementation supports only cellId level 8 and 7
          if (!(found != last && !(line < found->first)) && IsCellId(line))
          {
            line.resize(line.size() - 1);
            found = lower_bound(first, last, TTile(line, 0));
          }

          if (found != last && !(line < found->first))
            currentCountry->AddTile(*found);
        }
        break;
      case 1: // country group
      case 2: // country name
      case 3: // region
        currentCountry = &countries.AddAtDepth(spaces - 1, Country(line.substr(spaces)));
        break;
      default:
        return false;
      }
    }
    return countries.SiblingsCount() > 0;
  }

  void SaveTiles(string const & file, int32_t level, TDataFiles const & cellFiles, TCommonFiles const & commonFiles)
  {
    FileWriter writer(file);
    stream::SinkWriterStream<Writer> wStream(writer);
    wStream << static_cast<uint32_t>(Version::BUILD);
    wStream << level;
    wStream << cellFiles;
    wStream << commonFiles;
  }

  bool LoadTiles(TTilesContainer & tiles, string const & tilesFile, uint32_t & dataVersion)
  {
    tiles.clear();

    try
    {
      FileReader fileReader(tilesFile);
      ReaderSource<FileReader> source(fileReader);
      stream::SinkReaderStream<ReaderSource<FileReader> > stream(source);

      TDataFiles dataFiles;
      TCommonFiles commonFiles;

      int32_t level = -1;
      stream >> dataVersion;
      stream >> level;
      stream >> dataFiles;
      stream >> commonFiles;

      tiles.reserve(dataFiles.size() + commonFiles.size());

      for (TDataFiles::iterator it = dataFiles.begin(); it != dataFiles.end(); ++it)
        tiles.push_back(TTile(CountryCellId::FromBitsAndLevel(it->first, level).ToString(), it->second));
      for (TCommonFiles::iterator it = commonFiles.begin(); it != commonFiles.end(); ++it)
        tiles.push_back(TTile(it->first, it->second));

      sort(tiles.begin(), tiles.end());
    }
    catch (RootException const & e)
    {
      LOG(LWARNING, ("Can't read tiles file", e.what()));
      return false;
    }

    return true;
  }

//  void SaveCountries(TCountriesContainer const & countries, Writer & writer)
//  {
//    stream::SinkWriterStream<Writer> wStream(writer);
//    wStream << MAPS_MAJOR_VERSION_BINARY_FORMAT;
//    wStream << countries;
//  }
}
<commit_msg>Fixed missed data extension in downloader<commit_after>#include "country.hpp"

#include "../base/logging.hpp"

#include "../base/std_serialization.hpp"

#include "../coding/streams_sink.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/file_writer.hpp"

#include "../version/version.hpp"

#include "../platform/platform.hpp"

#include "../indexer/data_header.hpp"
#include "../indexer/data_header_reader.hpp"

#include "../std/fstream.hpp"

namespace storage
{
  /// Simple check - compare url size with real file size on disk
  bool IsTileDownloaded(TTile const & tile)
  {
    uint64_t size = 0;
    if (!GetPlatform().GetFileSize(GetPlatform().WritablePathForFile(tile.first), size))
      return false;
    return true;//tile.second == size;
  }

  struct CountryBoundsCalculator
  {
    m2::RectD & m_bounds;
    CountryBoundsCalculator(m2::RectD & bounds) : m_bounds(bounds) {}
    void operator()(TTile const & tile)
    {
      static feature::DataHeader header;
      if (feature::ReadDataHeader(GetPlatform().WritablePathForFile(tile.first), header))
        m_bounds.Add(header.Bounds());
    }
  };

  m2::RectD Country::Bounds() const
  {
    m2::RectD bounds;
    std::for_each(m_tiles.begin(), m_tiles.end(), CountryBoundsCalculator(bounds));
    return bounds;
  }

  struct SizeCalculator
  {
    uint64_t & m_localSize;
    uint64_t & m_remoteSize;
    SizeCalculator(uint64_t & localSize, uint64_t & remoteSize)
      : m_localSize(localSize), m_remoteSize(remoteSize) {}
    void operator()(TTile const & tile)
    {
      if (IsTileDownloaded(tile))
        m_localSize += tile.second;
      else
        m_remoteSize += tile.second;
    }
  };

  TLocalAndRemoteSize Country::Size() const
  {
    uint64_t localSize = 0;
    uint64_t remoteSize = 0;
    std::for_each(m_tiles.begin(), m_tiles.end(), SizeCalculator(localSize, remoteSize));
    return TLocalAndRemoteSize(localSize, remoteSize);
  }

  void Country::AddTile(TTile const & tile)
  {
    m_tiles.push_back(tile);
  }

  ////////////////////////////////////////////////////////////////////////

//  template <class TArchive> TArchive & operator << (TArchive & ar, storage::Country const & country)
//  {
//    ar << country.m_name;
//    ar << country.m_tiles;
//    return ar;
//  }

  inline bool IsCellId(string const & cellId)
  {
    size_t const size = cellId.size();
    if (size == 0)
      return false;
    for (size_t i = 0; i < size; ++i)
    {
      if (cellId[i] < '0' || cellId[i] > '3')
        return false;
    }
    return true;
  }

  bool LoadCountries(string const & countriesFile, TTilesContainer const & sortedTiles,
                     TCountriesContainer & countries)
  {
    countries.Clear();
    ifstream stream(countriesFile.c_str());
    std::string line;
    Country * currentCountry = &countries.Value();
    while (stream.good())
    {
      std::getline(stream, line);
      if (line.empty())
        continue;

      // calculate spaces - depth inside the tree
      int spaces = 0;
      for (size_t i = 0; i < line.size(); ++i)
      {
        if (line[i] == ' ')
          ++spaces;
        else
          break;
      }
      switch (spaces)
      {
      case 0: // this is value for current tree node
        {
          TTilesContainer::const_iterator const first = sortedTiles.begin();
          TTilesContainer::const_iterator const last = sortedTiles.end();
          TTilesContainer::const_iterator found = lower_bound(first, last, TTile(line, 0));
          // @TODO current implementation supports only cellId level 8 and 7
          if (!(found != last && !(line < found->first)) && IsCellId(line))
          {
            line.resize(line.size() - 1);
            found = lower_bound(first, last, TTile(line, 0));
          }

          if (found != last && !(line < found->first))
          {
            // fix extension for data files
            if (found->first.find_last_of(DATA_FILE_EXTENSION) == string::npos)
              currentCountry->AddTile(TTile(found->first + DATA_FILE_EXTENSION, found->second));
            else
              currentCountry->AddTile(*found);
          }
        }
        break;
      case 1: // country group
      case 2: // country name
      case 3: // region
        currentCountry = &countries.AddAtDepth(spaces - 1, Country(line.substr(spaces)));
        break;
      default:
        return false;
      }
    }
    return countries.SiblingsCount() > 0;
  }

  void SaveTiles(string const & file, int32_t level, TDataFiles const & cellFiles, TCommonFiles const & commonFiles)
  {
    FileWriter writer(file);
    stream::SinkWriterStream<Writer> wStream(writer);
    wStream << static_cast<uint32_t>(Version::BUILD);
    wStream << level;
    wStream << cellFiles;
    wStream << commonFiles;
  }

  bool LoadTiles(TTilesContainer & tiles, string const & tilesFile, uint32_t & dataVersion)
  {
    tiles.clear();

    try
    {
      FileReader fileReader(tilesFile);
      ReaderSource<FileReader> source(fileReader);
      stream::SinkReaderStream<ReaderSource<FileReader> > stream(source);

      TDataFiles dataFiles;
      TCommonFiles commonFiles;

      int32_t level = -1;
      stream >> dataVersion;
      stream >> level;
      stream >> dataFiles;
      stream >> commonFiles;

      tiles.reserve(dataFiles.size() + commonFiles.size());

      for (TDataFiles::iterator it = dataFiles.begin(); it != dataFiles.end(); ++it)
        tiles.push_back(TTile(CountryCellId::FromBitsAndLevel(it->first, level).ToString(), it->second));
      for (TCommonFiles::iterator it = commonFiles.begin(); it != commonFiles.end(); ++it)
        tiles.push_back(TTile(it->first, it->second));

      sort(tiles.begin(), tiles.end());
    }
    catch (RootException const & e)
    {
      LOG(LWARNING, ("Can't read tiles file", e.what()));
      return false;
    }

    return true;
  }

//  void SaveCountries(TCountriesContainer const & countries, Writer & writer)
//  {
//    stream::SinkWriterStream<Writer> wStream(writer);
//    wStream << MAPS_MAJOR_VERSION_BINARY_FORMAT;
//    wStream << countries;
//  }
}
<|endoftext|>


<commit_before>#include "program.h"
#include <vector>

namespace opencl {

// /* Program Object APIs  */
// extern CL_API_ENTRY cl_program CL_API_CALL
// clCreateProgramWithSource(cl_context        /* context */,
//                           cl_uint           /* count */,
//                           const char **     /* strings */,
//                           const size_t *    /* lengths */,
//                           cl_int *          /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(CreateProgramWithSource) {
  NanScope();
  REQ_ARGS(2);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_CONTEXT));
  }
  cl_context context=Unwrap<cl_context>(args[0]);
  REQ_STR_ARG(1, str);

  cl_int ret=CL_SUCCESS;
  size_t lengths[]={(size_t) str.length()};
  const char *strings[]={*str};
  cl_program p=::clCreateProgramWithSource(context, 1, strings, lengths, &ret);
  CHECK_ERR(ret);

  NanReturnValue(Wrap(p));
}

// extern CL_API_ENTRY cl_program CL_API_CALL
// clCreateProgramWithBinary(cl_context                     /* context */,
//                           cl_uint                        /* num_devices */,
//                           const cl_device_id *           /* device_list */,
//                           const size_t *                 /* lengths */,
//                           const unsigned char **         /* binaries */,
//                           cl_int *                       /* binary_status */,
//                           cl_int *                       /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(CreateProgramWithBinary) {
  NanScope();
  REQ_ARGS(3);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_CONTEXT));
  }
  cl_context context=Unwrap<cl_context>(args[0]);

  Local<Array> devices = Local<Array>::Cast(args[1]);
  vector<cl_device_id> cl_devices;
  for (uint32_t i=0; i < devices->Length(); i++)
    cl_devices.push_back(Unwrap<cl_device_id>(devices->Get(i)));

  Local<Array> binaries = Local<Array>::Cast(args[2]);
  const size_t n = binaries->Length();
  unique_ptr<size_t[]> lengths(new size_t[n]);
  unique_ptr<const unsigned char*[]> images(new const unsigned char*[n]);

  for (uint32_t i = 0; i < n; ++i) {
    Local<Object> obj=binaries->Get(i)->ToObject();
    images[i] = (const unsigned char*) obj->GetIndexedPropertiesExternalArrayData();
    lengths[i] = obj->GetIndexedPropertiesExternalArrayDataLength();
  }

  cl_int ret=CL_SUCCESS;
  cl_program p=::clCreateProgramWithBinary(
              context, (cl_uint) cl_devices.size(),
              &cl_devices.front(),
              lengths.get(), images.get(),
              NULL, &ret);
  CHECK_ERR(ret);

  NanReturnValue(Wrap(p));
}

// extern CL_API_ENTRY cl_program CL_API_CALL
// clCreateProgramWithBuiltInKernels(cl_context            /* context */,
//                                   cl_uint               /* num_devices */,
//                                   const cl_device_id *  /* device_list */,
//                                   const char *          /* kernel_names */,
//                                   cl_int *              /* errcode_ret */) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(CreateProgramWithBuiltInKernels) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clRetainProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(RetainProgram) {
  NanScope();
  REQ_ARGS(1);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_PROGRAM));
  }

  cl_program p = Unwrap<cl_program>(args[0]);
  cl_int count=clRetainProgram(p);

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clReleaseProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(ReleaseProgram) {
  NanScope();
  REQ_ARGS(1);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_PROGRAM));
  }

  cl_program p = Unwrap<cl_program>(args[0]);
  cl_int count=clRetainProgram(p);

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clBuildProgram(cl_program           /* program */,
//                cl_uint              /* num_devices */,
//                const cl_device_id * /* device_list */,
//                const char *         /* options */,
//                void (CL_CALLBACK *  /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
//                void *               /* user_data */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(BuildProgram) {
  NanScope();
  REQ_ARGS(1);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_PROGRAM));
  }

  cl_program p = Unwrap<cl_program>(args[0]);
  std::vector<cl_device_id> devices;

  if (!args[1]->IsNull() && !args[1]->IsUndefined()){
    getValuesFromArray(Local<Array>::Cast(args[1]),devices);
  }

  unique_ptr<String::Utf8Value> options = NULL;

  if (!args[2]->IsNull() && !args[2]->IsUndefined()){
    if (!args[2]->IsString())
      NanThrowTypeError("Argument 2 must be a string");
    options = unique_ptr<String::Utf8Value>( new String::Utf8Value(args[2]->ToString()));
  }

  //REQ_STR_ARG(2,options);

  // TODO callback + userdata

  CHECK_ERR(::clBuildProgram(p,
    devices.size(), devices.size() ? &devices.front() : nullptr,
    options != NULL ? **options : nullptr,
    nullptr, nullptr)); // TODO CB

  NanReturnValue(JS_INT(CL_SUCCESS));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clCompileProgram(cl_program           /* program */,
//                  cl_uint              /* num_devices */,
//                  const cl_device_id * /* device_list */,
//                  const char *         /* options */,
//                  cl_uint              /* num_input_headers */,
//                  const cl_program *   /* input_headers */,
//                  const char **        /* header_include_names */,
//                  void (CL_CALLBACK *  /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
//                  void *               /* user_data */) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(CompileProgram) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_program CL_API_CALL
// clLinkProgram(cl_context           /* context */,
//               cl_uint              /* num_devices */,
//               const cl_device_id * /* device_list */,
//               const char *         /* options */,
//               cl_uint              /* num_input_programs */,
//               const cl_program *   /* input_programs */,
//               void (CL_CALLBACK *  /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
//               void *               /* user_data */,
//               cl_int *             /* errcode_ret */ ) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(LinkProgram) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clUnloadPlatformCompiler(cl_platform_id /* platform */) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(UnloadPlatformCompiler) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clGetProgramInfo(cl_program         /* program */,
//                  cl_program_info    /* param_name */,
//                  size_t             /* param_value_size */,
//                  void *             /* param_value */,
//                  size_t *           /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(GetProgramInfo) {
  NanScope();
  REQ_ARGS(2);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_MEM_OBJECT));
  }
  cl_program prog=Unwrap<cl_program>(args[0]);
  cl_program_info param_name = args[1]->Uint32Value();

  switch(param_name) {
    case CL_PROGRAM_REFERENCE_COUNT:
    case CL_PROGRAM_NUM_DEVICES:
    {
      cl_uint val;
      CHECK_ERR(::clGetProgramInfo(prog,param_name,sizeof(cl_uint), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
    case CL_PROGRAM_CONTEXT:
    {
      cl_context val;
      CHECK_ERR(::clGetProgramInfo(prog,param_name,sizeof(cl_context), &val, NULL))
      // TODO NanReturnValue(JS_INT(val));
      break;
    }
    case CL_PROGRAM_DEVICES:
    {
      // TODO
      break;
    }
    case CL_PROGRAM_BINARY_SIZES:
    {
      size_t nsizes;
      CHECK_ERR(::clGetProgramInfo(prog, param_name, 0, NULL, &nsizes));

      // get CL_DEVICE_MAX_WORK_ITEM_SIZES array param
      unique_ptr<size_t[]> sizes(new size_t[nsizes]);
      CHECK_ERR(::clGetProgramInfo(prog, param_name, nsizes*sizeof(size_t), sizes.get(), NULL));
      Local<Array> arr = Array::New(nsizes);
      for(cl_uint i=0;i<nsizes;i++)
        arr->Set(i,JS_INT(sizes[i]));

      NanReturnValue(arr);
    }
    case CL_PROGRAM_BINARIES:
    {
      size_t nbins=0;
      CHECK_ERR(::clGetProgramInfo(prog, param_name, 0, NULL, &nbins));
      nbins /= sizeof(size_t);
      unique_ptr<char*[]> binaries(new char*[nbins]);
      CHECK_ERR(::clGetProgramInfo(prog, param_name, sizeof(char*)*nbins, binaries.get(), NULL));

      // TODO create an array for Uint8Array to return each binary associated to each device
      // Handle<Value> abv = Context::GetCurrent()->Global()->Get(String::NewSymbol("ArrayBuffer"));
      // Handle<Value> argv[] = { Integer::NewFromUnsigned(size) };
      // Handle<Object> arrbuf = Handle<Function>::Cast(abv)->NewInstance(1, argv);
      // void *buffer = arrbuf->GetPointerFromInternalField(0);
      // memcpy(buffer, data, size);

      NanReturnUndefined();
    }
    case CL_PROGRAM_NUM_KERNELS:
    {
      size_t val;
      CHECK_ERR(::clGetProgramInfo(prog,param_name,sizeof(size_t), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
    case CL_PROGRAM_SOURCE:
    case CL_PROGRAM_KERNEL_NAMES:
    {
      size_t nchars;
      CHECK_ERR(::clGetProgramInfo(prog, param_name, 0, NULL, &nchars));
      unique_ptr<char[]> names(new char[nchars]);
      CHECK_ERR(::clGetProgramInfo(prog, param_name, nchars*sizeof(char), names.get(), NULL));
      NanReturnValue(JS_STR(names.get()));
    }
  }
  return NanThrowError(JS_INT(CL_INVALID_VALUE));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clGetProgramBuildInfo(cl_program            /* program */,
//                       cl_device_id          /* device */,
//                       cl_program_build_info /* param_name */,
//                       size_t                /* param_value_size */,
//                       void *                /* param_value */,
//                       size_t *              /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(GetProgramBuildInfo) {
  NanScope();
  REQ_ARGS(3);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_MEM_OBJECT));
  }
  cl_program prog=Unwrap<cl_program>(args[0]);
  if(!isOpenCLObj(args[1])) {
    return NanThrowError(JS_INT(CL_INVALID_DEVICE));
  }
  cl_device_id device=Unwrap<cl_device_id>(args[1]);
  cl_program_build_info param_name = args[2]->Uint32Value();

  switch(param_name) {
    case CL_PROGRAM_BUILD_STATUS:
    {
      cl_build_status val;
      CHECK_ERR(::clGetProgramBuildInfo(prog, device,param_name,sizeof(cl_build_status), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
    case CL_PROGRAM_BUILD_OPTIONS:
    case CL_PROGRAM_BUILD_LOG:
    {
      size_t param_value_size_ret=0;
      CHECK_ERR(::clGetProgramBuildInfo(prog, device, param_name, 0, NULL, &param_value_size_ret));
      unique_ptr<char[]> param_value(new char[param_value_size_ret]);
      CHECK_ERR(::clGetProgramBuildInfo(prog, device, param_name, param_value_size_ret, param_value.get(), NULL));
      NanReturnValue(JS_STR(param_value.get(),(int)param_value_size_ret));
    }
    case CL_PROGRAM_BINARY_TYPE:
    {
      cl_program_binary_type val;
      CHECK_ERR(::clGetProgramBuildInfo(prog, device,param_name,sizeof(cl_program_binary_type), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
  }
  return NanThrowError(JS_INT(CL_INVALID_VALUE));
}

namespace Program {
void init(Handle<Object> exports)
{
  NODE_SET_METHOD(exports, "createProgramWithSource", CreateProgramWithSource);
  NODE_SET_METHOD(exports, "createProgramWithBinary", CreateProgramWithBinary);
  NODE_SET_METHOD(exports, "createProgramWithBuiltInKernels", CreateProgramWithBuiltInKernels);
  NODE_SET_METHOD(exports, "retainProgram", RetainProgram);
  NODE_SET_METHOD(exports, "releaseProgram", ReleaseProgram);
  NODE_SET_METHOD(exports, "buildProgram", BuildProgram);
  NODE_SET_METHOD(exports, "compileProgram", CompileProgram);
  NODE_SET_METHOD(exports, "linkProgram", LinkProgram);
  NODE_SET_METHOD(exports, "unloadPlatformCompiler", UnloadPlatformCompiler);
  NODE_SET_METHOD(exports, "getProgramInfo", GetProgramInfo);
  NODE_SET_METHOD(exports, "getProgramBuildInfo", GetProgramBuildInfo);
}
} // namespace Program

} // namespace opencl
<commit_msg>[Fix] releaseProgram now calls clReleaseProgram<commit_after>#include "program.h"
#include <vector>

namespace opencl {

// /* Program Object APIs  */
// extern CL_API_ENTRY cl_program CL_API_CALL
// clCreateProgramWithSource(cl_context        /* context */,
//                           cl_uint           /* count */,
//                           const char **     /* strings */,
//                           const size_t *    /* lengths */,
//                           cl_int *          /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(CreateProgramWithSource) {
  NanScope();
  REQ_ARGS(2);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_CONTEXT));
  }
  cl_context context=Unwrap<cl_context>(args[0]);
  REQ_STR_ARG(1, str);

  cl_int ret=CL_SUCCESS;
  size_t lengths[]={(size_t) str.length()};
  const char *strings[]={*str};
  cl_program p=::clCreateProgramWithSource(context, 1, strings, lengths, &ret);
  CHECK_ERR(ret);

  NanReturnValue(Wrap(p));
}

// extern CL_API_ENTRY cl_program CL_API_CALL
// clCreateProgramWithBinary(cl_context                     /* context */,
//                           cl_uint                        /* num_devices */,
//                           const cl_device_id *           /* device_list */,
//                           const size_t *                 /* lengths */,
//                           const unsigned char **         /* binaries */,
//                           cl_int *                       /* binary_status */,
//                           cl_int *                       /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(CreateProgramWithBinary) {
  NanScope();
  REQ_ARGS(3);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_CONTEXT));
  }
  cl_context context=Unwrap<cl_context>(args[0]);

  Local<Array> devices = Local<Array>::Cast(args[1]);
  vector<cl_device_id> cl_devices;
  for (uint32_t i=0; i < devices->Length(); i++)
    cl_devices.push_back(Unwrap<cl_device_id>(devices->Get(i)));

  Local<Array> binaries = Local<Array>::Cast(args[2]);
  const size_t n = binaries->Length();
  unique_ptr<size_t[]> lengths(new size_t[n]);
  unique_ptr<const unsigned char*[]> images(new const unsigned char*[n]);

  for (uint32_t i = 0; i < n; ++i) {
    Local<Object> obj=binaries->Get(i)->ToObject();
    images[i] = (const unsigned char*) obj->GetIndexedPropertiesExternalArrayData();
    lengths[i] = obj->GetIndexedPropertiesExternalArrayDataLength();
  }

  cl_int ret=CL_SUCCESS;
  cl_program p=::clCreateProgramWithBinary(
              context, (cl_uint) cl_devices.size(),
              &cl_devices.front(),
              lengths.get(), images.get(),
              NULL, &ret);
  CHECK_ERR(ret);

  NanReturnValue(Wrap(p));
}

// extern CL_API_ENTRY cl_program CL_API_CALL
// clCreateProgramWithBuiltInKernels(cl_context            /* context */,
//                                   cl_uint               /* num_devices */,
//                                   const cl_device_id *  /* device_list */,
//                                   const char *          /* kernel_names */,
//                                   cl_int *              /* errcode_ret */) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(CreateProgramWithBuiltInKernels) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clRetainProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(RetainProgram) {
  NanScope();
  REQ_ARGS(1);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_PROGRAM));
  }

  cl_program p = Unwrap<cl_program>(args[0]);
  cl_int count=clRetainProgram(p);

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clReleaseProgram(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(ReleaseProgram) {
  NanScope();
  REQ_ARGS(1);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_PROGRAM));
  }

  cl_program p = Unwrap<cl_program>(args[0]);
  cl_int count=clReleaseProgram(p);

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clBuildProgram(cl_program           /* program */,
//                cl_uint              /* num_devices */,
//                const cl_device_id * /* device_list */,
//                const char *         /* options */,
//                void (CL_CALLBACK *  /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
//                void *               /* user_data */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(BuildProgram) {
  NanScope();
  REQ_ARGS(1);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_PROGRAM));
  }

  cl_program p = Unwrap<cl_program>(args[0]);
  std::vector<cl_device_id> devices;

  if (!args[1]->IsNull() && !args[1]->IsUndefined()){
    getValuesFromArray(Local<Array>::Cast(args[1]),devices);
  }

  unique_ptr<String::Utf8Value> options = NULL;

  if (!args[2]->IsNull() && !args[2]->IsUndefined()){
    if (!args[2]->IsString())
      NanThrowTypeError("Argument 2 must be a string");
    options = unique_ptr<String::Utf8Value>( new String::Utf8Value(args[2]->ToString()));
  }

  //REQ_STR_ARG(2,options);

  // TODO callback + userdata

  CHECK_ERR(::clBuildProgram(p,
    devices.size(), devices.size() ? &devices.front() : nullptr,
    options != NULL ? **options : nullptr,
    nullptr, nullptr)); // TODO CB

  NanReturnValue(JS_INT(CL_SUCCESS));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clCompileProgram(cl_program           /* program */,
//                  cl_uint              /* num_devices */,
//                  const cl_device_id * /* device_list */,
//                  const char *         /* options */,
//                  cl_uint              /* num_input_headers */,
//                  const cl_program *   /* input_headers */,
//                  const char **        /* header_include_names */,
//                  void (CL_CALLBACK *  /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
//                  void *               /* user_data */) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(CompileProgram) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_program CL_API_CALL
// clLinkProgram(cl_context           /* context */,
//               cl_uint              /* num_devices */,
//               const cl_device_id * /* device_list */,
//               const char *         /* options */,
//               cl_uint              /* num_input_programs */,
//               const cl_program *   /* input_programs */,
//               void (CL_CALLBACK *  /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
//               void *               /* user_data */,
//               cl_int *             /* errcode_ret */ ) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(LinkProgram) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clUnloadPlatformCompiler(cl_platform_id /* platform */) CL_API_SUFFIX__VERSION_1_2;
NAN_METHOD(UnloadPlatformCompiler) {
  NanScope();
  // TODO
  NanReturnUndefined();

}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clGetProgramInfo(cl_program         /* program */,
//                  cl_program_info    /* param_name */,
//                  size_t             /* param_value_size */,
//                  void *             /* param_value */,
//                  size_t *           /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(GetProgramInfo) {
  NanScope();
  REQ_ARGS(2);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_MEM_OBJECT));
  }
  cl_program prog=Unwrap<cl_program>(args[0]);
  cl_program_info param_name = args[1]->Uint32Value();

  switch(param_name) {
    case CL_PROGRAM_REFERENCE_COUNT:
    case CL_PROGRAM_NUM_DEVICES:
    {
      cl_uint val;
      CHECK_ERR(::clGetProgramInfo(prog,param_name,sizeof(cl_uint), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
    case CL_PROGRAM_CONTEXT:
    {
      cl_context val;
      CHECK_ERR(::clGetProgramInfo(prog,param_name,sizeof(cl_context), &val, NULL))
      // TODO NanReturnValue(JS_INT(val));
      break;
    }
    case CL_PROGRAM_DEVICES:
    {
      // TODO
      break;
    }
    case CL_PROGRAM_BINARY_SIZES:
    {
      size_t nsizes;
      CHECK_ERR(::clGetProgramInfo(prog, param_name, 0, NULL, &nsizes));

      // get CL_DEVICE_MAX_WORK_ITEM_SIZES array param
      unique_ptr<size_t[]> sizes(new size_t[nsizes]);
      CHECK_ERR(::clGetProgramInfo(prog, param_name, nsizes*sizeof(size_t), sizes.get(), NULL));
      Local<Array> arr = Array::New(nsizes);
      for(cl_uint i=0;i<nsizes;i++)
        arr->Set(i,JS_INT(sizes[i]));

      NanReturnValue(arr);
    }
    case CL_PROGRAM_BINARIES:
    {
      size_t nbins=0;
      CHECK_ERR(::clGetProgramInfo(prog, param_name, 0, NULL, &nbins));
      nbins /= sizeof(size_t);
      unique_ptr<char*[]> binaries(new char*[nbins]);
      CHECK_ERR(::clGetProgramInfo(prog, param_name, sizeof(char*)*nbins, binaries.get(), NULL));

      // TODO create an array for Uint8Array to return each binary associated to each device
      // Handle<Value> abv = Context::GetCurrent()->Global()->Get(String::NewSymbol("ArrayBuffer"));
      // Handle<Value> argv[] = { Integer::NewFromUnsigned(size) };
      // Handle<Object> arrbuf = Handle<Function>::Cast(abv)->NewInstance(1, argv);
      // void *buffer = arrbuf->GetPointerFromInternalField(0);
      // memcpy(buffer, data, size);

      NanReturnUndefined();
    }
    case CL_PROGRAM_NUM_KERNELS:
    {
      size_t val;
      CHECK_ERR(::clGetProgramInfo(prog,param_name,sizeof(size_t), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
    case CL_PROGRAM_SOURCE:
    case CL_PROGRAM_KERNEL_NAMES:
    {
      size_t nchars;
      CHECK_ERR(::clGetProgramInfo(prog, param_name, 0, NULL, &nchars));
      unique_ptr<char[]> names(new char[nchars]);
      CHECK_ERR(::clGetProgramInfo(prog, param_name, nchars*sizeof(char), names.get(), NULL));
      NanReturnValue(JS_STR(names.get()));
    }
  }
  return NanThrowError(JS_INT(CL_INVALID_VALUE));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clGetProgramBuildInfo(cl_program            /* program */,
//                       cl_device_id          /* device */,
//                       cl_program_build_info /* param_name */,
//                       size_t                /* param_value_size */,
//                       void *                /* param_value */,
//                       size_t *              /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(GetProgramBuildInfo) {
  NanScope();
  REQ_ARGS(3);

  if(!isOpenCLObj(args[0])) {
    return NanThrowError(JS_INT(CL_INVALID_MEM_OBJECT));
  }
  cl_program prog=Unwrap<cl_program>(args[0]);
  if(!isOpenCLObj(args[1])) {
    return NanThrowError(JS_INT(CL_INVALID_DEVICE));
  }
  cl_device_id device=Unwrap<cl_device_id>(args[1]);
  cl_program_build_info param_name = args[2]->Uint32Value();

  switch(param_name) {
    case CL_PROGRAM_BUILD_STATUS:
    {
      cl_build_status val;
      CHECK_ERR(::clGetProgramBuildInfo(prog, device,param_name,sizeof(cl_build_status), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
    case CL_PROGRAM_BUILD_OPTIONS:
    case CL_PROGRAM_BUILD_LOG:
    {
      size_t param_value_size_ret=0;
      CHECK_ERR(::clGetProgramBuildInfo(prog, device, param_name, 0, NULL, &param_value_size_ret));
      unique_ptr<char[]> param_value(new char[param_value_size_ret]);
      CHECK_ERR(::clGetProgramBuildInfo(prog, device, param_name, param_value_size_ret, param_value.get(), NULL));
      NanReturnValue(JS_STR(param_value.get(),(int)param_value_size_ret));
    }
    case CL_PROGRAM_BINARY_TYPE:
    {
      cl_program_binary_type val;
      CHECK_ERR(::clGetProgramBuildInfo(prog, device,param_name,sizeof(cl_program_binary_type), &val, NULL))
      NanReturnValue(JS_INT(val));
    }
  }
  return NanThrowError(JS_INT(CL_INVALID_VALUE));
}

namespace Program {
void init(Handle<Object> exports)
{
  NODE_SET_METHOD(exports, "createProgramWithSource", CreateProgramWithSource);
  NODE_SET_METHOD(exports, "createProgramWithBinary", CreateProgramWithBinary);
  NODE_SET_METHOD(exports, "createProgramWithBuiltInKernels", CreateProgramWithBuiltInKernels);
  NODE_SET_METHOD(exports, "retainProgram", RetainProgram);
  NODE_SET_METHOD(exports, "releaseProgram", ReleaseProgram);
  NODE_SET_METHOD(exports, "buildProgram", BuildProgram);
  NODE_SET_METHOD(exports, "compileProgram", CompileProgram);
  NODE_SET_METHOD(exports, "linkProgram", LinkProgram);
  NODE_SET_METHOD(exports, "unloadPlatformCompiler", UnloadPlatformCompiler);
  NODE_SET_METHOD(exports, "getProgramInfo", GetProgramInfo);
  NODE_SET_METHOD(exports, "getProgramBuildInfo", GetProgramBuildInfo);
}
} // namespace Program

} // namespace opencl
<|endoftext|>


<commit_before><commit_msg>Fixed PushWoosh kMapLastDownloaded sending<commit_after><|endoftext|>