📄 httptransport.java
字号:
/************************************************************************
*
* $Id: HttpTransport.java,v 1.2 2002/03/04 21:42:57 echtcherbina Exp $
*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*********************************************************************************/
package net.jxta.impl.endpoint.http;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.lang.Math;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.io.InterruptedIOException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import net.jxta.impl.endpoint.http.HttpServer.ClientConnection;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.TextElement;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.EndpointService;
import net.jxta.endpoint.EndpointAddress;
import net.jxta.endpoint.EndpointMessenger;
import net.jxta.endpoint.EndpointProtocol;
import net.jxta.endpoint.Message;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.TransportAdvertisement;
import net.jxta.impl.cm.Cm;
import net.jxta.impl.endpoint.Address;
import net.jxta.impl.endpoint.MessageImpl;
import net.jxta.impl.protocol.HTTPAdv;
import net.jxta.impl.endpoint.http.HttpMessage;
import net.jxta.impl.endpoint.MessageWireFormat;
import net.jxta.impl.endpoint.MessageWireFormatFactory;
import net.jxta.impl.endpoint.MessageWireFormatXML;
import net.jxta.platform.Module;
/***
* This class implements the HTTP Transport Protocol
*
* @version $Revision: 1.2 $
**/
public class HttpTransport implements EndpointProtocol, Module {
private static final Category LOG = Category.getInstance(HttpTransport.class.getName());
/**
* Set useBlockingSend to false in order to force HttpTransport to
* always poll.
**/
static final boolean useBlockingSend = true;
static final String Register = "reg";
static final String Receive = "rec";
static final String BlockingReceive = "blk";
static final String Send = "snd";
static final String Ping = "ack";
static final Cm cm = new Cm("HttpTransport", false);
static final HttpMessage PingOk = new HttpMessage( "Jxta Ping OK" );
/*
static final Object[] HTTPRESPONSES = {
new Object[]{ 100, "Continue" },
new Object[]{ 101, "Switching Protocols" },
new Object[]{ 200, "OK" },
new Object[]{ 201, "Created" },
new Object[]{ 202, "Accepted" },
new Object[]{ 203, "Non-Authoritative Information" },
new Object[]{ 204, "No Content" },
new Object[]{ 205, "Reset Content" },
new Object[]{ 206, "Partial Content" },
new Object[]{ 300, "Multiple Choices" },
new Object[]{ 301, "Moved Permanently" },
new Object[]{ 302, "Found" },
new Object[]{ 303, "See Other" },
new Object[]{ 304, "Not Modified" },
new Object[]{ 305, "Use Proxy" },
new Object[]{ 306, "(Reserved)" },
new Object[]{ 307, "Temporary Redirect" },
new Object[]{ 400, "Bad Request" },
new Object[]{ 401, "Unauthorized" },
new Object[]{ 402, "Payment Required" },
new Object[]{ 403, "Forbidden" },
new Object[]{ 404, "Not Found" },
new Object[]{ 405, "Method Not Allowed" },
new Object[]{ 406, "Not Acceptable" },
new Object[]{ 407, "Proxy Authentication Required" },
new Object[]{ 408, "Request Timeout" },
new Object[]{ 409, "Conflict" },
new Object[]{ 410, "Gone" },
new Object[]{ 411, "Length Required" },
new Object[]{ 412, "Precondition Failed" },
new Object[]{ 413, "Request Entity Too Large" },
new Object[]{ 414, "Request-URI Too Large" },
new Object[]{ 415, "Unsupported Media Type" },
new Object[]{ 416, "Requested Range Not Satisfiable" },
new Object[]{ 417, "Expectation Failed" },
new Object[]{ 500, "Internal Server Error" },
new Object[]{ 501, "Not Implemented" },
new Object[]{ 502, "Bad Gateway" },
new Object[]{ 503, "Service Unavailable" },
new Object[]{ 504, "Gateway Timeout" },
new Object[]{ 505, "HTTP Version Not Supported" } };
*/
/**
* socket keep alive time in milliseconds
**/
static final int LingerDelay = 30 * 1000;
/**
* socket timeout time in milliseconds
**/
static final int SocketTimeout = 30 * 1000;
static final String MagicWord = "JxtaHttpClient";
static final int PollingSend = 1;
static final int BlockingSend = 2;
private PeerGroup group = null;
private EndpointService endpoint = null;
/**
* This is the thread group into which we will place all of the threads
* we create. THIS HAS NO EFFECT ON SCHEDULING. Java thread groups are
* only for organization and naming.
**/
private ThreadGroup myThreadGroup = null;
private String interfaceAddress = null;
/**
* The network interface on which all communication will occur.
**/
private InetAddress usingInterface = null;
private String serverName = null;
private int serverSocketPort = -1;
HttpServer server = null;
/**
* The peerID we are working for.
**/
String localClientId = null;
private Vector routers = null;
private String proxyName = null;
private URL proxyHost = null;
/**
* The thread which does server polling for us.
**/
private HttpClient pollingThread = null;
/**
* These are connections we have as a client with servers.
**/
private Hashtable servers = new Hashtable();
private EndpointAddress publicAddress = null;
private EndpointAddress publicServerAddress = null;
private EndpointAddress publicClientAddress = null;
/**
* This is the name of the cm spool folder
* MOVE TO SERVER
**/
String HttpSpool;
/**
* Our protocol name. By default "http" but can be changed by the
* impl adv.
*/
String protocolName = "http";
/**
* Manages a connection that we have with a server.
**/
static class ServerConnection extends Thread {
/**
* The interval between polling attempts.
**/
private static final int PollingDelay = 20000; // 20 seconds
private static final int MAXFAILURES = 2;
private long lastReceivedMsg = 0;
private long lastFailure = 0;
private int failureMode = 0;
private URL remoteUrl = null;
private HttpTransport tp = null;
private String routerName = null;
public ServerConnection( ThreadGroup inGroup, HttpTransport tp, String router, String clientId ) {
super( inGroup, (Runnable) null, "Server Connection to " + router );
this.tp = tp;
this.routerName = router;
try {
String host = router;
int port = 80;
int sepAt = router.lastIndexOf( ':' );
if( -1 != sepAt ) {
host = router.substring(0, sepAt );
port = Integer.parseInt( router.substring(sepAt + 1) );
}
this.remoteUrl = new URL( "http", host, port,
"/blk/" + clientId + "/" );
} catch( MalformedURLException badurl ) {
throw new IllegalArgumentException( "Invalid server or client id" );
}
}
/**
* The daemon thread which polls the server for messages.
**/
public void run() {
// record the connection
tp.recordConnection( routerName, this );
try {
while (true) {
if (!tp.pollRemote( remoteUrl, BlockingSend )) {
// Check when last time
long current = System.currentTimeMillis();
if (lastFailure == 0) {
// This is the first failure after a working period.
// Just record the time
lastFailure = current;
continue;
}
if ((current - lastFailure) > PollingDelay) {
// If the delay between two failures is greater than
// the polling delay, it is better, still, to use the
// BlockingSend protocol.
failureMode = 0;
lastFailure = current;
continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -