Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	QCoreApplication app(argc, argv);
	app.setOrganizationName("Cockatrice");
	app.setApplicationName("Servatrice");
	
	QStringList args = app.arguments();
	bool testRandom = args.contains("--test-random");
	bool testHashFunction = args.contains("--test-hash");
	bool logToConsole = args.contains("--log-to-console");
	
	qRegisterMetaType<QList<int> >("QList<int>");
	
	QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
	
	QSettings *settings = new QSettings("servatrice.ini", QSettings::IniFormat);
	
	loggerThread = new QThread;
	logger = new ServerLogger(logToConsole);
	logger->moveToThread(loggerThread);
	
	loggerThread->start();
	QMetaObject::invokeMethod(logger, "startLog", Qt::BlockingQueuedConnection, Q_ARG(QString, settings->value("server/logfile").toString()));
	
	if (logToConsole)
		qInstallMsgHandler(myMessageOutput);
	else
		qInstallMsgHandler(myMessageOutput2);
#ifdef Q_OS_UNIX	
	struct sigaction hup;
	hup.sa_handler = ServerLogger::hupSignalHandler;
	sigemptyset(&hup.sa_mask);
	hup.sa_flags = 0;
	hup.sa_flags |= SA_RESTART;
	sigaction(SIGHUP, &hup, 0);
	
	struct sigaction segv;
	segv.sa_handler = sigSegvHandler;
	segv.sa_flags = SA_RESETHAND;
	sigemptyset(&segv.sa_mask);
	sigaction(SIGSEGV, &segv, 0);
	sigaction(SIGABRT, &segv, 0);
#endif
	rng = new RNG_SFMT;
	
	std::cerr << "Servatrice " << VERSION_STRING << " starting." << std::endl;
	std::cerr << "-------------------------" << std::endl;
	
	if (testRandom)
		testRNG();
	if (testHashFunction)
		testHash();
	
	Servatrice *server = new Servatrice(settings);
	QObject::connect(server, SIGNAL(destroyed()), &app, SLOT(quit()), Qt::QueuedConnection);
	int retval = 0;
	if (server->initServer()) {
		std::cerr << "-------------------------" << std::endl;
		std::cerr << "Server initialized." << std::endl;
		
		qInstallMsgHandler(myMessageOutput);
		retval = app.exec();
		
		std::cerr << "Server quit." << std::endl;
		std::cerr << "-------------------------" << std::endl;
	}
	
	delete rng;
	delete settings;
	
	logger->deleteLater();
	loggerThread->wait();
	delete loggerThread;

	return retval;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    app.setOrganizationName("Cockatrice");
    app.setApplicationName("Servatrice");
    app.setApplicationVersion(VERSION_STRING);

    QCommandLineParser parser;
    parser.addHelpOption();
    parser.addVersionOption();

    QCommandLineOption testRandomOpt("test-random", "Test PRNG (chi^2)");
    parser.addOption(testRandomOpt);

    QCommandLineOption testHashFunctionOpt("test-hash", "Test password hash function");
    parser.addOption(testHashFunctionOpt);

    QCommandLineOption logToConsoleOpt("log-to-console", "Write server logs to console");
    parser.addOption(logToConsoleOpt);

    QCommandLineOption configPathOpt("config", "Read server configuration from <file>", "file", "");
    parser.addOption(configPathOpt);

    parser.process(app);

    bool testRandom = parser.isSet(testRandomOpt);
    bool testHashFunction = parser.isSet(testHashFunctionOpt);
    bool logToConsole = parser.isSet(logToConsoleOpt);
    QString configPath = parser.value(configPathOpt);

    qRegisterMetaType<QList<int>>("QList<int>");

    configPath = SettingsCache::guessConfigurationPath(configPath);
    qWarning() << "Using configuration file: " << configPath;
    settingsCache = new SettingsCache(configPath);

    loggerThread = new QThread;
    loggerThread->setObjectName("logger");
    logger = new ServerLogger(logToConsole);
    logger->moveToThread(loggerThread);

    loggerThread->start();
    QMetaObject::invokeMethod(logger, "startLog", Qt::BlockingQueuedConnection,
                              Q_ARG(QString, settingsCache->value("server/logfile", QString("server.log")).toString()));

    if (logToConsole)
        qInstallMessageHandler(myMessageOutput);
    else
        qInstallMessageHandler(myMessageOutput2);

    signalhandler = new SignalHandler();

    rng = new RNG_SFMT;

    std::cerr << "Servatrice " << VERSION_STRING << " starting." << std::endl;
    std::cerr << "-------------------------" << std::endl;

    PasswordHasher::initialize();

    if (testRandom)
        testRNG();
    if (testHashFunction)
        testHash();

    smtpClient = new SmtpClient();

    Servatrice *server = new Servatrice();
    QObject::connect(server, SIGNAL(destroyed()), &app, SLOT(quit()), Qt::QueuedConnection);
    int retval = 0;
    if (server->initServer()) {
        std::cerr << "-------------------------" << std::endl;
        std::cerr << "Server initialized." << std::endl;

        qInstallMessageHandler(myMessageOutput);

        retval = app.exec();

        std::cerr << "Server quit." << std::endl;
        std::cerr << "-------------------------" << std::endl;
    }

    delete smtpClient;
    delete rng;
    delete signalhandler;
    delete settingsCache;

    logger->deleteLater();
    loggerThread->wait();
    delete loggerThread;

    // Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return retval;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
	QCoreApplication app(argc, argv);
	app.setOrganizationName("Cockatrice");
	app.setApplicationName("Servatrice");
	
	QStringList args = app.arguments();
	bool testRandom = args.contains("--test-random");
	bool testHashFunction = args.contains("--test-hash");
	bool logToConsole = args.contains("--log-to-console");
	QString configPath;
	int hasConfigPath=args.indexOf("--config");
	if(hasConfigPath > -1 && args.count() > hasConfigPath + 1)
		configPath = args.at(hasConfigPath + 1);
	
	qRegisterMetaType<QList<int> >("QList<int>");

#if QT_VERSION < 0x050000
	// gone in Qt5, all source files _MUST_ be utf8-encoded
	QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#endif

	configPath = SettingsCache::guessConfigurationPath(configPath);
	qWarning() << "Using configuration file: " << configPath;
	settingsCache = new SettingsCache(configPath);
	
	loggerThread = new QThread;
	loggerThread->setObjectName("logger");
	logger = new ServerLogger(logToConsole);
	logger->moveToThread(loggerThread);
	
	loggerThread->start();
	QMetaObject::invokeMethod(logger, "startLog", Qt::BlockingQueuedConnection, Q_ARG(QString, settingsCache->value("server/logfile", QString("server.log")).toString()));

#if QT_VERSION < 0x050000
	if (logToConsole)
		qInstallMsgHandler(myMessageOutput);
	else
		qInstallMsgHandler(myMessageOutput2);
#else
	if (logToConsole)
		qInstallMessageHandler(myMessageOutput);
	else
		qInstallMessageHandler(myMessageOutput2);
#endif

#ifdef Q_OS_UNIX	
	struct sigaction hup;
	hup.sa_handler = ServerLogger::hupSignalHandler;
	sigemptyset(&hup.sa_mask);
	hup.sa_flags = 0;
	hup.sa_flags |= SA_RESTART;
	sigaction(SIGHUP, &hup, 0);
	
	struct sigaction segv;
	segv.sa_handler = sigSegvHandler;
	segv.sa_flags = SA_RESETHAND;
	sigemptyset(&segv.sa_mask);
	sigaction(SIGSEGV, &segv, 0);
	sigaction(SIGABRT, &segv, 0);
	
	signal(SIGPIPE, SIG_IGN);
#endif
	rng = new RNG_SFMT;
	
	std::cerr << "Servatrice " << VERSION_STRING << " starting." << std::endl;
	std::cerr << "-------------------------" << std::endl;
	
	PasswordHasher::initialize();
	
	if (testRandom)
		testRNG();
	if (testHashFunction)
		testHash();
	
	Servatrice *server = new Servatrice();
	QObject::connect(server, SIGNAL(destroyed()), &app, SLOT(quit()), Qt::QueuedConnection);
	int retval = 0;
	if (server->initServer()) {
		std::cerr << "-------------------------" << std::endl;
		std::cerr << "Server initialized." << std::endl;

#if QT_VERSION < 0x050000		
		qInstallMsgHandler(myMessageOutput);
#else
		qInstallMessageHandler(myMessageOutput);
#endif
		retval = app.exec();
		
		std::cerr << "Server quit." << std::endl;
		std::cerr << "-------------------------" << std::endl;
	}
	
	delete rng;
	delete settingsCache;
	
	logger->deleteLater();
	loggerThread->wait();
	delete loggerThread;

	// Delete all global objects allocated by libprotobuf.
	google::protobuf::ShutdownProtobufLibrary();

	return retval;
}