13742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanpackage javax.jmdns;
23742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
33742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport java.net.InetAddress;
43742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport java.net.NetworkInterface;
53742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport java.util.concurrent.atomic.AtomicReference;
63742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
73742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.NetworkTopologyDiscoveryImpl;
83742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
93742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman/**
103742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * This class is used to resolve the list of Internet address to use when attaching JmDNS to the network.
113742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * <p>
123742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * To create you own filtering class for Internet Addresses you will need to implement the class and the factory delegate. These must be called before any other call to JmDNS.
133742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
143742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * <pre>
153742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * public static class MyNetworkTopologyDiscovery implements NetworkTopologyDiscovery {
163742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
173742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     &#064;Override
183742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     public InetAddress[] getInetAddresses() {
193742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         // TODO Auto-generated method stub
203742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         return null;
213742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     }
223742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
233742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     &#064;Override
243742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     public boolean useInetAddress(NetworkInterface networkInterface, InetAddress interfaceAddress) {
253742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         // TODO Auto-generated method stub
263742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         return false;
273742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     }
283742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
293742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * }
303742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
313742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * public static class MyClass implements NetworkTopologyDiscovery.Factory.ClassDelegate {
323742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     public MyClass() {
333742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         super();
343742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         NetworkTopologyDiscovery.Factory.setClassDelegate(this);
353742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
363742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         // Access JmDNS or JmmDNS
373742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     }
383742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
393742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     &#064;Override
403742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     public NetworkTopologyDiscovery newNetworkTopologyDiscovery() {
413742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *         return new MyNetworkTopologyDiscovery();
423742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *     }
433742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
443742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * }
453742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * </pre>
463742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
473742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * </p>
483742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman *
493742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * @author Pierre Frisch
503742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman */
513742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanpublic interface NetworkTopologyDiscovery {
523742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
533742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /**
543742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * NetworkTopologyDiscovery.Factory enable the creation of new instance of NetworkTopologyDiscovery.
553742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
563742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public static final class Factory {
573742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        private static volatile NetworkTopologyDiscovery _instance;
583742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
593742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        /**
603742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * This interface defines a delegate to the NetworkTopologyDiscovery.Factory class to enable subclassing.
613742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         */
623742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        public static interface ClassDelegate {
633742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
643742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            /**
653742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman             * Allows the delegate the opportunity to construct and return a different NetworkTopologyDiscovery.
663742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman             *
673742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman             * @return Should return a new NetworkTopologyDiscovery Object.
683742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman             * @see #classDelegate()
693742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman             * @see #setClassDelegate(ClassDelegate anObject)
703742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman             */
713742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            public NetworkTopologyDiscovery newNetworkTopologyDiscovery();
723742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
733742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
743742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        private static final AtomicReference<Factory.ClassDelegate> _databaseClassDelegate = new AtomicReference<Factory.ClassDelegate>();
753742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
763742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        private Factory() {
773742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            super();
783742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
793742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
803742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        /**
813742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * Assigns <code>delegate</code> as NetworkTopologyDiscovery's class delegate. The class delegate is optional.
823742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         *
833742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @param delegate
843742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         *            The object to set as NetworkTopologyDiscovery's class delegate.
853742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @see #classDelegate()
863742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @see JmmDNS.Factory.ClassDelegate
873742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         */
883742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        public static void setClassDelegate(Factory.ClassDelegate delegate) {
893742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            _databaseClassDelegate.set(delegate);
903742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
913742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
923742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        /**
933742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * Returns NetworkTopologyDiscovery's class delegate.
943742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         *
953742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @return NetworkTopologyDiscovery's class delegate.
963742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @see #setClassDelegate(ClassDelegate anObject)
973742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @see JmmDNS.Factory.ClassDelegate
983742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         */
993742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        public static Factory.ClassDelegate classDelegate() {
1003742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            return _databaseClassDelegate.get();
1013742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
1023742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1033742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        /**
1043742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * Returns a new instance of NetworkTopologyDiscovery using the class delegate if it exists.
1053742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         *
1063742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @return new instance of NetworkTopologyDiscovery
1073742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         */
1083742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        protected static NetworkTopologyDiscovery newNetworkTopologyDiscovery() {
1093742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            NetworkTopologyDiscovery instance = null;
1103742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            Factory.ClassDelegate delegate = _databaseClassDelegate.get();
1113742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            if (delegate != null) {
1123742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman                instance = delegate.newNetworkTopologyDiscovery();
1133742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            }
1143742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            return (instance != null ? instance : new NetworkTopologyDiscoveryImpl());
1153742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
1163742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1173742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        /**
1183742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * Return the instance of the Multihommed Multicast DNS.
1193742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         *
1203742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         * @return the JmmDNS
1213742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman         */
1223742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        public static NetworkTopologyDiscovery getInstance() {
1233742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            if (_instance == null) {
1243742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman                synchronized (NetworkTopologyDiscovery.Factory.class) {
1253742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman                    if (_instance == null) {
1263742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman                        _instance = NetworkTopologyDiscovery.Factory.newNetworkTopologyDiscovery();
1273742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman                    }
1283742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman                }
1293742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            }
1303742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            return _instance;
1313742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
1323742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    }
1333742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1343742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /**
1353742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * Get all local Internet Addresses for the machine.
1363742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     *
1373742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @return Set of InetAddress
1383742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
1393742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public abstract InetAddress[] getInetAddresses();
1403742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1413742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /**
1423742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * Check if a given InetAddress should be used for mDNS
1433742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     *
1443742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @param networkInterface
1453742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @param interfaceAddress
1463742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @return <code>true</code> is the address is to be used, <code>false</code> otherwise.
1473742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
1483742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public boolean useInetAddress(NetworkInterface networkInterface, InetAddress interfaceAddress);
1493742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1503742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /**
1513742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * Locks the given InetAddress if the device requires it.
1523742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     *
1533742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @param interfaceAddress
1543742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
1553742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public void lockInetAddress(InetAddress interfaceAddress);
1563742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1573742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /**
1583742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * Locks the given InetAddress if the device requires it.
1593742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     *
1603742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @param interfaceAddress
1613742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
1623742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public void unlockInetAddress(InetAddress interfaceAddress);
1633742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
1643742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman}