Skip to content

Commit

Permalink
feat: run refresh on new connections not made by jack-matchmaker
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
  • Loading branch information
SpotlightKid committed Jun 30, 2023
1 parent 0b6118d commit 0c335be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
29 changes: 26 additions & 3 deletions jackmatchmaker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import time

from collections import defaultdict
from functools import lru_cache
from itertools import chain

try:
import queue
except ImportError:
import Queue as queue

from cachetools import cached, TTLCache

from . import jacklib
from .jacklib_helpers import c_char_p_p_to_list, get_jack_status_error_string
from .version import __version__
Expand All @@ -30,6 +31,10 @@
jacklib.PropertyChanged: 'changed',
jacklib.PropertyDeleted: 'deleted'
}
PORT_CACHE_MAXSIZE = 10
PORT_CACHE_TTL = 10
CONNECTION_CACHE_MAXSIZE = 1024
CONNECTION_CACHE_TTL = 10
log = logging.getLogger(__program__)

if not hasattr(re, 'Pattern'):
Expand Down Expand Up @@ -87,6 +92,7 @@ def __init__(self, patterns, pattern_file=None, name=__program__, exact_matching
for pair in patterns:
self.add_patterns(*pair)

self.connection_cache = TTLCache(maxsize=CONNECTION_CACHE_MAXSIZE, ttl=CONNECTION_CACHE_TTL)
self.queue = queue.Queue()
self.client = None

Expand Down Expand Up @@ -186,6 +192,21 @@ def rename_callback(self, port_id, old_name, new_name, *args):
log.debug("Port name %s changed to %s.", old_name, new_name)
self._refresh()

def connect_callback(self, port_a_id, port_b_id, connect, *args):
if connect == 0:
return

port_a = jacklib.port_by_id(self.client, port_a_id)
port_b = jacklib.port_by_id(self.client, port_b_id)
port_a_name = jacklib.port_name(port_a)
port_b_name = jacklib.port_name(port_b)
log.debug("New port connection: '%s' -> '%s'", port_a_name, port_b_name)

if self.connection_cache.pop((port_a_name, port_b_name), False):
log.debug("Connection in cache. Skipping refresh.")
else:
self._refresh()

def reg_callback(self, port_id, action, *args):
if action == 0:
return
Expand Down Expand Up @@ -265,7 +286,7 @@ def shutdown_callback(self, *args):
self.client = None
self.queue.put(None)

@lru_cache()
@cached(cache=TTLCache(maxsize=PORT_CACHE_MAXSIZE, ttl=PORT_CACHE_TTL))
def _get_port(self, name):
return jacklib.port_by_name(self.client, name)

Expand Down Expand Up @@ -305,7 +326,7 @@ def get_connections(self, ports=None):
for port_name in ports:
port = jacklib.port_by_name(self.client, port_name)

if jacklib.port_connected(port):
if port and jacklib.port_connected(port):
for other in jacklib.port_get_all_connections(self.client, port):
yield((port_name, other))

Expand Down Expand Up @@ -347,6 +368,7 @@ def run(self):
try:
self.connect()
jacklib.set_port_registration_callback(self.client, self.reg_callback, None)
jacklib.set_port_connect_callback(self.client, self.connect_callback, None)
jacklib.set_port_rename_callback(self.client, self.rename_callback, None)
jacklib.set_property_change_callback(self.client, self.property_callback, None)
jacklib.activate(self.client)
Expand Down Expand Up @@ -384,6 +406,7 @@ def run(self):

if not jacklib.port_connected_to(port, inport):
log.info("Connecting ports: '%s' --> '%s'.", outport, inport)
self.connection_cache[(outport, inport)] = True
jacklib.connect(self.client, outport, inport)
else:
log.debug("Ports already connected: '%s' --> '%s'.", outport, inport)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
url="https://github.com/SpotlightKid/jack-matchmaker",
license="GPL2",
packages=["jackmatchmaker"],
install_requires=["cachetools"],
entry_points={
"console_scripts": [
"jack-matchmaker = jackmatchmaker:main"
Expand Down

0 comments on commit 0c335be

Please sign in to comment.