소스 검색

:tada: Ajout fichier delay (remplace timer)

Arthur Brandao 6 년 전
부모
커밋
62626a8913
5개의 변경된 파일89개의 추가작업 그리고 55개의 파일을 삭제
  1. 51 0
      Serveur/delay.c
  2. 33 0
      Serveur/delay.h
  3. 0 44
      Serveur/handler.c
  4. 0 8
      Serveur/handler.h
  5. 5 3
      Serveur/makefile

+ 51 - 0
Serveur/delay.c

@@ -0,0 +1,51 @@
+/* 
+ * File:   delay.c
+ * Author: Arthur Brandao
+ *
+ * Created on 4 décembre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
+#include "error.h"
+#include "delay.h"
+
+/* --- Fonctions privées --- */
+/**
+ * Thread d'attente de la fonction timer
+ * @param void* La structure timer_data cast en void*
+ * @return NULL
+ */
+void* delay_thread(void* data){
+    delay_t* d;
+    //Recup données
+    d = (delay_t*) data;
+    //Detache le thread
+    if (pthread_detach(pthread_self()) != 0) {
+        adderror("Impossible de détacher le thread Delay");
+        return NULL;
+    }
+    //Attend le temps indiqué
+    sleep(d->second);
+    //Appel callback
+    if(d->callback(&game[d->game], d->player) != SUCCESS){
+        adderror("Erreur callback Delay");
+    }
+    return NULL;
+}
+
+/* --- Fonctions publiques --- */
+void delay(int second, int game, int player, int(*callback)(Game*, int)){
+    pthread_t thread;
+    delay_t* d = malloc(sizeof(timer_t));
+    d->second = second;
+    d->game = game;
+    d->player = player;
+    d->callback = callback;
+    //Creation thread
+    if (pthread_create(&thread, NULL, delay_thread, d) != 0) {
+        adderror("Impossible de créer le thread Delay");
+    }
+}

+ 33 - 0
Serveur/delay.h

@@ -0,0 +1,33 @@
+/* 
+ * File:   delay.h
+ * Author: Arthur Brandao
+ *
+ * Created on 4 décembre 2018
+ */
+
+#ifndef DELAY_H
+#define DELAY_H
+
+/* --- Include --- */
+#include "constante.h"
+#include "game.h"
+
+/* --- Structure --- */
+typedef struct{
+    int second; //Seconde à attendre
+    int game; //Index de la game dans le tableau
+    int player; //Index du joueur dans le tableau de game
+    int(*callback)(Game*, int);
+}delay_t;
+
+/**
+ * Attend X secondes (sans bloquer l'execution) avant d'executer le callback
+ * @param int Nombre de second en attente
+ * @param int Index de la game
+ * @param int Index du joueur dans la game
+ * @param int(*)(Game*, int) Le callback
+ */
+void delay(int, int, int, int(*)(Game*, int));
+
+#endif /* DELAY_H */
+

+ 0 - 44
Serveur/handler.c

@@ -8,7 +8,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <pthread.h>
 #include "error.h"
 #include "bomberstudent_server.h"
 #include "client.h"
@@ -20,49 +19,6 @@ extern Game game[MAXGAME];
 extern int nbGame;
 
 /* --- Fonctions privées --- */
-/**
- * Thread d'attente de la fonction timer
- * @param void* La structure timer_data cast en void*
- * @return NULL
- */
-void* timer_thread(void* data){
-    timer_data* t;
-    //Recup données
-    t = (timer_data*) data;
-    //Detache le thread
-    if (pthread_detach(pthread_self()) != 0) {
-        adderror("Impossible de détacher le thread Timer");
-        return NULL;
-    }
-    //Attend le temps indiqué
-    sleep(t->second);
-    //Appel callback
-    if(t->callback(&game[t->game], t->player) != SUCCESS){
-        adderror("Erreur callback Timer");
-    }
-    return NULL;
-}
-
-/**
- * Attend X second (sans bloquer l'execution) avant d'executer le callback
- * @param int Nombre de second en attente
- * @param int Index de la game
- * @param int Index du joueur dans la game
- * @param int(*)(Game*, int) Le callback
- */
-void timer(int second, int game, int player, int(*callback)(Game*, int)){
-    pthread_t thread;
-    timer_data* t = malloc(sizeof(timer_t));
-    t->second = second;
-    t->game = game;
-    t->player = player;
-    t->callback = callback;
-    //Creation thread
-    if (pthread_create(&thread, NULL, timer_thread, t) != 0) {
-        adderror("Impossible de créer le thread Timer");
-    }
-}
-
 /**
  * Cherche dans quel partie est un client
  * @param int L'id du client

+ 0 - 8
Serveur/handler.h

@@ -13,14 +13,6 @@
 #include "json.h"
 #include "game.h"
 
-/* --- Structure --- */
-typedef struct{
-    int second; //Seconde à attendre
-    int game; //Index de la game dans le tableau
-    int player; //Index du joueur dans le tableau de game
-    int(*callback)(Game*, int);
-}timer_data;
-
 /* --- Fonctions --- */
 /**
  * Initialise les handlers

+ 5 - 3
Serveur/makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = main test
-OBJETS = str.o json_parser.o json_encoder.o json_array.o error.o arraylist.o server_tcp.o server_udp.o bomberstudent_server.o client.o file.o handler.o player.o game.o
+OBJETS = str.o json_parser.o json_encoder.o json_array.o error.o arraylist.o server_tcp.o server_udp.o bomberstudent_server.o client.o file.o handler.o player.o game.o delay.o
 NOM_PROJET = Projet Reseau
 
 #
@@ -107,10 +107,12 @@ bomberstudent_server.o: bomberstudent_server.c arraylist.h constante.h \
 client.o: client.c client.h constante.h server.h
 file.o: file.c error.h str.h file.h constante.h
 handler.o: handler.c error.h bomberstudent_server.h constante.h server.h \
- json.h str.h client.h game.h player.h handler.h
-player.o: player.c player.h constante.h client.h server.h
+ json.h str.h client.h player.h handler.h
+player.o: player.c player.h constante.h client.h server.h json.h str.h
 game.o: game.c error.h str.h file.h constante.h game.h player.h client.h \
  server.h json.h
+delay.o: delay.c error.h delay.h constante.h game.h player.h client.h \
+ server.h json.h str.h
 main.o: main.c error.h bomberstudent_server.h constante.h server.h json.h \
  str.h client.h main.h handler.h game.h player.h
 test.o: test.c json.h str.h constante.h arraylist.h server.h error.h \