Quellcode durchsuchen

WIP BomberStudent Server

Arthur Brandao vor 6 Jahren
Ursprung
Commit
6eeb8a181b
6 geänderte Dateien mit 59 neuen und 11 gelöschten Zeilen
  1. 3 3
      Serveur/arraylist.c
  2. 1 1
      Serveur/arraylist.h
  3. 44 2
      Serveur/bomberstudent_server.c
  4. 6 0
      Serveur/bomberstudent_server.h
  5. 2 2
      Serveur/main.c
  6. 3 3
      Serveur/makefile

+ 3 - 3
Serveur/arraylist.c

@@ -17,7 +17,7 @@ void arraylist_ini(arraylist* al){
     al->size = 0;
 }
 
-al_node* arraylist_add(arraylist* al, char* key, int(*handler)(char*)){
+al_node* arraylist_add(arraylist* al, char* key, int(*handler)(Server, JsonParser*)){
     int length;
     al_node* aln;
     //Regarde si la clef existe
@@ -63,12 +63,12 @@ al_node* arraylist_search(arraylist* al, char* key){
     return NULL;
 }
 
-int arraylist_call(arraylist* al, char* key, char* arg){
+int arraylist_call(arraylist* al, char* key, Server serv, JsonParser* arg){
     al_node* aln = arraylist_search(al, key);
     if(aln == NULL){
         return -1;
     }
-    return aln->handler(arg);
+    return aln->handler(serv, arg);
 }
 
 void arraylist_delete(arraylist* al, al_node* aln){

+ 1 - 1
Serveur/arraylist.h

@@ -59,7 +59,7 @@ al_node* arraylist_search(arraylist*, char*);
  * @param char* Argument pour la fonction
  * @return int Resultat de la fonction ou -1 si introuvable
  */
-int arraylist_call(arraylist*, char*, char*);
+int arraylist_call(arraylist*, char*, Server, JsonParser*);
 
 /**
  * Supprime un noeud

+ 44 - 2
Serveur/bomberstudent_server.c

@@ -10,17 +10,59 @@
 #include <pthread.h>
 #include "arraylist.h"
 #include "bomberstudent_server.h"
+#include "error.h"
 
 /* --- Globale --- */
 arraylist get;
 arraylist post;
 
+/* --- Fonctions privées --- */
+void* udp_thread(void* data){
+    /*Server s;
+    //Cast data en serveur
+    s = (Server) data;
+    //Detache le thread
+    if(pthread_detach(pthread_self()) != 0){
+        return NULL;
+    }*/
+    return NULL;
+}
+
+
 /* --- Fonctions publiques --- */
 void ini_server(){
     arraylist_ini(&get);
     arraylist_ini(&post);
 }
 
+void add_handler(char* method, char* ressource, int(*handler)(Server, JsonParser*)){
+    if(strncmp(method, "POST", 5) == 0){
+        arraylist_add(&post, ressource, handler);
+    } else if(strncmp(method, "POST", 5) == 0){
+        arraylist_add(&get, ressource, handler);
+    }
+}
+
 boolean launch_udp_server(int port){
-    return false;
-}
+    Server s;
+    boolean res;
+    pthread_t udp;
+    //Creation serveur
+    s = server_create_udp();
+    res = s->server_bind(s, port);
+    if(!res){
+        return false;
+    }
+    //Lancement thread serveur udp
+    if(pthread_create(&udp, NULL, udp_thread, s) != 0){
+        adderror("Impossible de créer le thread UDP");
+        server_close(s);
+        return false;
+    }
+    return true;
+}
+
+boolean launch_tcp_server(int);
+boolean receive_client(Server);
+boolean send_client(Server, JsonEncoder*);
+boolean notify_client(Server, char*, char*, JsonEncoder*);

+ 6 - 0
Serveur/bomberstudent_server.h

@@ -11,6 +11,7 @@
 /* --- Include --- */
 #include "constante.h"
 #include "server.h"
+#include "json.h"
 
 /* --- Constantes --- */
 #define PORT_UDP 18624
@@ -18,7 +19,12 @@
 
 /* --- Fonctions --- */
 void ini_server();
+void add_handler(char*, char*, int(*)(Server, JsonParser*));
 boolean launch_udp_server(int);
+boolean launch_tcp_server(int);
+boolean receive_client(Server);
+boolean send_client(Server, JsonEncoder*);
+boolean notify_client(Server, char*, char*, JsonEncoder*);
 
 #endif /* BOMBERSTUDENT_SERVER_H */
 

+ 2 - 2
Serveur/main.c

@@ -101,14 +101,14 @@ int tion(char* arg){
 }
 
 int array_list(){
-    arraylist al;
+    /*arraylist al;
     arraylist_ini(&al);
     arraylist_add(&al, "func1", func);
     arraylist_add(&al, "func2", tion);
     
     printf("%d\n", arraylist_call(&al, "func2", "Pouet"));
     printf("%d\n", arraylist_call(&al, "func1", "Loul"));
-    printf("%d\n", arraylist_call(&al, "func3", "Nop"));
+    printf("%d\n", arraylist_call(&al, "func3", "Nop"));*/
     
     return 0;
 }

+ 3 - 3
Serveur/makefile

@@ -98,9 +98,9 @@ str.o: str.c str.h
 json_parser.o: json_parser.c json.h str.h constante.h
 json_encoder.o: json_encoder.c json.h str.h constante.h
 error.o: error.c str.h error.h
-arraylist.o: arraylist.c str.h arraylist.h constante.h
+arraylist.o: arraylist.c str.h arraylist.h constante.h server.h json.h
 server_tcp.o: server_tcp.c error.h server.h constante.h
 server_udp.o: server_udp.c error.h server.h constante.h
-bomberstudent_server.o: bomberstudent_server.c server.h constante.h \
- bomberstudent_server.h
+bomberstudent_server.o: bomberstudent_server.c arraylist.h constante.h \
+ server.h json.h str.h bomberstudent_server.h error.h
 main.o: main.c json.h str.h constante.h arraylist.h server.h error.h