Procházet zdrojové kódy

:art: Separation client serveur

Arthur Brandao před 6 roky
rodič
revize
b880f28403
6 změnil soubory, kde provedl 446 přidání a 0 odebrání
  1. 5 0
      Serveur/README.md
  2. 9 0
      Serveur/boolean.h
  3. 42 0
      Serveur/json.h
  4. 274 0
      Serveur/json_parser.c
  5. 24 0
      Serveur/main.c
  6. 92 0
      Serveur/makefile

+ 5 - 0
Serveur/README.md

@@ -0,0 +1,5 @@
+# Projet Reseau Master 1 Artois
+
+Création d'un bomberman multijoueur (Bomberstudent)
+
+Par : Arthur Brandao, Maxence Bacquet

+ 9 - 0
Serveur/boolean.h

@@ -0,0 +1,9 @@
+#ifndef BOOLEAN_H
+#define BOOLEAN_H
+
+#define boolean int
+#define true 1
+#define false 0
+
+#endif /* BOOLEAN_H */
+

+ 42 - 0
Serveur/json.h

@@ -0,0 +1,42 @@
+/* 
+ * File:   json.h
+ * Author: Arthur Brandao
+ *
+ * Created on 28 octobre 2018, 17:53
+ */
+#ifndef JSON_H
+#define JSON_H
+
+/* --- Include --- */
+#include <string.h>
+#include "boolean.h"
+
+/* --- Constante --- */
+#define JSON_ERROR -1
+#define JSON_OK 0
+#define JSON_STRING 1
+#define JSON_NUMBER 2
+#define JSON_BOOLEAN 3
+#define JSON_ARRAY 4
+#define JSON_OBJECT 5
+
+/* --- Structure --- */
+typedef struct{
+    char* str; //La chaine de carac json
+    int elt; //Le nombre d'element
+    char** key; //La position des clef dans la chaine
+    char** val; //La position de la valeur 
+    int* key_length; //La taille des clefs
+    int* val_length; //La taille des valeurs
+    int* type; //Le type des valeurs
+}JsonParser;
+
+/* --- fonctions ---- */
+int json_parse(JsonParser*, char*);
+void clean_json_parser(JsonParser*);
+int get_type(JsonParser*, char*);
+char* get(JsonParser*, char*);
+char* get_index(JsonParser*, int);
+
+#endif /* JSON_H */
+

+ 274 - 0
Serveur/json_parser.c

