Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3b79cdba4e59e6601b0a49a04fc0f6ab41487c90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//Copyright 2003-2005 Arthur van Hoff, Rick Blair
//Licensed under Apache License version 2.0
//Original license LGPL

package javax.jmdns.impl;

import java.io.IOException;
import java.net.DatagramPacket;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jmdns.impl.constants.DNSConstants;

/**
 * Listen for multicast packets.
 */
class SocketListener implements Runnable
{
    static Logger logger = Logger.getLogger(SocketListener.class.getName());

    /**
     *
     */
    private final JmDNSImpl _jmDNSImpl;

    /**
     * @param jmDNSImpl
     */
    SocketListener(JmDNSImpl jmDNSImpl)
    {
        super();
        this._jmDNSImpl = jmDNSImpl;
    }

    @Override
    public void run()
    {
        try
        {
            byte buf[] = new byte[DNSConstants.MAX_MSG_ABSOLUTE];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            while (!this._jmDNSImpl.isCanceling() && !this._jmDNSImpl.isCanceled())
            {
                packet.setLength(buf.length);
                this._jmDNSImpl.getSocket().receive(packet);
                if (this._jmDNSImpl.isCanceling() || this._jmDNSImpl.isCanceled())
                {
                    break;
                }
                try
                {
                    if (this._jmDNSImpl.getLocalHost().shouldIgnorePacket(packet))
                    {
                        continue;
                    }

                    DNSIncoming msg = new DNSIncoming(packet);
                    if (logger.isLoggable(Level.FINEST))
                    {
                        logger.finest(this.getName() + ".run() JmDNS in:" + msg.print(true));
                    }
                    if (msg.isQuery())
                    {
                        if (packet.getPort() != DNSConstants.MDNS_PORT)
                        {
                            this._jmDNSImpl.handleQuery(msg, packet.getAddress(), packet.getPort());
                        }
                        this._jmDNSImpl.handleQuery(msg, this._jmDNSImpl.getGroup(), DNSConstants.MDNS_PORT);
                    }
                    else
                    {
                        this._jmDNSImpl.handleResponse(msg);
                    }
                }
                catch (IOException e)
                {
                    logger.log(Level.WARNING, this.getName() + ".run() exception ", e);
                }
            }
        }
        catch (IOException e)
        {
            if (!this._jmDNSImpl.isCanceling() && !this._jmDNSImpl.isCanceled())
            {
                logger.log(Level.WARNING, this.getName() + ".run() exception ", e);
                this._jmDNSImpl.recover();
            }
        }
        // jP: 20010-01-18. Per issue #2933183. If this thread was stopped
        // by closeMulticastSocket, we need to signal the other party via
        // the jmDNS monitor. The other guy will then check to see if this
        // thread has died.
        // Note: This is placed here to avoid locking the IoLock object and
        // 'this' instance together.
        synchronized (this._jmDNSImpl)
        {
            this._jmDNSImpl.notifyAll();
        }
    }

    public String getName()
    {
        return "SocketListener(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
    }

    public JmDNSImpl getDns()
    {
        return _jmDNSImpl;
    }

}

Back to the top