avec Services et Service

This commit is contained in:
eleonore12345 2024-08-02 19:26:43 +02:00
parent d81176ffa6
commit 45320747b5
8 changed files with 128 additions and 27 deletions

View File

@ -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)
$(RM) $(OBJECTS) $(EXE)

View File

@ -4,7 +4,6 @@
#include "Module.h"
Module::Module(char * serv):service(serv){}
Module::~Module(){}

View File

@ -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

27
src/Service.cpp Normal file
View File

@ -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<string> 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;
}

28
src/Service.h Normal file
View File

@ -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 <iostream>
#include <string>
#include <string>
#include <list>
using namespace std;
class Service {
public:
Service(int aUserID, string aUsername, list <string> aServers):userID(aUserID),username(aUsername),servers(aServers){}
~Service(){}
int getUserID () const;
string getUsername() const;
list<string> getServers() const;
friend ostream & operator<<(ostream & out, const Service & s);
private:
int userID;
string username;
list <string> servers;
};
#endif

View File

@ -9,18 +9,59 @@
#include <string>
#include "Services.h"
//constructor
Services::Services(string ServicesCSV)
{
services=readServicesFromCSV(ServicesCSV);
}
//destructor
Services::~Services(){}
//public methods
vector<Service> Services::getServices()const
{
return services;
}
vector <serviceData> 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<const Service*> Services::findByServer(string aServer) const
{
list<const Service*> 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 <Service> 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 <serviceData>, with serviceData a structure defined in the header
vector <serviceData> result;
//and returns them in a vector <Service>, with Service a structure defined in the header
vector <Service> result;
ifstream streamServices(CSV);
if (!streamServices){
cout << "Invalid services.csv file." << endl;
@ -29,8 +70,8 @@ vector <serviceData> Services::readServicesFromCSV (string CSV) const
string tmpUserID; //used before converting to int
int userID;
string username;
string serveur;
list <string> serveurs;
string server;
list <string> servers;
while(getline(streamServices,line)){
if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
continue;
@ -39,12 +80,13 @@ vector <serviceData> 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;
}
}

View File

@ -9,24 +9,23 @@
#include <cstring>
#include <list>
#include <vector>
#include "Service.h"
using namespace std;
struct serviceData {
int userID;
string username;
list <string> serveurs;
};
class Services
//extracts the list of uid|username|service from the services.csv file
{
public:
Services(string servicesCSV="../src/services.csv");
vector<Service> getServices() const;
const Service * findByUsername(string aUsername) const;
const Service * findByID(int aUserID) const;
list<const Service*> findByServer(string aServer) const;
~Services();
private:
vector <serviceData> readServicesFromCSV (string CSV) const;
vector <serviceData> services;
vector <Service> readServicesFromCSV (string CSV) const;
vector <Service> services;
};
#endif

View File

@ -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"<<endl;
}else{
cout << "service found" << endl;
cout << *ptr_myService << endl;
}
}
return 0;
}