avec Services et Service
This commit is contained in:
		
							parent
							
								
									d81176ffa6
								
							
						
					
					
						commit
						45320747b5
					
				
							
								
								
									
										12
									
								
								src/Makefile
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/Makefile
									
									
									
									
									
								
							| @ -2,15 +2,15 @@ ECHO = @echo | |||||||
| GCC = g++ | GCC = g++ | ||||||
| RM = @rm -f | RM = @rm -f | ||||||
| CCFLAGS = -c -O3 -g -std=c++11 -pedantic -Wall  | CCFLAGS = -c -O3 -g -std=c++11 -pedantic -Wall  | ||||||
| OBJETS = $(SRC:.cpp=.o) |  | ||||||
| SRC = $(wildcard *.cpp)  | SRC = $(wildcard *.cpp)  | ||||||
| EXE = deployer |  | ||||||
| BIN = ../bin | BIN = ../bin | ||||||
| OBJETS_DIR = $(OBJETS:%=$(BIN)/%) | EXE = deployer | ||||||
| EXE_DIR = $(EXE:%=$(BIN)/%) | OBJECTS = $(patsubst %.cpp,$(BIN)/%.o,$(SRC)) | ||||||
| LIBRARIES =  | LIBRARIES =  | ||||||
| 
 | 
 | ||||||
| $(EXE_DIR) : $(OBJETS_DIR) | $(EXE) : $(OBJECTS) | ||||||
|  | 	$(ECHO) "objects  $(OBJECTS) exe $(EXE) " | ||||||
|  | 
 | ||||||
| 	$(ECHO) "-Linking $(EXE)-" | 	$(ECHO) "-Linking $(EXE)-" | ||||||
| 	$(GCC) -o $@ $^ $(LIBRARIES) | 	$(GCC) -o $@ $^ $(LIBRARIES) | ||||||
| 	 | 	 | ||||||
| @ -21,4 +21,4 @@ $(BIN)/%.o:%.cpp | |||||||
| .PHONY: clean  | .PHONY: clean  | ||||||
| clean: | clean: | ||||||
| 	$(ECHO) "-Cleaning-" | 	$(ECHO) "-Cleaning-" | ||||||
| 	$(RM) $(OBJETS) $(EXE) | 	$(RM) $(OBJECTS) $(EXE) | ||||||
| @ -4,7 +4,6 @@ | |||||||
| 
 | 
 | ||||||
| #include "Module.h" | #include "Module.h" | ||||||
| 
 | 
 | ||||||
| Module::Module(char * serv):service(serv){} |  | ||||||
| Module::~Module(){} | Module::~Module(){} | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -10,8 +10,8 @@ | |||||||
| class Module  | class Module  | ||||||
| { | { | ||||||
|     protected: |     protected: | ||||||
|     Module(char * serv); |     Module(){}; | ||||||
|     virtual ~Module(); |     virtual ~Module()=0; | ||||||
| 
 | 
 | ||||||
|     private: |     private: | ||||||
|     bool activated; //true if the service requires this module
 |     bool activated; //true if the service requires this module
 | ||||||
|  | |||||||
							
								
								
									
										27
									
								
								src/Service.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/Service.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										28
									
								
								src/Service.h
									
									
									
									
									
										Normal 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 | ||||||
| @ -9,18 +9,59 @@ | |||||||
| #include <string> | #include <string> | ||||||
| #include "Services.h" | #include "Services.h" | ||||||
| 
 | 
 | ||||||
|  | //constructor
 | ||||||
| Services::Services(string ServicesCSV) | Services::Services(string ServicesCSV) | ||||||
| { | { | ||||||
|     services=readServicesFromCSV(ServicesCSV); |     services=readServicesFromCSV(ServicesCSV); | ||||||
| } | } | ||||||
|  | //destructor
 | ||||||
| Services::~Services(){} | 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 
 |     //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
 |     //and returns them in a vector <Service>, with Service a structure defined in the header
 | ||||||
|     vector <serviceData> result; |     vector <Service> result; | ||||||
|     ifstream streamServices(CSV);  |     ifstream streamServices(CSV);  | ||||||
|     if (!streamServices){ |     if (!streamServices){ | ||||||
|         cout << "Invalid services.csv file." << endl; |         cout << "Invalid services.csv file." << endl; | ||||||
| @ -29,8 +70,8 @@ vector <serviceData> Services::readServicesFromCSV (string CSV) const | |||||||
|         string tmpUserID; //used before converting to int
 |         string tmpUserID; //used before converting to int
 | ||||||
|         int userID; |         int userID; | ||||||
|         string username; |         string username; | ||||||
|         string serveur; |         string server; | ||||||
|         list <string> serveurs; |         list <string> servers; | ||||||
|         while(getline(streamServices,line)){ |         while(getline(streamServices,line)){ | ||||||
|             if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
 |             if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
 | ||||||
|                 continue;  |                 continue;  | ||||||
| @ -39,12 +80,13 @@ vector <serviceData> Services::readServicesFromCSV (string CSV) const | |||||||
|             getline(streamLine,tmpUserID,';'); //extracting the userID
 |             getline(streamLine,tmpUserID,';'); //extracting the userID
 | ||||||
|             userID=stoi(tmpUserID); |             userID=stoi(tmpUserID); | ||||||
|             getline(streamLine,username,';'); //extracting the username
 |             getline(streamLine,username,';'); //extracting the username
 | ||||||
|             while(getline(streamLine,serveur,';')){ //extracting the server(s)
 |             while(getline(streamLine,server,';')){ //extracting the server(s)
 | ||||||
|                 serveurs.push_back(serveur); |                 servers.push_back(server); | ||||||
|             }          |             }          | ||||||
|             serviceData entry = {userID,username,serveurs}; |             Service entry = {userID,username,servers}; | ||||||
|             result.push_back(entry); |             result.push_back(entry); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     return result; |     return result; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | |||||||
| @ -9,24 +9,23 @@ | |||||||
| #include <cstring> | #include <cstring> | ||||||
| #include <list> | #include <list> | ||||||
| #include <vector> | #include <vector> | ||||||
|  | #include "Service.h" | ||||||
| 
 | 
 | ||||||
| using namespace std; | using namespace std; | ||||||
| 
 | 
 | ||||||
| struct serviceData { |  | ||||||
|     int userID; |  | ||||||
|     string username; |  | ||||||
|     list <string> serveurs; |  | ||||||
| }; |  | ||||||
| 
 | 
 | ||||||
| class Services  | class Services  | ||||||
| //extracts the list of uid|username|service from the services.csv file 
 | //extracts the list of uid|username|service from the services.csv file 
 | ||||||
| { | { | ||||||
|     public: |     public: | ||||||
|     Services(string servicesCSV="../src/services.csv"); |     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(); |     ~Services(); | ||||||
|     private: |     private: | ||||||
|     vector <serviceData> readServicesFromCSV (string CSV) const; |     vector <Service> readServicesFromCSV (string CSV) const; | ||||||
|     vector <serviceData> services; |     vector <Service> services; | ||||||
| 
 |  | ||||||
| }; | }; | ||||||
| #endif | #endif | ||||||
| @ -37,7 +37,13 @@ int main(int argc, char *argv[]) | |||||||
|             exit(1); |             exit(1); | ||||||
|         } |         } | ||||||
|         Services myServices=Services("../src/services.csv"); |         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; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user