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

📄 servlethttptransport.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Returns an EndpointAddress of the local endpoint
     * managed by the EndpointProtocol. This is the address that
     * other peers (possibly on the other side of a NAT can use to connect to
     * this peer).
     *
     * @return an EndpointAddress containing the public address
     */
    public EndpointAddress getPublicAddress() {
        return localAddress;
    }
    
    /**
     * Returns true if the endpoint protocol can be used by the EndpointRouter
     *
     * @return boolean true if the protocol can be used by the EndpointRouter
     */
    public boolean allowRouting() {
        return true;
    }
    
    
    /**
     * We use persistent connections wherever possible. And the default case
     * for relay clients is to use persistent connections. So, returning true
     * is the best we can do.
     */
    public boolean isConnectionOriented() {
        return true;
    }
    
    /**
     * Returns true if the target address is reachable. Otherwise
     * returns false.
     * This passes through to the appropriate message sender based on whether
     * the address is for an HTTP server or a relay client.
     */
    public boolean ping(EndpointAddress addr) {
        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Trying to ping " + addr);
        if (isRelayClientAddress(addr)) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("  Using relay client sender to ping " + addr);
            return relayClientSender.ping(addr);
        } else {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("  Using http client sender to ping " + addr);
            return httpClientSender.ping(addr);
        }
    }
    
    /** computes the local peer's endpoint address.
     *  @param httpAdv the http transport adv.
     *  @param peerID The peer'd ID
     */
    private EndpointAddress computeEndpointAddress(EndpointService endpoint,
    HTTPAdv httpAdv,
    String peerID) {
        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("computeEndpointAddress()");
        
        if (httpAdv.getServerEnabled()) {
            return endpoint.newEndpointAddress(protocolName + "://"
            + httpAdv.getServer());
        } else {
            String localClientId = peerID;
            return endpoint.newEndpointAddress(protocolName + "://"
            + MAGIC_WORD
            + localClientId);
        }
    }
    
    /**
     * creates/oconfigures the http client sender
     */
    private void configureHttpClientSender(EndpointAddress localAddress,
    HTTPAdv httpAdv) {
        
        // get the proxy server to use, if any
        String proxyName = null;
        if (httpAdv.getProxyEnabled()) {
            proxyName = httpAdv.getProxy();
        }
        
        httpClientSender =
        new HttpClientMessageSender(localAddress);
    }
    
    /**
     * creates/configures a set of relay clients
     */
    private void configureRelayClients(PeerGroup group,
    EndpointService endpoint,
    HTTPAdv httpAdv) {
        
        // for each relay server in the adv, get a relay client up and running
        //
        Vector routers = httpAdv.getRouters();
        if (routers != null) {
            Enumeration e = routers.elements();
            while (e.hasMoreElements()) {
                String urlStr = (String) e.nextElement();
                try {
                    URL url = new URL("http://" + urlStr + RELAY_RELATIVE_URI);
                    HttpRelayClientMessageReceiver receiver =
                    new HttpRelayClientMessageReceiver(url);
                    receiver.init(group, endpoint, httpAdv);
                    // XXX jbeatty check out the threading model here:
                    // it's desireable to start this on a different thread
                    // than the calling thread I believe.
                    receiver.start();
                } catch (MalformedURLException ex) {
                    if (LOG.isEnabledFor(Priority.ERROR)) LOG.error("Invalid relay server specified: " + urlStr);
                } catch (IOException ex) {
                    if (LOG.isEnabledFor(Priority.ERROR)) LOG.error("Problem starting relay client: " + urlStr +
                    " Exception = " + ex );
                }
            }
        }
    }
    
    /**
     * compute the local interface that should be used for the entire
     * transport.
     */
    private InetAddress computeInterface(HTTPAdv httpAdv)
    throws UnknownHostException {
        
        // determine the interface to use. If the user specifies one, use that.
        // otherwise, using the default interface.
        String interfaceAddressStr = httpAdv.getInterfaceAddress();
        if (interfaceAddressStr == null) {
            interfaceAddressStr = InetAddress.getLocalHost().getHostAddress();
        }
        InetAddress usingInterface =
        InetAddress.getByName( interfaceAddressStr );
        
        return usingInterface;
    }
    
    /**
     * Configures and starts the HTTP server. Starts the message receiver,
     * along with the relay server.
     */
    private void configureServer(HTTPAdv httpAdv,
    PeerGroup peerGroup,
    InetAddress iface,
    EndpointService endpoint,
    Properties prop) {
        
        try {
            // get the server hostname and port
            String serverName = httpAdv.getServer();
            int port = Integer.parseInt(httpAdv.getPort());
            
            // create the HTTP server
            //
            JxtaHttpServer server = new JettyHttpServer();
            server.init( iface,
            port,
            endpoint,
            minThreads,
            maxThreads,
            maxThreadIdleTime,
            maxReqReadTime );
            
            server.addServlet(MSG_RECEIVER_RELATIVE_URI,
            HttpMessageReceiverServlet.class.getName());
            
            // start up the HTTP-based message receiver
            //
            MessageReceiver receiver = new HttpServerMessageReceiver(server);
            receiver.init(peerGroup, endpoint, httpAdv);
            receiver.start();
            
            // start up relay server
            //
            RelayServer relayServer = new RelayServer(endpoint, prop);
            server.setContextAttribute("relayServer", relayServer);
            server.addServlet(RELAY_RELATIVE_URI,
            HttpRelayServlet.class.getName());
            relayClientSender =
            new RelayClientMessageSender(relayServer,localAddress);
            
            server.start();
            
        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.ERROR)) LOG.error( "Error starting HTTP server: ", e);
            
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.ERROR)) LOG.error( "Error starting HTTP server: ", e);
        }
    }
    
    /** sets up the proxy server if the user has specified one **/
    private void configureProxyServer(HTTPAdv httpAdv) {
        
        if (httpAdv.getProxyEnabled()) {
            String proxyName = httpAdv.getProxy();
            if (proxyName != null) {
                String host = proxyName;
                String port = "8080";
                
                int sepAt = proxyName.lastIndexOf( ':' );
                if( -1 != sepAt ) {
                    host = proxyName.substring(0, sepAt );
                    port = proxyName.substring(sepAt + 1);
                }
                
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Configuring HTTP proxy server");
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("  host: " + host);
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("  port: " + port);
                Properties prop = System.getProperties();
                prop.put("http.proxyHost", host);
                prop.put("http.proxyPort", port);
            } else {
                if (LOG.isEnabledFor(Priority.WARN))
                    LOG.warn("Configuration error: proxy server turned on, but "+
                    "no proxy server specified");
            }
        }
    }
    
    
    /** returns true iff endpointAddress is for a relay client **/
    private boolean isRelayClientAddress(EndpointAddress addr) {
        
        if (addr.getProtocolAddress().indexOf(ServletHttpTransport.MAGIC_WORD) == -1) {
            return false;
        } else {
            return true;
        }
        
    }
    
    /**
     * Returns a Properties instance for jxta.properties if the file exists;
     * otherwise, returns null.
     * The search order for jxta.properties is: (1) $PWD; (2) classpath
     */
    private Properties getJxtaProperties() {
        Properties prop = null;
        InputStream in = null;
        
        try {
            in = new FileInputStream("jxta.properties");
            if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Read jxta.properties from present working directory");
        } catch (FileNotFoundException e) {
            ClassLoader loader = ClassLoader.getSystemClassLoader();
            in = loader.getResourceAsStream("jxta.properties");
            if (in != null) {
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Read jxta.properties from classpath");
            }
        }
        
        if (in != null) {
            try {
                prop = new Properties();
                prop.load(in);
            } catch (IOException e) {
                if (LOG.isEnabledFor(Priority.ERROR)) LOG.error("Error reading jxta.properties");
                prop = null;
            }
        } else {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("jxta.properties cannot be found");
        }
        
        return prop;
    }
    
    /** Reads the properties from the jxta.properties file **/
    private void initFromProperties(Properties prop) {
        
        if (prop !=null) {
            if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Using jxta.properties to configure HTTP server");
            
            String minThreadsStr =
            prop.getProperty("HttpServer.MinThreads");
            String maxThreadsStr =
            prop.getProperty("HttpServer.MaxThreads");
            String maxReqReadTimeStr =
            prop.getProperty("HttpServer.MaxRequestReadTime");
            String maxThreadIdleTimeStr =
            prop.getProperty("HttpServer.MaxThreadIdleTime");
            
            try {
                if (minThreadsStr != null) {
                    minThreads = Integer.parseInt(minThreadsStr);
                }
            } catch (NumberFormatException e) {
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Invalid HttpServer.MinThreads value; using default");
            }
            
            try {
                if (maxThreadsStr != null) {
                    maxThreads = Integer.parseInt(maxThreadsStr);
                }
            } catch (NumberFormatException e) {
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Invalid HttpServer.MaxThreads value; using default");
            }
            
            try {
                if (maxReqReadTimeStr != null) {
                    maxReqReadTime = Integer.parseInt(maxReqReadTimeStr);
                }
            } catch (NumberFormatException e) {
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Invalid HttpServer.MaxReqReadTime value; using default");
            }
            
            try {
                if (maxThreadIdleTimeStr != null) {
                    maxThreadIdleTime =
                    Integer.parseInt(maxThreadIdleTimeStr);
                }
            } catch (NumberFormatException e) {
                if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Invalid HttpServer.MaxThreadIdleTime value; using default");
            }
        } else {
            if (LOG.isEnabledFor(Priority.INFO)) LOG.info("jxta.properties not found: using default values");
        }
        
        if (LOG.isEnabledFor(Priority.INFO)) LOG.info("minThreads = " + minThreads);
        if (LOG.isEnabledFor(Priority.INFO)) LOG.info("maxThreads = " + maxThreads);
        if (LOG.isEnabledFor(Priority.INFO)) LOG.info("maxReqReadTime = " + maxReqReadTime);
        if (LOG.isEnabledFor(Priority.INFO)) LOG.info("maxThreadIdleTime = " + maxThreadIdleTime);
    }
}

⌨️ 快捷键说明

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