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

View file

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 2.8.12)
project(wServerUtils)
find_package(Qt5Core REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
commands.h
connector.h
)
set(SOURCES
commands.cpp
connector.cpp
)
add_library(wServerUtils ${HEADERS} ${SOURCES})
target_link_libraries(wServerUtils Qt5::Core)
target_link_libraries(wServerUtils wType)
target_link_libraries(wServerUtils wModel)
target_link_libraries(wServerUtils wSocket)
target_link_libraries(wServerUtils wDispatcher)

View file

@ -0,0 +1,85 @@
#include "commands.h"
U::Commands::Commands(const W::Address& address, QObject* parent):
M::Vocabulary(address, parent),
commands(new Map())
{
}
U::Commands::~Commands()
{
Map::iterator beg = commands->begin();
Map::iterator end = commands->end();
for (; beg != end; ++beg) {
Command* cmd = beg->second;
if (cmd->enabled) {
removeHandler(cmd->handler);
}
delete cmd->handler;
delete cmd;
}
delete commands;
}
void U::Commands::addCommand(const W::String& key, W::Handler* handler, const W::Vocabulary& args)
{
Map::const_iterator itr = commands->find(key);
if (itr != commands->end()) {
throw 1;
}
Command* cmd = new Command{key, handler, args, false};
commands->insert(std::make_pair(cmd->name, cmd));
}
void U::Commands::enableCommand(const W::String& key, bool value)
{
Map::const_iterator itr = commands->find(key);
if (itr == commands->end()) {
throw 2;
}
Command* cmd = itr->second;
if (cmd->enabled != value) {
if (value) {
enableCommand(cmd);
} else {
disableCommand(cmd);
}
}
}
void U::Commands::enableCommand(U::Commands::Command* cmd)
{
addHandler(cmd->handler);
cmd->enabled = true;
W::Vocabulary* vc = new W::Vocabulary;
vc->insert(u"address", cmd->handler->getAddress());
vc->insert(u"arguments", cmd->arguments);
insert(cmd->name, vc);
}
void U::Commands::disableCommand(U::Commands::Command* cmd)
{
removeHandler(cmd->handler);
cmd->enabled = false;
erase(cmd->name);
}
void U::Commands::removeCommand(const W::String& key)
{
Map::const_iterator itr = commands->find(key);
if (itr == commands->end()) {
throw 2;
}
Command* cmd = itr->second;
if (cmd->enabled) {
disableCommand(cmd);
}
commands->erase(itr);
delete cmd->handler;
delete cmd;
}

View file

@ -0,0 +1,43 @@
#ifndef SERVERUTILS_COMMANDS_H
#define SERVERUTILS_COMMANDS_H
#include <map>
#include <wType/address.h>
#include <wType/vocabulary.h>
#include <wType/event.h>
#include <wType/string.h>
#include <wModel/vocabulary.h>
namespace U {
class Commands : public M::Vocabulary
{
struct Command;
typedef std::map<W::String, Command*> Map;
public:
Commands(const W::Address& address, QObject* parent = 0);
~Commands();
void addCommand(const W::String& key, W::Handler* handler, const W::Vocabulary& args);
void removeCommand(const W::String& key);
void enableCommand(const W::String& key, bool value);
private:
void enableCommand(Command* cmd);
void disableCommand(Command* cmd);
Map* commands;
struct Command {
W::String name;
W::Handler* handler;
W::Vocabulary arguments;
bool enabled;
};
};
}
#endif // SERVERUTILS_COMMANDS_H

View file

@ -0,0 +1,124 @@
#include "connector.h"
U::Connector::Connector(W::Dispatcher* dp, W::Server* srv, U::Commands* cmds, QObject* parent):
QObject(parent),
dispatcher(dp),
server(srv),
commands(cmds),
nodes(),
ignoredNodes()
{
connect(server, SIGNAL(newConnection(const W::Socket&)), SLOT(onNewConnection(const W::Socket&)));
connect(server, SIGNAL(closedConnection(const W::Socket&)), SLOT(onClosedConnection(const W::Socket&)));
W::String cn = W::String(u"connect");
W::Handler* ch = W::Handler::create(commands->getAddress() + W::Address({cn}), this, &U::Connector::_h_connect);
W::Vocabulary vc;
vc.insert(u"address", W::Uint64(W::Object::string));
vc.insert(u"port", W::Uint64(W::Object::uint64));
commands->addCommand(cn, ch, vc);
commands->enableCommand(cn, true);
}
U::Connector::~Connector()
{
commands->removeCommand(W::String(u"connect"));
Map::const_iterator itr = nodes.begin();
Map::const_iterator end = nodes.begin();
W::String dc = W::String(u"disconnect");
for (; itr != end; ++itr) {
commands->removeCommand(dc + itr->first);
}
}
void U::Connector::addIgnoredNode(const W::String& name)
{
ignoredNodes.insert(name);
}
void U::Connector::sendTo(const W::String& name, const W::Event& event)
{
Map::const_iterator itr = nodes.find(name);
if (itr != nodes.end()) {
throw new NodeAccessError(name);
} else {
server->getConnection(itr->second).send(event);
}
}
void U::Connector::onNewConnection(const W::Socket& socket)
{
W::String name = socket.getRemoteName();
std::set<W::String>::const_iterator ign = ignoredNodes.find(name);
if (ign == ignoredNodes.end()) {
Map::const_iterator itr = nodes.find(name);
if (itr == nodes.end()) {
if (server->getName() == name) {
emit serviceMessage("An attempt to connect node to itself, closing connection");
server->closeConnection(socket.getId());
} else {
W::String dc = W::String(u"disconnect");
W::String dn = dc + name;
W::Handler* dh = W::Handler::create(commands->getAddress() + W::Address({dc, name}), this, &U::Connector::_h_disconnect);
commands->addCommand(dn, dh, W::Vocabulary());
commands->enableCommand(dn, true);
nodes.insert(std::make_pair(name, socket.getId()));
emit serviceMessage(QString("New connection, id: ") + socket.getId().toString().c_str());
connect(&socket, SIGNAL(message(const W::Event&)), dispatcher, SLOT(pass(const W::Event&)));
emit nodeConnected(name);
}
} else {
emit serviceMessage(QString("Node ") + QString(name.toString().c_str()) + " tried to connect, but connection with that node is already open, closing new connection");
server->closeConnection(socket.getId());
}
} else {
emit serviceMessage(QString("New connection, id: ") + socket.getId().toString().c_str());
connect(&socket, SIGNAL(message(const W::Event&)), dispatcher, SLOT(pass(const W::Event&)));
}
}
void U::Connector::onClosedConnection(const W::Socket& socket)
{
emit serviceMessage(QString("Connection closed, id: ") + socket.getId().toString().c_str());
W::String name = socket.getRemoteName();
std::set<W::String>::const_iterator ign = ignoredNodes.find(name);
if (ign == ignoredNodes.end()) {
Map::const_iterator itr = nodes.find(name);
if (itr != nodes.end()) {
emit nodeDisconnected(name);
commands->removeCommand(W::String(u"disconnect") + name);
nodes.erase(itr);
}
}
}
void U::Connector::h_connect(const W::Event& ev)
{
const W::Vocabulary& vc = static_cast<const W::Vocabulary&>(ev.getData());
const W::String& addr = static_cast<const W::String&>(vc.at(u"address"));
const W::Uint64& port = static_cast<const W::Uint64&>(vc.at(u"port"));
server->openConnection(addr, port);
}
void U::Connector::h_disconnect(const W::Event& ev)
{
const W::Address& addr = static_cast<const W::Address&>(ev.getDestination());
const W::String& name = addr.back();
Map::const_iterator itr = nodes.find(name);
server->closeConnection(itr->second);
}
const W::Socket& U::Connector::getNodeSocket(const W::String& name)
{
Map::const_iterator itr = nodes.find(name);
if (itr == nodes.end()) {
throw new NodeAccessError(name);
}
return server->getConnection(itr->second);
}

View file

@ -0,0 +1,68 @@
#ifndef CONNECTOR_H
#define CONNECTOR_H
#include <QtCore/QObject>
#include <map>
#include <set>
#include <wDispatcher/dispatcher.h>
#include <wSocket/socket.h>
#include <wSocket/server.h>
#include <wType/string.h>
#include <wType/uint64.h>
#include <wType/event.h>
#include <utils/exception.h>
#include "commands.h"
namespace U {
class Connector : public QObject
{
Q_OBJECT
typedef std::map<W::String, uint64_t> Map;
public:
Connector(W::Dispatcher* dp, W::Server* srv, Commands* cmds, QObject* parent = 0);
~Connector();
void addIgnoredNode(const W::String& name);
void sendTo(const W::String& name, const W::Event& event);
const W::Socket& getNodeSocket(const W::String& name);
signals:
void serviceMessage(const QString& msg);
void nodeConnected(const W::String& name);
void nodeDisconnected(const W::String& name);
private:
W::Dispatcher* dispatcher;
W::Server* server;
U::Commands* commands;
Map nodes;
std::set<W::String> ignoredNodes;
protected:
handler(connect);
handler(disconnect);
private slots:
void onNewConnection(const W::Socket& socket);
void onClosedConnection(const W::Socket& socket);
public:
class NodeAccessError:
public Utils::Exception
{
W::String name;
public:
NodeAccessError(const W::String& p_name):Exception(), name(p_name){}
std::string getMessage() const{return std::string("An attempt to access non existing node ") + name.toString();}
};
};
}
#endif // CONNECTOR_H