initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

31
perturabo/CMakeLists.txt Normal file
View file

@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 2.8.12)
project(perturabo)
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
perturabo.h
)
set(SOURCES
main.cpp
perturabo.cpp
)
add_executable(perturabo ${HEADERS} ${SOURCES})
target_link_libraries(perturabo Qt5::Core)
target_link_libraries(perturabo Qt5::Network)
target_link_libraries(perturabo wSocket)
target_link_libraries(perturabo wDispatcher)
target_link_libraries(perturabo utils)
target_link_libraries(perturabo wModel)
target_link_libraries(perturabo wServerUtils)
target_link_libraries(perturabo wDatabase)
install(TARGETS perturabo RUNTIME DESTINATION bin)

18
perturabo/main.cpp Normal file
View file

@ -0,0 +1,18 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QTimer>
#include <utils/signalcatcher.h>
#include "perturabo.h"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
W::SignalCatcher sc(&app);
Perturabo* perturabo = new Perturabo(&app);
QTimer::singleShot(0, perturabo, SLOT(start()));
QObject::connect(&app, SIGNAL(aboutToQuit()), perturabo, SLOT(stop()));
return app.exec();
}

189
perturabo/perturabo.cpp Normal file
View file

@ -0,0 +1,189 @@
#include "perturabo.h"
#include <iostream>
using std::cout;
using std::endl;
Perturabo* Perturabo::perturabo = 0;
Perturabo::Perturabo(QObject *parent):
QObject(parent),
server(new W::Server(W::String(u"Perturabo"), this)),
logger(new W::Logger()),
parentReporter(new W::ParentReporter()),
attributes(new M::Attributes(W::Address({u"attributes"}))),
commands(new U::Commands(W::Address{u"management"})),
connector(0),
databases(),
dispatcher(new W::Dispatcher())
{
if (perturabo != 0)
{
throw SingletonError();
}
Perturabo::perturabo = this;
connector = new U::Connector(dispatcher, server, commands);
connector->addIgnoredNode(W::String(u"Lorgar"));
connector->addIgnoredNode(W::String(u"Roboute"));
connect(attributes, SIGNAL(serviceMessage(const QString&)), SLOT(onModelServiceMessage(const QString&)));
connect(commands, SIGNAL(serviceMessage(const QString&)), SLOT(onModelServiceMessage(const QString&)));
connect(connector, SIGNAL(serviceMessage(const QString&)), SLOT(onModelServiceMessage(const QString&)));
connect(server, SIGNAL(connectionCountChange(uint64_t)), SLOT(onConnectionCountChanged(uint64_t)));
dispatcher->registerDefaultHandler(parentReporter);
dispatcher->registerDefaultHandler(logger);
attributes->addAttribute(W::String(u"connectionsCount"), new M::String(W::String(u"0"), W::Address({u"attributes", u"connectionCount"})));
attributes->addAttribute(W::String(u"name"), new M::String(W::String(u"Perturabo"), W::Address({u"attributes", u"name"})));
attributes->addAttribute(W::String(u"version"), new M::String(W::String(u"0.0.3"), W::Address({u"attributes", u"version"})));
createDatabases();
W::Handler* clearDatabase = W::Handler::create(W::Address({u"management", u"clearDatabase"}), this, &Perturabo::_h_clearDatabase);
W::Vocabulary clearArgs;
clearArgs.insert(u"name", W::Uint64(W::Object::string));
commands->addCommand(W::String(u"clearDatabase"), clearDatabase, clearArgs);
}
Perturabo::~Perturabo()
{
std::map<W::String, Database*>::iterator beg = databases.begin();
std::map<W::String, Database*>::iterator end = databases.end();
for (; beg != end; ++beg) {
delete beg->second;
}
delete connector;
dispatcher->unregisterDefaultHandler(logger);
dispatcher->unregisterDefaultHandler(parentReporter);
delete commands;
delete attributes;
delete parentReporter;
delete logger;
delete dispatcher;
Perturabo::perturabo = 0;
}
void Perturabo::onConnectionCountChanged(uint64_t count)
{
attributes->setAttribute(W::String(u"connectionsCount"), new W::String(std::to_string(count)));
}
void Perturabo::start()
{
std::map<W::String, Database*>::iterator beg = databases.begin();
std::map<W::String, Database*>::iterator end = databases.end();
cout << "Starting perturabo..." << endl;
server->listen(8082);
cout << "Registering models..." << endl;
attributes->registerModel(dispatcher, server);
commands->registerModel(dispatcher, server);
for (; beg != end; ++beg) {
beg->second->registerModel(dispatcher, server);
}
cout << "Opening and indexing databases..." << endl;
beg = databases.begin();
for (; beg != end; ++beg) {
beg->second->open();
}
commands->enableCommand(W::String(u"clearDatabase"), true);
cout << "Perturabo is ready" << endl;
}
void Perturabo::stop()
{
std::map<W::String, Database*>::iterator beg = databases.begin();
std::map<W::String, Database*>::iterator end = databases.end();
cout << "Stopping perturabo..." << endl;
commands->enableCommand(W::String(u"clearDatabase"), false);
for (; beg != end; ++beg) {
beg->second->unregisterModel();
}
commands->unregisterModel();
attributes->unregisterModel();
server->stop();
}
void Perturabo::onModelServiceMessage(const QString& msg)
{
cout << msg.toStdString() << endl;
}
void Perturabo::h_clearDatabase(const W::Event& ev)
{
const W::Vocabulary& vc = static_cast<const W::Vocabulary&>(ev.getData());
const W::String& name = static_cast<const W::String&>(vc.at(u"name"));
cout << "received command to clear database " << name.toString() << endl;
std::map<W::String, Database*>::iterator itr = databases.find(name);
if (itr == databases.end()) {
cout << "database " << name.toString() << " doesn't exist" << endl;
} else {
itr->second->clear();
}
}
void Perturabo::addDatabase(Database* db)
{
connect(db, SIGNAL(serviceMessage(const QString&)), SLOT(onModelServiceMessage(const QString&)));
parentReporter->registerParent(db->getAddress(), db->subscribeMember);
databases.insert(std::make_pair(db->name, db));
}
void Perturabo::createDatabases()
{
Database* artists = new Database(W::String(u"artists"));
Database* albums = new Database(W::String(u"albums"));
Database* songs = new Database(W::String(u"songs"));
artists->addIndex(W::String(u"name"), W::Object::string);
albums->addIndex(W::String(u"name"), W::Object::string);
albums->addIndex(W::String(u"artist"), W::Object::uint64);
songs->addIndex(W::String(u"name"), W::Object::string);
songs->addIndex(W::String(u"artist"), W::Object::uint64);
songs->addIndex(W::String(u"album"), W::Object::uint64);
attributes->addAttribute(W::String(artists->name), new M::String(W::String(u"0"), W::Address({u"attributes", artists->name})));
attributes->addAttribute(W::String(albums->name), new M::String(W::String(u"0"), W::Address({u"attributes", albums->name})));
attributes->addAttribute(W::String(songs->name), new M::String(W::String(u"0"), W::Address({u"attributes", songs->name})));
connect(artists, SIGNAL(countChange(uint64_t)), SLOT(onDatabaseCountChange(uint64_t)));
connect(albums, SIGNAL(countChange(uint64_t)), SLOT(onDatabaseCountChange(uint64_t)));
connect(songs, SIGNAL(countChange(uint64_t)), SLOT(onDatabaseCountChange(uint64_t)));
addDatabase(artists);
addDatabase(albums);
addDatabase(songs);
}
void Perturabo::onDatabaseCountChange(uint64_t count)
{
Database* db = static_cast<Database*>(sender());
attributes->setAttribute(db->name, W::String(std::to_string(count)));
}

