Esempio n. 1
0
Market::Market(const ConfigManager<>& config, QObject* parent)
    : QObject(parent)
{
    qDebug() << "[Market] Ładowanie danych konfiguracyjnych...";

    qint32 sessionOnInterval = config.intAt("SESSION ON DURATION");
    qint32 sessionOffInterval = config.intAt("SESSION OFF DURATION");

    int serverPort = config.intAt("SERVER PORT");

    QString dbUserName = config["DATABASE USERNAME"];
    QString dbPassword = config["DATABASE PASSWORD"];
    QString dbName = config["DATABASE NAME"];
    QString dbHost = config["DATABASE HOST"];
    int dbPort = config.intAt("DATABASE PORT");

    _database = QSqlDatabase::addDatabase("QPSQL",
                                          QLatin1String("Rynki finansowe 1"));

    _database.setHostName(dbHost);
    _database.setDatabaseName(dbName);
    _database.setUserName(dbUserName);
    _database.setPassword(dbPassword);
    _database.setPort(dbPort);

    qDebug() << "[Market] Otwieranie nowego połączenia z bazą danych...";
    if(!_database.open())
    {
        qDebug() << "[Market] Wykryto błąd" << _database.lastError().text()
                 << "podczas otwierania nowego połączenia z bazą daynch.";
        throw DatabaseError();
    }

    _database.driver()->subscribeToNotification(BUY_TRANSACTIONS_CHANNEL);
    _database.driver()->subscribeToNotification(SELL_TRANSACTIONS_CHANNEL);
    _database.driver()->subscribeToNotification(TRANSACTION_CHANNEL);

    qDebug() << "[Market] Ustanowiono połączenie z bazą danych.";

    //qDebug() << "Database tables:\n" << _database.tables();

    qDebug() << "[Market] Uruchamianie serwera...";
    _server = new Server(this, serverPort);

    _sessionOffTimer = new QTimer(this);
    _sessionOffTimer->setInterval(sessionOffInterval * 1000);

    _sessionOnTimer = new QTimer(this);
    _sessionOnTimer->setInterval(sessionOnInterval * 1000);


    connect(_server, SIGNAL(registerUserRequestFromServer(Connection*, QString)),
            this,     SLOT(registerNewUser(Connection*, QString)) );

    connect(_server, SIGNAL(loginUserRequestFromServer(Connection*, NetworkProtocol::DTO::Types::UserIdType, QString)),
            this,     SLOT(loginUser(Connection*, NetworkProtocol::DTO::Types::UserIdType, QString)));

    connect(_database.driver(), SIGNAL(notification(const QString&, QSqlDriver::NotificationSource,
                                       const QVariant&)),
            this,               SLOT(notificationHandler(const QString&, QSqlDriver::NotificationSource,
                                     const QVariant&)));

    connect(_server, SIGNAL(sellStock(NetworkProtocol::DTO::Types::UserIdType, NetworkProtocol::DTO::Types::StockIdType,
                                      NetworkProtocol::DTO::Types::AmountType, NetworkProtocol::DTO::Types::PriceType)),
            this,     SLOT(sellStock(NetworkProtocol::DTO::Types::UserIdType, NetworkProtocol::DTO::Types::StockIdType,
                                     NetworkProtocol::DTO::Types::AmountType, NetworkProtocol::DTO::Types::PriceType)));

    connect(_server, SIGNAL(buyStock(NetworkProtocol::DTO::Types::UserIdType, NetworkProtocol::DTO::Types::StockIdType,
                                     NetworkProtocol::DTO::Types::AmountType, NetworkProtocol::DTO::Types::PriceType)),
            this,     SLOT(buyStock(NetworkProtocol::DTO::Types::UserIdType, NetworkProtocol::DTO::Types::StockIdType,
                                    NetworkProtocol::DTO::Types::AmountType, NetworkProtocol::DTO::Types::PriceType)));

    connect(_server, SIGNAL(getMyStocks(NetworkProtocol::DTO::Types::UserIdType)),
            this,     SLOT(getMyStocks(NetworkProtocol::DTO::Types::UserIdType)));

    connect(_server, SIGNAL(getMyOrders(NetworkProtocol::DTO::Types::UserIdType)),
            this, SLOT(getMyOrders(NetworkProtocol::DTO::Types::UserIdType)));

    connect(_server, SIGNAL(getStockInfo(NetworkProtocol::DTO::Types::UserIdType,
                                         NetworkProtocol::DTO::Types::StockIdType)),
            this,     SLOT(getStockInfo(NetworkProtocol::DTO::Types::UserIdType,
                                        NetworkProtocol::DTO::Types::StockIdType)));

    connect(_server, SIGNAL(cancelOrder(NetworkProtocol::DTO::Types::UserIdType,
                                        NetworkProtocol::DTO::Types::StockIdType)),
            this,     SLOT(cancelOrder(NetworkProtocol::DTO::Types::UserIdType,
                                       NetworkProtocol::DTO::Types::StockIdType)));

    connect(_sessionOnTimer, SIGNAL(timeout()),
            this,             SLOT(stopSession()));

    connect(_sessionOffTimer, SIGNAL(timeout()),
            this,              SLOT(startSession()));


    qDebug() << "[Market] Serwer jest aktywny.";
    startSession();
}