-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveur.py
executable file
·223 lines (178 loc) · 8.21 KB
/
serveur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# -*- coding: utf8 -*-
import socket
import select
from outils import *
from rappel import *
from config import *
from liste_rappel import *
class Serveur:
def messageRecu(self,message):
# Recupere la chaine et renvoie la réponsee
print("messageRecu")
chaine = "-1"
argument = Outils.recupereBaliseAuto(message,"a",1)
if Outils.recupereBaliseAuto(message,"c",1) == "connexion":
chaine = "0"
elif Outils.recupereBaliseAuto(message,"c",1) == "ajoutRappel":
r = Rappel()
#print("rappel")
if Outils.compter(argument,"Ajout") == 0:
r.openChaine(argument,False)
else:
r.openChaine(Outils.recupereBaliseAuto(argument,"Ajout",1),False)
#print("rappel2")
reussi = r.save()
if (reussi):
chaine = "1" # Ajout de rappel réussi
else :
chaine = "2" # Ajout de rappel pas réussi
elif Outils.recupereBaliseAuto(message,"c",1) == "modifieRappel":
argument = Outils.recupereBaliseAuto(message,"a",1)
r = Rappel()
r.openChaine(Outils.recupereBaliseAuto(argument,"Ajout",1),False)
endroit = r.getEndroit()
if (endroit == ""):
return self.createChaine("6") # Modification de rappel pas réussi
nomAjout = endroit
endroit = r.createPath(endroit)
supression = Outils.recupereBaliseAuto(argument,"Suppression",1)
l = ListeRappel(int(Outils.recupereBaliseAuto(supression,"Type",1)))
nom = Outils.recupereBaliseAuto(supression,"ListeDateHeureAncien",1)
for i in range(1,Outils.compter(supression,"<ListeDateHeureAncien>")):
nom += "_"+Outils.recupereBaliseAuto(supression,"ListeDateHeureAncien",i+1)
nom += ".f"
if (nom != nomAjout):
if Outils.testPresence(endroit):
chaine = "6"# Modification de rappel pas réussi
if (l.delRappelFichier(nom)):
r.save()
chaine = "5"# Modification de rappel réussie
else:
chaine = "6"# Modification de rappel pas réussi
else:
if (l.delRappelFichier(nom)):
r.save()
chaine = "5"# Modification de rappel réussie
else:
chaine = "6"# Modification de rappel pas réussi
elif Outils.recupereBaliseAuto(message,"c",1) == "supprimeRappel":
argument = Outils.recupereBaliseAuto(message,"a",1)
l = ListeRappel(int(Outils.recupereBaliseAuto(argument,"Type",1)))
reussi = l.delRappelFichier(Outils.recupereBaliseAuto(argument,"Nom",1))
if reussi:
chaine = "3" # Suppression de rappel réussie
else :
chaine = "4" # Suppression de rappel pas réussie
elif Outils.recupereBaliseAuto(message,"c",1) == "getCommande":
c = Config()
print("getCommande detecté")
liste = []
print("liste")
for i in range (len(c.listeModule)):
liste.append(c.listeModule[i][0])
print ("liste : "+str(liste))
chaine = self.listToString(liste)
elif Outils.recupereBaliseAuto(message,"c",1) == "getRappelJournalier":
l = ListeRappel(0)
chaine = self.listToString(l.getListe())
elif Outils.recupereBaliseAuto(message,"c",1) == "getRappelHebdomadaire":
l = ListeRappel(1)
chaine = self.listToString(l.getListe())
elif Outils.recupereBaliseAuto(message,"c",1) == "getRappelUnique":
l = ListeRappel(2)
chaine = self.listToString(l.getListe())
elif Outils.recupereBaliseAuto(message,"c",1) == "getRappel":
argument = Outils.recupereBaliseAuto(message,"a",1)
l = ListeRappel(int(Outils.recupereBaliseAuto(argument,"Type",1)))
r = l.getRappelFichier(Outils.recupereBaliseAuto(argument,"Nom",1))
chaine = r.toString()
elif Outils.recupereBaliseAuto(message,"c",1) == "getBouton":
c = Config()
if c.bouton:
chaine = "7" # Bouton appuyé
else :
chaine = "8" # Bouton pas appuyé
elif Outils.recupereBaliseAuto(message,"c",1) == "getPresence":
c = Config()
if c.presence:
chaine = "9" # Present
else :
chaine = "10" # Pas présent
elif Outils.recupereBaliseAuto(message,"c",1) == "setBouton":
c = Config()
if Outils.recupereBaliseAuto(message,"a",1) == "1":
c.setBouton(True)
else :
c.setBouton(False)
chaine = "11" # setBouton terminé avec succès
elif Outils.recupereBaliseAuto(message,"c",1) == "setPresence":
c = Config()
if Outils.recupereBaliseAuto(message,"a",1) == "1":
c.setPresence(True)
else :
c.setPresence(False)
chaine = "12" # setPresence terminée avec succès
print("messageRecu2")
return self.createChaine(chaine)
def createChaine(self,chaine):
chaine = chaine.replace("\n","[n]")
chaine +="\n"
return chaine
def listToString(self,liste):
# Fonction qui renvoie une chaine a partir d'une liste ou chaque item est dans la balise <i>
chaine = "-2"
print("createString")
for i in range(len(liste)):
chaine += Outils.constitueBalise("i",liste[i])
print("liste[i]:"+liste[i])
return chaine
def boucleInfinie(self):
# Fonction lançant le serveur
hote = ''
port = 12800
connexion_principale = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_principale.bind((hote, port))
connexion_principale.listen(5)
print("Le serveur ecoute a present sur le port {}".format(port))
serveur_lance = True
clients_connectes = []
while serveur_lance:
# On va verifier que de nouveaux clients ne demandent pas à se connecter
# Pour cela, on écoute la connexion_principale en lecture
# On attend maximum 50ms
connexions_demandees, wlist, xlist = select.select([connexion_principale],[], [], 0.05)
for connexion in connexions_demandees:
connexion_avec_client, infos_connexion = connexion.accept()
# On ajoute le socket connecté à la liste des clients
clients_connectes.append(connexion_avec_client)
# Maintenant, on écoute la liste des clients connectés
# Les clients renvoyés par select sont ceux devant etre lus (recv)
# On attend là encore 50ms maximum
# On enferme l'appel à select.select dans un bloc try
# En effet, si la liste de clients connectés est vide, une exception
# Peut etre levée
clients_a_lire = []
try:
clients_a_lire, wlist, xlist = select.select(clients_connectes,[], [], 0.05)
except select.error:
pass
else:
# On parcourt la liste des clients à lire
for client in clients_a_lire:
# Client est de type socket
msg_recu = client.recv(1024)
if msg_recu.decode() != "":
print("Recu {}".format(msg_recu.decode()))
try :
chaine = self.messageRecu(msg_recu.decode())
print("chaine:"+chaine)
chaine = bytes(chaine.encode('UTF-8'))
client.send(chaine)
except :
pass
if msg_recu == "fin":
serveur_lance = False
print("Fermeture des connexions")
for client in clients_connectes:
client.close()
connexion_principale.close()