84
perturabo/perturabo.h Normal file
View file

@ -0,0 +1,84 @@
#ifndef PERTURABO_H
#define PERTURABO_H
#include <map>
#include <QtCore/QObject>
#include <QtCore/QString>
#include <wSocket/socket.h>
#include <wSocket/server.h>
#include <wType/string.h>
#include <wType/uint64.h>
#include <wType/event.h>
#include <wType/address.h>
#include <wType/vocabulary.h>
#include <wDispatcher/dispatcher.h>
#include <wDispatcher/logger.h>
#include <wDispatcher/parentreporter.h>
#include <wModel/modelstring.h>
#include <wModel/attributes.h>
#include <utils/exception.h>
#include <tools/file.h>
#include <wServerUtils/commands.h>
#include <wServerUtils/connector.h>
#include <wDatabase/database.h>
class Perturabo: public QObject
{
Q_OBJECT
public:
Perturabo(QObject *parent = 0);
~Perturabo();
static Perturabo* perturabo;
private:
W::Server *server;
W::Logger *logger;
W::ParentReporter* parentReporter;
M::Attributes* attributes;
U::Commands* commands;
U::Connector* connector;
std::map<W::String, Database*> databases;
handler(clearDatabase);
// handler(parseDirectory);
public:
W::Dispatcher *dispatcher;
public slots:
void start();
void stop();
private slots:
void onModelServiceMessage(const QString& msg);
void onConnectionCountChanged(uint64_t count);
void onDatabaseCountChange(uint64_t count);
private:
void createDatabases();
void addDatabase(Database* db);
private:
class SingletonError:
public Utils::Exception
{
public:
SingletonError():Exception(){}
std::string getMessage() const{return "Perturabo is a singleton, there was an attempt to construct it at the second time";}
};
};
#endif // PERTURABO_H