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 20, 2021
1 parent cf77020 commit ca7636e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
21 changes: 20 additions & 1 deletion client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@

active = True

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


def signalHandler(sig, frame):
"""Executed when a user press control + c"""
global active
active = False
print('Interrupt received, shutting down ...')
sys.exit(0)


def getArgs():
"""Gets and parses user's concole input
Returns:
[string]: [the registered name]
[string]: [the host name]
[string]: [the port]
"""
parser = argparse.ArgumentParser()
parser.add_argument("name", help="Host name of server")
parser.add_argument("address", help="Port number of server")
Expand All @@ -41,8 +50,12 @@ def getArgs():


def read(sock):
"""Retrieves server data and print it to console
Args:
sock (socket): the current socket/connection
"""
try:
# retrieve server data and print it to console
msg = sock.recv(BUFFER_SIZE).decode(FORMAT)
if msg:
print(msg)
Expand All @@ -52,6 +65,12 @@ def read(sock):


def getStdinInput(stdin, conn):
"""Read user input from stdin, and send it to the server
Args:
stdin (sys.stdin): standard input object
conn (the socket): the socket
"""
line = stdin.read()
conn.send(line.rstrip().encode(FORMAT))

Expand Down
22 changes: 21 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,40 @@


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


def broadcast(clientName, message):
"""Broadcasts the message to all clients except for the sender
Args:
clientName (string): the registered nickname
message (string): the sent message
"""
for name, conn in clients.items():
formatedMessage = f'@{clientName}: {message}'
if name != clientName:
conn.sendall(formatedMessage.encode(FORMAT))


def acceptWrapper(sock):
"""Sign the user up through:
1. retrieve registered user name from
Args:
sock (socket object): the socket
"""

# accept connection
conn, addr = sock.accept()

print(f'Accepted connection from client address: {addr}')

conn.setblocking(False)

# retrieves the username from the client
msg = conn.recv(BUFFER_SIZE).decode(FORMAT)
username = msg.lstrip('USERNAME:')

Expand All @@ -47,7 +62,7 @@ def acceptWrapper(sock):

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

data = types.SimpleNamespace(
addr=addr,
name=username
Expand All @@ -59,6 +74,11 @@ def acceptWrapper(sock):


def performService(key):
"""Retrieve and broadcast the message from the client
Args:
key (events): the event key
"""

conn = key.fileobj
data = key.data
Expand Down

0 comments on commit ca7636e

Please sign in to comment.