3 import interfaces
.DJInterface
;
5 import java
.io
.IOException
;
6 import java
.net
.InetAddress
;
7 import java
.net
.ServerSocket
;
8 import java
.net
.Socket
;
9 import java
.util
.HashSet
;
10 import java
.util
.Hashtable
;
11 import java
.util
.Scanner
;
14 import javax
.jmdns
.JmDNS
;
15 import javax
.jmdns
.ServiceInfo
;
17 import reader
.DACPRequestParser
;
18 import util
.command
.Command
;
19 import util
.node
.Node
;
20 import writer
.DACPResponseGenerator
;
23 public class DACPServer
{
25 private final int PORT
;
27 private final ServerSocket SERVER_SOCK
;
29 private final DJInterface dj
;
31 private final DACPResponseGenerator printer
= new DACPResponseGenerator();
33 public DACPServer(String device
, int port
, DJInterface dj
) throws IOException
{
36 SERVER_SOCK
= new ServerSocket(PORT
);
37 System
.out
.println("Server starting.\n--------\n");
38 new ServerSocketThread().start();
40 Set
<InetAddress
> addresses
= new HashSet
<InetAddress
>();
42 addresses
.add(InetAddress
.getByName(device
));
44 String hostname
= InetAddress
.getLocalHost().getHostName();
45 hostname
= new Scanner(hostname
).useDelimiter("[.]").next();
47 String hash
= Integer
.toHexString(hostname
.hashCode()).toUpperCase();
48 hash
= (hash
+hash
).substring(0,13);
50 System
.out
.println("registering mDNS for " + hostname
+ " (" + hash
+ ")");
52 Hashtable
<String
, String
> records
= new Hashtable
<String
, String
>();
54 records
.put("CtlN","Stereo on " + hostname
);
55 records
.put("OSsi","0x1F6");
56 records
.put("Ver","131073");
57 records
.put("txtvers","1");
58 records
.put("DvTy","iTunes");
59 records
.put("DvSv","2049");
60 records
.put("DbId", hash
);
62 ServiceInfo dmcp
= ServiceInfo
.create("_touch-able._tcp.local.", hash
, PORT
, 0, 0, records
);
64 for (InetAddress a
: addresses
) {
65 final JmDNS mdns
= JmDNS
.create(a
);
66 System
.out
.println("binding on " + a
);
67 mdns
.registerService(dmcp
);
71 private class ServerSocketThread
extends Thread
{
72 public ServerSocketThread() {
77 System
.out
.println("Waiting for connection.");
79 new ServerRunnable(SERVER_SOCK
.accept()).start();
80 } catch (IOException e
) {}
81 System
.out
.println("Accepting connections.");
86 private class ServerRunnable
extends Thread
{
88 private final Socket sock
;
92 Scanner scan
= new Scanner(sock
.getInputStream());
94 while (scan
.hasNextLine()){
97 String current
= scan
.nextLine();
98 if(current
.equals("")) break;
103 Command s
= DACPRequestParser
.parse(parseText
);
106 Node reply
= s
.run(dj
);
107 printer
.visit(reply
, sock
.getOutputStream());
110 System
.out
.println("No command to execute for " + parseText
);
113 catch (IllegalArgumentException ex
) {
114 ex
.printStackTrace();
116 printer
.error("204 No Content", sock
.getOutputStream());
120 } catch (IOException e
) {
126 private ServerRunnable(Socket s
) {