Ignore rules for git users
[stereo.git] / DAAPClient / src / daap / DAAPClient.java
blob42d93d11c93a9a723093aaac02276c191c5332fb
1 package daap;
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;
14 import java.util.Set;
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);
23 try {
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());
29 throw ex;
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) {
39 String dbname = null;
40 int dbid = 0;
42 for (DAAPEntry a: e.children()) {
43 switch (a.code()) {
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));
71 return collections;
74 private final DAAPUtilities connection;
75 private final int dbid;
76 private final Collection<DAAPTrack> collection;
78 private int revision;
80 public DAAPClient(int id, long per, int rev, int dbid, String name, DAAPUtilities connection) {
82 this.revision = rev;
83 this.dbid = dbid;
84 this.connection = connection;
86 collection = new ConcreteCollection<DAAPTrack>(id, per, name, Collection.GENERATED, false, null, 0, this) {
87 public int size() {
88 return _size();
92 updateTracks();
95 private int _size() {
96 return size();
99 public Collection<DAAPTrack> collection() {
100 return collection;
103 public void update() throws IOException {
105 int rev = connection.update(revision);
107 if (rev > revision) {
108 updateTracks();
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()) {
118 l.removed(trackSet);
122 private void updateTracks() {
124 List<DAAPEntry> tracks;
126 try {
127 tracks = connection.tracks(dbid, revision);
129 catch (IOException ex) {
130 System.err.println("Unable to get track list");
131 ex.printStackTrace();
132 return;
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);
139 if (track != null) {
140 update.add(track);
141 if (!this.store.contains(track)) {
142 added.add(track);
147 Set<DAAPTrack> removed = new HashSet<DAAPTrack>();
148 for (DAAPTrack t: this.store.tracks()) {
149 if (!update.contains(t)) {
150 removed.add(t);
154 for (DAAPTrack t: removed) {
155 this.store.remove(t);
158 for (DAAPTrack t: added) {
159 this.store.add(t);
162 for (Source.Listener l: this.listeners()) {
163 l.added(added);
164 l.removed(removed);
169 public void readStream(DAAPTrack track, Track.StreamReader reader) throws IOException {
170 int song = track.id();
171 connection.readSong(dbid, song, reader);