You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.0 KiB
133 lines
3.0 KiB
//
|
|
// main.cpp
|
|
// Created on 21/10/2018
|
|
//
|
|
|
|
#include <iostream>
|
|
#include "Base.h"
|
|
#include "Video.h"
|
|
#include "Photo.h"
|
|
#include "Film.h"
|
|
#include "Groupe.h"
|
|
#include "Gestion.h"
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <map>
|
|
#include <sstream>
|
|
#include "tcpserver.h"
|
|
|
|
const int PORT = 3331;
|
|
|
|
/**
|
|
* @brief fichier principal
|
|
*/
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
|
|
/**
|
|
* @e map utilisé pour associer une commande à son action
|
|
*/
|
|
|
|
std::map<std::string,int> comm;
|
|
comm["afficher"] = 1;
|
|
comm["jouer"] = 2;
|
|
comm["supprimer"] = 3;
|
|
comm["lister"] = 4;
|
|
|
|
/**
|
|
* éléments utilisés pour des tests
|
|
*/
|
|
|
|
Gestion *matable = new Gestion();
|
|
GroupePtr g = matable->creerGroupe("myGrpe");
|
|
VideoPtr v = matable->creerVideo("vid", "~/Vidéos/OPS.mp4");
|
|
VideoPtr v2 = matable->creerVideo("vid2", "~/Vidéos/OPS.mp4");
|
|
g->push_back(v);
|
|
g->push_back(v2);
|
|
|
|
/**
|
|
* partie TCPserver
|
|
*/
|
|
|
|
auto* server =
|
|
new TCPServer( [&](std::string const& request, std::string& response) {
|
|
|
|
// the request sent by the client to the server
|
|
std::cout << "request: " << request << std::endl;
|
|
|
|
std::stringstream ss;
|
|
std::stringstream resp;
|
|
|
|
ss << request;
|
|
|
|
std::string command, name;
|
|
|
|
ss >> command >> name;
|
|
|
|
/**
|
|
* se référer à la documentation de @e Gestion pour les commandes disponibles
|
|
*/
|
|
|
|
switch (comm[command]) {
|
|
case 1:
|
|
response = "commande choisie : afficher;";
|
|
if (name != "") {
|
|
matable->afficher(name,resp);
|
|
|
|
while (!resp.eof()) {
|
|
std::string buff;
|
|
resp >> buff;
|
|
response += " " + buff;
|
|
}
|
|
|
|
// response += " " + resp.str();
|
|
}
|
|
else response = "Veuillez entrer un nom après la commande 'afficher'";
|
|
break;
|
|
case 2:
|
|
response = "commande choisie : jouer;";
|
|
if (name != "") matable->jouer(name);
|
|
else response = "Veuillez entrer un nom après la commande 'jouer'";
|
|
break;
|
|
case 3:
|
|
response = "commande choisie : supprimer;";
|
|
if (name != "") matable->supprimer(name);
|
|
else response = "Veuillez entrer un nom après la commande 'supprimer'";
|
|
break;
|
|
case 4:
|
|
response = "commande choisie : lister;";
|
|
matable->lister(resp);
|
|
while (!resp.eof()) {
|
|
std::string buff;
|
|
resp >> buff;
|
|
response += " " + buff;
|
|
}
|
|
break;
|
|
default:
|
|
response = "Les commandes suivantes sont disponibles : 'afficher', 'jouer', 'supprimer' et 'lister'";
|
|
}
|
|
// the response that the server sends back to the client
|
|
//response = "RECEIVED: " + a + "AND: " + b;
|
|
|
|
// return false would close the connecytion with the client
|
|
return true;
|
|
});
|
|
|
|
|
|
// lance la boucle infinie du serveur
|
|
std::cout << "Starting Server on port " << PORT << std::endl;
|
|
|
|
int status = server->run(PORT);
|
|
|
|
// en cas d'erreur
|
|
if (status < 0) {
|
|
std::cerr << "Could not start Server on port " << PORT << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|