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 spi
.StereoServer
;
19 import util
.command
.Command
;
20 import writer
.DACPResponseGenerator
;
24 public class DACPServer
implements StereoServer
{
26 private String DEVICE
;
28 private ServerSocket SERVER_SOCK
;
29 private DJInterface dj
;
31 private DACPResponseGenerator printer
= new DACPResponseGenerator();
33 public void start(DJInterface dj
, String
[] args
) {
35 this.DEVICE
= (args
.length
>0)?args
[0]:"localhost";
36 this.PORT
= (args
.length
>1)?
new Scanner(args
[1]).nextInt():3689;
40 SERVER_SOCK
= new ServerSocket(PORT
);
41 System
.out
.println("Server starting.\n--------\n");
42 new ServerSocketThread().start();
44 Set
<InetAddress
> addresses
= new HashSet
<InetAddress
>();
46 addresses
.add(InetAddress
.getByName(this.DEVICE
));
48 String hostname
= InetAddress
.getLocalHost().getHostName();
49 hostname
= new Scanner(hostname
).useDelimiter("[.]").next();
51 String hash
= Integer
.toHexString(hostname
.hashCode()).toUpperCase();
52 hash
= (hash
+hash
).substring(0,13);
54 System
.out
.println("registering mDNS for " + hostname
+ " (" + hash
+ ")");
56 Hashtable
<String
, String
> records
= new Hashtable
<String
, String
>();
58 records
.put("CtlN","Stereo on " + hostname
);
59 records
.put("OSsi","0x1F6");
60 records
.put("Ver","131073");
61 records
.put("txtvers","1");
62 records
.put("DvTy","iTunes");
63 records
.put("DvSv","2049");
64 records
.put("DbId", hash
);
66 ServiceInfo dmcp
= ServiceInfo
.create("_touch-able._tcp.local.", hash
, PORT
, 0, 0, records
);
68 for (InetAddress a
: addresses
) {
69 final JmDNS mdns
= JmDNS
.create(a
);
70 System
.out
.println("binding on " + a
);
71 mdns
.registerService(dmcp
);
74 catch (IOException ex
) {
79 private class ServerSocketThread
extends Thread
{
80 public ServerSocketThread() {
85 System
.out
.println("Waiting for connection.");
87 new ServerRunnable(SERVER_SOCK
.accept()).start();
88 } catch (IOException e
) {}
89 System
.out
.println("Accepting connections.");
94 private class ServerRunnable
extends Thread
{
96 private final Socket sock
;
100 Scanner scan
= new Scanner(sock
.getInputStream());
102 while (scan
.hasNextLine()){
105 String current
= scan
.nextLine();
106 if(current
.equals("")) break;
111 Command s
= DACPRequestParser
.parse(parseText
);
115 Response content
= s
.run(dj
);
117 if (content
!= null) {
118 printer
.success(content
, sock
.getOutputStream());
121 printer
.error("204 No Content", sock
.getOutputStream());
126 System
.out
.println("No command to execute for " + parseText
);
129 catch (Exception ex
) {
130 ex
.printStackTrace();
132 printer
.error("204 No Content", sock
.getOutputStream());
135 } catch (IOException e
) {
141 private ServerRunnable(Socket s
) {