bomberstudent_server.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * File: bomberstudent_server.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 14 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10. #include <signal.h>
  11. #include "arraylist.h"
  12. #include "bomberstudent_server.h"
  13. #include "error.h"
  14. #include "handler.h"
  15. /* --- Extern --- */
  16. extern int serrno;
  17. /* --- Globale --- */
  18. arraylist get;
  19. arraylist post;
  20. int error_code[] = {
  21. 520,
  22. 400,
  23. 403
  24. };
  25. char* error_message[] = {
  26. "Unknown error",
  27. "Bad Request",
  28. "Forbidden action"
  29. };
  30. /* --- Fonctions privées --- */
  31. /**
  32. * Thread de gestion d'un client
  33. * @param data
  34. * @return
  35. */
  36. void* client_thread(void* data) {
  37. int* tmp, cliId, nbError = 0;
  38. Client* cli;
  39. //Recup la valeur de data
  40. tmp = (int*) data;
  41. cliId = *tmp;
  42. //Detache le thread
  43. if (pthread_detach(pthread_self()) != 0) {
  44. adderror("Impossible de détacher le thread Client");
  45. return NULL;
  46. }
  47. //Recup le client
  48. cli = get_client(cliId);
  49. if (cli == NULL) {
  50. adderror("Le client n'existe pas");
  51. return NULL;
  52. }
  53. printf("Connexion du client %d\n", cliId);
  54. //Attente requete du client
  55. while (true) {
  56. if (!receive_client(cli)) {
  57. adderror("Erreur lors de la reception d'une requete du client");
  58. nbError++;
  59. } else {
  60. nbError = 0;
  61. }
  62. //Si trop d'erreur d'affilé
  63. if (nbError == NBERRORRESET) {
  64. //Fermeture du client et fin du thread
  65. printf("Connexion perdu avec le client %d\n", cliId);
  66. adderror("Client non accessible, fin de la connexion");
  67. handler_client_end(cliId, NULL);
  68. break;
  69. }
  70. //Regarde si le client existe toujours
  71. if (get_client(cliId) == NULL) {
  72. break;
  73. }
  74. }
  75. return NULL;
  76. }
  77. /**
  78. * Thread de gestion des connexions UDP
  79. * @param data
  80. * @return
  81. */
  82. void* udp_thread(void* data) {
  83. Server s;
  84. char buffer[BUFFER_SIZE];
  85. //Cast data en serveur
  86. s = (Server) data;
  87. //Detache le thread
  88. if (pthread_detach(pthread_self()) != 0) {
  89. adderror("Impossible de détacher le thread UDP");
  90. return NULL;
  91. }
  92. //Boucle attente
  93. while (true) {
  94. //Attente connexion
  95. if (s->server_receive(s, buffer, BUFFER_SIZE) == ERR) {
  96. addserror("Impossible de recevoir le demande de recherche");
  97. continue;
  98. }
  99. //Si la phrase est correcte
  100. if (strncmp(buffer, SEARCH_SERV, strlen(buffer)) == 0) {
  101. if (!s->server_send(s, "i'm a bomberstudent server")) {
  102. addserror("Impossible de repondre à la recherche");
  103. }
  104. }
  105. }
  106. return NULL;
  107. }
  108. /**
  109. * Thread de gestion des connexions TCP
  110. * @param data
  111. * @return
  112. */
  113. void* tcp_thread(void* data) {
  114. Server* s;
  115. boolean res;
  116. int cliId;
  117. pthread_t client;
  118. //Cast data en serveur
  119. s = (Server*) data;
  120. //Detache le thread
  121. if (pthread_detach(pthread_self()) != 0) {
  122. adderror("Impossible de détacher le thread TCP");
  123. return NULL;
  124. }
  125. //Boucle attente
  126. while (true) {
  127. res = s[0]->server_accept(s[0]);
  128. res = res && s[1]->server_accept(s[1]);
  129. if (!res) {
  130. addserror("Impossible d'etablir la connexion TCP");
  131. continue;
  132. }
  133. //Creation d'un client et de son thread
  134. cliId = add_client(s[0], s[1]);
  135. if (pthread_create(&client, NULL, client_thread, &cliId) != 0) {
  136. adderror("Impossible de créer le thread Client");
  137. /*ToDo Avertir Client du probleme*/
  138. }
  139. }
  140. return NULL;
  141. }
  142. /* --- Fonctions publiques --- */
  143. void ini_server() {
  144. arraylist_ini(&get);
  145. arraylist_ini(&post);
  146. }
  147. void add_handler(char* method, char* ressource, int(*handler)(int, JsonParser*)) {
  148. if (strncmp(method, "POST", 5) == 0) {
  149. arraylist_add(&post, ressource, handler);
  150. } else if (strncmp(method, "GET", 4) == 0) {
  151. arraylist_add(&get, ressource, handler);
  152. }
  153. }
  154. boolean launch_udp_server(int port) {
  155. Server s;
  156. pthread_t udp;
  157. //Creation serveur
  158. s = server_create_udp();
  159. if (s == NULL) {
  160. addserror("Impossible de créer le serveur UDP");
  161. return false;
  162. }
  163. if (!s->server_bind(s, port)) {
  164. addserror("Impossible de bind le serveur UDP");
  165. return false;
  166. }
  167. //Lancement thread serveur udp
  168. if (pthread_create(&udp, NULL, udp_thread, s) != 0) {
  169. adderror("Impossible de créer le thread UDP");
  170. server_close(s);
  171. return false;
  172. }
  173. return true;
  174. }
  175. boolean launch_tcp_server(int port) {
  176. Server* s;
  177. pthread_t tcp;
  178. //Creation serveur
  179. s = malloc(sizeof (Server) * 2);
  180. s[0] = server_create_tcp();
  181. s[1] = server_create_tcp();
  182. if (s[0] == NULL || s[1] == NULL) {
  183. addserror("Impossible de créer les serveurs TCP");
  184. return false;
  185. }
  186. if (!(s[0]->server_bind(s[0], port) && s[1]->server_bind(s[1], port + 1))) {
  187. addserror("Impossible de bind les serveurs TCP");
  188. return false;
  189. }
  190. //Lancement Thread attente connexion TCP
  191. if (pthread_create(&tcp, NULL, tcp_thread, s) != 0) {
  192. adderror("Impossible de créer le thread TCP");
  193. server_close(s[0]);
  194. server_close(s[1]);
  195. return false;
  196. }
  197. return true;
  198. }
  199. boolean receive_client(Client* cli) {
  200. char buffer[BUFFER_SIZE];
  201. char* reader, * ressource, * json = NULL;
  202. int method, pos, compteur = 0;
  203. JsonParser* jp = NULL;
  204. arraylist* al = &get;
  205. //Attente reception
  206. if (cli->main->server_receive(cli->main, buffer, BUFFER_SIZE) == ERR) {
  207. //Si la conneion est coupée
  208. if (serrno == SEABORT) {
  209. //On ferme le client
  210. printf("Connexion perdu avec le client %d\n", cli->id);
  211. handler_client_end(cli->id, NULL);
  212. }
  213. addserror("Impossible de recevoire les données du client");
  214. return false;
  215. }
  216. //Recup la methode
  217. if (buffer[0] == 'P') {
  218. pos = 5;
  219. reader = buffer + pos;
  220. method = POST;
  221. } else if (buffer[0] == 'G') {
  222. pos = 4;
  223. reader = buffer + pos;
  224. method = GET;
  225. } else {
  226. //Methode incorrect
  227. adderror("Methode incorrect");
  228. send_err_client(cli->id, EREQUEST);
  229. return false;
  230. }
  231. //Recup la ressource
  232. while (buffer[pos] != '\n' && buffer[pos] != '\0') {
  233. compteur++;
  234. pos++;
  235. }
  236. if (compteur == 0) {
  237. adderror("Aucune ressource demandée");
  238. send_err_client(cli->id, EREQUEST);
  239. return false;
  240. }
  241. ressource = malloc(sizeof (char) * (compteur + 1));
  242. memset(ressource, 0, compteur + 1);
  243. strncpy(ressource, reader, compteur);
  244. //Recup param JSON
  245. if (method == POST) {
  246. //Si un parametre est present
  247. if (buffer[pos] != '\0') {
  248. json = reader + compteur + 1;
  249. jp = malloc(sizeof (JsonParser));
  250. if (json_parse(jp, json) != JSON_OK) {
  251. adderror("Impossible de parser le JSON");
  252. send_err_client(cli->id, EREQUEST);
  253. return false;
  254. }
  255. }
  256. //Change la liste à utiliser
  257. al = &post;
  258. }
  259. //Appel le callback
  260. if (arraylist_call(al, ressource, cli->id, jp) == ERR) {
  261. adderror("Impossible d'executer le callback");
  262. send_err_client(cli->id, EREQUEST);
  263. return false;
  264. }
  265. //Nettoyage
  266. free(ressource);
  267. if (jp != NULL) {
  268. free(jp);
  269. }
  270. return true;
  271. }
  272. boolean send_client(int cliId, JsonEncoder* je) {
  273. Client* cli;
  274. char* answer, * msg;
  275. int length;
  276. //Recup client
  277. cli = get_client(cliId);
  278. if (cli == NULL) {
  279. adderror("Le client n'existe pas");
  280. return false;
  281. }
  282. //Preparation message
  283. answer = json_encode(je);
  284. length = strlen(answer) + 2;
  285. msg = malloc(sizeof (char) + length);
  286. memset(msg, 0, length);
  287. snprintf(msg, length, "%s\n", answer);
  288. //Envoi la reponse
  289. if (!cli->main->server_send(cli->main, msg)) {
  290. //Si la conneion est coupée
  291. if (serrno == SEABORT) {
  292. //On ferme le client
  293. printf("Connexion perdu avec le client %d\n", cliId);
  294. handler_client_end(cliId, NULL);
  295. }
  296. addserror("Impossible de repondre à la requete du client");
  297. free(answer);
  298. free(msg);
  299. return false;
  300. }
  301. //Nettoyage
  302. free(answer);
  303. free(msg);
  304. return true;
  305. }
  306. boolean send_err_client(int cliId, int error) {
  307. JsonEncoder je;
  308. //Creation JSON
  309. ini_encoder(&je);
  310. add_integer(&je, "status", error_code[error]);
  311. add_string(&je, "message", error_message[error]);
  312. //Envoi
  313. if (!send_client(cliId, &je)) {
  314. adderror("Impossible d'avertir le client de l'erreur");
  315. return false;
  316. }
  317. return true;
  318. }
  319. boolean notify_client(Client* cli, char* method, char* ressource, JsonEncoder* param) {
  320. int length;
  321. char* answer, * msg;
  322. //Creation message
  323. answer = json_encode(param);
  324. length = strlen(method) + 1 + strlen(ressource) + 1 + strlen(answer) + 2;
  325. msg = malloc(sizeof (char) +length);
  326. memset(msg, 0, length);
  327. sprintf(msg, "%s %s\n%s\n", method, ressource, answer);
  328. //Envoi la reponse
  329. if (!cli->notify->server_send(cli->notify, msg)) {
  330. adderror("Impossible de notifier le client");
  331. free(answer);
  332. free(msg);
  333. return false;
  334. }
  335. //Nettoyage
  336. free(answer);
  337. free(msg);
  338. return true;
  339. }
  340. boolean notify_all(char* method, char* ressource, JsonEncoder* param) {
  341. Client* cli;
  342. boolean res = true;
  343. //Parcours tous les clients
  344. int nbClient = get_number_client();
  345. for (int i = 0; i < nbClient; i++) {
  346. cli = get_client(i);
  347. //Si le client existe toujours
  348. if (cli == NULL) {
  349. continue;
  350. }
  351. //Lui envoi le message
  352. res = res && notify_client(cli, method, ressource, param);
  353. }
  354. return res;
  355. }