Skip to content

Commit

Permalink
Merge branch 'v3.4-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicGRENON committed Jan 4, 2024
2 parents 0dc1ed3 + 7e01916 commit da3ac13
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name="dsf-python",
version="3.4.6-2",
version="3.4.6-4",
description="Python interface to access DuetSoftwareFramework",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
15 changes: 11 additions & 4 deletions src/dsf/connections/base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import json
import socket

from .exceptions import IncompatibleVersionException, InternalServerException, TaskCanceledException
from .exceptions import (IncompatibleVersionException, InternalServerException, IPCSocketBrokenException,
TaskCanceledException)
from .init_messages import client_init_messages, server_init_message
from ..commands import responses

Expand All @@ -11,9 +12,13 @@ class BaseConnection:
"""
Base class for connections that access the control server via the Duet API
using a UNIX socket
Constructor arguments:
:param debug: Whether to print debug information or not
:param timeout: Set a timeout value on blocking socket operations or None to set the socket in non-blocking mode
"""

def __init__(self, debug: bool = False, timeout: int = 3):
def __init__(self, debug: bool = False, timeout: int = None):
self.debug = debug
self.timeout = timeout
self.socket: socket.socket | None = None
Expand All @@ -25,7 +30,7 @@ def connect(self, init_message: client_init_messages.ClientInitMessage, socket_f

self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.connect(socket_file)
self.socket.setblocking(True)
self.socket.settimeout(self.timeout)
server_init_msg = server_init_message.ServerInitMessage.from_json(
json.loads(self.socket.recv(50).decode("utf8"))
)
Expand Down Expand Up @@ -112,7 +117,9 @@ def receive_json(self) -> str:
except Exception as e:
raise e
# either 0 or end of data
if len(part) < BUFF_SIZE:
if part == b"":
raise IPCSocketBrokenException()
elif len(part) < BUFF_SIZE:
break

json_string += data.decode("utf8")
Expand Down
7 changes: 7 additions & 0 deletions src/dsf/connections/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ def __init__(self, command, error_type: str, error_message: str):

class TaskCanceledException(Exception):
"""Exception returned by the server if the task has been cancelled remotely"""


class IPCSocketBrokenException(Exception):
"""Exception raised if the IPC connection has been closed on the remote server"""

def __init__(self):
super().__init__("IPC socket connection broken")

0 comments on commit da3ac13

Please sign in to comment.