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

📄 rdvmonitor.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/*
 * $Id: RdvMonitor.java,v 1.36 2002/05/17 15:52:11 hamada 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.rendezvous;

import java.util.Vector;

import java.io.IOException;

import org.apache.log4j.Category;
import org.apache.log4j.Priority;

import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.protocol.RdvAdvertisement;
import net.jxta.rendezvous.RendezVousMonitor;
import net.jxta.peer.PeerID;

import net.jxta.impl.util.JxtaTimer;
import net.jxta.impl.util.JxtaTimerHandler;
import net.jxta.impl.rendezvous.RdvMonitor.RdV;
import net.jxta.impl.peergroup.RefPeerGroup;

/**
 * This class implements a very simple RendezVousService Monitor
 */

public class RdvMonitor implements JxtaTimerHandler, RendezVousMonitor {
    private final static Category LOG = Category.getInstance(RdvMonitor.class.getName());
    
    // Must start worrying about renewing a lease 10 minutes before it
    // expires. Renewal is attempted at each iteration until it is
    // either safe again or expired.
    
    private final static long LeaseMargin = 10 * 60 * 1000;
    
    // LeaseRenewalDelay is the period of the timer that will renew the lease
    // to the rendezvous. We check every 2 minutes but renew only the
    // endangered ones.
    // If a rendezvous gives a lease less than that the delay
    // this client may lose the lease. A lease of less than 5 minutes does not
    // seem to make any sense, since some of the endpoint protocols have at
    // least a 2 minutes latency, 2 minutes should be safe enough.
    
    private final static long LeaseRenewalDelay = 2 * 60 * 1000;
    
    
    private PeerGroup group = null;
    private DiscoveryService discovery = null;
    private long lease = 0;
    private JxtaTimer timer = null;
    private Vector rdvs = new Vector();
    private RendezVousServiceImpl rendezvous = null;
    
    
    /**
     *Description of the Class
     */
    static public class RdV {
        
