⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcptransport.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        if (target instanceof TcpTransport) {            TcpTransport likeMe = (TcpTransport) target;                        if (!getProtocolName().equals(likeMe.getProtocolName())) {                return false;            }                        // FIXME 20020630 bondolo@jxta.org Compare the multicasts.                        Iterator myAddrs = publicAddresses.iterator();            Iterator itsAddrs = likeMe.publicAddresses.iterator();                        while (myAddrs.hasNext()) {                if (!itsAddrs.hasNext()) {                    return false;                } // it has fewer than i do.                                EndpointAddress mine = (EndpointAddress) myAddrs.next();                EndpointAddress its = (EndpointAddress) itsAddrs.next();                                if (!mine.equals(its)) {                    return false;                }       // content didnt match            }                        return (!itsAddrs.hasNext()); // ran out at the same time?        }                return false;    }        /**     * {@inheritDoc}     **/    public int hashCode() {        return getPublicAddress().hashCode();    }        /**     * Initialization of the TcpTransport (called by Platform)     */    public void init(PeerGroup g, ID assignedID, Advertisement impl) throws PeerGroupException {                group = g;        endpoint = g.getEndpointService();                try {            ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) impl;            ConfigParams configAdv = (ConfigParams) g.getConfigAdvertisement();                        // Get out invariable parameters from the implAdv            Element param = implAdv.getParam();            if (param != null) {                Enumeration list = param.getChildren("Proto");                if (list.hasMoreElements()) {                    TextElement pname = (TextElement) list.nextElement();                    protocolName = pname.getTextValue();                }            }                        // Get our peer-defined parameters in the configAdv            param = configAdv.getServiceParam(assignedID);                        Enumeration tcpChilds = param.getChildren(TransportAdvertisement.getAdvertisementType());                        // get the TransportAdv            if (tcpChilds.hasMoreElements()) {                param = (Element) tcpChilds.nextElement();                Attribute typeAttr = ((Attributable) param).getAttribute("type");                                if (!TCPAdv.getAdvertisementType().equals(typeAttr.getValue())) {                    throw new IllegalArgumentException("transport adv is not a " + TCPAdv.getAdvertisementType());                }                                if (tcpChilds.hasMoreElements()) {                    throw new IllegalArgumentException("Multiple transport advs detected for " + assignedID);                }            } else {                throw new IllegalArgumentException(TransportAdvertisement.getAdvertisementType() + " could not be located.");            }                        Advertisement paramsAdv = null;                        try {                paramsAdv = AdvertisementFactory.newAdvertisement((TextElement) param);            } catch (NoSuchElementException notThere) {                ;            }                        if (!(paramsAdv instanceof TCPAdv)) {                throw new IllegalArgumentException("Provided Advertisement was not a " + TCPAdv.getAdvertisementType());            }                        TCPAdv adv = (TCPAdv) paramsAdv;                        // determine the local interface to use. If the user specifies            // one, use that. Otherwise, use the all the available interfaces.            interfaceAddressStr = adv.getInterfaceAddress();            if (interfaceAddressStr != null) {                try {                    usingInterface = InetAddress.getByName(interfaceAddressStr);                } catch (UnknownHostException failed) {                    if (LOG.isEnabledFor(Level.WARN)) {                        LOG.warn("Invalid address for local interface address, using default");                    }                    usingInterface = IPUtils.ANYADDRESS;                }            } else {                usingInterface = IPUtils.ANYADDRESS;            }                        serverName = adv.getServer();                        // Even when server is not enabled, we use the serverSocketPort            // as a discriminant for the simulated network partitioning,            // human readable messages, and a few things of that sort.                        serverSocketPort = adv.getPort();                        // should we expose other than a public address if one was            // specified ?            publicAddressOnly = adv.getPublicAddressOnly();                        // Start the servers            myThreadGroup = new ThreadGroup(group.getHomeThreadGroup(), "TcpTransport " + usingInterface.getHostAddress());                        /*             * DVT KLUDGE TO MAKE THE ROUTER'S LIFE MISERABLE.             * This permits to restrict this peer to connect only to             * peers that use one of three TCP port numbers: its own,             * its own - 1, its own + 1. As a result, it is fairly easy             * to configure peers so that they can only connect in daisy             * chained peers. B sees  A or C, but not D. So a message from A has to go             * through B to get to C, etc...             *             * IMPORTANT: If incoming connections connections are not enabled             * we set the port of the public address to zero, to signify that             * this address is not realy usable. This means that this             * trick will NOT be consistent in stopping multicasts if you do             * not enable incoming connections.             *             * Keep commented-out for normal behaviour.             */            // restrictionPort = serverSocketPort;                        if (adv.isServerEnabled()) {                unicastServer = new IncomingUnicastServer(this, usingInterface, serverSocketPort, adv.getStartPort(), adv.getEndPort());                InetSocketAddress boundAddresss = unicastServer.getLocalSocketAddress();                                // XXX bondolo 20040628 Save the port back as a preference to TCPAdv                                // Build the publicAddresses                                // first in the list is the "public server name". We don't try to                // resolve this since it might not be resolvable in the context                // we are running in, we just assume it's good.                if (serverName != null) {                    // use speced server name.                    EndpointAddress newAddr = new EndpointAddress(protocolName, serverName, null, null);                                        publicAddresses.add(newAddr);                }                                // then add the rest of the local interfaces as appropriate                // Unless we find an non-loopback interface, we're in local                // only mode.                boolean localOnly = true;                                if (usingInterface.equals(IPUtils.ANYADDRESS)) {                    // its wildcarded                    Iterator eachLocal = IPUtils.getAllLocalAddresses();                    List    wildAddrs = new ArrayList();                                        while (eachLocal.hasNext()) {                        InetAddress anAddress = (InetAddress) eachLocal.next();                                                String hostAddress = IPUtils.getHostAddress(anAddress);                                                EndpointAddress newAddr = new EndpointAddress(protocolName,                                hostAddress + ":" + Integer.toString(boundAddresss.getPort()), null, null);                                                // don't add it if its already in the list                        if (!anAddress.isLoopbackAddress()) {                            localOnly = false;                        }                                                if (!publicAddresses.contains(newAddr)) {                            wildAddrs.add(newAddr);                        }                    }                                        // we sort them so that later equals() will be deterministic.                    // the result of IPUtils.getAllLocalAddresses() is not known                    // to be sorted.                    Collections.sort(wildAddrs, new Comparator() {                                                public int compare(Object one, Object two) {                            return one.toString().compareTo(two.toString());                        }                                                public boolean equals(Object that) {                            return (this == that);                        }                    });                                        // Add public addresses:                    // don't add them if we have a hand-set public address                    // and the publicAddressOnly property is set.                    if (!(serverName != null && publicAddressOnly)) {                        publicAddresses.addAll(wildAddrs);                    }                } else {                    // use speced interface                    if (!usingInterface.isLoopbackAddress()) {                        localOnly = false;                    }                                        String hostAddress = IPUtils.getHostAddress(usingInterface);                                            EndpointAddress newAddr = new EndpointAddress(protocolName,                            hostAddress + ":" + Integer.toString(boundAddresss.getPort()), null, null);                                        // Add public address:                    // don't add it if its already in the list                    // don't add it if we have a hand-set public address                    // and the publicAddressOnly property is set.                    if (!(serverName != null && publicAddressOnly)) {                        if (!publicAddresses.contains(newAddr)) {                            publicAddresses.add(newAddr);                        }                    }                }                                // If the only available interface is LOOPBACK,                // then make sure we use only that (that includes                // resetting the outgoing/listening interface                // from ANYADDRESS to LOOPBACK).                                if (localOnly) {                    usingInterface = IPUtils.LOOPBACK;                    publicAddresses.clear();                                        String hostAddress = IPUtils.getHostAddress(usingInterface);                                            EndpointAddress pubAddr = new EndpointAddress(protocolName,                            hostAddress + ":" + Integer.toString(boundAddresss.getPort()), null, null);                    publicAddresses.add(pubAddr);                }                                // Set the "prefered" public address. This is the address we                // will use for identifying outgoing requests.                publicAddress = (EndpointAddress) publicAddresses.get(0);                            } else {                                // Only the outgoing interface matters.                                // Verify that ANY interface does not in fact mean                // LOOPBACK only. If that's the case, we want to make                // that explicit, so that consistency checks regarding                // the allowed use of that interface work properly.                                if (usingInterface.equals(IPUtils.ANYADDRESS)) {                                        boolean localOnly = true;                                        Iterator eachLocal = IPUtils.getAllLocalAddresses();                                        while (eachLocal.hasNext()) {                        InetAddress anAddress = (InetAddress) eachLocal.next();                                                if (!anAddress.isLoopbackAddress()) {                            localOnly = false;                            break;                        }                    }                                        if (localOnly) {                        usingInterface = IPUtils.LOOPBACK;                    }                }                                // The "public" address is just an internal label                // it is not usefull to anyone outside.                // IMPORTANT: we set the port to zero, to signify that this                // address is not realy usable. This means that the                // TCP restriction port HACK will NOT be consistent in stopping                // multicasts if you do not enable incoming connections.                String hostAddress = IPUtils.getHostAddress(usingInterface);                                        publicAddress = new EndpointAddress(protocolName, hostAddress + ":0", null, null);            }                        msgSrcAddrElement = new StringMessageElement(EndpointServiceImpl.MESSAGE_SOURCE_NAME, publicAddress.toString(), (MessageElement) null);                        // Get the multicast configuration.                        allowMulticast = adv.getMulticastState();            if (allowMulticast) {                multicastAddress = adv.getMulticastAddr();                multicastPortNb = new Integer(adv.getMulticastPort()).intValue();                multicastPacketSize = new Integer(adv.getMulticastSize()).intValue();                                mAddress = new EndpointAddress(protocolName, multicastAddress + ":" + Integer.toString(multicastPortNb), null, null);                                // Create the multicast input socket                propagatePort = multicastPortNb;                propagateSize = multicastPacketSize;                propagateInetAddress = InetAddress.getByName(multicastAddress);                multicastSocket = new MulticastSocket(propagatePort);                                if (!usingInterface.equals(IPUtils.ANYADDRESS)) {                    try {                        multicastSocket.setInterface(usingInterface);                    } catch (SocketException e1) {                        if (LOG.isEnabledFor(Level.ERROR)) {                            LOG.error("Could not bind multicast socket to explicit address: mcast is left bound to all.");                        }                    }                }                try {                    multicastSocket.joinGroup(propagateInetAddress);                } catch (SocketException soe) {                    if (LOG.isEnabledFor(Level.ERROR)) {                        LOG.error("Could not join multicast group, setting Multicast off");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -