Compare commits

...
7 Commits
Author SHA1 Message Date
Brad Arant 275c7298ee Adding LineParser layer. 2025-06-20 01:22:06 +00:00
Brad Arant b5f4947f7c minor format change, 2025-06-05 16:54:36 +00:00
Brad Arant 1d16d0c17f merge conflicts resolution with develop->master. 2025-06-05 16:07:50 +00:00
Brad Arant 4230734ee6 Remove intellisense work files. 2025-06-05 16:03:53 +00:00
Brad Arant 57f47fdecb Convert Subscription and SubscriptionManager to MString. 2023-10-26 18:28:51 -07:00
Matt Arant 3a75e920d9 Servercore Changes 2023-10-06 18:31:22 +00:00
Brad 9a39b6224c *NEXT/*DIE 2023-06-19 23:22:46 +00:00
20 changed files with 274 additions and 251 deletions

No files matched your search

+7 -4
View File
@@ -1,14 +1,17 @@
#include "Command.h" #include "Command.h"
#include "Log.h"
#include "CommandList.h" #include "CommandList.h"
#include "Log.h"
namespace core { namespace core
{
int Command::processCommand(coreutils::ZString &request, TCPSession &session) { int Command::processCommand(coreutils::ZString &request, TCPSession &session)
{
return 0; return 0;
} }
void Command::output(std::stringstream &out) { void Command::output(std::stringstream &out)
{
out << "Write your own command description here for the help system." << std::endl; out << "Write your own command description here for the help system." << std::endl;
} }
+5 -5
View File
@@ -1,12 +1,13 @@
#ifndef __Command_h__ #ifndef __Command_h__
#define __Command_h__ #define __Command_h__
#include "includes"
#include "Object.h" #include "Object.h"
#include "TCPSession.h" #include "TCPSession.h"
#include "ZString.h" #include "ZString.h"
#include "includes"
namespace core { namespace core
{
class CommandList; class CommandList;
@@ -19,10 +20,10 @@ namespace core {
/// a list of functions that can be invoked as a result of processing a request. /// a list of functions that can be invoked as a result of processing a request.
/// ///
class Command { class Command
{
public: public:
/// ///
/// This method is used to implement the functionality of the requested command. /// This method is used to implement the functionality of the requested command.
/// This pure virtual function must be implemented in your inheriting object. /// This pure virtual function must be implemented in your inheriting object.
@@ -43,7 +44,6 @@ namespace core {
/// ///
virtual void output(std::stringstream &out); virtual void output(std::stringstream &out);
}; };
} }
+7 -18
View File
@@ -48,17 +48,6 @@ namespace core
return descriptor; return descriptor;
} }
void Socket::setBufferSize(int length)
{
this->length = length;
buffer = (char *)realloc(buffer, length);
}
int Socket::getBufferSize()
{
return length;
}
void Socket::onRegister() {} void Socket::onRegister() {}
void Socket::onRegistered() {} void Socket::onRegistered() {}
@@ -79,9 +68,8 @@ namespace core
} }
if(event.events & EPOLLIN) { if(event.events & EPOLLIN) {
// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN"; // coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN";
coreutils::ZString zbuffer(buffer, length);
lock.lock(); lock.lock();
receiveData(zbuffer); receiveDataNotification();
if(!shutDown) { if(!shutDown) {
inHandler = false; inHandler = false;
lock.unlock(); lock.unlock();
@@ -99,17 +87,18 @@ namespace core
return !shutDown; return !shutDown;
} }
void Socket::onDataReceived(std::string data) void Socket::onDataReceived(std::string data) {
{
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1); throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
} }
void Socket::onDataReceived(coreutils::ZString &data) void Socket::onDataReceived(coreutils::ZString &data) {
{
onDataReceived(std::string(data.getData(), data.getLength())); onDataReceived(std::string(data.getData(), data.getLength()));
} }
void Socket::receiveData(coreutils::ZString &buffer) { void Socket::receiveDataNotification() {}
coreutils::ZString blank(""); coreutils::ZString blank("");
if(buffer.getLength() <= 0) if(buffer.getLength() <= 0)
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1); throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
+2 -26
View File
@@ -124,10 +124,6 @@ namespace core {
EPoll &ePoll; // The EPoll control object. EPoll &ePoll; // The EPoll control object.
void setBufferSize(int length);
int getBufferSize();
/// ///
/// The onConnected method is called when the socket is ready to communicate. /// The onConnected method is called when the socket is ready to communicate.
/// Writing to the socket can begin on this call to initiate a contact with the /// Writing to the socket can begin on this call to initiate a contact with the
@@ -143,27 +139,10 @@ namespace core {
// virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate. // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
/// ///
/// The onDataReceived method is called when the socket has received an event from /// This method is called when data is ready to be read from the socket.
/// epoll and there is data ready to be read from the socket. The default handler
/// will pull the data and put it into the streambuf for the socket. EPOLLIN
///
/// @param data the data that has been received from the socket.
/// ///
virtual void onDataReceived(std::string data); ///< Called when data is received from the socket. virtual void readDataNotification();
///
///
///
virtual void onDataReceived(coreutils::ZString &data);
///
/// receiveData will read the data from the socket and place it in the socket buffer.
/// TLS layer overrides this to be able to read from SSL.
///
virtual void receiveData(coreutils::ZString &buffer);
private: private:
@@ -187,9 +166,6 @@ namespace core {
// int_type pbackfail(int_type ch); // int_type pbackfail(int_type ch);
// streamsize showmanyc(); // streamsize showmanyc();
char *buffer; // This is a pointer to the managed buffer space.
int length; // This is the length of the buffer.
std::queue<std::string> fifo; std::queue<std::string> fifo;
void resetSocket(); void resetSocket();
+22 -11
View File
@@ -1,23 +1,29 @@
#include "Subscription.h" #include "Subscription.h"
#include "Log.h" #include "Log.h"
#include "MString.h"
#include "TCPSession.h" #include "TCPSession.h"
#include "TCPSocket.h"
#include <algorithm> #include <algorithm>
namespace core { namespace core {
Subscription::Subscription(std::string id, std::string mode) Subscription::Subscription(coreutils::MString id, coreutils::MString mode)
: id(id), mode(mode), owner(NULL), handler(NULL) {} : id(id), mode(mode), owner(""), handler(NULL) {}
Subscription::Subscription(std::string id, TCPSession &session, std::string mode) Subscription::Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString mode)
: id(id), mode(mode), owner(&session), handler(NULL) {} : id(id), mode(mode), owner(owner), handler(NULL) {}
Subscription::Subscription(std::string id, TCPSession &session, std::string mode, SubscriptionHandler *handler) Subscription::Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString ownership, coreutils::MString mode, SubscriptionHandler *handler, coreutils::MString will, coreutils::MString timer)
: id(id), mode(mode), owner(&session), handler(handler) { : id(id), ownership(ownership), mode(mode), owner(owner), handler(handler), will(will), timer(timer) {
// coreutils::Log(coreutils::LOG_DEBUG_3) << "Subscription '" << id << "' with handler '" << handler->name << "'"; coreutils::Log(coreutils::LOG_DEBUG_3) << owner;
coreutils::Log(coreutils::LOG_DEBUG_3) << will;
subscriptionOwner(owner, will);
} }
Subscription::~Subscription() { Subscription::~Subscription() {
std::stringstream out; std::stringstream out;
out << "cancel:" << id << std::endl; out << "cancel:" << id << std::endl;
for (auto subscriber : subscribers) { for (auto subscriber : subscribers) {
subscriber->write(out.str()); subscriber->write(out.str());
@@ -29,21 +35,27 @@ namespace core {
handler->onSubscribe(session, this); handler->onSubscribe(session, this);
else else
onSubscribe(session); onSubscribe(session);
subscribers.push_back(&session); subscribers.push_back(&session);
return 1; return 1;
} }
int Subscription::unsubscribe(TCPSession &session) { int Subscription::unsubscribe(TCPSession &session) {
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) { for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
if (*subscriber == &session) { if (*subscriber == &session) {
subscribers.erase(subscriber++); subscribers.erase(subscriber++);
return 1; return 1;
} }
}
return 0; return 0;
} }
bool Subscription::subscriptionOwner(coreutils::MString alias, coreutils::MString ownerSelection) {
subscriptionOwners = alias;
selectionOfOwnerForSubscription = ownerSelection;
coreutils::Log(coreutils::LOG_DEBUG_1) << "Owner of Subscription: " << subscriptionOwners;
coreutils::Log(coreutils::LOG_DEBUG_1) << "Will of Subscription: " << selectionOfOwnerForSubscription;
return true;
}
int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) { int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) {
if (handler) if (handler)
handler->process(request, out, session, this); handler->process(request, out, session, this);
@@ -77,5 +89,4 @@ namespace core {
data.str(""); data.str("");
} }
} }
+16 -11
View File
@@ -1,23 +1,23 @@
#ifndef __Subscription_h__ #ifndef __Subscription_h__
#define __Subscription_h__ #define __Subscription_h__
#include "SubscriptionHandler.h" #include "MString.h"
#include "SessionFilter.h" #include "SessionFilter.h"
#include "SubscriptionHandler.h"
#include "ZString.h" #include "ZString.h"
#include <string> #include <string>
#include <vector> #include <vector>
namespace core namespace core {
{
class TCPSession; class TCPSession;
class Subscription { class Subscription {
public: public:
Subscription(std::string id, std::string mode = "*AUTHOR"); Subscription(coreutils::MString id, coreutils::MString mode = "*AUTHOR");
Subscription(std::string id, TCPSession &session, std::string mode); Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString mode);
Subscription(std::string id, TCPSession &session, std::string mode, SubscriptionHandler *handler); Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString ownership, coreutils::MString mode, SubscriptionHandler *handler, coreutils::MString selection, coreutils::MString time);
virtual ~Subscription(); virtual ~Subscription();
int subscribe(TCPSession &session); int subscribe(TCPSession &session);
@@ -28,7 +28,7 @@ namespace core
virtual int onSubscribe(TCPSession &session); virtual int onSubscribe(TCPSession &session);
int event(std::stringstream &out); int event(std::stringstream &out);
bool subscriptionOwner(coreutils::MString owner, coreutils::MString selection);
bool ifSubscriber(TCPSession &session); bool ifSubscriber(TCPSession &session);
bool subInvite(TCPSession &session); bool subInvite(TCPSession &session);
@@ -36,13 +36,18 @@ namespace core
void sendToAll(std::stringstream &data, TCPSession &sender); void sendToAll(std::stringstream &data, TCPSession &sender);
void sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter); void sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter);
std::string id; coreutils::MString id;
std::string mode; coreutils::MString mode;
TCPSession *owner; coreutils::MString owner;
coreutils::MString subscriptionOwners;
coreutils::MString selectionOfOwnerForSubscription;
coreutils::MString will;
coreutils::MString ownership;
coreutils::MString timer;
SubscriptionHandler *handler; SubscriptionHandler *handler;
std::vector<TCPSession *> subscribers; std::vector<TCPSession *> subscribers;
}; };
} }
+57 -31
View File
@@ -1,9 +1,9 @@
#include "SubscriptionManager.h" #include "SubscriptionManager.h"
#include "Log.h" #include "Log.h"
#include "Subscription.h" #include "Subscription.h"
#include "SubscriptionHandlerFactory.h"
#include "TCPServer.h" #include "TCPServer.h"
#include <algorithm> #include <algorithm>
#include "SubscriptionHandlerFactory.h"
namespace core { namespace core {
@@ -14,29 +14,51 @@ namespace core {
return 1; return 1;
} }
bool SubscriptionManager::onClearSubscription(coreutils::MString temp, coreutils::MString key) {
temp = "";
key = "";
return true;
}
int SubscriptionManager::removeSessionSubscriptions(TCPSession &session) { int SubscriptionManager::removeSessionSubscriptions(TCPSession &session) {
int countSubscribed = 0; int countSubscribed = 0;
int countPublished = 0; int countPublished = 0;
int count = 0;
lock.lock(); lock.lock();
std::string temp = ""; coreutils::MString temp = "";
for (auto [key, subscription] : subscriptions) for (auto [key, subscription] : subscriptions) {
{ if (temp != "") {
if (temp != "")
{
subscriptions.erase(temp); subscriptions.erase(temp);
temp = ""; temp = "";
} }
countSubscribed += subscription->unsubscribe(session); countSubscribed += subscription->unsubscribe(session);
if (subscription->owner == &session) if (subscription->subscriptionOwners == session.alias) {
{ if (subscription->selectionOfOwnerForSubscription == "*DIE") {
temp = key; onClearSubscription(temp, key);
delete subscription; }
else if (subscription->will == "*NEXT") {
if (subscription->subscribers.size() < 1) {
coreutils::Log(coreutils::LOG_DEBUG_1) << "There is not enough people to move to the next";
onClearSubscription(temp, key);
}
else {
for (auto subscribed = subscription->subscribers.begin(); subscribed < subscription->subscribers.end(); subscribed++) {
if ((*subscribed)->alias == session.alias) {
subscription->subscribers.erase(subscribed++);
}
subscription->subscriptionOwner(session.alias, "*NEXT");
}
}
}
else {
coreutils::Log(coreutils::LOG_DEBUG_1) << "Subscription doesn't exist";
}
++countPublished; ++countPublished;
} }
} }
if (temp != "") if (temp != "") {
{
subscriptions.erase(temp); subscriptions.erase(temp);
temp = ""; temp = "";
} }
@@ -47,29 +69,34 @@ namespace core {
} }
int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session) { int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session) {
if (request[0].equals("publish")) { if (request[0] == "publish") {
SubscriptionHandler *handler = NULL; SubscriptionHandler *handler = NULL;
if (request.getList().size() > 3) { if (request.getList().size() > 3) {
factory->getSubscriptionHandler(request[3].str()); handler = factory->getSubscriptionHandler(request[4].str());
} }
newSubscription = new Subscription(request[1].str(), session, request[2].str(), handler); if (request[2] == "*OWNER") {
newSubscription = new Subscription(request[1].str(), session.alias, request[2].str(), request[3].str(), handler, request[5].str(), request[6].str());
subscriptions.insert(std::make_pair(request[1].str(), newSubscription)); subscriptions.insert(std::make_pair(request[1].str(), newSubscription));
newSubscription->owner = &session;
return 1; return 1;
} else if (request[0].equals("catalog")) { }
return 1;
}
else if (request[0] == "catalog") {
session.out << ":catalog:"; session.out << ":catalog:";
for (auto const &[key, subscription] : subscriptions) { for (auto const &[key, subscription] : subscriptions) {
session.out << subscription->id << ";"; session.out << subscription->id << ";";
} }
session.out << std::endl; session.out << std::endl;
return 1; return 1;
} else if (request[0].equals("invite")) { }
else if (request[0] == "invite") {
std::stringstream out; std::stringstream out;
coreutils::Log(coreutils::LOG_DEBUG_1) << request[2]; coreutils::Log(coreutils::LOG_DEBUG_1) << request[2];
std::string invitee = request[2].str(); invitee = (coreutils::MString *)&request[2];
TCPSession *tempSession = session.server.getSessionByAlias(&invitee); TCPSession *tempSession = session.server.getSessionByAlias(invitee);
std::stringstream temp; std::stringstream temp;
temp << "invite:" << request[1] << ":" << *(std::string *)session.alias; temp << "invite:" << request[1] << ":" << invitee;
tempSession->write(temp.str()); tempSession->write(temp.str());
return 1; return 1;
} }
@@ -77,30 +104,29 @@ namespace core {
auto subscription = subscriptions[request[1].str()]; auto subscription = subscriptions[request[1].str()];
if (request[1].equals(subscription->id)) { if (request[1].equals(subscription->id)) {
if (request[0].equals("unpublish")) { if (request[0] == "unpublish") {
subscriptions.erase(request[1].str()); subscriptions.erase(request[1].str());
} else if (request[0].equals("subscribe")) { }
else if (request[0] == "subscribe") {
subscription->subscribe(session); subscription->subscribe(session);
return 1; return 1;
} else if (request[0].equals("unsubscribe")) { }
else if (request[0] == "unsubscribe") {
subscription->unsubscribe(session); subscription->unsubscribe(session);
return 1; return 1;
} else if (request[0].equals("event")) { }
else if (request[0] == "event") {
std::stringstream out; std::stringstream out;
subscription->process(request, out, session); subscription->process(request, out, session);
if (subscription->mode == "*ANYONE") { if (subscription->mode == "*ANYONE") {
subscription->event(out); subscription->event(out);
return 1; return 1;
} else if (subscription->mode == "*SUBSCRIBERS") { }
else if (subscription->mode == "*SUBSCRIBERS") {
if (subscription->ifSubscriber(session)) { if (subscription->ifSubscriber(session)) {
subscription->event(out); subscription->event(out);
return 1; return 1;
} }
} else if (subscription->mode == "*AUTHOR") {
if (subscription->owner == &session) {
subscription->event(out);
return 1;
}
} }
} }
return 0; return 0;
+11 -4
View File
@@ -2,32 +2,39 @@
#define __SubscriptionManager_h__ #define __SubscriptionManager_h__
#include "Command.h" #include "Command.h"
#include "MString.h"
#include "SessionFilter.h"
#include "Subscription.h" #include "Subscription.h"
#include "SubscriptionHandler.h" #include "SubscriptionHandler.h"
#include "TCPSession.h" #include "TCPSession.h"
#include "ZString.h" #include "ZString.h"
#include <string>
#include <vector> #include <vector>
namespace core { namespace core {
//*AUTHOR -> Dies when player disconnects
//*ANYONE -> Does not die when player disconnects
//*SUBSCRIPTION -> Does not die when player disconnects
//*OWNER -> Does die and transfers Ownership before he does
class SubscriptionHandlerFactory; class SubscriptionHandlerFactory;
class SubscriptionManager : public Command { class SubscriptionManager : public Command {
public: public:
int add(Subscription &subscription); int add(Subscription &subscription);
int removeSessionSubscriptions(TCPSession &session); int removeSessionSubscriptions(TCPSession &session);
int processCommand(coreutils::ZString &request, TCPSession &session) override; int processCommand(coreutils::ZString &request, TCPSession &session) override;
bool onClearSubscription(coreutils::MString temp, coreutils::MString key);
SubscriptionHandlerFactory *factory = NULL; SubscriptionHandlerFactory *factory = NULL;
private: private:
Subscription *subscription; Subscription *subscription;
std::map<std::string, Subscription *> subscriptions; std::map<coreutils::MString, Subscription *> subscriptions;
Subscription *newSubscription; Subscription *newSubscription;
std::mutex lock; std::mutex lock;
coreutils::MString invitee;
}; };
} }
+29
View File
@@ -0,0 +1,29 @@
#include "TCPLineBufferParser.h"
namespace core {
TCPLineBufferParser::TCPLineBufferParser() {
buffer = nullptr;
bufferSize = 4096;
buffer = realloc(buffer, bufferSize);
cursor = buffer;
}
void TCPLineBufferParser::readDataNotification() {
len = ::read(getDescriptor(), buffer, bufferSize);
for(unsigned int index = 0; index < bufferSize; ++index) {
if(cursor[index] == '\n') {
coreutils::ZString data(cursor, index);
dataReceived(data);
}
}
}
void TCPLineBufferParser::onDataReceived(coreutils::ZString data) {}
}
+43
View File
@@ -0,0 +1,43 @@
#ifndef __TCPLineBufferParser_h__
#define __TCPLineBufferParser_h__
#include "TCPSocket.h"
namespace core {
///
/// TCPLineBufferParser
///
/// This object will read lines of data from the source one line at a time.
///
///
class TCPLineBufferParser : public TCPSocket {
protected:
///
/// This method is called when a line is ready to be processed.
///
virtual void onDataReceived();
private:
///
/// Read the data for the socket and split into lines and call dataReceived
/// for each line parsed.
///
void readDataNotification() override;
char *buffer;
size_t bufferSize;
char *cursor;
};
}
#endif
+22 -14
View File
@@ -8,7 +8,8 @@ namespace core
{ {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text) TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
: TCPSocket(ePoll, text), commands(delimiter, depth) { : TCPSocket(ePoll, text), commands(delimiter, depth)
{
commands.add(subscriptions, "publish"); commands.add(subscriptions, "publish");
commands.add(subscriptions, "unpublish"); commands.add(subscriptions, "unpublish");
@@ -35,7 +36,8 @@ namespace core
close(getDescriptor()); close(getDescriptor());
} }
void TCPServer::onDataReceived(std::string data) { void TCPServer::onDataReceived(std::string data)
{
lock.lock(); lock.lock();
TCPSession *session = accept(); TCPSession *session = accept();
if (session) if (session)
@@ -43,9 +45,11 @@ namespace core
lock.unlock(); lock.unlock();
} }
TCPSession * TCPServer::accept() { TCPSession *TCPServer::accept()
{
try { try
{
TCPSession *session = getSocketAccept(ePoll); TCPSession *session = getSocketAccept(ePoll);
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength)); session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
@@ -72,30 +76,36 @@ namespace core
return NULL; return NULL;
} }
void TCPServer::removeFromSessionList(TCPSession *session) { void TCPServer::removeFromSessionList(TCPSession *session)
{
std::vector<TCPSession *>::iterator cursor; std::vector<TCPSession *>::iterator cursor;
lock.lock(); lock.lock();
for (cursor = sessions.begin(); cursor < sessions.end(); ++cursor) for (cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
if(*cursor == session) { if (*cursor == session)
{
sessions.erase(cursor); sessions.erase(cursor);
break; break;
} }
lock.unlock(); lock.unlock();
} }
void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) { void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out)
{
throw coreutils::Exception(errorString); throw coreutils::Exception(errorString);
} }
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) { TCPSession *TCPServer::getSocketAccept(EPoll &ePoll)
{
return new TCPSession(ePoll, *this); return new TCPSession(ePoll, *this);
} }
void TCPServer::output(std::stringstream &out) { void TCPServer::output(std::stringstream &out)
{
out << "Use the 'help' command to list the commands for this server." << std::endl; out << "Use the 'help' command to list the commands for this server." << std::endl;
} }
int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session) { int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session)
{
int sequence = 0; int sequence = 0;
for (auto *sessionx : sessions) for (auto *sessionx : sessions)
{ {
@@ -130,11 +140,9 @@ namespace core
data.str(""); data.str("");
} }
TCPSession *TCPServer::getSessionByAlias(void *alias) TCPSession *TCPServer::getSessionByAlias(coreutils::MString alias) {
{
coreutils::Log(coreutils::LOG_DEBUG_1) << alias;
for (auto session : sessions) for (auto session : sessions)
if (session->compareAlias(alias)) if (session->alias == alias)
return session; return session;
return NULL; return NULL;
} }
+1 -1
View File
@@ -122,7 +122,7 @@ namespace core
/// of the alias pointer. /// of the alias pointer.
/// ///
TCPSession *getSessionByAlias(void *alias); TCPSession *getSessionByAlias(coreutils::MString alias);
protected: protected:
/// ///
+16 -35
View File
@@ -4,46 +4,32 @@
#include "TCPServer.h" #include "TCPServer.h"
#include "uuid/uuid.h" #include "uuid/uuid.h"
namespace core namespace core {
{
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) {
{ uuid_t uuid;
uuid_t uuidObject; uuid_generate(uuid);
uuid_generate(uuidObject); alias = coreutils::MString((char *)uuid, 26);
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
alias = (void *)uuidObject;
} }
TCPSession::~TCPSession() TCPSession::~TCPSession() {
{
server.removeFromSessionList(this); server.removeFromSessionList(this);
server.subscriptions.removeSessionSubscriptions(*this); server.subscriptions.removeSessionSubscriptions(*this);
} }
void TCPSession::output(std::stringstream &data) void TCPSession::output(std::stringstream &data) {
{
data << "|" << ipAddress.getClientAddressAndPort(); data << "|" << ipAddress.getClientAddressAndPort();
} }
void TCPSession::protocol(coreutils::ZString &data) void TCPSession::protocol(coreutils::ZString &data) {
{
if (data.getLength() != 0) if (data.getLength() != 0)
{
if (!server.commands.processRequest(data, *this)) if (!server.commands.processRequest(data, *this))
{
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str(); coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
} }
}
}
bool TCPSession::compareAlias(void *alias) { // bool TCPSession::compareAlias(coreutils::MString alias) {
return this->alias = alias; // return this->alias == alias;
} // }
void TCPSession::outputAlias(std::stringstream &out) {
out << alias;
}
void TCPSession::onRegistered() { void TCPSession::onRegistered() {
onConnected(); onConnected();
@@ -75,8 +61,7 @@ namespace core
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
} }
else if (lineBufferSize >= blockLength) else if (lineBufferSize >= blockLength) {
{
coreutils::ZString zBlock(lineBuffer, blockLength); coreutils::ZString zBlock(lineBuffer, blockLength);
onBlockReceived(zBlock); onBlockReceived(zBlock);
lineBufferSize -= blockLength; lineBufferSize -= blockLength;
@@ -88,21 +73,18 @@ namespace core
} }
} }
void TCPSession::setBlockSize(int blockSize) void TCPSession::setBlockSize(int blockSize) {
{
this->blockSize = blockSize; this->blockSize = blockSize;
} }
void TCPSession::onLineReceived(coreutils::ZString &line) void TCPSession::onLineReceived(coreutils::ZString &line) {
{
protocol(line); protocol(line);
send(); send();
if (term) if (term)
shutdown("termination requested"); shutdown("termination requested");
} }
void TCPSession::onBlockReceived(coreutils::ZString &block) void TCPSession::onBlockReceived(coreutils::ZString &block) {
{
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]"; coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]";
if (term) if (term)
shutdown("termination requested"); shutdown("termination requested");
@@ -114,8 +96,7 @@ namespace core
out.str(""); out.str("");
} }
void TCPSession::terminate() void TCPSession::terminate() {
{
term = true; term = true;
} }
+13 -29
View File
@@ -1,11 +1,12 @@
#ifndef __Session_h__ #ifndef __Session_h__
#define __Session_h__ #define __Session_h__
#include "TCPLineBufferParser.h"
#include "MString.h"
#include "SessionFilter.h" #include "SessionFilter.h"
#include "TCPSocket.h" #include "TCPSocket.h"
namespace core namespace core {
{
class Command; class Command;
class TCPServer; class TCPServer;
@@ -22,8 +23,7 @@ namespace core
/// ///
/// ///
class TCPSession : public TCPSocket class TCPSession : public TCPLineBufferParser {
{
public: public:
/// ///
@@ -76,20 +76,20 @@ namespace core
char uuid[37]; char uuid[37];
/// ///
/// alias is a void pointer that can be set to point to any object that identifies /// alias is a MString that can be set to any string of characters that identifies
/// this session uniquely. Using this approach, inheriting objects can determine /// this session uniquely. Alias's should be unique between sessions.
/// how it knows the contacts that this server manages.
/// ///
void *alias; coreutils::MString alias;
///
///
///
/// ///
/// ///
/// ///
virtual bool compareAlias(void *alias); // bool compareAlias(coreutils::MString alias);
virtual void outputAlias(std::stringstream &out);
protected: protected:
/// ///
@@ -103,7 +103,8 @@ namespace core
/// received. If you need data split by line termination characters then /// received. If you need data split by line termination characters then
/// override the onLineReceived method instead. /// override the onLineReceived method instead.
/// ///
virtual void onDataReceived(coreutils::ZString &data) override;
void onDataReceived(coreutils::ZString &data) override;
/// ///
/// Override the onLineReceived method to receive a string of characters that /// Override the onLineReceived method to receive a string of characters that
@@ -112,23 +113,6 @@ namespace core
/// this method explicitly using the class and member name. /// this method explicitly using the class and member name.
/// ///
virtual void onLineReceived(coreutils::ZString &line);
///
/// Override the onBlockReceived method to receive a string of characters that
/// represents a single block of data of length determined by the block length value. If
/// onDataReceived was overriden this method will not be called unless the onDataReceived
/// calls this method explicitly using the class and member name.
///
virtual void onBlockReceived(coreutils::ZString &block);
///
/// This method is called from within the protocol method when protocol is called
/// on the initial connection where the data is an empty string. Use this method
/// to deliver a message to the connection upon connection.
///
virtual void onConnected(); virtual void onConnected();
/// ///
+2 -4
View File
@@ -1,9 +1,9 @@
#ifndef __TCPSocket_h__ #ifndef __TCPSocket_h__
#define __TCPSocket_h__ #define __TCPSocket_h__
#include "includes"
#include "Socket.h"
#include "IPAddress.h" #include "IPAddress.h"
#include "Socket.h"
#include "includes"
namespace core { namespace core {
@@ -20,7 +20,6 @@ namespace core {
class TCPSocket : public Socket { class TCPSocket : public Socket {
public: public:
TCPSocket(EPoll &ePoll); TCPSocket(EPoll &ePoll);
TCPSocket(EPoll &ePoll, std::string text); TCPSocket(EPoll &ePoll, std::string text);
virtual ~TCPSocket(); virtual ~TCPSocket();
@@ -37,7 +36,6 @@ namespace core {
/// ///
virtual void output(std::stringstream &out); virtual void output(std::stringstream &out);
}; };
} }
+16 -9
View File
@@ -1,16 +1,20 @@
#include "Timer.h" #include "Timer.h"
namespace core { namespace core
{
Timer::Timer(EPoll &ePoll, double delay = 0.0f) : Socket(ePoll, "Timer") { Timer::Timer(EPoll &ePoll, double delay = 0.0f) : Socket(ePoll, "Timer")
{
setDescriptor(timerfd_create(CLOCK_REALTIME, 0)); setDescriptor(timerfd_create(CLOCK_REALTIME, 0));
setTimer(delay); setTimer(delay);
} }
Timer::~Timer() { Timer::~Timer()
{
} }
void Timer::setTimer(double delay) { void Timer::setTimer(double delay)
{
double integer; double integer;
double fraction; double fraction;
@@ -27,10 +31,10 @@ namespace core {
timer.it_value.tv_nsec = (int)(fraction * 1000000000); timer.it_value.tv_nsec = (int)(fraction * 1000000000);
timerfd_settime(getDescriptor(), 0, &timer, NULL); timerfd_settime(getDescriptor(), 0, &timer, NULL);
} }
void Timer::clearTimer() { void Timer::clearTimer()
{
struct itimerspec timer; struct itimerspec timer;
@@ -42,18 +46,21 @@ namespace core {
timerfd_settime(getDescriptor(), 0, &timer, NULL); timerfd_settime(getDescriptor(), 0, &timer, NULL);
} }
double Timer::getElapsed() { double Timer::getElapsed()
{
struct itimerspec timer; struct itimerspec timer;
timerfd_gettime(getDescriptor(), &timer); timerfd_gettime(getDescriptor(), &timer);
double toTimeout = (double)((timer.it_value.tv_sec * 1000000000L) + timer.it_value.tv_nsec) / 1000000000L; double toTimeout = (double)((timer.it_value.tv_sec * 1000000000L) + timer.it_value.tv_nsec) / 1000000000L;
return delayValue - toTimeout; return delayValue - toTimeout;
} }
void Timer::onDataReceived(std::string data) { void Timer::onDataReceived(std::string data)
{
onTimeout(); onTimeout();
} }
double Timer::getEpoch() { double Timer::getEpoch()
{
return (double)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() / 1000000000L; return (double)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() / 1000000000L;
} }
+5 -5
View File
@@ -1,10 +1,11 @@
#ifndef __Timer_h__ #ifndef __Timer_h__
#define __Timer_h__ #define __Timer_h__
#include "Socket.h"
#include "EPoll.h" #include "EPoll.h"
#include "Socket.h"
namespace core { namespace core
{
/// ///
/// Timer /// Timer
@@ -15,7 +16,8 @@ namespace core {
/// interface. It cannot be instantiated directly but must be extended. /// interface. It cannot be instantiated directly but must be extended.
/// ///
class Timer : Socket { class Timer : Socket
{
public: public:
Timer(EPoll &ePoll); Timer(EPoll &ePoll);
@@ -48,7 +50,6 @@ namespace core {
double getEpoch(); double getEpoch();
protected: protected:
/// ///
/// This method is called when the time out occurs. /// This method is called when the time out occurs.
/// ///
@@ -58,7 +59,6 @@ namespace core {
private: private:
void onDataReceived(std::string data) override; void onDataReceived(std::string data) override;
double delayValue; double delayValue;
}; };
} }
BIN
View File
Binary file not shown.
-6
View File
@@ -1,6 +0,0 @@
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello world!" << std::endl;
}
-38
View File
@@ -1,38 +0,0 @@
{
"folders": [
{
"path": "../../CoreUtils"
},
{
"path": ".."
},
{
"path": "../../HTTPServer"
}
],
"settings": {},
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/barant/Development/HTTPServer/HTTPServer",
"args": [],
"stopAtEntry": false,
"cwd": "/home/barant/Development/HTTPServer/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
}