        /**
         *Description of the Field
         */
        protected String peer;
        /**
         *Description of the Field
         */
        protected long lease;
        
        
        /**
         *Constructor for the RdV object
         *
         * @param  peer  Description of Parameter
         */
        public RdV(String peer) {
            
            this.peer = peer;
            this.lease = 0;
        }
        
        
        /**
         *Description of the Method
         *
         * @param  obj  Description of Parameter
         * @return      Description of the Returned Value
         */
        public boolean equals(Object obj) {
            
            if( obj == this )
                return true;
            
            if (obj instanceof RdV ) {
                RdV rdv = (RdV) obj;
                return (rdv.peer.equals(peer));
            }
            
            return false;
        }
    }
    
    
    /**
     *Constructor for the RdvMonitor object
     *
     * @param  g           Description of Parameter
     * @param  rendezvous  Description of Parameter
     */
    public RdvMonitor(PeerGroup g, RendezVousServiceImpl rendezvous) {
        
        this.group = g;
        this.rendezvous = rendezvous;
        this.discovery = group.getDiscoveryService();
        if (group.isRendezvous()) {
            // This peer is a rendezvous for the group. Let's publish an rdv adv
            publishAdv( (PeerID) group.getPeerID(), LeaseRenewalDelay * 2 );
        }
        
        this.timer = new JxtaTimer( this, LeaseRenewalDelay, true );
    }
    
    
    /**
     * This method is called by the RendezVousService service to notify the
     * monitor that a new RendezVousService has accepted the connection.
     *
     * @param  peer   is the PeerId of the new connect RendezVousService peer.
     * @param  lease  is the time in millisecond that the RendezVousService is
     * ready to serve.
     */
    public void connected(PeerID peer, long lease) {
        
        if (LOG.isDebugEnabled() ) {
            LOG.debug("connected: new connection to a RendezVous" +
            "\n  group = " + group.getPeerGroupID() +
            "\n   peer = " + peer +
            "\n  lease = " + lease );
        }
        
        addRendezVous( peer.toString(), lease );
        
        // Publish locally an rendezvous advertisement for that rendezvous
        publishAdv(peer, lease);
    }
    
    
    /**
     * This method is called each time a RendezVousService peer is not
     * reachable anymore.
     *
     * @param  peer  is the PeerId of the RendezVousService peer.
     */
    public void disconnected(PeerID peer) {
        
        if (LOG.isDebugEnabled() ) {
            LOG.debug("disconnected from RendezVous" +
            "\n  group = " + group.getPeerGroupID() +
            "\n peerId = " + peer.toString() );
        }
    }
    
    
    /**
     * This method is called by the RendezVousService service in order
     * to provide advertisement about other RendezVousService peers.
     * This is usefull when a RendezVousService peer wants to balance its load
     * to other RendezVousService
     *
     * @param  adv  Description of Parameter
     */
    public void discovered(Advertisement adv) {
        
        if (LOG.isEnabledFor(Priority.DEBUG))
            LOG.debug("discovered: got new RendezVousService advertisement.");
        // Publish the rendez vous advertisement
        // First publish the advertisement
        if (discovery != null) {
            try {
                discovery.publish(adv, DiscoveryService.ADV);
            } catch (Exception e) {
            }
        }
    }
    
    
    /**
     *Description of the Method
     *
     * @param  timer  Description of Parameter
     */
    public synchronized void signal(JxtaTimer timer) {
        
        if (LOG.isEnabledFor(Priority.DEBUG))
            LOG.debug( "monitor daemon awakes for " + group.getPeerGroupID() );
        
        if (group.isRendezvous()) {
            // This peer is a rendezvous for the group. Let's publish an rdv adv
            publishAdv((PeerID) group.getPeerID(), LeaseRenewalDelay * 2 );
        }
        
        for (int i = 0; i < rdvs.size(); ++i) {
            RdV rdv = null;
            try {
                rdv = (RdV) rdvs.elementAt(i);
                long leaseRemaining = rdv.lease - System.currentTimeMillis();
                if ( leaseRemaining < LeaseMargin) {
                    rendezvous.reconnectToRendezVous(rdv.peer);
                }
            } catch (Exception e) {
                if (LOG.isEnabledFor(Priority.DEBUG))
                    LOG.debug("rdv renewal failed.", e );
                continue;
            }
        }
    }
    
    
    /**
     *Adds a feature to the RendezVousService attribute of the RdvMonitor object
     *
     * @param  peer   The feature to be added to the RendezVousService attribute
     * @param  lease  The feature to be added to the RendezVousService attribute
     */
    private synchronized void addRendezVous(String peer, long lease) {
        
        RdV rdv = new RdV(peer);
        if (rdvs.contains(rdv)) {
            // This is only a confirmation of a renewal of the lease.
            try {
                int index = rdvs.indexOf(rdv);
                if (index == -1) {
                    // This should not happen
                    if (LOG.isEnabledFor(Priority.DEBUG))
                        LOG.debug("addRendezVous cannot access RdV object");
                    return;
                }
                rdv = (RdV) rdvs.elementAt(index);
            } catch (Exception e) {
                if (LOG.isEnabledFor(Priority.DEBUG))
                    LOG.debug("addRendez failed [1] " + e);
            }
        } else {
            // This is a new Rendezvous
            rdvs.addElement(rdv);
        }
        // Set the lease
        if ( lease < 0 ) {
            // The rendez has given an infinite lease.
            // However, it is a good thing to still renew the lease once in a while
            lease = 60 * 60 * 1000;
            // 1 Hour
        }
        rdv.lease = System.currentTimeMillis() + lease;
    }
    
    
    /**
     *Description of the Method
     *
     * @param  gid      Description of Parameter
     * @param  rdvid    Description of Parameter
     * @param  timeout  Description of Parameter
     * @return          Description of the Returned Value
     */
    private RdvAdvertisement createRdvAdv(PeerGroupID gid,
    PeerID rdvid,
    long timeout) {
        
        RdvAdvertisement adv = null;
        try {
            // Create a rendezvous advertisement
            adv = (RdvAdvertisement)
            AdvertisementFactory.newAdvertisement(
            RdvAdvertisement.getAdvertisementType());
        } catch (Exception all) {
            if (LOG.isEnabledFor(Priority.WARN))
                LOG.warn("Advertisement document could not be created");
            return null;
        }
        
        adv.setGroupID(gid);
        adv.setPeerID(rdvid);
        
        return adv;
    }
    
    protected void startRdv() {
        publishAdv((PeerID) group.getPeerID(), LeaseRenewalDelay);
    }
    
    /**
     *  Publish a Rendezvous advertisment into the parent group of our peer
     *  group.
     *
     * @param  pid    The peer which is a rendezvous.
     * @param  lease  The duration of publication.
     */
    private void publishAdv(PeerID pid, long lease) {
        RdvAdvertisement rdv = createRdvAdv((PeerGroupID) group.getPeerGroupID(),
                                            pid,
					    lease);
        
        if (rdv == null) {
            if (LOG.isEnabledFor(Priority.DEBUG))
                LOG.debug("could not construct advertisement");
            return;
        }
        
        // Publish into the parent group.
        PeerGroup parent = ((RefPeerGroup) group).getParentGroup();
        if (parent == null) {
            // No parent? publish into ourself.
            parent = group;
        }
        
        if (LOG.isDebugEnabled() ) {
            LOG.debug("Publish RdvAdvertisement into group : " + parent.getPeerGroupID().toString() +
            "\n        gid = " + group.getPeerGroupID().toString() +
            "\n        pid = " + pid.toString() +
            "\n      lease = " + (lease / 1000) + " secs" );
        }
        
        DiscoveryService parentDiscovery = parent.getDiscoveryService();
        if (parentDiscovery == null) {
            if (LOG.isEnabledFor(Priority.WARN))
                LOG.warn("Cannot access parent's Discovery Service");
            return;
        }
        
        try {
            parentDiscovery.publish(rdv,
                                    DiscoveryService.ADV,
				    lease,
				    lease);
        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.WARN))
                LOG.warn("Could not locally publish advertisement", e);
        }
    }
}


⌨️ 快捷键说明

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