From 9640cd143c054c8e6a0e0d4fbc861999bea074d7 Mon Sep 17 00:00:00 2001 From: Fred Z Date: Sat, 24 Feb 2018 15:57:55 +0100 Subject: [PATCH] Mise en constante des messages --- networking/client.py | 25 +++++++++++++++---------- networking/server.py | 12 +++++++++--- roboc/cartes/ma_carte_complexe.txt | 13 +++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 roboc/cartes/ma_carte_complexe.txt diff --git a/networking/client.py b/networking/client.py index 87b8913..8ac2f69 100755 --- a/networking/client.py +++ b/networking/client.py @@ -8,22 +8,27 @@ Networking test, client-server talking script """ import socket -hote = "localhost" -port = 12800 +HOST = "localhost" +PORT = 12800 +RECV_BUFFER = 1024 -connexion_avec_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -connexion_avec_serveur.connect((hote, port)) -print("Connexion établie avec le serveur sur le port {}".format(port)) +MSG_SERVER_CONNECTED = "Serveur connecté @{}:{}" +MSG_CLOSE_CONNECTION = "Connexion vers [{}:{}] fermée" + +STOP_COMMAND = "fin" + +SERVER_CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +SERVER_CONNECTION.connect((HOST, PORT)) +print(MSG_SERVER_CONNECTED.format(HOST, PORT)) msg_a_envoyer = b"" -while msg_a_envoyer != b"fin": +while msg_a_envoyer != bytes(STOP_COMMAND, 'utf8'): msg_a_envoyer = input("> ") # Peut planter si vous tapez des caractères spéciaux msg_a_envoyer = msg_a_envoyer.encode() # On envoie le message - connexion_avec_serveur.send(msg_a_envoyer) - msg_recu = connexion_avec_serveur.recv(1024) + SERVER_CONNECTION.send(msg_a_envoyer) + msg_recu = SERVER_CONNECTION.recv(RECV_BUFFER) print(msg_recu.decode()) # Là encore, peut planter s'il y a des accents -print("Fermeture de la connexion") -connexion_avec_serveur.close() +SERVER_CONNECTION.close() diff --git a/networking/server.py b/networking/server.py index 55bb361..2fff6bf 100755 --- a/networking/server.py +++ b/networking/server.py @@ -14,11 +14,16 @@ PORT = 12800 MSG_NEW_CLIENT = "Nouveau client: {}" MSG_CLIENT_ID = "Client[{}] {}" +MSG_CLIENT_DISCONNECTED = "Le client {} c'est deconnecté" +MSG_SERVER_STOP = "Arrêt du serveur" +MSG_START_SERVER = "Le serveur écoute à présent sur le PORT {}" + +STOP_COMMAND = "fin" MAIN_CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM) MAIN_CONNECTION.bind((HOST, PORT)) MAIN_CONNECTION.listen(5) -print("Le serveur écoute à présent sur le PORT {}".format(PORT)) +print(MSG_START_SERVER.format(PORT)) server_on = True connected_clients = [] @@ -68,10 +73,11 @@ while server_on: print(MSG_CLIENT_ID.format(client.fileno(), msg_recu)) - if msg_recu == "fin": + if msg_recu == STOP_COMMAND: + client.send(bytes(MSG_SERVER_STOP, 'utf8')) server_on = False -print("Fermeture des connexions") +print(MSG_SERVER_STOP) for client in connected_clients: client.close() diff --git a/roboc/cartes/ma_carte_complexe.txt b/roboc/cartes/ma_carte_complexe.txt new file mode 100644 index 0000000..9229b25 --- /dev/null +++ b/roboc/cartes/ma_carte_complexe.txt @@ -0,0 +1,13 @@ +OOOOOOOOOOOOOOOOOOOOO +OXO O O O +O . O OOO OO OO.OO.O +O O O . O O O O +O O O O O O OO.OOOOO +O O OOOO O O U +O . . O OOO OO.OOOOOOOOOO +O O O O OO OO O +O O OO OO O OOOOOO.OOOO O +O O O O +OOO O OOOOOOOOOOOOOOOOOOOO + O O + OOOOO