Readme requierement

This commit is contained in:
sidya82 2015-04-03 12:09:00 +02:00
parent 0a3489ab05
commit a2a2fa3e4d

View File

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