123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "json.h"
- #include "arraylist.h"
- #include "server.h"
- #include "error.h"
- #include "bomberstudent_server.h"
- #include "client.h"
- #include "file.h"
- #include "game.h"
- /* --- Extern --- */
- extern Error error;
- int parse(){
- char str[200];
- char* key = NULL;
- char* val = NULL;
- JsonParser json;
- strcpy(str, "{\"name\" : \"Jack\", \"age\": 27, \"test\": true, \"tab\": [1, 2, 3, 4, 5], \"obj\": {\"name\" : \"Jack\", \"age\": 27}, \"nb\": 27.8 }");
- //Parse
- int a = json_parse(&json, str);
- printf("Parse : %d\n", a);
- //Affiche toutes les clefs : valeurs
- for(int i = 0; i < json.elt; i++){
- key = key_index(&json, i);
- val = get_index(&json, i);
- printf("%s : %s\n", key, val);
- }
- //Recup un nombre
- printf("Double : %f %.2f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
- //Recup boolean
- printf("Bool : %d %d\n", get_boolean(&json, "test"), get_boolean(&json, "tab"));
- //Recup obj
- JsonParser* js;
- js = get_object(&json, "obj");
- if(js != NULL){
- int key_i = get_pos(js, "name");
- printf("JSON : %s %s, age %d\n", key_index(js, key_i), get_value(js, "name"), get_integer(js, "age"));
- } else {
- printf("JSON : Error");
- }
- //Supprime
- free(key);
- free(val);
- clean_json_parser(&json);
- return 0;
- }
- int encode(){
- //Encode
- JsonEncoder json;
- ini_encoder(&json);
- add_string(&json, "name", "robert");
- add_number(&json, "nb", 25.698, 2);
- add_integer(&json, "int", 846);
- add_string(&json, "aze", "rty");
- add_boolean(&json, "bool", false);
- add_value(&json, "\"test\": \"aze\nrty\"");
- printf("Json\n");
- printf("%s\n", json_encode(&json));
-
- //Encode un JsonEncoder
- JsonEncoder json2;
- ini_encoder(&json2);
- add_integer(&json2, "vie", 42);
- add_object(&json2, "obj", &json);
- printf("\nJson 2\n");
- printf("%s\n", json_encode(&json2));
-
- //Decode
- JsonParser parser, *parser2;
- char* key, *val;
- json_parse(&parser, json_encode(&json2));
- //Affiche toutes les clefs : valeurs
- printf("\nParser\n");
- for(int i = 0; i < parser.elt; i++){
- key = key_index(&parser, i);
- val = get_index(&parser, i);
- printf("%s : %s\n", key, val);
- }
- //Lecture du sous json
- parser2 = get_object(&parser, "obj");
- //Affiche toutes les clefs : valeurs
- printf("\nParser 2\n");
- for(int i = 0; i < parser2->elt; i++){
- key = key_index(parser2, i);
- val = get_index(parser2, i);
- printf("%s : %s\n", key, val);
- }
-
- //Clean
- clean_json_encoder(&json);
- clean_json_encoder(&json2);
- clean_json_parser(&parser);
-
- return 0;
- }
- int serv(){
- char str[BUFFER_SIZE];
-
- Server s = server_create_tcp();
- s->server_bind(s, 8080);
- s->server_accept(s);
- s->server_receive(s, str, BUFFER_SIZE);
- printf("%s\n", str);
- server_close_client(s);
- server_close(s);
-
- s = server_create_udp();
- s->server_bind(s, 8080);
- s->server_receive(s, str, BUFFER_SIZE);
- printf("%s\n", str);
- server_close(s);
-
- return 0;
- }
- int serv_udp(){
- char str[BUFFER_SIZE];
- Server s = server_create_udp();
- s->server_bind(s, 18624);
- s->server_receive(s, str, BUFFER_SIZE);
- s->server_send(s, "i'm a bomberstudent server");
- printf("%s\n", str);
- server_close(s);
-
- return 0;
- }
- int serv_tcp(){
- char str[BUFFER_SIZE];
- Server s = server_create_tcp();
- s->server_bind(s, 18642);
- s->server_accept(s);
- s->server_receive(s, str, BUFFER_SIZE);
- printf("%s\n", str);
- s->server_send(s, "Hello");
- server_close_client(s);
- server_close(s);
-
- return 0;
- }
- int handler(int cliId, JsonParser* jp){
- printf("Handler : %d - %s\n", cliId, get_value(jp, "key"));
- JsonEncoder je;
- ini_encoder(&je);
- add_integer(&je, "code", 200);
- add_string(&je, "etat", "ok");
- send_client(cliId, &je);
- return 1;
- }
- void notify(){
- Client* cli = get_client(0);
- JsonEncoder je;
- ini_encoder(&je);
- add_string(&je, "etat", "ok");
- add_integer(&je, "value", 42);
- notify_client(cli, "POST", "test", &je);
- }
- int files(){
- char** result = NULL, * content = NULL;
- char path[500];
- int length;
- result = file_list("map", &length);
- if(length == ERR){
- return 0;
- }
- for(int i = 0; i < length; i++){
- printf("Name : %s\n", result[i]);
- sprintf(path, "map/%s", result[i]);
- content = file_get_content(path);
- if(content != NULL){
- printf("Content :\n%s\n", content);
- free(content);
- }
- free(result[i]);
- }
- free(result);
- return 1;
- }
- int array_encode(){
- JsonArray ja;
- ini_array_encoder(&ja);
- add_array_number(&ja, 3, 4.5896);
- add_array_string(&ja, "String");
- add_array_boolean(&ja, 1);
- add_array_array(&ja, &ja);
-
- JsonEncoder je;
- ini_encoder(&je);
- add_string(&je, "clef", "val");
- add_boolean(&je, "bool", 0);
- add_object(&je, "self", &je);
-
- add_array_object(&ja, &je);
- add_array(&je, "tab", &ja);
-
- printf("%s\n", json_encode(&je));
-
- clean_json_array(&ja);
- clean_json_encoder(&je);
- return 1;
- }
- int array_parse(){
- // "[4.000, \"String\", true, [4.000, \"String\", true, [1, 2, 3]]"
-
- JsonArray ja;
- ini_array_parser(&ja);
- int a = json_parse_array(&ja, "[4.000, \"String\", true, [4.000, \"String\", true, [1, 2, 3]], {\"aze\": \"rty\", \"tab\": [1, 2, 3]}]");
- printf("Res : %d\n", a);
-
- printf("%f\n", get_array_number(&ja, 0));
- printf("%s\n", get_array_string(&ja, 1));
- printf("%d\n", get_array_boolean(&ja, 2));
- printf("%s\n", get_array_value(&ja, 3));
-
- JsonParser* jp = get_array_object(&ja, 4);
- printf("%s\n", get_string(jp, "aze"));
-
- JsonArray* ja2 = get_array(jp, "tab");
- printf("%s\n", get_array_value(ja2, 1));
-
- return 1;
- }
- int size(){
- char* content;
- int* size;
- //char** map;
-
- content = file_get_content("map/map1");
- size = map_size(content);
- printf("Width : %d\n", size[WIDTH]);
- printf("Height : %d\n", size[HEIGHT]);
-
- /*map = parse_map(content, size[WIDTH], size[HEIGHT]);
- printf("[1, 2] %c\n", map[1][2]);
- for(int i = 0; i < size[HEIGHT]; i++){
- for(int j = 0; j < size[WIDTH]; j++){
- printf("%c", map[j][i]);
- }
-
- printf("\n");
- }
-
- for(int i = 0; i < size[WIDTH]; i++){
- free(map[i]);
- }
- free(map);*/
- free(content);
- free(size);
-
- return 1;
- }
- /*boolean parse_pos(char* pos, int* x, int* y){
- int index = 0;
- char* cy, * cx;
- while(pos[index] != '\0' && pos[index] != ','){
- index++;
- }
- if(pos[index] == '\0'){
- return false;
- }
- cx = malloc(sizeof(char) * index + 1);
- memset(cx, 0, index + 1);
- strncpy(cx, pos, index);
- cy = pos + index + 1;
- *x = atoi(cx);
- *y = atoi(cy);
- free(cx);
- return true;
- }*/
- int main(){
- //return parse();
- //return encode();
- //return serv();
- //return serv_udp();
- //return serv_tcp();
-
- /*
- error_finit("bomberstudent_server.log");
- ini_server();
- add_handler("POST", "aze/rty", handler);
- if(!launch_udp_server(PORT_UDP)){
- error.print("Impossible de démarrer le serveur");
- error.exit_err();
- }
- if(!launch_tcp_server(PORT_TCP)){
- error.print("Impossible de démarrer le serveur");
- error.exit_err();
- }
- sleep(10);
- notify();
- sleep(15);
- error.exit();
- //*/
-
- //return files();
- //return array_encode();
- //return array_parse();
- //return size();
-
- /*
- int x, y;
- printf("Res %d\n", parse_pos("5890,84", &x, &y));
- printf("%d, %d\n", x, y);
- //*/
-
- return 0;
- }
|