📄 tcptransport.java
字号:
return messagesSent; } /** * Gets the number of 'messagesReceived'. * * @return the number of 'messagesReceived'. */ public long getMessagesReceived() { return messagesReceived; } /** * Gets the number of 'bytesSent'. * * @return the number of 'bytesSent'. */ public long getBytesSent() { return bytesSent; } /** * Gets the number of 'bytesReceived'. * * @return the number of 'bytesReceived'. */ public long getBytesReceived() { return bytesReceived; } /** * {@inheritDoc} */ public boolean equals(Object target) { if (this == target) { return true; } if (null == target) { return false; } if (target instanceof TcpTransport) { TcpTransport likeMe = (TcpTransport) target; if (!getProtocolName().equals(likeMe.getProtocolName())) { return false; } Iterator<EndpointAddress> itsAddrs = likeMe.publicAddresses.iterator(); for (EndpointAddress publicAddress1 : publicAddresses) { if (!itsAddrs.hasNext()) { return false; } // it has fewer than i do. EndpointAddress mine = publicAddress1; EndpointAddress its = itsAddrs.next(); if (!mine.equals(its)) { // content didnt match return false; } } // ran out at the same time? return (!itsAddrs.hasNext()); } return false; } /** * {@inheritDoc} */ public int hashCode() { return getPublicAddress().hashCode(); } /** * {@inheritDoc} */ public void init(PeerGroup group, ID assignedID, Advertisement impl) throws PeerGroupException { this.group = group; ModuleImplAdvertisement implAdvertisement = (ModuleImplAdvertisement) impl; this.executor = ((StdPeerGroup) group).getExecutor(); ConfigParams configAdv = group.getConfigAdvertisement(); // Get out invariable parameters from the implAdv XMLElement param = (XMLElement) implAdvertisement.getParam(); if (param != null) { Enumeration<XMLElement> list = param.getChildren("Proto"); if (list.hasMoreElements()) { XMLElement pname = list.nextElement(); protocolName = pname.getTextValue(); } } // Get our peer-defined parameters in the configAdv param = (XMLElement) configAdv.getServiceParam(assignedID); if (null == param) { throw new IllegalArgumentException(TransportAdvertisement.getAdvertisementType() + " could not be located."); } Enumeration<XMLElement> tcpChilds = param.getChildren(TransportAdvertisement.getAdvertisementType()); // get the TransportAdv if (tcpChilds.hasMoreElements()) { param = tcpChilds.nextElement(); Attribute typeAttr = 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(param); } catch (NoSuchElementException notThere) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Could not find parameter document", 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 (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("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 if (adv.isServerEnabled()) { try { unicastServer = new IncomingUnicastServer(this, usingInterface, serverSocketPort, adv.getStartPort(), adv.getEndPort()); } catch (IOException failed) { throw new PeerGroupException("Failed to open server socket.", failed); } InetSocketAddress boundAddress = unicastServer.getLocalSocketAddress(); // TODO bondolo 20040628 Save the port back as a preference to TCPAdv /* if(-1 != adv.getStartPort()) { adv.setPort(boundAddress.getPort()); } */ // 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<EndpointAddress> wildAddrs = new ArrayList<EndpointAddress>(); while (eachLocal.hasNext()) { InetAddress anAddress = (InetAddress) eachLocal.next(); String hostAddress = IPUtils.getHostAddress(anAddress); EndpointAddress newAddr = new EndpointAddress(protocolName, hostAddress + ":" + Integer.toString(boundAddress.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<EndpointAddress>() { public int compare(EndpointAddress one, EndpointAddress 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 specified interface if (!usingInterface.isLoopbackAddress()) { localOnly = false; } String hostAddress = IPUtils.getHostAddress(usingInterface); EndpointAddress newAddr = new EndpointAddress(protocolName, hostAddress + ":" + Integer.toString(boundAddress.getPort()), null, null); // Add public address: // don't add it if its already in the list // don't add it if specified as public address and publicAddressOnly 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(boundAddress.getPort()), null, null); publicAddresses.add(pubAddr); } // Set the "preferred" public address. This is the address we will // use for identifying outgoing requests. publicAddress = 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. String hostAddress = IPUtils.getHostAddress(usingInterface); publicAddress = new EndpointAddress(protocolName, hostAddress + ":0", null, null); } // Tell tell the world about our configuration. if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -