diff --git a/src/Makefile b/src/Makefile index 3eb2561..6f61f28 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,15 +2,15 @@ ECHO = @echo GCC = g++ RM = @rm -f CCFLAGS = -c -O3 -g -std=c++11 -pedantic -Wall -OBJETS = $(SRC:.cpp=.o) SRC = $(wildcard *.cpp) -EXE = deployer BIN = ../bin -OBJETS_DIR = $(OBJETS:%=$(BIN)/%) -EXE_DIR = $(EXE:%=$(BIN)/%) +EXE = deployer +OBJECTS = $(patsubst %.cpp,$(BIN)/%.o,$(SRC)) LIBRARIES = -$(EXE_DIR) : $(OBJETS_DIR) +$(EXE) : $(OBJECTS) + $(ECHO) "objects $(OBJECTS) exe $(EXE) " + $(ECHO) "-Linking $(EXE)-" $(GCC) -o $@ $^ $(LIBRARIES) @@ -21,4 +21,4 @@ $(BIN)/%.o:%.cpp .PHONY: clean clean: $(ECHO) "-Cleaning-" - $(RM) $(OBJETS) $(EXE) \ No newline at end of file + $(RM) $(OBJECTS) $(EXE) \ No newline at end of file diff --git a/src/Module.cpp b/src/Module.cpp index f1db616..6f90997 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -4,7 +4,6 @@ #include "Module.h" -Module::Module(char * serv):service(serv){} Module::~Module(){} diff --git a/src/Module.h b/src/Module.h index bf33d3f..0fc1e9e 100644 --- a/src/Module.h +++ b/src/Module.h @@ -10,8 +10,8 @@ class Module { protected: - Module(char * serv); - virtual ~Module(); + Module(){}; + virtual ~Module()=0; private: bool activated; //true if the service requires this module diff --git a/src/Service.cpp b/src/Service.cpp new file mode 100644 index 0000000..309516d --- /dev/null +++ b/src/Service.cpp @@ -0,0 +1,27 @@ +//deployer Service implementation +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#include "Service.h" + +int Service::getUserID () const +{ + return userID; +} +string Service::getUsername() const +{ + return username; +} +list Service::getServers() const +{ + return servers; +} + +ostream & operator << (ostream & out, const Service & s) +{ + out << "userID: " << s.userID << endl << "username: " << s.username << endl << "servers: "; + for (string server : s.servers){ + out << server << " " << endl; + } + return out; +} \ No newline at end of file diff --git a/src/Service.h b/src/Service.h new file mode 100644 index 0000000..a5c50a8 --- /dev/null +++ b/src/Service.h @@ -0,0 +1,28 @@ +//deployer Service header +// Copyright (C) 2024 Jean-Cloud +// GNU General Public License v3 + +#if !defined(SERVICE_H) +#define SERVICE_H + +#include +#include +#include +#include + +using namespace std; + +class Service { + public: + Service(int aUserID, string aUsername, list aServers):userID(aUserID),username(aUsername),servers(aServers){} + ~Service(){} + int getUserID () const; + string getUsername() const; + list getServers() const; + friend ostream & operator<<(ostream & out, const Service & s); + private: + int userID; + string username; + list servers; +}; +#endif \ No newline at end of file diff --git a/src/Services.cpp b/src/Services.cpp index dc2b86b..07b2502 100644 --- a/src/Services.cpp +++ b/src/Services.cpp @@ -9,18 +9,59 @@ #include #include "Services.h" +//constructor Services::Services(string ServicesCSV) { services=readServicesFromCSV(ServicesCSV); } +//destructor Services::~Services(){} +//public methods +vector Services::getServices()const +{ + return services; +} -vector Services::readServicesFromCSV (string CSV) const +const Service* Services::findByUsername(string aUsername) const +{ + for (const Service & service : services){ + if (service.getUsername().compare(aUsername)==0){ + return &service; + } + } + return nullptr; +} + +const Service * Services::findByID(int aUserID) const +{ + for (const Service & service : services){ + if (service.getUserID()==aUserID){ + return &service; + } + } + return nullptr; +} + +list Services::findByServer(string aServer) const +{ + list result; + for (const Service & service : services){ + for (string server : service.getServers()){ + if(server.compare(aServer)==0){ + result.push_back(&service); + } + } + } + return result; +} + +//private methods +vector Services::readServicesFromCSV (string CSV) const { //this method extracts the list of uid|username|servers from the services.csv file - //and returns them in a vector , with serviceData a structure defined in the header - vector result; + //and returns them in a vector , with Service a structure defined in the header + vector result; ifstream streamServices(CSV); if (!streamServices){ cout << "Invalid services.csv file." << endl; @@ -29,8 +70,8 @@ vector Services::readServicesFromCSV (string CSV) const string tmpUserID; //used before converting to int int userID; string username; - string serveur; - list serveurs; + string server; + list servers; while(getline(streamServices,line)){ if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account continue; @@ -39,12 +80,13 @@ vector Services::readServicesFromCSV (string CSV) const getline(streamLine,tmpUserID,';'); //extracting the userID userID=stoi(tmpUserID); getline(streamLine,username,';'); //extracting the username - while(getline(streamLine,serveur,';')){ //extracting the server(s) - serveurs.push_back(serveur); + while(getline(streamLine,server,';')){ //extracting the server(s) + servers.push_back(server); } - serviceData entry = {userID,username,serveurs}; + Service entry = {userID,username,servers}; result.push_back(entry); } } return result; -} \ No newline at end of file +} + diff --git a/src/Services.h b/src/Services.h index cff7efd..0d21a23 100644 --- a/src/Services.h +++ b/src/Services.h @@ -9,24 +9,23 @@ #include #include #include +#include "Service.h" using namespace std; -struct serviceData { - int userID; - string username; - list serveurs; -}; class Services //extracts the list of uid|username|service from the services.csv file { public: Services(string servicesCSV="../src/services.csv"); + vector getServices() const; + const Service * findByUsername(string aUsername) const; + const Service * findByID(int aUserID) const; + list findByServer(string aServer) const; ~Services(); private: - vector readServicesFromCSV (string CSV) const; - vector services; - + vector readServicesFromCSV (string CSV) const; + vector services; }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 821aa4c..9256ffb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,7 +37,13 @@ int main(int argc, char *argv[]) exit(1); } Services myServices=Services("../src/services.csv"); - + const Service * ptr_myService=myServices.findByID(1); + if (ptr_myService==nullptr){ + cout<< "service not found"<