]> kaliko git repositories - python-musicpdaio.git/blobdiff - mpdaio/connection.py
Add unix socket (closes #1)
[python-musicpdaio.git] / mpdaio / connection.py
index f327a92f109afbc062d6006a31861e064b3df2d2..c47cd7ea3f52705d49ca50f6ba40b0442278f1e9 100644 (file)
@@ -51,6 +51,8 @@ class ConnectionPool(base):
         # find an un-used connection for this host
         connection = next(
             (conn for conn in connections if not conn.in_use), None)
+        #if connection:
+        #    log.debug('reusing %s', connection)
         if connection is None:
             # disconnect the least-recently-used un-used connection to make space
             # for a new connection. There will be at least one.
@@ -59,13 +61,19 @@ class ConnectionPool(base):
                     if not conn.in_use:
                         await conn.close()
                         break
-
-            log.debug('about to connect %s', host)
-            reader, writer = await asyncio.wait_for(
-                    asyncio.open_connection(server, port),
-                    timeout
-                    )
-            log.info('Connected to %s:%s', host[0], host[1])
+            if server[0] in ['/', '@']:
+                log.debug('about to connect unix socket %s', server)
+                reader, writer = await asyncio.wait_for(
+                        asyncio.open_unix_connection(path=server),
+                        timeout
+                        )
+            else:
+                log.debug('about to connect tcp socket %s:%s', *host)
+                reader, writer = await asyncio.wait_for(
+                        asyncio.open_connection(server, port),
+                        timeout
+                        )
+            #log.debug('Connected to %s:%s', host[0], host[1])
             connection = Connection(self, host, reader, writer)
             await connection._hello()
             connections.append(connection)
@@ -81,6 +89,7 @@ class ConnectionPool(base):
         """Close all connections"""
         connections = [c for cs in self._connections.values() for c in cs]
         self._connections = OrderedDict()
+        log.info('Closing all connections')
         for connection in connections:
             await connection.close()
 
@@ -169,7 +178,6 @@ class Connection(base):
         self.version = rcv.split('\n')[0][len(HELLO_PREFIX):]
         log.info('protocol version: %s', self.version)
 
-
     def __getattr__(self, name: str) -> Any:
         """All unknown attributes are delegated to the reader and writer"""
         if self._closed or not self.in_use: