uvic-aero / onboard-computer

0 stars 1 forks source link

Listen thread #45

Closed logankuzyk closed 4 years ago

logankuzyk commented 4 years ago

I implemented a new technique to listen for new connections which prevents race conditions and thread blocking.

There was an error in the connections.add() method, which set the initial connection time as 0 instead of time.time(). This caused all connections to be removed every time connections.cleanup() got called, because the expression for the delta would simplify to time.time() - 0. There were also issues with disconnecting and reconnecting to the video server due to my incorrect usage of locks.

Main and listen are still the only two threads in the program. The listen() method got changed the most. Instead of being constantly blocked by socket.recvfrom() and sleeping, I used socket.timeout and an exception handler to prevent the thread from blocking broadcast() when updating connections.

With this new design, listen() can continuously loop with only one thread. This allows for the periodic execution of connections.cleanup() and avoiding down time while listening by not using sleep (connections would be rejected if they tried to connect when listen was sleeping).

dragonprevost commented 4 years ago

nnections.cleanup

Good eye finding that bug, those things can be difficult to diagnose! Next meeting I am just going to get you to go over how you are testing the module, I will get it running on my machine then we can get everything merged up!