Added volume to client plus other fixes
[stereo.git] / DAAPLib / src / dacp / DACPServer.java
blob50361f5bc3d02dac81fdf985d3c0d9ae7270292f
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 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 {
34 this.PORT = port;
35 this.dj = dj;
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() {
73 super("DACP Server");
75 public void run() {
76 while (true) {
77 System.out.println("Waiting for connection.");
78 try {
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;
90 public void run() {
91 try {
92 Scanner scan = new Scanner(sock.getInputStream());
94 while (scan.hasNextLine()){
95 String parseText="";
96 while(true){
97 String current = scan.nextLine();
98 if(current.equals("")) break;
99 parseText+=current;
102 try {
103 Command s = DACPRequestParser.parse(parseText);
105 if (s != null) {
106 Node reply = s.run(dj);
107 printer.visit(reply, sock.getOutputStream());
109 else {
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) {
121 e.printStackTrace();
126 private ServerRunnable(Socket s) {
127 this.sock = s;