3 import interfaces
.Library
;
4 import interfaces
.Track
;
6 import java
.io
.IOException
;
7 import java
.util
.ArrayList
;
8 import java
.util
.HashMap
;
9 import java
.util
.HashSet
;
10 import java
.util
.List
;
14 import spi
.SourceProvider
;
15 import stereo
.dnssd
.DNSSD
;
16 import stereo
.dnssd
.DNSSDProvider
;
17 import stereo
.dnssd
.DNSSDProvider
.Service
;
20 * DAAPLackey polls DAAP libraries for changes and updates the DJ's library.
22 public class DAAPLackey
implements SourceProvider
{
24 private Set
<String
> hosts
;
25 private Map
<DAAPClient
, String
> clients
;
27 private Library
<?
extends Track
> library
;
29 public void create(Library
<?
extends Track
> library
) {
30 this.library
= library
;
31 this.hosts
= new HashSet
<String
>();
32 this.clients
= new HashMap
<DAAPClient
, String
>();
34 new PollThread().start();
36 DNSSD
.impl().registerListener(new DNSSDProvider
.ServiceListener() {
38 public void serviceAvailable(Service service
) {
39 connect(service
.name
, "daap://"+service
.host
+":"+service
.port
);
42 public void serviceUnavailable(Service service
) {}
44 public String
type() {
51 public void connect(final String path
) {
57 public synchronized void connect(final String name
, final String path
) {
59 if (hosts
.contains(name
)) return;
62 if (!path
.substring(0, 4).equals("daap")) return;
64 final String pth
= "http"+path
.substring(4);
66 new Thread("DAAP Connection Thread") {
70 List
<DAAPClient
> pls
= DAAPClient
.create(pth
, library
.nextCollectionId());
72 for (DAAPClient c
: pls
) {
76 catch (IOException ex
) {}
81 private synchronized void add(String name
, DAAPClient client
) {
83 clients
.put(client
, name
);
84 library
.addSource(client
);
85 library
.addCollection(client
.collection());
88 private synchronized void remove(DAAPClient client
) {
89 library
.removeCollection(client
.collection());
90 library
.removeSource(client
);
91 clients
.remove(client
);
95 private synchronized List
<DAAPClient
> clients() {
96 return new ArrayList
<DAAPClient
>(clients
.keySet());
99 private class PollThread
extends Thread
{
103 List
<DAAPClient
> clients
= clients();
105 for (DAAPClient client
: clients
) {
109 catch (IOException ex
) {
110 System
.err
.println("Unable to update client, closing (" + client
.collection().name() + ")");
111 ex
.printStackTrace();
120 } catch (InterruptedException e
) {