servlethttptransport.java

来自「JXTA&#8482 is a set of open, generalize」· Java 代码 · 共 474 行 · 第 1/2 页

JAVA
474
字号
        }        Advertisement paramsAdv = AdvertisementFactory.newAdvertisement(param);        if (!(paramsAdv instanceof HTTPAdv)) {            throw new IllegalArgumentException("Provided advertisement was not a " + HTTPAdv.getAdvertisementType());        }        HTTPAdv httpAdv = (HTTPAdv) paramsAdv;        // determine the local interface to use. If the user specifies        // one, use that. Otherwise, use the all the available interfaces.        String interfaceAddressStr = httpAdv.getInterfaceAddress();        boolean publicAddressOnly = httpAdv.getPublicAddressOnly();        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;        }        usingPort = httpAdv.getPort();        configClient = httpAdv.isClientEnabled();        configServer = httpAdv.isServerEnabled();        publicAddresses = getPublicAddresses(configServer, httpAdv.getServer(), usingInterface, usingPort, publicAddressOnly);        if (!configClient && !configServer) {            throw new IllegalArgumentException("Neither incoming nor outgoing connections configured.");        }        publicAddress = publicAddresses.get(0);        // Tell tell the world about our configuration.        if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {            StringBuilder configInfo = new StringBuilder("Configuring HTTP Message Transport : " + assignedID);            if (implAdvertisement != null) {                configInfo.append("\n\tImplementation:");                configInfo.append("\n\t\tModule Spec ID: ").append(implAdvertisement.getModuleSpecID());                configInfo.append("\n\t\tImpl Description: ").append(implAdvertisement.getDescription());                configInfo.append("\n\t\tImpl URI: ").append(implAdvertisement.getUri());                configInfo.append("\n\t\tImpl Code: ").append(implAdvertisement.getCode());            }            configInfo.append("\n\tGroup Params:");            configInfo.append("\n\t\tGroup: ").append(group.getPeerGroupName());            configInfo.append("\n\t\tGroup ID: ").append(group.getPeerGroupID());            configInfo.append("\n\t\tPeer ID: ").append(group.getPeerID());            configInfo.append("\n\tConfiguration :");            configInfo.append("\n\t\tProtocol: ").append(httpAdv.getProtocol());            configInfo.append("\n\t\tClient Enabled: ").append(configClient);            configInfo.append("\n\t\tServer Enabled: ").append(configServer);            configInfo.append("\n\t\tPublic address: ").append(httpAdv.getServer() == null ? "(unspecified)" : httpAdv.getServer());            configInfo.append("\n\t\tInterface address: ").append(interfaceAddressStr == null ? "(unspecified)" : interfaceAddressStr);            configInfo.append("\n\t\tUnicast Server Bind Addr: ").append(IPUtils.getHostAddress(usingInterface)).append(":").append(usingPort);            configInfo.append("\n\t\tPublic Addresses: ");            configInfo.append("\n\t\t\tDefault Endpoint Addr : ").append(publicAddress);            for (EndpointAddress anAddr : publicAddresses) {                configInfo.append("\n\t\t\tEndpoint Addr : ").append(anAddr);            }            LOG.config(configInfo.toString());        }    }    /**     * {@inheritDoc}     */    public synchronized int startApp(String[] args) {        endpoint = group.getEndpointService();        if (null == endpoint) {            if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {                LOG.warning("Stalled until there is an endpoint service");            }            return START_AGAIN_STALLED;        }        if (TransportMeterBuildSettings.TRANSPORT_METERING) {            TransportServiceMonitor transportServiceMonitor = (TransportServiceMonitor) MonitorManager.getServiceMonitor(group,                    MonitorResources.transportServiceMonitorClassID);            if (transportServiceMonitor != null) {                transportMeter = transportServiceMonitor.createTransportMeter("HTTP", publicAddress);                unknownTransportBindingMeter = transportMeter.getTransportBindingMeter(TransportMeter.UNKNOWN_PEER,                        TransportMeter.UNKNOWN_ADDRESS);            }        }        if (configServer) {            // Start the http server that runs the receiver.            try {                receiver = new HttpMessageReceiver(this, publicAddresses, usingInterface, usingPort);                receiver.start();            } catch (PeerGroupException e) {                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {                    LOG.log(Level.SEVERE, "Could not start http message receiver", e);                }                return -1; // Can't go on; if we were configured to be a server we must make the failure obvious.            }        }        if (configClient) {            // create the MessageSender            try {                sender = new HttpMessageSender(this,                        new EndpointAddress("jxta", group.getPeerID().getUniqueValue().toString(), null, null));                sender.start();            } catch (PeerGroupException e) {                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {                    LOG.log(Level.SEVERE, "Could not start http message sender", e);                }                return -1; // Can't go on; if we were configured to be a server we must make the failure obvious.            }        }        return 0;    }    /**     * {@inheritDoc}     */    public synchronized void stopApp() {        if (receiver != null) {            receiver.stop();        }        if (sender != null) {            sender.stop();        }        endpoint = null;    }    /**     * {@inheritDoc}     */    EndpointService getEndpointService() {        return endpoint;    }    private List<EndpointAddress> getPublicAddresses(boolean serverEnabled, String serverName, InetAddress usingInterface, int serverSocketPort, boolean publicAddressOnly) {        List<EndpointAddress> publicAddresses = new ArrayList<EndpointAddress>();        if (serverEnabled) {            // 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(HTTP_PROTOCOL_NAME, serverName, null, null);                publicAddresses.add(newAddr);                if (publicAddressOnly) {                    return publicAddresses;                }            }        }        // then add the rest of the local interfaces as appropriate        if (usingInterface.equals(IPUtils.ANYADDRESS)) {            // its wildcarded            Iterator<InetAddress> eachLocal = IPUtils.getAllLocalAddresses();            List<EndpointAddress> wildAddrs = new ArrayList<EndpointAddress>();            while (eachLocal.hasNext()) {                InetAddress anAddress = eachLocal.next();                String hostAddress = IPUtils.getHostAddress(anAddress);                EndpointAddress newAddr = new EndpointAddress(HTTP_PROTOCOL_NAME,                        hostAddress + ":" + Integer.toString(serverSocketPort), null, null);                // don't add it if its already in the list                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());                }                @Override                public boolean equals(Object that) {                    return this == that;                }            });            publicAddresses.addAll(wildAddrs);        } else {            // use specified interface            String hostAddress = IPUtils.getHostAddress(usingInterface);            EndpointAddress newAddr = new EndpointAddress(HTTP_PROTOCOL_NAME,                    hostAddress + ":" + Integer.toString(serverSocketPort), null, null);            // don't add it if its already in the list            if (!publicAddresses.contains(newAddr)) {                publicAddresses.add(newAddr);            }        }        return publicAddresses;    }    TransportBindingMeter getTransportBindingMeter(String peerIDString, EndpointAddress destinationAddress) {        if (transportMeter != null) {            return transportMeter.getTransportBindingMeter((peerIDString != null) ? peerIDString : TransportMeter.UNKNOWN_PEER,                    destinationAddress);        } else {            return null;        }    }    TransportBindingMeter getUnknownTransportBindingMeter() {        return unknownTransportBindingMeter;    }}

⌨️ 快捷键说明

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