Readme requierement
This commit is contained in:
parent
0a3489ab05
commit
a2a2fa3e4d
@ -5,8 +5,6 @@ from serveur import Log
|
|||||||
def handleConnection(connection, client_address):
|
def handleConnection(connection, client_address):
|
||||||
try:
|
try:
|
||||||
log.printL("Connection from IP -> {}".format(client_address), Log.lvl.INFO)
|
log.printL("Connection from IP -> {}".format(client_address), Log.lvl.INFO)
|
||||||
#userListActive(connection)
|
|
||||||
#userListAway(connection)
|
|
||||||
while True:
|
while True:
|
||||||
data = connection.recv(4096)
|
data = connection.recv(4096)
|
||||||
if data:
|
if data:
|
||||||
@ -16,13 +14,13 @@ def handleConnection(connection, client_address) :
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.printL(str(e), Log.lvl.FAIL)
|
log.printL("Handle connection fail : ".format(str(e)), Log.lvl.FAIL)
|
||||||
finally:
|
finally:
|
||||||
quit(connection)
|
quit(connection)
|
||||||
|
|
||||||
|
|
||||||
def handleRequest(connection, data):
|
def handleRequest(connection, data):
|
||||||
#try:
|
try:
|
||||||
arrayData = data.split(" ")
|
arrayData = data.split(" ")
|
||||||
if usersConnected[connection][1] is not None:
|
if usersConnected[connection][1] is not None:
|
||||||
if (not arrayData[0][0] == "/"):
|
if (not arrayData[0][0] == "/"):
|
||||||
@ -33,6 +31,12 @@ def handleRequest(connection, data):
|
|||||||
if arrayData[0] == "/name":
|
if arrayData[0] == "/name":
|
||||||
changeName(connection, arrayData[1])
|
changeName(connection, arrayData[1])
|
||||||
return
|
return
|
||||||
|
if arrayData[0] == "/userlist":
|
||||||
|
userListActive(connection)
|
||||||
|
return
|
||||||
|
if arrayData[0] == "/userlistaway":
|
||||||
|
userListAway(connection)
|
||||||
|
return
|
||||||
if arrayData[0] == "/askpm":
|
if arrayData[0] == "/askpm":
|
||||||
askPrivateMsg(connection, arrayData[1])
|
askPrivateMsg(connection, arrayData[1])
|
||||||
return
|
return
|
||||||
@ -52,7 +56,7 @@ def handleRequest(connection, data):
|
|||||||
disableUser(connection)
|
disableUser(connection)
|
||||||
return
|
return
|
||||||
if arrayData[0] == "/quit":
|
if arrayData[0] == "/quit":
|
||||||
quit(connection)
|
connection.shutdown(socket.SHUT_RD)
|
||||||
return
|
return
|
||||||
connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
|
connection.sendall("ERR_COMMAND_NOT_FOUND".encode())
|
||||||
else:
|
else:
|
||||||
@ -60,17 +64,18 @@ def handleRequest(connection, data):
|
|||||||
newName(connection, arrayData[1])
|
newName(connection, arrayData[1])
|
||||||
return
|
return
|
||||||
if arrayData[0] == "/quit":
|
if arrayData[0] == "/quit":
|
||||||
quit(connection)
|
connection.shutdown(socket.SHUT_RD)
|
||||||
return
|
return
|
||||||
connection.sendall("ERR_NO_NICKNAME".encode())
|
connection.sendall("ERR_NO_NICKNAME".encode())
|
||||||
"""except Exception as e :
|
except Exception as e :
|
||||||
log.printL(str(e), Log.lvl.FAIL)"""
|
log.printL("Handle request fail : {}".format(str(e)), Log.lvl.FAIL)
|
||||||
|
connection.sendall("ERR_INTERNAL_SERVER_ERROR".encode())
|
||||||
|
|
||||||
|
|
||||||
def broadcastMsg(connection, message):
|
def broadcastMsg(connection, message):
|
||||||
log.printL("User Connected : {}".format(usersConnected), Log.lvl.DEBUG)
|
log.printL("User Connected : {}".format(usersConnected), Log.lvl.DEBUG)
|
||||||
for con, value in usersConnected.items():
|
for con, value in usersConnected.items():
|
||||||
if usersConnected[con][1] is not None and con != connection and connection[con][2] == True:
|
if value[1] is not None and con != connection and value[2] == True:
|
||||||
try:
|
try:
|
||||||
con.sendall(message.encode())
|
con.sendall(message.encode())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -80,7 +85,7 @@ def broadcastMsg(connection,message):
|
|||||||
def userListActive(connection):
|
def userListActive(connection):
|
||||||
l = "USERLIST "
|
l = "USERLIST "
|
||||||
for con, value in usersConnected.items():
|
for con, value in usersConnected.items():
|
||||||
if value[2] :
|
if value[1] is not None and value[2]:
|
||||||
l += value[1] + " "
|
l += value[1] + " "
|
||||||
connection.sendall(l[:-1].encode())
|
connection.sendall(l[:-1].encode())
|
||||||
|
|
||||||
@ -88,7 +93,7 @@ def userListActive(connection):
|
|||||||
def userListAway(connection):
|
def userListAway(connection):
|
||||||
l = "USERAWAY "
|
l = "USERAWAY "
|
||||||
for con, value in usersConnected.items():
|
for con, value in usersConnected.items():
|
||||||
if value[2]:
|
if value[1] is not None and not value[2]:
|
||||||
l += value[1] + " "
|
l += value[1] + " "
|
||||||
connection.sendall(l[:-1].encode())
|
connection.sendall(l[:-1].encode())
|
||||||
|
|
||||||
@ -109,6 +114,8 @@ def newName(connection, pseudo):
|
|||||||
broadcastMsg(connection, "HAS_JOIN {} ".format(pseudo))
|
broadcastMsg(connection, "HAS_JOIN {} ".format(pseudo))
|
||||||
connection.sendall("SUCC_CHANNEL_JOINED".encode())
|
connection.sendall("SUCC_CHANNEL_JOINED".encode())
|
||||||
usersConnected[connection][1] = pseudo
|
usersConnected[connection][1] = pseudo
|
||||||
|
userListActive(connection)
|
||||||
|
userListAway(connection)
|
||||||
|
|
||||||
|
|
||||||
def askPrivateMsg(connection, pseudo):
|
def askPrivateMsg(connection, pseudo):
|
||||||
@ -182,7 +189,11 @@ def disableUser(connection):
|
|||||||
|
|
||||||
|
|
||||||
def quit(connection):
|
def quit(connection):
|
||||||
|
try:
|
||||||
connection.sendall("SUCCESSFUL_LOGOUT".encode())
|
connection.sendall("SUCCESSFUL_LOGOUT".encode())
|
||||||
|
except OSError: # Client close the socket in this side not properly
|
||||||
|
log.printL("Client IP -> {} close connection not properly"
|
||||||
|
"".format(usersConnected[connection][0]), Log.lvl.WARNING)
|
||||||
connection.close()
|
connection.close()
|
||||||
log.printL("Disconnection from IP -> {}".format(usersConnected[connection][0]), Log.lvl.INFO)
|
log.printL("Disconnection from IP -> {}".format(usersConnected[connection][0]), Log.lvl.INFO)
|
||||||
pseudo = usersConnected[connection][1]
|
pseudo = usersConnected[connection][1]
|
||||||
@ -190,7 +201,6 @@ def quit(connection) :
|
|||||||
broadcastMsg(connection, "HAS_LEFT {}".format(pseudo))
|
broadcastMsg(connection, "HAS_LEFT {}".format(pseudo))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getConnectionByPseudo(pseudo):
|
def getConnectionByPseudo(pseudo):
|
||||||
for con, value in usersConnected.items():
|
for con, value in usersConnected.items():
|
||||||
if value[1] == pseudo:
|
if value[1] == pseudo:
|
||||||
@ -201,9 +211,9 @@ def getConnectionByPseudo(pseudo):
|
|||||||
def main():
|
def main():
|
||||||
# Global vars
|
# Global vars
|
||||||
global usersConnected, log, sock
|
global usersConnected, log, sock
|
||||||
usersConnected = {}
|
|
||||||
global askPM, validatePM
|
global askPM, validatePM
|
||||||
global askFT, validateFT
|
global askFT, validateFT
|
||||||
|
usersConnected = {}
|
||||||
askPM = []
|
askPM = []
|
||||||
validatePM = []
|
validatePM = []
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@ -224,7 +234,6 @@ def main():
|
|||||||
sock.listen(5)
|
sock.listen(5)
|
||||||
log.printL("Server Listen on port {}".format(config["NETWORK"]["port"]), Log.lvl.INFO)
|
log.printL("Server Listen on port {}".format(config["NETWORK"]["port"]), Log.lvl.INFO)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
#Connection client
|
#Connection client
|
||||||
@ -241,10 +250,6 @@ def main():
|
|||||||
for t in threading.enumerate():
|
for t in threading.enumerate():
|
||||||
if t != threading.main_thread():
|
if t != threading.main_thread():
|
||||||
t.join()
|
t.join()
|
||||||
#Close all clients sockets
|
|
||||||
for con, value in usersConnected.items():
|
|
||||||
quit(con)
|
|
||||||
#Close the socket server
|
|
||||||
sock.close()
|
sock.close()
|
||||||
log.printL("Server shutdown", Log.lvl.INFO)
|
log.printL("Server shutdown", Log.lvl.INFO)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
Reference in New Issue
Block a user