2024-08-05 14:32:18 +00:00
|
|
|
// deployer Services implementation
|
|
|
|
// Copyright (C) 2024 Jean-Cloud
|
|
|
|
// GNU General Public License v3
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <fstream>
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Service* Services::findByUsername(string aUsername) const
|
|
|
|
{
|
2024-08-05 16:06:54 +00:00
|
|
|
//this method may disappear. Serves development purposes for now.
|
2024-08-05 14:32:18 +00:00
|
|
|
for (const Service & service : services){
|
|
|
|
if (service.getUsername().compare(aUsername)==0){
|
|
|
|
return &service;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Service * Services::findByID(int aUserID) const
|
|
|
|
{
|
2024-08-05 16:06:54 +00:00
|
|
|
//this method may disappear. Serves development purposes for now.
|
2024-08-05 14:32:18 +00:00
|
|
|
for (const Service & service : services){
|
|
|
|
if (service.getUserID()==aUserID){
|
|
|
|
return &service;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
list<const Service*> Services::findByServer(string aServer) const
|
|
|
|
{
|
2024-08-05 16:06:54 +00:00
|
|
|
//this method may disappear. Serves development purposes for now.
|
2024-08-05 14:32:18 +00:00
|
|
|
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 <Service>, with Service a structure defined in the header
|
|
|
|
vector <Service> result;
|
|
|
|
ifstream streamServices(CSV);
|
|
|
|
if (!streamServices){
|
|
|
|
cout << "Invalid services.csv file." << endl;
|
|
|
|
}else{
|
|
|
|
string line;
|
|
|
|
string tmpUserID; //used before converting to int
|
|
|
|
int userID;
|
2024-08-06 10:37:15 +00:00
|
|
|
string username="";
|
|
|
|
string server="";
|
2024-08-05 14:32:18 +00:00
|
|
|
list <string> servers;
|
|
|
|
while(getline(streamServices,line)){
|
2024-08-05 16:06:54 +00:00
|
|
|
servers.clear();
|
2024-08-05 14:32:18 +00:00
|
|
|
if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stringstream streamLine(line);
|
|
|
|
getline(streamLine,tmpUserID,';'); //extracting the userID
|
|
|
|
userID=stoi(tmpUserID);
|
|
|
|
getline(streamLine,username,';'); //extracting the username
|
|
|
|
while(getline(streamLine,server,';')){ //extracting the server(s)
|
2024-08-06 10:37:15 +00:00
|
|
|
if(!server.empty()){
|
2024-08-05 14:32:18 +00:00
|
|
|
servers.push_back(server);
|
2024-08-06 10:37:15 +00:00
|
|
|
}
|
2024-08-05 14:32:18 +00:00
|
|
|
}
|
2024-08-05 16:06:54 +00:00
|
|
|
Service entry = Service(userID,username,servers);
|
2024-08-05 14:32:18 +00:00
|
|
|
result.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|