a handler to add players in Corax

This commit is contained in:
Blue 2018-08-12 01:11:33 +03:00 committed by Gitea
parent 00f26c431e
commit fe1b55bb39
2 changed files with 72 additions and 5 deletions

View File

@ -9,6 +9,7 @@ Corax* Corax::corax = 0;
Corax::Corax(QObject *parent): Corax::Corax(QObject *parent):
QObject(parent), QObject(parent),
started(false)
server(new W::Server(W::String(u"Corax"), this)), server(new W::Server(W::String(u"Corax"), this)),
logger(new W::Logger()), logger(new W::Logger()),
parentReporter(new W::ParentReporter()), parentReporter(new W::ParentReporter()),
@ -17,7 +18,8 @@ Corax::Corax(QObject *parent):
connector(0), connector(0),
dispatcher(new W::Dispatcher()), dispatcher(new W::Dispatcher()),
caches(), caches(),
parsers() parsers(),
players()
{ {
if (corax != 0) if (corax != 0)
{ {
@ -42,6 +44,7 @@ Corax::Corax(QObject *parent):
attributes->addAttribute(W::String(u"connectionsCount"), new M::String(W::String(u"0"), W::Address({u"attributes", u"connectionCount"}))); 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"Corax"), W::Address({u"attributes", u"name"}))); attributes->addAttribute(W::String(u"name"), new M::String(W::String(u"Corax"), W::Address({u"attributes", u"name"})));
attributes->addAttribute(W::String(u"version"), new M::String(W::String(u"0.0.2"), W::Address({u"attributes", u"version"}))); attributes->addAttribute(W::String(u"version"), new M::String(W::String(u"0.0.2"), W::Address({u"attributes", u"version"})));
attributes->addAttribute(W::String(u"players"), new M::String(W::String(u"0"), W::Address({u"attributes", u"players"})));
createCaches(); createCaches();
createHandlers(); createHandlers();
@ -49,20 +52,27 @@ Corax::Corax(QObject *parent):
Corax::~Corax() Corax::~Corax()
{ {
std::map<W::String, Parser*>::iterator pbeg = parsers.begin(); std::map<W::String, Parser*>::const_iterator pbeg = parsers.begin();
std::map<W::String, Parser*>::iterator pend = parsers.end(); std::map<W::String, Parser*>::const_iterator pend = parsers.end();
for (; pbeg != pend; ++pbeg) { for (; pbeg != pend; ++pbeg) {
delete pbeg->second; delete pbeg->second;
} }
std::map<W::String, ResourceCache*>::iterator beg = caches.begin(); std::map<W::String, ResourceCache*>::const_iterator beg = caches.begin();
std::map<W::String, ResourceCache*>::iterator end = caches.end(); std::map<W::String, ResourceCache*>::const_iterator end = caches.end();
for (; beg != end; ++beg) { for (; beg != end; ++beg) {
delete beg->second; delete beg->second;
} }
std::map<W::Address, M::Player*>::const_iterator plit = players.begin();
std::map<W::Address, M::Player*>::const_iterator plend = players.end();
for (; plit != plend; ++plit) {
delete plit->second;
}
delete connector; delete connector;
dispatcher->unregisterDefaultHandler(logger); dispatcher->unregisterDefaultHandler(logger);
@ -83,6 +93,9 @@ void Corax::onConnectionCountChanged(uint64_t count)
void Corax::start() void Corax::start()
{ {
if (started) {
throw 3;
}
std::map<W::String, ResourceCache*>::iterator beg = caches.begin(); std::map<W::String, ResourceCache*>::iterator beg = caches.begin();
std::map<W::String, ResourceCache*>::iterator end = caches.end(); std::map<W::String, ResourceCache*>::iterator end = caches.end();
@ -97,6 +110,13 @@ void Corax::start()
beg->second->getRegistered(connector); beg->second->getRegistered(connector);
} }
std::map<W::Address, M::Player*>::const_iterator plit = players.begin();
std::map<W::Address, M::Player*>::const_iterator plend = players.end();
for (; plit != plend; ++plit) {
plit->second->getRegistered(connector);;
}
cout << "Opening caches..." << endl; cout << "Opening caches..." << endl;
beg = caches.begin(); beg = caches.begin();
@ -106,11 +126,16 @@ void Corax::start()
commands->enableCommand(W::String(u"clearCache"), true); commands->enableCommand(W::String(u"clearCache"), true);
started = true;
cout << "Corax is ready" << endl; cout << "Corax is ready" << endl;
} }
void Corax::stop() void Corax::stop()
{ {
if (!started) {
throw 3;
}
std::map<W::String, ResourceCache*>::iterator beg = caches.begin(); std::map<W::String, ResourceCache*>::iterator beg = caches.begin();
std::map<W::String, ResourceCache*>::iterator end = caches.end(); std::map<W::String, ResourceCache*>::iterator end = caches.end();
@ -122,7 +147,17 @@ void Corax::stop()
beg->second->getUnregistered(); beg->second->getUnregistered();
} }
std::map<W::Address, M::Player*>::const_iterator plit = players.begin();
std::map<W::Address, M::Player*>::const_iterator plend = players.end();
for (; plit != plend; ++plit) {
plit->second->getUnregistered();
}
server->stop(); server->stop();
started = false;
cout << "Corax is stopped" << endl;
} }
void Corax::onModelServiceMessage(const QString& msg) void Corax::onModelServiceMessage(const QString& msg)
@ -202,6 +237,9 @@ void Corax::createHandlers()
W::Vocabulary parseArgs; W::Vocabulary parseArgs;
parseArgs.insert(u"path", W::Uint64(W::Object::string)); parseArgs.insert(u"path", W::Uint64(W::Object::string));
commands->addCommand(W::String(u"parseDirectory"), parseDirectory, parseArgs); commands->addCommand(W::String(u"parseDirectory"), parseDirectory, parseArgs);
W::Handler* givePlayer = W::Handler::create(W::Address({u"management", u"givePlayer"}), this, &Corax::_h_givePlayer);
commands->addCommand(W::String(u"givePlayer"), givePlayer, W::Vocabulary());
} }
void Corax::onParserDone(const W::String& path) void Corax::onParserDone(const W::String& path)
@ -234,3 +272,28 @@ void Corax::onNodeDisconnected(const W::String& name)
commands->enableCommand(W::String(u"parseDirectory"), false); commands->enableCommand(W::String(u"parseDirectory"), false);
} }
} }
void Corax::h_givePlayer(const W::Event& ev)
{
W::String num(std::to_string(players.size()));
W::Address addr{u"players", num};
M::Player* pl = new M::Player(addr);
connect(pl, SIGNAL(serviceMessage(const QString&)), SLOT(onModelServiceMessage(const QString&)));
players.insert(std::make_pair(addr, pl));
attributes->setAttribute(W::String(u"players"), W::String(std::to_string(players.size())));
pl->getRegistered(connector); //it's a handler, so I assume corax is started here;
W::Vocabulary* vc = new W::Vocabulary();
vc->insert(u"address", addr);
const W::Vocabulary& svc = static_cast<const W::Vocabulary&>(ev.getData());
const W::Address& source = static_cast<const W::Address&>(svc.at(u"source"));
uint64_t id = ev.getSenderId();
vc->insert(u"source", W::Address{u"management"}); //TODO think about it, may be Corax should be a model?
W::Event res(source + W::Address{u"getPlayer"}, vc);
res.setSenderId(id);
connector->getConnection(id)->send(ev);
}

View File

@ -30,6 +30,7 @@
#include <wDatabase/resourcecache.h> #include <wDatabase/resourcecache.h>
#include "tools/parser.h" #include "tools/parser.h"
#include "models/player.h"
class Corax: public QObject class Corax: public QObject
{ {
@ -42,6 +43,7 @@ public:
static Corax* corax; static Corax* corax;
private: private:
bool started;
W::Server *server; W::Server *server;
W::Logger *logger; W::Logger *logger;
W::ParentReporter* parentReporter; W::ParentReporter* parentReporter;
@ -53,9 +55,11 @@ private:
std::map<W::String, ResourceCache*> caches; std::map<W::String, ResourceCache*> caches;
std::map<W::String, Parser*> parsers; std::map<W::String, Parser*> parsers;
std::map<W::Address, M::Player*> players;
handler(clearCache); handler(clearCache);
handler(parseDirectory); handler(parseDirectory);
handler(givePlayer);
public slots: public slots:
void start(); void start();