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

📄 endpointserviceimpl.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/************************************************************************
 *
 * $Id: EndpointServiceImpl.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;

import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.Document;
import net.jxta.document.Element;
import net.jxta.document.TextElement;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.EndpointService;
import net.jxta.endpoint.EndpointAddress;
import net.jxta.endpoint.EndpointProtocol;
import net.jxta.endpoint.EndpointMessenger;
import net.jxta.endpoint.EndpointListener;
import net.jxta.endpoint.EndpointFilterListener;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.MessageElementEnumeration;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.ID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.service.Service;
import net.jxta.util.StringEnumeration;
import net.jxta.impl.peergroup.RefPeerGroup;

import java.io.*;
import java.util.*;
import java.lang.reflect.InvocationTargetException;
import org.apache.log4j.Category; import org.apache.log4j.Priority;

/**
 * This class implements the frontend for all the JXTA
 * endpoint protocols, as well as the API for the implementation
 * of the core protocols that use directely the EndpointService
 */

public class EndpointServiceImpl implements EndpointService {
    
    private static final Category LOG = Category.getInstance(EndpointServiceImpl.class.getName());
    
    private Hashtable protocols = new Hashtable();
    private Hashtable listeners = new Hashtable();
    private Hashtable incomingFilterListeners = new Hashtable();
    private Hashtable outgoingFilterListeners = new Hashtable ();
    private EndpointService parentEndpoint = null;
    
    private boolean runRouter = true;
    private boolean routerInitialized = false;

    private Advertisement implAdv = null;
    private ID assignedID = null;

    private PeerGroup group = null;
    private String localPeerId = null;
    
    private Hashtable inboundTraffic = new Hashtable();
    private Hashtable outboundTraffic = new Hashtable();
    
    /**
     * Time in seconds since midnight, January 1, 1970 UTC of when
     * this peer last sent a message out
     */
    private int lastMessageSentAt = 0;
    
    /**
     * Time in seconds since midnight, January 1, 1970 UTC of when
     * this peer last recieved a message
     */
    private int lastMessageRecdAt = 0;
    
    /**
     * Traffic recording is disabled by default, in the interest of
     * efficiency. The first PIP request to getTrafficChannels enables
     * it. We can go further and disable it after a timeout if no PIP
     * request has been received for some time (TODO).
     */
    private boolean isTrafficRecordingEnabled = false;
    
    private static final String EndpointHeaderSrcPeer = "jxta:EndpointHeaderSrcPeer";
    
    /**
     * Service objects are not manipulated directly to protect usage
     * of the service. A Service interface is returned to access the service
     * methods.
     *
     * @return Service public interface of the service
     *
     * @since JXTA 1.0
     */
    public Service getInterface() {
        return new EndpointServiceInterface(this);
    }
    
    /**
     * Returns the advertisment for this service.
     *
     * @return Advertisement the advertisement.
     *
     * @since JXTA 1.0
     */
    public Advertisement getImplAdvertisement() {
        return implAdv;
    }
    
    public EndpointServiceImpl() {
    }
    
    /**
     *  Initialize the application passing it its peer group and advertisement.
     *
     *  @param group PeerGroup this application is started from
     *  @param adv The advertisement for this application
     *
     *  @exception PeerGroupException failure to initialize this application.
     *
     * @since   JXTA 1.0
     */
    public void init(PeerGroup group, ID assignedID, Advertisement impl)
        throws PeerGroupException {
        // There's no config of interrest in our implAdv, but we must be able
        // to return it, if queried. We also need our assigned ID; that's the
        // selector for the element of the peer adv that we have to update.
        this.implAdv = (ModuleImplAdvertisement) impl;
        this.assignedID = assignedID;
        this.group = group;
        this.localPeerId = group.getPeerID().toString();

        PeerGroup parentGroup = ((RefPeerGroup) group).getParentGroup();
        if (parentGroup == null) return;
        parentEndpoint = parentGroup.getEndpointService();
        if (parentEndpoint == null) return;
        
        // To speed things up, each endpoint has the list of all
        // the endpoint protocols at or below its own level. Grab them, now.
        // This way, propagate, send message
        // or getEndpointProtocols does not have to be recursive.
        // Limitation: for now, endpoints have no way to notify their
        // descendants of any change.
        // On the other end, addListener HAS TO be propagate down the
        // stack, so that demux does not have to be recursive...same idea.
        
        Enumeration protosBelow = parentEndpoint.getEndpointProtocols();
        while (protosBelow.hasMoreElements()) {
	    addEndpointProtocol((EndpointProtocol) protosBelow.nextElement());
	}
    }
    
    /**
     *  Some applications will wait for start() being called, before proceeding
     *  beyond a certain point. That's also the opportunity to supply
     *  arguments.
     *
     *  @params args An array of Strings forming the parameters for this
     *  application.
     *
     *  @return int status indication.
     */
    public int startApp(String[] args) {
        return 0;
    }
    
    /**
     * We are asked to stop. Do nothing; the protocols and services are going
     * to be stopped as well. When they are, they will unreference us and
     * we'll go into oblivion. That's all that's needed.
     */
    public void stopApp() {
    }
    
    /**
     * Returns the group to which this EndpointServiceImpl is attached.
     *
     * @return PeerGroup the group.
     */
    public PeerGroup getGroup() {
        return group;
    }

    private synchronized void addProtoToAdv(EndpointProtocol proto) {
        PeerAdvertisement padv = group.getPeerAdvertisement();
        StructuredDocument myParam =
            (StructuredDocument) padv.getServiceParam(assignedID);

        if (myParam == null) {
                myParam =
                    StructuredDocumentFactory.newStructuredDocument
                    (new MimeMediaType("text", "xml"), "Parm");
        }

        Element a =
            myParam.createElement("Addr", proto.getPublicAddress().toString());

        myParam.appendChild(a);
        padv.putServiceParam(assignedID, myParam);
    }

    private void clearProtoFromAdv(EndpointProtocol proto) {
        PeerAdvertisement padv = group.getPeerAdvertisement();
        StructuredDocument myParam = padv.getServiceParam(assignedID);
        if (myParam == null) return;

        StructuredDocument newParam =
            StructuredDocumentFactory.newStructuredDocument
            (new MimeMediaType("text", "xml"), "Parm");

        String addrToRemove = proto.getPublicAddress().toString();
        Enumeration addresses = myParam.getChildren();
        while (addresses.hasMoreElements()) {
            TextElement a = (TextElement) addresses.nextElement();
            if (! (   a.getName().equals("Addr")
                   && a.getTextValue().equals(addrToRemove))) {
                Element n = newParam.createElement(a.getName(),
                                                   a.getTextValue());
                newParam.appendChild(n);
            }
        }
        padv.putServiceParam(assignedID, newParam);
    }

    /**
     * Installs the given endpoint protocol in this endpoint.
     *
     * The endpoint protocol becomes usable by the endpoint service to send
     * unicast messages and optionaly propagation and ping messages. The
     * endpoint service becomes usable by this protocol to handle incoming
     * messages.
     *
     * If an endpoint protocol by the same name exists in one of the parent
     * endpoint services, this protocol takes precedence and hides the other.
     *
     * If an endpoint protocol by that name exists in this endpoint service,
     * addEndpointProtocol fails and throws IllegalArgumentException.
     *
     * @param proto the endpoint protocol to be installed.
     */
    public synchronized void addEndpointProtocol (EndpointProtocol proto)
        throws IllegalArgumentException
    {
        if (protocols.contains (proto.getProtocolName())) {

⌨️ 快捷键说明

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