@@ -0,0 +1,274 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "json.h"
+
+/* --- Fonctions privées --- */
+
+int skip_object(char* json) {
+    int compteur = 1;
+    json++;
+    //On compte le nombre caractere à sauter
+    while (*json && *json != '}') {
+        //Si on trouve un autre objet dans l'objet
+        if (*json == '{') {
+            skip_object(json);
+        }
+        compteur++;
+        json++;
+    }
+    return compteur;
+}
+
+int skip_array(char* json) {
+    int compteur = 1;
+    json++;
+    //On compte le nombre caractere à sauter
+    while (*json && *json != ']') {
+        json++;
+        compteur++;
+    }
+    return compteur;
+}
+
+int key_counter(char* json){
+    //Declaration variable
+    int compteur = 0;
+    boolean key = true;
+    //On parcours la chaine une 1er fois pour compter le nombre de clef
+    while (*json && *json != '}') {
+        //Si on trouve le debut d'une clef
+        if (*json == '"' && key) {
+            compteur++;
+            key = false;
+        }
+        //Si on tombe sur un autre objet json
+        else if (*json == '{' && !key) {
+            json += skip_object(json);
+        }            
+        //Si on tombe sur un tableau
+        else if (*json == '[' && !key) {
+            json += skip_array(json);
+        }            
+        //Si on trouve un separateur de valeur
+        else if (*json == ',' && !key) {
+            //Le prochain element est une clefS
+            key = true;
+        }
+        json++;
+    }
+    //Retour
+    return compteur;
+}
+
+int parse_key(JsonParser* this, int index, char* json) {
+    //Declaration variable
+    int compteur = 0;
+    //On parcours jusqu'a la fin de la clef
+    json++;
+    while (*json && *json != '"') {
+        json++;
+        compteur++;
+    }
+    //Si json mal formé
+    if (!*json) {
+        return JSON_ERROR;
+    }
+    //Setup les infos
+    this->key[index] = json - compteur;
+    this->key_length[index] = compteur;
+    //Sinon retourne ok
+    return ++compteur;
+}
+
+int parse_val(JsonParser* this, int index, char* json){
+    //Declaration variable
+    int compteur = 0;
+    //Regarde le type de valeur
+    switch(*json){
+        //String
+        case '"':
+            //Cherche le guillement de fin
+            json++;
+            while (*json && *json != '"') {
+                json++;
+                compteur++;
+            }
+            //Si json mal formé
+            if (!*json) {
+                return JSON_ERROR;
+            }
+            //Setup les infos
+            this->val[index] = json - compteur;
+            this->val_length[index] = compteur;
+            this->type[index] = JSON_STRING;
+            break;
+        //Boolean
+        case 't':
+            compteur = 4;
+            this->val[index] = json;
+            this->val_length[index] = compteur;
+            this->type[index] = JSON_BOOLEAN;
+            break;
+        case 'f':
+            compteur = 5;
+            this->val[index] = json;
+            this->val_length[index] = compteur;
+            this->type[index] = JSON_BOOLEAN;
+            break;
+        //Nombre
+        case '-':
+        case '0':
+        case '1':
+        case '2':
+        case '3':
+        case '4':
+        case '5':
+        case '6':
+        case '7':
+        case '8':
+        case '9':
+            //Cherche espace de fin ou fin json ou suite json
+            while (*json && *json != ' ' && *json != '}' && *json != ',') {
+                json++;
+                compteur++;
+            }
+            //Si json mal formé
+            if (!*json) {
+                return JSON_ERROR;
+            }
+            //Setup les infos
+            this->val[index] = json - compteur;
+            this->val_length[index] = compteur;
+            this->type[index] = JSON_NUMBER;
+            break;
+        //Tableau
+        case '[':
+            compteur = skip_array(json) + 1;
+            this->val[index] = json;
+            this->val_length[index] = compteur;
+            this->type[index] = JSON_ARRAY;
+            break;
+        //Objet
+        case '{':
+            compteur = skip_object(json) + 1;
+            this->val[index] = json;
+            this->val_length[index] = compteur;
+            this->type[index] = JSON_OBJECT;
+            break;
+        //Autre
+        default:
+            return JSON_ERROR;
+    }
+    //Retour
+    return ++compteur;
+}
+
+int parse_error(JsonParser* this){
+    clean_json_parser(this);
+    return JSON_ERROR;
+}
+
+int search_index(JsonParser* this, char* key){
+    int length = strlen(key);
+    //Cherche la clef
+    for(int i = 0; i < this->elt; i++){
+        if(strncmp(this->key[i], key, length) == 0){
+            return i;
+        }
+    }
+    //Si rien trouver
+    return JSON_ERROR;
+}
+
+/* --- Fonctions publiques --- */
+
+int json_parse(JsonParser* this, char* json) {
+    //Declaration variable
+    char* tmp;
+    int temp, compteur;
+    boolean key = true;
+    //Compte le nombre de clef
+    compteur = key_counter(json);
+    //Allocation de la taille des tableaux
+    this->str = malloc(strlen(json) * sizeof (char));
+    strcpy(this->str, json);
+    this->elt = compteur;
+    this->key = malloc(compteur * sizeof (char*));
+    this->key_length = malloc(compteur * sizeof (int));
+    this->val = malloc(compteur * sizeof (char*));
+    this->val_length = malloc(compteur * sizeof (int));
+    this->type = malloc(compteur * sizeof (int));
+    //On reparcours le tableau pour parser
+    tmp = this->str;
+    compteur = 0;
+    while (*tmp && *tmp != '}') {
+        //Si on trouve une clef
+        if (*tmp == '"') {
+            //Lecture clef de la clef
+            if((temp = parse_key(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
+            tmp += temp;
+            key = false;
+        }
+        //Si on trouve une valeur
+        else if(*tmp == ':'){
+            //Si pas de claf avant
+            if(key){ return parse_error(this); }
+            //Saute les espaces
+            tmp++;
+            while(*tmp == ' ') { tmp++; }
+            //Lecture valeur
+            if((temp = parse_val(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
+            tmp += temp;
+            key = true;
+            compteur++;
+        }
+        tmp++;
+    }
+    //Si on s'arrete sur une clef
+    if(!key){
+        return parse_error(this);
+    }
+    //Sinon ok
+    return JSON_OK;
+}
+
+int get_type(JsonParser* this, char* key){
+    //Recup index
+    int index;
+    if((index = search_index(this, key)) == JSON_ERROR){
+        return JSON_ERROR;
+    }
+    return this->type[index];
+}
+
+char* get(JsonParser* this, char* key){
+    //Recup index
+    int index;
+    if((index = search_index(this, key)) == JSON_ERROR){
+        return NULL;
+    }
+    return get_index(this, index);
+}
+
+char* get_index(JsonParser* this, int index){
+    //Index incorrect
+    if(index < 0 || index >= this->elt){
+        return NULL;
+    }
+    //Creation string d'accueil
+    char* val;
+    val = malloc(this->val_length[index] * sizeof(char));
+    //Copie valeur
+    strncpy(val, this->val[index], this->val_length[index]);
+    //Retour
+    return val;
+}
+
+void clean_json_parser(JsonParser* this) {
+    free(this->key);
+    free(this->key_length);
+    free(this->val);
+    free(this->val_length);
+    free(this->type);
+    free(this->str);
+}

+ 24 - 0
Serveur/main.c

@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "json.h"
+
+int main(){
+    char str[200];
+    char key[100];
+    char* val;
+    JsonParser json;
+    strcpy(str, "{\"name\" : \"Jack\", \"age\": 27, \"test\": true, \"tab\": [1, 2, 3, 4, 5], \"obj\": {\"name\" : \"Jack\", \"age\": 27}, \"age\": 27.8 }");
+    int a = json_parse(&json, str);
+    printf("\nParse : %d\n", a);
+    
+    for(int i = 0; i < json.elt; i++){
+        memset(key, 0, 100);
+        strncpy(key, json.key[i], json.key_length[i]);
+        val = get_index(&json, i);
+        printf("%s : %s\n", key, val);
+        //printf("%d %d\n", json.key_length[i], json.val_length[i]);
+    }
+    
+    clean_json_parser(&json);
+    return 0;
+}

+ 92 - 0
Serveur/makefile

@@ -0,0 +1,92 @@
+#
+# CONFIGURATION GENERALE
+#
+
+EXEC = main
+OBJETS = json_parser.o
+NOM_PROJET = Porjet Reseau
+
+#
+# SUFFIXES
+#
+
+.SUFFIXES: .c .o
+
+#
+# OBJETS
+#
+
+EXEC_O = $(EXEC:=.o)
+OBJETS_O = $(OBJETS) $(EXEC_O)
+
+#
+# ARGUMENTS ET COMPILATEUR
+#
+
+CC = gcc
+CCFLAGS_STD = -Wall -O3 -Werror -ansi -pedantic -std=c11
+CCFLAGS_DEBUG = -D _DEBUG_
+CCFLAGS = $(CCFLAGS_STD)
+CCLIBS = -lncurses
+
+#
+# REGLES
+#
+
+all: msg $(OBJETS) $(EXEC_O)
+	@echo "Creation des executables..."
+	@for i in $(EXEC); do \
+	$(CC) -o $$i $$i.o $(OBJETS) $(CCLIBS); \
+	done
+	@echo "Termine."
+
+msg:
+	@echo "Creation des objets..."
+
+debug: CCFLAGS = $(CCFLAGS_STD) $(CCFLAGS_DEBUG)
+debug: all
+
+#
+# REGLES PAR DEFAUT
+#
+
+.c.o: .h
+	@cd $(dir $<) && ${CC} ${CCFLAGS} -c $(notdir $<) -o $(notdir $@)
+
+#
+# REGLES GENERALES
+#
+
+clean:
+	@echo "Suppresion des objets, des fichiers temporaires..."
+	@rm -f $(OBJETS) $(EXEC_O)
+	@rm -f *~ *#
+	@rm -f $(EXEC)
+	@rm -f dependances
+	@echo "Termine."
+
+depend:
+	@echo "Creation des dependances..."
+	@sed -e "/^# DEPENDANCES/,$$ d" makefile > dependances
+	@echo "# DEPENDANCES" >> dependances
+	@for i in $(OBJETS_O); do \
+	$(CC) -MM -MT $$i $(CCFLAGS) `echo $$i | sed "s/\(.*\)\\.o$$/\1.c/"` >> dependances; \
+	done
+	@cat dependances > makefile
+	@rm dependances
+	@echo "Termine."
+
+#
+# CREATION ARCHIVE
+#
+
+ARCHIVE_FILES = *
+
+archive: clean
+	@echo "Creation de l'archive $(NOM_PROJET)$(shell date '+%y%m%d.tar.gz')..."
+	@REP=`basename "$$PWD"`; cd .. && tar zcf $(NOM_PROJET)$(shell date '+%y%m%d.tar.gz') $(addprefix "$$REP"/,$(ARCHIVE_FILES))
+	@echo "Termine."
+
+# DEPENDANCES
+json_parser.o: json_parser.c json.h boolean.h
+main.o: main.c json.h boolean.h