Refuses client connection beyond a certain number

FIX TODO19: add max player number
This commit is contained in:
Fred Z 2018-03-11 22:43:19 +01:00
parent 44514971ef
commit f90a1a38d5
2 changed files with 26 additions and 10 deletions

View File

@ -49,10 +49,12 @@ class ConnectSocket:
_MSG_START_SERVER = "Server is running, listening on port {}"
_MSG_USER_IN = "<entered the network>"
_MSG_WELCOME = "Welcome. First do something usefull and type your name: "
_MSG_UNWELCOME = "Sorry, no more place here.\n"
_SERVER_LOG = "{}:{}|{name}|{msg}"
# Others const
_MAX_CLIENT_NAME_LEN = 8
_MAX_CLIENT_NB = 5
def __init__(self, host=_HOST, port=_PORT):
"""
@ -183,15 +185,29 @@ class ConnectSocket:
for sckt in rlist:
# Listen for new client connection
if sckt == self._CONNECTION:
sckt_object, sckt_addr = sckt.accept()
self._inputs.append(sckt_object)
self._user_name.append(False)
print(self._SERVER_LOG.format(
*sckt_addr,
name="unknow",
msg="connected")
)
sckt_object.send(self._MSG_WELCOME.encode())
if len(self._inputs) <= self._MAX_CLIENT_NB:
sckt_object, sckt_addr = sckt.accept()
sckt_object.send(self._MSG_WELCOME.encode())
self._inputs.append(sckt_object)
self._user_name.append(False)
print(self._SERVER_LOG.format(
*sckt_addr,
name="unknow",
msg="connected")
)
else:
sckt_object, sckt_addr = sckt.accept()
sckt_object.send(self._MSG_UNWELCOME.encode())
print(self._SERVER_LOG.format(
*sckt_addr,
name="unknow",
msg="rejected")
)
sckt_object.close()
break
else: # receiving data
data = sckt.recv(self._BUFFER).decode().strip()

View File

@ -6,6 +6,7 @@
- [x] ~~conversion to client/server mode: server creation TODO02~~
- [x] ~~conversion to client/server mode: client creation TODO03~~
- [x] ~~verify if user name is already used TODO17~~
- [x] ~~add max player number TODO19~~
- [ ] multiplayer turn-by-turn TODO04
- [ ] each player has a differant robot TODO05
- [ ] starting position has to be random TODO06
@ -20,7 +21,6 @@
- [ ] chat commands: listing players TODO15
- [ ] chat commands: chating with other players TODO16
- [ ] rename ConnectSocket._user_name to ConnectSocket._user_name TODO18
- [ ] add max player number TODO19
- [ ] … TODO
Ideas after correcting [exercice 3](https://openclassrooms.com/courses/apprenez-a-programmer-en-python/exercises/180):