3 import interfaces
.Track
;
4 import interfaces
.collection
.AbstractSetSource
;
5 import interfaces
.collection
.Collection
;
6 import interfaces
.collection
.ConcreteCollection
;
7 import interfaces
.collection
.Source
;
9 import java
.io
.IOException
;
10 import java
.math
.BigInteger
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
16 public class DAAPClient
extends AbstractSetSource
<DAAPTrack
>
17 implements Source
<DAAPTrack
> {
19 public static List
<DAAPClient
> create(String path
, int id
) throws IOException
{
21 DAAPUtilities helper
= new DAAPUtilities(path
);
24 final String name
= helper
.connect();
25 System
.out
.println("connected to " + name
+ " (" + path
+ ")");
27 catch (IOException ex
) {
28 System
.err
.println("error connecting to " + path
+ ": " + ex
.getMessage());
32 final int revision
= helper
.update(0);
34 List
<DAAPEntry
> dbs
= helper
.databases(revision
);
36 List
<DAAPClient
> collections
= new ArrayList
<DAAPClient
>(dbs
.size());
38 for (DAAPEntry e
: dbs
) {
42 for (DAAPEntry a
: e
.children()) {
44 case dmap_itemid
: dbid
= (Integer
)a
.value(); break;
45 case dmap_itemname
: dbname
= (String
)a
.value(); break;
49 System
.out
.println(dbname
);
51 int na
= dbname
.hashCode();
52 int hs
= path
.hashCode();
54 byte[] persistant
= new byte[8];
55 persistant
[0] = (byte)(na
>>24 & 0xFF);
56 persistant
[1] = (byte)(na
>>16 & 0xFF);
57 persistant
[2] = (byte)(na
>>8 & 0xFF);
58 persistant
[3] = (byte)(na
& 0xFF);
59 persistant
[4] = (byte)(hs
>>24 & 0xFF);
60 persistant
[5] = (byte)(hs
>>16 & 0xFF);
61 persistant
[6] = (byte)(hs
>>8 & 0xFF);
62 persistant
[7] = (byte)(hs
& 0xFF);
64 long per
= new BigInteger(persistant
).longValue();
66 if (dbname
== null) throw new IOException("Database name not found (" + path
+ ")");
68 collections
.add(new DAAPClient(id
++, per
, revision
, dbid
, dbname
, helper
));
74 private final DAAPUtilities connection
;
75 private final int dbid
;
76 private final Collection
<DAAPTrack
> collection
;
80 public DAAPClient(int id
, long per
, int rev
, int dbid
, String name
, DAAPUtilities connection
) {
84 this.connection
= connection
;
86 collection
= new ConcreteCollection
<DAAPTrack
>(id
, per
, name
, Collection
.GENERATED
, false, null, 0, this) {
99 public Collection
<DAAPTrack
> collection() {
103 public void update() throws IOException
{
105 int rev
= connection
.update(revision
);
107 if (rev
> revision
) {
112 public void close() {
114 List
<DAAPTrack
> tracks
= this.store
.tracks();
115 Set
<DAAPTrack
> trackSet
= new HashSet
<DAAPTrack
>(tracks
);
117 for (Source
.Listener l
: this.listeners()) {
122 private void updateTracks() {
124 List
<DAAPEntry
> tracks
;
127 tracks
= connection
.tracks(dbid
, revision
);
129 catch (IOException ex
) {
130 System
.err
.println("Unable to get track list");
131 ex
.printStackTrace();
135 List
<DAAPTrack
> added
= new ArrayList
<DAAPTrack
>();
136 Set
<DAAPTrack
> update
= new HashSet
<DAAPTrack
>();
137 for (DAAPEntry e
: tracks
) {
138 DAAPTrack track
= DAAPTrack
.create(e
, this);
141 if (!this.store
.contains(track
)) {
147 Set
<DAAPTrack
> removed
= new HashSet
<DAAPTrack
>();
148 for (DAAPTrack t
: this.store
.tracks()) {
149 if (!update
.contains(t
)) {
154 for (DAAPTrack t
: removed
) {
155 this.store
.remove(t
);
158 for (DAAPTrack t
: added
) {
162 for (Source
.Listener l
: this.listeners()) {
169 public void readStream(DAAPTrack track
, Track
.StreamReader reader
) throws IOException
{
170 int song
= track
.id();
171 connection
.readSong(dbid
, song
, reader
);