Skip to content

Commit

Permalink
introduce Device dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
bleykauf committed Jun 2, 2023
1 parent 750ba3f commit 2edaad3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
32 changes: 20 additions & 12 deletions linien-client/linien_client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import random
import string
from dataclasses import dataclass
from socket import gaierror
from time import sleep
from traceback import print_exc
Expand All @@ -37,6 +38,15 @@
from linien_common.config import DEFAULT_SERVER_PORT


@dataclass
class Device:
host: str
user: str
password: str
port: int = DEFAULT_SERVER_PORT
name: str = ""


class RPYCClientWithAuthentication(rpyc.Service):
"""
An rpyc client that authenticates using a hash.
Expand Down Expand Up @@ -68,16 +78,12 @@ def __init__(
name: str = "",
):
"""Connect to a RedPitaya that runs linien server."""
self.host = host
self.user = user
self.password = password
self.port = port
self.name = name
self.device = Device(host, user, password, port, name)

if self.host in ("localhost", "127.0.0.1"):
if self.device.host in ("localhost", "127.0.0.1"):
# RP is configured such that "localhost" doesn't point to 127.0.0.1 in all
# cases
self.host = "127.0.0.1"
self.device.host = "127.0.0.1"

self.uuid = "".join(random.choice(string.ascii_lowercase) for _ in range(10))

Expand All @@ -96,11 +102,11 @@ def connect(
while True:
i += 1
try:
print(f"Try to connect to {self.host}:{self.port}")
print(f"Try to connect to {self.device.host}:{self.device.port}")

self.connection = rpyc.connect(
self.host,
self.port,
self.device.host,
self.device.port,
service=self.client_service,
config={"allow_pickle": True},
)
Expand All @@ -117,7 +123,7 @@ def connect(
break
except gaierror:
# host not found
print(f"Error: host {self.host} not found")
print(f"Error: host {self.device.host} not found")
break
except EOFError:
print("EOFError! Probably authentication failed")
Expand All @@ -129,7 +135,9 @@ def connect(

if i == 0:
print("Server is not running. Launching it!")
start_remote_server(self.host, self.user, self.password)
start_remote_server(
self.device.host, self.device.user, self.device.password
)
sleep(3)
else:
if i < 20:
Expand Down
4 changes: 2 additions & 2 deletions linien-client/linien_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def __init__(self, client_version, remote_version):
self.remote_version = remote_version

super().__init__(
"Version mismatch: Client is %s and server is %s"
% (client_version, remote_version)
"Version mismatch: "
"Client is {client_version} and server is {remote_version}"
)


Expand Down

0 comments on commit 2edaad3

Please sign in to comment.