Moved MPRIS to a new project
[stereo.git] / DAAPLib / src / dacp / DACPServer.java
blob588afea47b90f261740d3a1b36820f25a46ea917
1 package dacp;
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;
12 import java.util.Set;
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;
21 import api.Response;
24 public class DACPServer implements StereoServer {
26 private String DEVICE;
27 private int PORT;
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;
37 this.dj = dj;
39 try {
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) {
75 ex.printStackTrace();
79 private class ServerSocketThread extends Thread {
80 public ServerSocketThread() {
81 super("DACP Server");
83 public void run() {
84 while (true) {
85 System.out.println("Waiting for connection.");
86 try {
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;
98 public void run() {
99 try {
100 Scanner scan = new Scanner(sock.getInputStream());
102 while (scan.hasNextLine()){
103 String parseText="";
104 while(true){
105 String current = scan.nextLine();
106 if(current.equals("")) break;
107 parseText+=current;
110 try {
111 Command s = DACPRequestParser.parse(parseText);
113 if (s != null) {
115 Response content = s.run(dj);
117 if (content != null) {
118 printer.success(content, sock.getOutputStream());
120 else {
121 printer.error("204 No Content", sock.getOutputStream());
125 else {
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) {
136 e.printStackTrace();
141 private ServerRunnable(Socket s) {
142 this.sock = s;