View Javadoc

1   // Copyright 2003-2005 Arthur van Hoff, Rick Blair
2   // Licensed under Apache License version 2.0
3   // Original license LGPL
4   
5   package javax.jmdns.impl;
6   
7   import java.io.IOException;
8   import java.net.DatagramPacket;
9   import java.util.logging.Level;
10  import java.util.logging.Logger;
11  
12  import javax.jmdns.impl.constants.DNSConstants;
13  
14  /**
15   * Listen for multicast packets.
16   */
17  class SocketListener extends Thread {
18      static Logger           logger = Logger.getLogger(SocketListener.class.getName());
19  
20      /**
21       *
22       */
23      private final JmDNSImpl _jmDNSImpl;
24  
25      /**
26       * @param jmDNSImpl
27       */
28      SocketListener(JmDNSImpl jmDNSImpl) {
29          super("SocketListener(" + (jmDNSImpl != null ? jmDNSImpl.getName() : "") + ")");
30          this.setDaemon(true);
31          this._jmDNSImpl = jmDNSImpl;
32      }
33  
34      @Override
35      public void run() {
36          try {
37              byte buf[] = new byte[DNSConstants.MAX_MSG_ABSOLUTE];
38              DatagramPacket packet = new DatagramPacket(buf, buf.length);
39              while (!this._jmDNSImpl.isCanceling() && !this._jmDNSImpl.isCanceled()) {
40                  packet.setLength(buf.length);
41                  this._jmDNSImpl.getSocket().receive(packet);
42                  if (this._jmDNSImpl.isCanceling() || this._jmDNSImpl.isCanceled() || this._jmDNSImpl.isClosing() || this._jmDNSImpl.isClosed()) {
43                      break;
44                  }
45                  try {
46                      if (this._jmDNSImpl.getLocalHost().shouldIgnorePacket(packet)) {
47                          continue;
48                      }
49  
50                      DNSIncoming msg = new DNSIncoming(packet);
51                      if (logger.isLoggable(Level.FINEST)) {
52                          logger.finest(this.getName() + ".run() JmDNS in:" + msg.print(true));
53                      }
54                      if (msg.isQuery()) {
55                          if (packet.getPort() != DNSConstants.MDNS_PORT) {
56                              this._jmDNSImpl.handleQuery(msg, packet.getAddress(), packet.getPort());
57                          }
58                          this._jmDNSImpl.handleQuery(msg, this._jmDNSImpl.getGroup(), DNSConstants.MDNS_PORT);
59                      } else {
60                          this._jmDNSImpl.handleResponse(msg);
61                      }
62                  } catch (IOException e) {
63                      logger.log(Level.WARNING, this.getName() + ".run() exception ", e);
64                  }
65              }
66          } catch (IOException e) {
67              if (!this._jmDNSImpl.isCanceling() && !this._jmDNSImpl.isCanceled() && !this._jmDNSImpl.isClosing() && !this._jmDNSImpl.isClosed()) {
68                  logger.log(Level.WARNING, this.getName() + ".run() exception ", e);
69                  this._jmDNSImpl.recover();
70              }
71          }
72          if (logger.isLoggable(Level.FINEST)) {
73              logger.finest(this.getName() + ".run() exiting.");
74          }
75      }
76  
77      public JmDNSImpl getDns() {
78          return _jmDNSImpl;
79      }
80  
81  }