Skip to content

Commit

Permalink
Merge pull request #13 from matias-gonz/client-log-levels
Browse files Browse the repository at this point in the history
Client log levels
  • Loading branch information
matias-gonz authored Sep 26, 2022
2 parents 9c2c5c7 + fb90dc8 commit dda42df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
44 changes: 26 additions & 18 deletions src/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@

from lib import constant, parser, protocol

HOST = "127.0.0.1"
PORT = 6543


log.basicConfig(level=log.DEBUG)
log.info(f"Server Address: {HOST}:{PORT}")


def send_and_ack(s, msg):
s.sendto(msg, (HOST, PORT))
def send_and_ack(s, msg, address):
s.sendto(msg, address)
msg, address = s.recvfrom(constant.MAX_PKT_SIZE)
return msg


def send_request(s, msg):
def send_request(s, msg, address):
attempts = 0
while True:
try:
msg = send_and_ack(s, msg)
msg = send_and_ack(s, msg, address)
ack = protocol.msg_number(msg)
log.debug(f"ack {ack}, iseqnum {constant.CONN_START_SEQNUM}")
if ack == constant.CONN_START_SEQNUM + 1:
Expand All @@ -35,19 +28,34 @@ def send_request(s, msg):
raise


def set_logging_level(quiet, verbose):
log.basicConfig(level=log.INFO)
if quiet:
log.basicConfig(level=log.ERROR, force=True)
if verbose:
log.basicConfig(level=log.DEBUG, force=True)


def download():
p = parser.download_parser()
args = p.parse_args()
host = args.host
port = args.port
quiet = args.quiet
verbose = args.verbose
dst = path.expanduser(args.dst)
name = args.name

set_logging_level(quiet, verbose)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(constant.RETRY_DELAY)

log.debug(f"Sending first message to {(HOST, PORT)}")
log.info(f"Server Address: {host}:{port}")
log.debug(f"Sending first message to {(host, port)}")
request = protocol.compose_request_msg(constant.DOWNLOAD, name)
msg = send_request(s, request)
log.debug(f"First Message sent to {(HOST, PORT)}")
msg = send_request(s, request, (host, port))
log.debug(f"First Message sent to {(host, port)}")

receiver = protocol.Receiver(dst)

Expand All @@ -56,19 +64,19 @@ def download():
responses = receiver.respond_to(msg)

for resp in responses:
s.sendto(resp, (HOST, PORT))
s.sendto(resp, (host, port))

try:
address = tuple()
while address != (HOST, PORT):
while address != (host, port):
msg, address = s.recvfrom(constant.MAX_PKT_SIZE)

except TimeoutError:
for resp in receiver.timeout_response():
s.sendto(resp, (HOST, PORT))
s.sendto(resp, (host, port))

except TimeoutError:
print("Se perdió la conexión con el servidor")
log.error("Connection with server was lost")
sys.exit(1)
except StopIteration:
break
Expand Down
4 changes: 2 additions & 2 deletions src/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def send_request(s, addr, msg):
except TimeoutError:
attempts += 1
if attempts >= constant.RETRY_NUMBER:
print("Couldn't connect with server")
log.error("Couldn't connect with server")
sys.exit(1)


Expand Down Expand Up @@ -59,7 +59,7 @@ def upload(server_address, src, name):
s.sendto(resp, server_address)

except TimeoutError:
print("Connection with server was lost")
log.error("Connection with server was lost")
sys.exit(1)
except StopIteration:
break
Expand Down

0 comments on commit dda42df

Please sign in to comment.