📄 httpclient.java
字号:
/************************************************************************
*
* $Id: HttpClient.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.InputStream;
import java.io.OutputStream;
import java.lang.Math;
import java.net.InetAddress;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
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.protocol.EndpointAdvertisement;
import net.jxta.impl.cm.Cm;
import net.jxta.impl.endpoint.Address;
import net.jxta.impl.endpoint.MessageImpl;
import net.jxta.impl.protocol.HTTPAdv;
/**
* the purpose of this Thread is to periodically query remote HTTP
* servers for incoming messages. This is needed if this peer is behind
* a firewall, and cannot be directely contacted.
**/
class HttpClient extends Thread {
private static final Category LOG = Category.getInstance(HttpClient.class.getName());
/**
* The maximum number of messages we will poll from one server in a single
* go around of the polling loop.
**/
private static final long MaxMsgPoll = 10;
/**
* The interval between polling attempts. in milliseconds.
**/
private static final int PollingDelay = 20 * 1000;
/**
* The state of polling.
**/
private boolean polling = false;
/**
* are we quitting?
**/
private boolean quit = false;
/**
* The transport we are working for
**/
HttpTransport tpt = null;
/**
* The servers we are polling.
**/
private Vector pollingHttps = new Vector();
/**
* Constructor for the thread which does message polling for a client
*/
public HttpClient( ThreadGroup inGroup, HttpTransport tpt ) {
super( inGroup, (Runnable) null, "Client Polling" );
this.tpt = tpt;
setPolling( true );
}
/**
* Returns current state of the client polling.
*
* @return Boolean True if currently polling otherwise false.
*
**/
boolean isPolling( ) {
return (polling && (pollingHttps.size() > 0));
}
/**
* Enables or disables polling.
*
* @param enable If true then polling will be enabled otherwise false.
* @return Boolean true if polling was previously enabled otherwise false.
**/
synchronized boolean setPolling( boolean enable ) {
boolean current = polling;
polling = enable;
notifyAll();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "polling is now " + (polling ? "enabled" : "disabled") );
return current;
}
/**
* terminate polling permanently. This thread will exit.
**/
synchronized void endPolling( ) {
setPolling( false );
quit = true;
this.interrupt();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "polling is now termiating" );
}
/**
* returns true if the specified server is one of the ones we are polling.
*
* @param url the server to check on.
* @return Boolean true if we are polling the server otherwise false.
**/
public boolean isPollingHttp( String url ) {
return pollingHttps.contains( url );
}
synchronized public void addPollingHttp(String url) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("addPollingHttp: " + url);
// Test if the address is already in the table.
// Don't trust Vector.contains().
boolean found = false;
for (int i = 0; i < pollingHttps.size(); ++i) {
try {
if (((String) pollingHttps.elementAt(i)).equals(url)) {
found = true;
break;
}
} catch (Exception e) {
break;
}
}
if (found) {
return;
}
pollingHttps.addElement(url);
setPolling( true );
}
synchronized public void removePollingHttp(String url) {
// Test if the address is already in the table.
// Don't trust Vector.contains().
boolean found = false;
int i = 0;
for (i = 0; i < pollingHttps.size(); ++i) {
try {
if (((String) pollingHttps.elementAt(i)).equals(url)) {
found = true;
break;
}
} catch (Exception e) {
break;
}
}
if (!found) {
// Nothing to remove
return;
}
try {
pollingHttps.removeElementAt(i);
} catch (Exception e) {
// This should not happen.
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("cannot remove polling HTTP server");
}
return;
}
/**
* The daemon which does the polling.
**/
public void run() {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Begining client polling loop" );
try {
while( !quit ) {
if( isPolling() ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "polling begins" );
// First poll static remote http
for (int i=0; i < pollingHttps.size(); i++ ) {
String addr = null;
try {
addr = (String) pollingHttps.elementAt(i);
} catch (Exception e) {
continue;
}
// Build the proper url
URL fullAddr = null;
try {
String host = addr;
int port = 80;
int sepAt = addr.lastIndexOf( ':' );
if( -1 != sepAt ) {
host = addr.substring(0, sepAt );
port = Integer.parseInt( addr.substring(sepAt + 1) );
}
fullAddr = new URL( "http",
host, port,
"/rec/" + tpt.localClientId + "/" );
} catch( MalformedURLException badurl ) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "bad URL for either serverer or client" );
continue;
}
// Polls at least MaxMsgPoll from the server HTTP
int cnt = 0;
while ( cnt++ < MaxMsgPoll) {
if (!tpt.pollRemote(fullAddr, HttpTransport.PollingSend)) {
// If pollRemote returns false, either something bad
// happened, or there is no more messages to get
break;
}
}
}
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "snoozing for " + PollingDelay / 1000 + " secs" );
try {
Thread.sleep(PollingDelay);
} catch (InterruptedException e) {
// just acknowledge. if we are being asked to quit
// then that will be caught by the loop.
Thread.interrupted();
}
} else {
/* If we are not polling then we just sleep waiting for
* something to happen. We will be notified if polling
* is needed or the daemon is to quit.
*/
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "waiting for " + PollingDelay / 1000 + " secs" );
synchronized( this ) {
try {
wait(PollingDelay);
} catch( InterruptedException woken ) {
// just acknowledge. if we are being asked to quit
// then that will be caught by the loop.
Thread.interrupted();
}
}
}
}
} catch ( Throwable all ) {
if (LOG.isEnabledFor(Priority.FATAL)) LOG.fatal( "Uncaught Throwable in thread :" + Thread.currentThread().getName(), all );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -