diff --git a/Backend/Networking/main.cpp b/Backend/Networking/main.cpp index ea64b56..a1f7a66 100644 --- a/Backend/Networking/main.cpp +++ b/Backend/Networking/main.cpp @@ -28,11 +28,10 @@ using namespace GNet; -GNet::GServer::GServer() +GServer::GServer() + : socks(shmea::GPointer(new Sockets(this))), + logger(shmea::GPointer(new shmea::GLogger(shmea::GLogger::LOG_INFO))) { - logger = shmea::GPointer(new shmea::GLogger(shmea::GLogger::LOG_INFO)); - logger->setPrintLevel(shmea::GLogger::LOG_INFO); - socks = shmea::GPointer(new Sockets(this)); sockfd = -1; cryptEnabled = true; LOCAL_ONLY = false; @@ -66,7 +65,7 @@ GNet::GServer::GServer() addService(br); } -GNet::GServer::~GServer() +GServer::~GServer() { running = false; shutdown(getSockFD(), 2); @@ -110,13 +109,13 @@ GNet::GServer::~GServer() writersBlock = NULL; } -void GNet::GServer::send(shmea::ServiceData* cData, bool localFallback, bool networkingDisabled) +void GServer::send(shmea::ServiceData* cData, bool localFallback, bool networkingDisabled) { if (!cData) return; // Default instance - GNet::Connection* destination = cData->getConnection(); + Connection* destination = cData->getConnection(); if (!destination) { if (localFallback) @@ -144,11 +143,11 @@ void GNet::GServer::send(shmea::ServiceData* cData, bool localFallback, bool net } else { - GNet::Service::ExecuteService(this, cData, destination); + Service::ExecuteService(this, cData, destination); } } -unsigned int GNet::GServer::addService(GNet::Service* newServiceObj) +unsigned int GServer::addService(Service* newServiceObj) { shmea::GString newServiceName = newServiceObj->getName(); std::map::const_iterator itr = service_depot.find(newServiceName); @@ -160,7 +159,7 @@ unsigned int GNet::GServer::addService(GNet::Service* newServiceObj) return service_depot.size(); } -GNet::Service* GNet::GServer::DoService(shmea::GString cCommand, shmea::GString newKey) +Service* GServer::DoService(shmea::GString cCommand, shmea::GString newKey) { // Does it exist at all? std::map::const_iterator itr = service_depot.find(cCommand); @@ -169,7 +168,7 @@ GNet::Service* GNet::GServer::DoService(shmea::GString cCommand, shmea::GString if(newKey.length() == 0) { - GNet::Service* cService = service_depot[cCommand]->MakeService(this); + Service* cService = service_depot[cCommand]->MakeService(this); return cService; } else if(newKey.length() > 0) @@ -177,13 +176,13 @@ GNet::Service* GNet::GServer::DoService(shmea::GString cCommand, shmea::GString std::map::const_iterator itr2 = running_services.find(newKey); if(itr2 == running_services.end()) { - GNet::Service* cService = service_depot[cCommand]->MakeService(this); + Service* cService = service_depot[cCommand]->MakeService(this); running_services[newKey] = cService; return cService; } else { - GNet::Service* cService = running_services[newKey]; + Service* cService = running_services[newKey]; return cService; } } @@ -191,17 +190,17 @@ GNet::Service* GNet::GServer::DoService(shmea::GString cCommand, shmea::GString return NULL; } -const bool& GNet::GServer::getRunning() const +const bool& GServer::getRunning() const { return running; } -shmea::GString GNet::GServer::getPort() const +shmea::GString GServer::getPort() const { return socks->getPort(); } -void GNet::GServer::stop() +void GServer::stop() { running = false; @@ -211,7 +210,7 @@ void GNet::GServer::stop() pthread_join(*writerThread, NULL); } -void GNet::GServer::run(shmea::GString newPort, bool _networkingDisabled) +void GServer::run(shmea::GString newPort, bool _networkingDisabled) { LOCAL_ONLY = _networkingDisabled; running = true; @@ -222,37 +221,37 @@ void GNet::GServer::run(shmea::GString newPort, bool _networkingDisabled) pthread_create(writerThread, NULL, ListWLauncher, this); } -bool GNet::GServer::isNetworkingDisabled() +bool GServer::isNetworkingDisabled() { return LOCAL_ONLY; } -void GNet::GServer::enableEncryption() +void GServer::enableEncryption() { cryptEnabled = true; } -void GNet::GServer::disableEncryption() +void GServer::disableEncryption() { cryptEnabled = false; } -bool GNet::GServer::isEncryptedByDefault() const +bool GServer::isEncryptedByDefault() const { return cryptEnabled; } -int GNet::GServer::getSockFD() +int GServer::getSockFD() { return sockfd; } -Connection* GNet::GServer::getLocalConnection() +Connection* GServer::getLocalConnection() { return localConnection; } -const std::vector GNet::GServer::getClientConnections() +const std::vector GServer::getClientConnections() { std::map >::const_iterator itr = clientCLookUp.begin(); @@ -270,7 +269,7 @@ const std::vector GNet::GServer::getClientConnections() return clientConnections; } -void GNet::GServer::removeClientConnection(Connection* cConnection) +void GServer::removeClientConnection(Connection* cConnection) { if (!cConnection) return; @@ -301,7 +300,7 @@ void GNet::GServer::removeClientConnection(Connection* cConnection) } -const std::vector GNet::GServer::getServerConnections() +const std::vector GServer::getServerConnections() { std::map >::const_iterator itr = serverCLookUp.begin(); std::vector serverConnections; @@ -317,7 +316,7 @@ const std::vector GNet::GServer::getServerConnections() return serverConnections; } -void GNet::GServer::removeServerConnection(GNet::Connection* cConnection) +void GServer::removeServerConnection(Connection* cConnection) { if (!cConnection) return; @@ -348,12 +347,12 @@ void GNet::GServer::removeServerConnection(GNet::Connection* cConnection) } } -bool GNet::GServer::isConnection(int _sockfd, const fd_set& fdarr) +bool GServer::isConnection(int _sockfd, const fd_set& fdarr) { return FD_ISSET(_sockfd, &fdarr); } -GNet::Connection* GNet::GServer::setupNewConnection(int max_sock) +Connection* GServer::setupNewConnection(int max_sock) { struct sockaddr_in from; socklen_t clientLength = sizeof(from); @@ -398,8 +397,8 @@ GNet::Connection* GNet::GServer::setupNewConnection(int max_sock) return NULL; } -GNet::Connection* -GNet::GServer::findExistingConnection(const std::vector& instances, +Connection* +GServer::findExistingConnection(const std::vector& instances, const fd_set& fdarr) { for (unsigned int i = 0; i < instances.size(); ++i) @@ -418,7 +417,7 @@ GNet::GServer::findExistingConnection(const std::vector& inst //TODO: To be finished, since each server connection and client connnections can have multiple connections from the same IP //and there is no way to differentiate between them with the current implementation -GNet::Connection* GNet::GServer::getConnection(shmea::GString newServerIP, shmea::GString clientName, shmea::GString newPort) +Connection* GServer::getConnection(shmea::GString newServerIP, shmea::GString clientName, shmea::GString newPort) { std::map >::const_iterator itr = serverCLookUp.find(newServerIP); @@ -450,7 +449,7 @@ GNet::Connection* GNet::GServer::getConnection(shmea::GString newServerIP, shmea return NULL; } -GNet::Connection* GNet::GServer::getConnectionFromName(shmea::GString clientName) +Connection* GServer::getConnectionFromName(shmea::GString clientName) { @@ -482,7 +481,7 @@ GNet::Connection* GNet::GServer::getConnectionFromName(shmea::GString clientName return NULL; } -void* GNet::GServer::commandLauncher(void* y) +void* GServer::commandLauncher(void* y) { GServer* x = (GServer*)y; if (x) @@ -491,7 +490,7 @@ void* GNet::GServer::commandLauncher(void* y) return NULL; } -void GNet::GServer::commandCatcher(void*) +void GServer::commandCatcher(void*) { // socket stuff sockfd = -1; @@ -661,7 +660,7 @@ void GNet::GServer::commandCatcher(void*) close(sockfd); } -void* GNet::GServer::LaunchInstanceLauncher(void* y) +void* GServer::LaunchInstanceLauncher(void* y) { LaunchInstanceHelperArgs* x = (LaunchInstanceHelperArgs*)y; if (x->serverInstance) @@ -670,7 +669,7 @@ void* GNet::GServer::LaunchInstanceLauncher(void* y) return NULL; } -void GNet::GServer::LaunchInstanceHelper(void* y) +void GServer::LaunchInstanceHelper(void* y) { LaunchInstanceHelperArgs* x = (LaunchInstanceHelperArgs*)y; if (!x->serverInstance) @@ -721,7 +720,7 @@ void GNet::GServer::LaunchInstanceHelper(void* y) socks->writeConnection(destination, sockfd2, cData); } -void GNet::GServer::LaunchInstance(const shmea::GString& serverIP, const shmea::GString& serverPort, const shmea::GString& clientName) +void GServer::LaunchInstance(const shmea::GString& serverIP, const shmea::GString& serverPort, const shmea::GString& clientName) { //Checks if the serverIP key exists in serverConnections then checks the indexs in the vector serverC bool serverCKeyExists = serverCLookUp.find(serverIP) != serverCLookUp.end(); @@ -766,16 +765,16 @@ void GNet::GServer::LaunchInstance(const shmea::GString& serverIP, const shmea:: //Log the server out of the client ServiceData* wData; wData.addInt(Service::LOGOUT_SERVER); - GNet::Service::ExecuteService(this, wData, cConnection); + Service::ExecuteService(this, wData, cConnection); }*/ } -void GNet::GServer::wakeWriter() +void GServer::wakeWriter() { pthread_cond_signal(writersBlock); // wake the ListWriter thread } -void* GNet::GServer::ListWLauncher(void* y) +void* GServer::ListWLauncher(void* y) { GServer* x = (GServer*)y; if (x) @@ -784,7 +783,7 @@ void* GNet::GServer::ListWLauncher(void* y) return NULL; } -void GNet::GServer::ListWriter(void*) +void GServer::ListWriter(void*) { while (getRunning()) { @@ -803,18 +802,18 @@ void GNet::GServer::ListWriter(void*) } } -void GNet::GServer::LaunchLocalInstance(const shmea::GString& clientName) +void GServer::LaunchLocalInstance(const shmea::GString& clientName) { shmea::GString serverIP = "127.0.0.1"; LaunchInstance(serverIP, socks->getPort(), clientName); } -void GNet::GServer::LogoutInstance(Connection* cConnection) +void GServer::LogoutInstance(Connection* cConnection) { if (!cConnection) return; // Log out the connection shmea::ServiceData* cData = new shmea::ServiceData(localConnection, "Logout_Client"); - GNet::Service::ExecuteService(this, cData, cConnection); + Service::ExecuteService(this, cData, cConnection); } diff --git a/Backend/Networking/service.cpp b/Backend/Networking/service.cpp index d16900e..0481f04 100644 --- a/Backend/Networking/service.cpp +++ b/Backend/Networking/service.cpp @@ -31,9 +31,11 @@ using namespace GNet; * @brief Service constructor * @details creates a Service object and initialize timeExecuted */ -Service::Service() +Service::Service() : logger(shmea::GPointer(new shmea::GLogger(shmea::GLogger::LOG_INFO))) { timeExecuted = 0; + if (logger) + logger->info("SERVICE", "Service object created."); } /*! @@ -42,6 +44,8 @@ Service::Service() */ Service::~Service() { + if (logger) + logger->info("SERVICE", "Service object is being destroyed. Time executed: " + shmea::GString::format("%lld", timeExecuted) + " seconds."); timeExecuted = 0; } @@ -59,6 +63,9 @@ bool Service::getRunning() const void Service::ExecuteService(GServer* serverInstance, const shmea::ServiceData* sockData, Connection* cConnection) { + if (serverInstance->logger) + serverInstance->logger->info("SERVICE", "Executing service asynchronously."); + // set the args to pass in newServiceArgs* x = new newServiceArgs[sizeof(newServiceArgs)]; x->serverInstance = serverInstance; @@ -69,7 +76,16 @@ void Service::ExecuteService(GServer* serverInstance, const shmea::ServiceData* // launch a new service thread pthread_create(x->sThread, NULL, &launchService, (void*)x); if (x->sThread) - pthread_detach(*x->sThread); + { + pthread_detach(*x->sThread); + if (serverInstance->logger) + serverInstance->logger->info("SERVICE", "Service thread launched and detached."); + } + else + { + if (serverInstance->logger) + serverInstance->logger->error("SERVICE", "Failed to launch service thread."); + } } /*! @@ -85,13 +101,22 @@ void* Service::launchService(void* y) newServiceArgs* x = (newServiceArgs*)y; if (!x->serverInstance) - return NULL; + { + if (x->serverInstance->logger) + x->serverInstance->logger->error("SERVICE", "Server instance is null. Cannot launch service."); + return NULL; + } + GServer* serverInstance = x->serverInstance; // Get the command in order to tell the service what to do x->command = x->sockData->getCommand(); if(x->command.length() == 0) - return NULL; + { + if (serverInstance->logger) + serverInstance->logger->warning("SERVICE", "Command is empty. Service will not execute."); + return NULL; + } // Can be 0 len x->serviceKey = x->sockData->getServiceKey(); @@ -99,13 +124,20 @@ void* Service::launchService(void* y) // Connection is dead so ignore it Connection* cConnection = x->cConnection; if (!cConnection) - return NULL; + { + if (serverInstance->logger) + serverInstance->logger->warning("SERVICE", "Connection is null. Service will not execute."); + return NULL; + } if (!cConnection->isFinished()) { Service* cService = serverInstance->DoService(x->command, x->serviceKey); if (cService) { + if (serverInstance->logger) + serverInstance->logger->info("SERVICE", "Service found and starting execution."); + // start the service cService->StartService(x); @@ -116,6 +148,9 @@ void* Service::launchService(void* y) //Response Service Number will be given by the service received by the server retData->setResponseServiceNum(x->sockData->getResponseServiceNum()); serverInstance->socks->addResponseList(serverInstance, cConnection, retData); + + if (serverInstance->logger) + serverInstance->logger->info("SERVICE", "Service executed successfully. Response added to outbound list."); } // exit the service @@ -123,6 +158,11 @@ void* Service::launchService(void* y) delete cService; } + else + { + if (serverInstance->logger) + serverInstance->logger->warning("SERVICE", "No service found for the given command."); + } } if (x) @@ -130,7 +170,11 @@ void* Service::launchService(void* y) // delete the Connection if (cConnection->isFinished()) - delete cConnection; + { + if (serverInstance->logger) + serverInstance->logger->info("SERVICE", "Connection is finished. Deleting connection."); + delete cConnection; + } return NULL; } @@ -150,9 +194,8 @@ void Service::StartService(newServiceArgs* x) if (!cConnection->isFinished()) ipAddress = cConnection->getIP(); - // const shmea::GString& command = x->command; - // const shmea::GString& serviceKey = x->serviceKey; - //printf("---------Service Start: %s (%s: %s)---------\n", ipAddress.c_str(), x->command.c_str(), x->serviceKey.c_str()); + if (logger) + logger->info("SERVICE", "Service started for IP: " + ipAddress); // add the thread to the connection's active thread vector cThread = x->sThread; @@ -176,7 +219,9 @@ void Service::ExitService(newServiceArgs* x) // Set and print the execution time timeExecuted = time(NULL) - timeExecuted; - //printf("---------Service Exit: %s (%s: %s); %llds---------\n", ipAddress.c_str(), x->command.c_str(), x->serviceKey.c_str(), timeExecuted); + + if (logger) + logger->info("SERVICE", "Service exited for IP: " + ipAddress + ". Execution time: " + shmea::GString::format("%lld", timeExecuted) + " seconds."); pthread_exit(0); } diff --git a/Backend/Networking/service.h b/Backend/Networking/service.h index 060dbb9..879b275 100644 --- a/Backend/Networking/service.h +++ b/Backend/Networking/service.h @@ -18,6 +18,7 @@ #define _GSERVICE #include "../Database/GString.h" +#include "../Database/GLogger.h" #include #include #include @@ -57,6 +58,8 @@ class Service static void ExecuteService(GServer*, const shmea::ServiceData*, Connection* = NULL); public: + shmea::GPointer logger; + Service(); virtual ~Service();