📄 abstractrelayclient.java
字号:
/************************************************************************
*
* $Id: AbstractRelayClient.java,v 1.2 2002/03/04 21:43:00 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.relay;
//PDA requirements 21.02.2002
//package net.jxta.impl.endpoint.servlethttp.* is not ported to PDA
//import net.jxta.impl.endpoint.servlethttp.MessageReceiver;
//PDA requirements 21.02.2002
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
/**
* Base class for relay clients. This base class provides lease management.
* Instances of AbstractRelayClient subclasses represent a MessageReceiver
* that is in a relationship with a <b>particular</b> message relay. This
* base class handles all the lease management, timers, etc. It also implements
* the MessageReceiver's start() and stop() methods to get the receiver going.
*/
//PDA requirements 21.02.2002
//package net.jxta.impl.endpoint.servlethttp.* is not ported to PDA
public abstract class AbstractRelayClient /*implements MessageReceiver*/ {
//PDA requirements 21.02.2002
private static final Category LOG = Category.getInstance(AbstractRelayClient.class.getName());
/** Global timer used for lease renewal for all relay clients. **/
protected static Timer timer = new Timer(true);
/** This is the interval at which we will retry obtaining a lease if we
try and fail. **/
private static final int LEASE_RETRY_INTERVAL = 5 * 60 * 1000;
/** This controls how long before the lease is going to expire that the
lease renewal task will be schedule. This is a divisor **/
private static final int LEASE_RENEWAL_DIV = 5;
/** The lease object that the relay client has **/
protected RelayLease lease = null;
/** The current lease renewal timer task **/
private TimerTask leaseRenewalTask = null;
/** Whether we're in slow schedule or not **/
private boolean noLease = true;
/**
* Tells the relay client to obtain a new lease. This is a blocking call.
*
* @return Returns true if the lease was obtained; false otherwise.
*/
public abstract boolean obtainLease();
/**
* Tells the relay client to renew its lease. This is a blocking call.
*
* @return Returns true if the lease renewal succeeded; false otherwise.
*/
public abstract boolean renewLease();
/**
* Subclassers should call this whenever they obtain a lease. This will
* schedule a timer to renew the lease a short time before the existing
* lease is going to expire. The renewal time is scheduled to occur half
* way into the lease. TODO. This should be configurable.
*/
protected void recordLease (String relayPeerId,
String leaseId,
int leaseLength) {
lease = new RelayLease (relayPeerId, leaseId, leaseLength);
if (LOG.isEnabledFor(Priority.INFO)) LOG.info ("Recorded lease; relayPeerId, leaseId, " + "leaseLength=="+relayPeerId+","+leaseId+","+ leaseLength);
leaseMode(leaseLength);
onLeaseObtained();
}
/**
* Returns true if the client has a valid lease with the relay peer
*/
public boolean isLeaseValid() {
RelayLease tmp = lease;
if (tmp == null)
return false;
return tmp.isValid();
}
/**
* Tells the lease ID for the relay client. May be null.
*/
protected String getLeaseId() {
RelayLease tmp = lease;
if (tmp == null || ! tmp.isValid())
return null;
else {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("returning lease id = " + lease.getLeaseId());
return tmp.getLeaseId();
}
}
/** Nullifies the lease **/
protected void nullifyLease() {
lease = null;
}
/**
* Subclassers must implement this. It will be called when a new lease
* is obtained (<b>not</b> called when a lease is renewed).
*/
protected abstract void onLeaseObtained();
/**
* Starts the relay client by trying to obtain a lease. If one cannot
* be obtained, it will keep trying on the interval. <b>Subclassers will
* likely not need to override this method;</b> rather, they will
* implement onLeaseObtained().
*/
public void start() throws IOException {
// create a new lease renewal task and schedule it right away.
// If we finaly obtain the lease, it will get canceled and
// replaced with a different schedule.
if (LOG.isEnabledFor(Priority.INFO)) LOG.info ("start");
noLease = false;
lostLease(); // Will schedule lease retries
}
// Something tells us that we've lost the lease and that's news. We need
// to start the noLease mode right away. That means calling obtain lease in
// addition.
public void lostLease() {
if (noLease) return;
noLeaseMode(true);
}
// The renewal task tells us that the lease has gone stale and that we
// therefore switch it to a slower schedule if we weren't already.
protected synchronized void noLeaseMode(boolean startNow) {
if (noLease) return;
if (leaseRenewalTask != null) leaseRenewalTask.cancel();
leaseRenewalTask = new LeaseRenewalTask();
nullifyLease();
noLease = true;
timer.schedule(leaseRenewalTask,
startNow ? 0 : LEASE_RETRY_INTERVAL,
LEASE_RETRY_INTERVAL);
}
// Change the schedule of the lease renewal task to start renewing
// the lease at 4/5 lease and twice more after that before the lease
// expires (unless one of the attempts works and thus resets this
// schedule to 4/5 of the new lease). So, for a 30 minutes lease, we'll
// try to renew at minutes 24, 27, 30, and 33. At either 30 or 33, if
// we reach that point, we'll notice that the lease is stale and switch
// back to the retry schedule.
protected synchronized void leaseMode(int leaseLength) {
noLease = false;
if (leaseRenewalTask != null) leaseRenewalTask.cancel();
leaseRenewalTask = new LeaseRenewalTask();
timer.schedule(leaseRenewalTask,
leaseLength
* (LEASE_RENEWAL_DIV - 1)
/ LEASE_RENEWAL_DIV,
leaseLength / LEASE_RENEWAL_DIV / 2);
}
/**
* Stops the relay client. Nullifies the lease. Subclassers will likely
* need to override and provide their own stop() code.
*/
public void stop() throws InterruptedException {
if (LOG.isEnabledFor(Priority.INFO)) LOG.info ("stop");
timer.cancel();
nullifyLease();
}
/**
* Simple timer task that tells a relay client to renew its lease.
* This taks attempts to obtain a lease at slow regular intervals.
* When a lease is obtained, this task is re-scheduled to best maintain
* the lease. Whenever the lease is stale this task calls slowSched()
* which schedules it back to the regular intervals if it is not the
* current schedule.
*/
class LeaseRenewalTask extends TimerTask {
public void run() {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("LeaseRenewalTask: trying to renew lease: " +
getLeaseId());
if (isLeaseValid()) {
renewLease();
} else {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug (" Lease is not valid; obtaining new lease");
noLeaseMode(false); // Switch to noLease mode if not there yet.
obtainLease();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -