Skip to content

Commit

Permalink
Send update command to all available sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Frewacom committed Aug 3, 2020
1 parent 1e51619 commit 880d5a6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
7 changes: 4 additions & 3 deletions pywalfox/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ def get_python_version():
def send_update_action():
"""Sends the update command to the socket server."""
client = Client()
connected = client.start()

if connected is True:
client.send_message('update')
for host in client.hosts:
connected = client.connect(host)
if connected is True:
client.send_message('update')

def open_log_file():
"""Opens the daemon log file in an editor."""
Expand Down
13 changes: 10 additions & 3 deletions pywalfox/channel/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ class Connector:
since UNIX-sockets are not properly supported on Windows.
:param platform_id str: the current platform identifier, e.g. win32
:param validate_host bool: check if the socket host is available before binding
"""
def __init__(self, platform_id):
def __init__(self, platform_id, validate_host=True):
if platform_id == 'win32':
self.host = self.get_win_socket_host()
if validate_host is True:
self.host = self.get_win_socket_host()

self.hosts = [WIN_SOCKET_HOST, WIN_SOCKET_HOST_ALT]
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
logging.debug('Setup socket server using AF_INET (win32)')
else:
self.host = self.get_unix_socket_path()
if validate_host is True:
self.host = self.get_unix_socket_path()

self.hosts = [UNIX_SOCKET_PATH, UNIX_SOCKET_PATH_ALT]
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
logging.debug('Setup socket server using AF_UNIX (linux/darwin)')

Expand Down
12 changes: 6 additions & 6 deletions pywalfox/channel/unix/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
class Client(Connector):
"""UNIX-socket client used to communicate with the daemon."""
def __init__(self):
Connector.__init__(self, 'unix')
Connector.__init__(self, 'unix', False)

def start(self):
def connect(self, host):
"""
Connects to the UNIX-socket if it exists.
:return: if the connection to the socket was successfull
:rType: bool
"""
if os.path.exists(self.host):
if os.path.exists(host):
try:
self.socket.connect(self.host)
logging.debug('Successfully connected to UNIX socket at: %s' % self.host)
self.socket.connect(host)
logging.debug('Successfully connected to UNIX socket at: %s' % host)
return True
except OSError as e:
logging.error('Failed to connect to socket: %s' % e.strerror)
else:
logging.error('Could not find socket: %s' % self.host)
logging.error('Could not find socket: %s' % host)

return False
8 changes: 4 additions & 4 deletions pywalfox/channel/win/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
class Client(Connector):
"""UDP-socket client used to communicate with the daemon."""
def __init__(self):
Connector.__init__(self, 'win32')
Connector.__init__(self, 'win32', False)

def start(self):
def connect(self, host):
"""
Connects to the UDP socket.
:return: if the connection to the socket was successfull
:rType: bool
"""
try:
self.socket.connect(self.host)
logging.debug('Successfully connected to UDP socket at: %s:%s' % (self.host[0], self.host[1]))
self.socket.connect(host)
logging.debug('Successfully connected to UDP socket at: %s:%s' % (host[0], host[1]))
return True
except Exception as e:
logging.error('Failed to connect to socket: %s' % str(e))
Expand Down

0 comments on commit 880d5a6

Please sign in to comment.