Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

Commit

Permalink
heart
Browse files Browse the repository at this point in the history
  • Loading branch information
ByPikod committed Apr 26, 2023
1 parent 7c0d7f7 commit 8ee2e59
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension-pkg-whitelist=cv2
1 change: 1 addition & 0 deletions quick run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
py src
14 changes: 8 additions & 6 deletions src/groundstation/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time

from .logging import logger as log
from .utilities import PeriodicTimer


class Client:
Expand All @@ -19,7 +20,7 @@ class Client:
listen: bool = True

thread_listening: t.Thread
heartbeat_timer: t.Timer = None
heartbeat_timer: PeriodicTimer = None

# How much time after client will be disconnected from the moment that heartbeat stopped (second).
heartbeat_timeout = 5
Expand Down Expand Up @@ -50,12 +51,13 @@ def check_heartbeat(self) -> None:
"""
Checks if client is still connected.
"""

if self.listen + self.heartbeat_timeout > time.time():
self.heartbeat_timer = t.Timer(
if self.heartbeat_last + self.heartbeat_timeout > time.time():
self.heartbeat_timer = PeriodicTimer(
float(self.heartbeat_check_frequency),
0.1,
self.check_heartbeat
)
self.heartbeat_timer.start()
return

self.kick()
Expand All @@ -71,7 +73,8 @@ def handle_tcp(self) -> None:
try:
data = self.socket.recv(512)
if b"heartbeat" in data:
log().info(f"Heartbeat received: {self.socket.getsockname()[0]}:{self.socket.getsockname()[1]}")
addr, port = self.socket.getsockname()
log().info(f"Heartbeat received: {addr}:{port}")
self.heartbeat_last = time.time()
except OSError:
self.cb_disconnected(self.socket, self.addr)
Expand All @@ -82,7 +85,6 @@ def kick(self) -> None:
"""
Terminate connection.
"""

self.listen = False
self.socket.close()
if self.heartbeat_timer is not None:
Expand Down
2 changes: 1 addition & 1 deletion src/groundstation/gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ def get_stream(self) -> None:
self.label_image.imgtk = image_tk
self.label_image.configure(image=image_tk)

self.after(5, self.get_stream)
self.after(int(1000 / 30), self.get_stream)
4 changes: 3 additions & 1 deletion src/groundstation/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import threading as t
import socket as s
import time

from .logging import logger as log
from .client import Client
Expand Down Expand Up @@ -68,7 +69,7 @@ def handle_tcp(self) -> None:
self.on_disconnected
)
self.on_connected()

# Wait until connection broke
self.client.thread_listening.join()

Expand All @@ -79,6 +80,7 @@ def handle_udp(self) -> None:

while self.running:
if self.client is None:
time.sleep(0.1)
continue

# Listen for packets
Expand Down
48 changes: 48 additions & 0 deletions src/groundstation/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import threading
import time


class PeriodicTimer:
"""
Periodic timer can be cancelled without waiting through the cooldown
thanks to its algorithm which is sleeping in specified periods.
"""
__cancel: bool = False

def __init__(
self,
delay: float,
period: float,
callback: callable
) -> None:

self.delay = delay
self.period = period
self.callback = callback

def loop(self) -> None:
"""
Call the callback function when wait ends.
"""

while self.delay > 0:
self.delay = self.delay - self.period
time.sleep(self.period)
if self.__cancel:
return

self.callback()

def start(self) -> None:
"""
Start waiting.
"""

self.thread = threading.Thread(target=self.loop)
self.thread.start()

def cancel(self) -> None:
"""
Cancel the callback.
"""
self.__cancel = True

0 comments on commit 8ee2e59

Please sign in to comment.