Skip to content

Commit

Permalink
docs: added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chefZau committed Oct 21, 2021
1 parent ce5bf0d commit 3bbc5de
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
32 changes: 30 additions & 2 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# create default selector for handling multiple IO
sel = selectors.DefaultSelector()

# set input non blocking
# set input non-blocking
origFl = fcntl.fcntl(sys.stdin, fcntl.F_GETFL)
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, origFl | os.O_NONBLOCK)

# the variable checks whether the client is signed in or not
signIn = False


Expand Down Expand Up @@ -58,18 +59,32 @@ def read(sock):
msg = sock.recv(BUFFER_SIZE).decode(FORMAT)
if msg:

# When a client opens the app for the first time, the app will send
# their entered nickname to the server. The server has three return options:
# 1. 200 Registration successful
# 2. 401 Client already registered
# 3. 400 Invalid registration
# The following if-else block checks whether the received message
# contains the above control message. If there isn't a control
# message, print it out to the console.

if msg == '200 Registration successful':

msg = 'Connection to server established. Sending intro message...\nRegistration successful. Ready for messageing!\n'
print(msg)

# the variable checks whether the client is signed in or not
# (registered successfully), since the nickname is available
# we turn it to True

global signIn
signIn = True

# asks client for console input
sel.register(sys.stdin, selectors.EVENT_READ, getStdinInput)

elif msg in ['401 Client already registered', '400 Invalid registration']:
print(f'\n{msg}')
print(f'\n{msg} ... Please try again later ')
sel.unregister(sock)
sock.close()
sys.exit(0)
Expand Down Expand Up @@ -99,8 +114,11 @@ def getStdinInput(stdin, conn):
def main():

# Register our signal handler for shutting down.

signal.signal(signal.SIGINT, signalHandler)

# retrieves the arguments from the console

NAME, HOST, PORT = getArgs()
ADDR = (HOST, PORT)

Expand All @@ -116,11 +134,21 @@ def main():
sel.register(client, selectors.EVENT_READ, read)

while True:

# prompts (displays '>' sign) client input only if they are registered

if signIn:
sys.stdout.write('> ')
sys.stdout.flush()

for k, _ in sel.select(timeout=None):

# notice that k.data here is a function,
# since in Python, functions are first-class citizens
# we can assign it to a variable

callback = k.data

if callback == getStdinInput:
callback(k.fileobj, client)
else:
Expand Down
12 changes: 12 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def acceptWrapper(sock):
msg = conn.recv(BUFFER_SIZE).decode(FORMAT)
username = msg.lstrip('REGISTER ').rstrip(' CHAT/1.0')

# checks whether the entered nickname is valid
# checks whether the entered nickname already exists

validRegistration = True
controlMsg = '200 Registration successful'
if 'REGISTER ' not in msg or ' CHAT/1.0' not in msg:
Expand All @@ -63,12 +66,17 @@ def acceptWrapper(sock):
conn.sendall(controlMsg.encode(FORMAT))

if validRegistration:

print(
f'Connection to client estatblished, waiting to reveive messages from user "{username}" ... ')

# add client to the dicitonary
clients[username] = conn

# selectors module allows us store data
# the following code creates the data and stores it
# using the register method

data = types.SimpleNamespace(
addr=addr,
name=username
Expand All @@ -93,6 +101,8 @@ def performService(key):

if message:
print(f'Received message from user {data.name}: {message}')

# broadcast the message to everyone except self
broadcast(data.name, message)

else:
Expand All @@ -118,13 +128,15 @@ def main():

sel.register(server, selectors.EVENT_READ)

# create a closure for signalHandler
def signalHandler(sig, frame):
"""Executed when a user press control + c"""
print('Interrupt received, shutting down ...')
server.close()
sys.exit(0)

# Register our signal handler for shutting down.

signal.signal(signal.SIGINT, signalHandler)

print('Waiting for incoming client connections ...')
Expand Down

0 comments on commit 3bbc5de

Please sign in to comment.