Advertisement
sebp

SqueezeboxServerDiscoverer.java

Mar 1st, 2012
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package fr.phelep.java.squeezebox;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.InetAddress;
  7. import java.net.SocketTimeoutException;
  8. import java.util.ArrayList;
  9.  
  10. public class SqueezeboxServerDiscoverer {
  11.  
  12.     private static final int        DISCOVERY_PORT = 3483;
  13.     private static final int        TIMEOUT_MS = 2500;
  14.     private static final String     DISCOVERY_PACKET = "eIPAD\0NAME\0JSON\0VERS\0";
  15.    
  16.     private int             ipAddress;
  17.     private int             netmask;
  18.     private DatagramSocket          socket;
  19.     private ArrayList<SqueezeboxServer> servers = new ArrayList<SqueezeboxServer>();
  20.    
  21.    
  22.     public SqueezeboxServerDiscoverer( int ipAddress, int netmask ) throws IOException {
  23.         this.ipAddress = ipAddress;
  24.         this.netmask = netmask;
  25.        
  26.             this.socket = new DatagramSocket(DISCOVERY_PORT);
  27.         this.socket.setBroadcast(true);
  28.         this.socket.setSoTimeout(TIMEOUT_MS);
  29.  
  30.         this.sendDiscoveryRequest();
  31.         this.listenForResponses();
  32.         this.socket.close();
  33.     }
  34.    
  35.     private void sendDiscoveryRequest() throws IOException {
  36.         DatagramPacket packet = new DatagramPacket(DISCOVERY_PACKET.getBytes(),
  37.                 DISCOVERY_PACKET.length(), getBroadcastAddress(), DISCOVERY_PORT);
  38.         this.socket.send(packet);
  39.     }
  40.    
  41.     private InetAddress getBroadcastAddress() throws IOException {
  42.         int broadcast = (this.ipAddress & this.netmask) | ~this.netmask;
  43.         byte[] quads = new byte[4];
  44.         for (int k = 0; k < 4; k++)
  45.             quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
  46.  
  47.         return InetAddress.getByAddress(quads);
  48.     }
  49.  
  50.     private void listenForResponses() throws IOException {
  51.         byte[] buf = new byte[256];
  52.         try {
  53.             while (true) {
  54.                 DatagramPacket packet = new DatagramPacket(buf, buf.length);
  55.                 this.socket.receive(packet);
  56.                 buf = packet.getData();
  57.                 int i = 1;
  58.                 int l;
  59.                 String t;
  60.                 String v;
  61.  
  62.                 if( (char)buf[0] == 'E' ) {
  63.                     String  ipAddress   = packet.getAddress().getHostAddress();
  64.                     String  version     = null;
  65.                     String  name        = null;
  66.                     int port        = -1;
  67.                    
  68.                     while( i< packet.getLength() ) {
  69.                         t = new String(buf,i,4);
  70.                         l = (int) buf[i+4];
  71.                         v = new String(buf,i+5,l);
  72.                         i = i + 5 + l;
  73.                         if( t.equals("JSON") ) {
  74.                             port = Integer.parseInt(v);
  75.                         } else if( t.equals("NAME" ) ) {
  76.                             name = v;
  77.                         } else if( t.equals("VERS") ) {
  78.                             version = v;
  79.                         }
  80.                     }
  81.                    
  82.                     if( port > 0 ) {
  83.                         this.servers.add(new SqueezeboxServer(ipAddress, port, name, version));
  84.                     }
  85.                 }
  86.             }
  87.         } catch (SocketTimeoutException e) {
  88.             // Should handle this sometime
  89.         }
  90.     }
  91.    
  92.     public int serversCount() {
  93.         return this.servers.size();
  94.     }
  95.    
  96.     public SqueezeboxServer getServer(int index) {
  97.         return this.servers.get(index);
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement