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

📄 relayclientmessagesender.java

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

import java.io.*;

//PDA requirements 21.02.2002
//package net.jxta.impl.endpoint.servlethttp.* is not ported to PDA
//import net.jxta.impl.endpoint.servlethttp.MessageSender;
//PDA requirements 21.02.2002
import net.jxta.impl.util.BoundedQueue;
import net.jxta.endpoint.EndpointMessenger;
import net.jxta.endpoint.EndpointService;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.EndpointAddress;

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

/**
 * MesseageSender that sends messages to peers that are connected to the
 * relay running on the local peer.
 */
//PDA requirements 21.02.2002
//package net.jxta.impl.endpoint.servlethttp.* is not ported to PDA
public class RelayClientMessageSender /*implements MessageSender*/ {
//PDA requirements 21.02.2002

    private static final Category LOG = Category.getInstance(RelayClientMessageSender.class.getName());

    /** The router server. Will always be non-null **/
    RelayServer relayServer = null;

    /** the local address that this sender puts on messages **/
    private EndpointAddress localAddress = null;

    /**
     * Constructs the sender given the router server and the local address
     * that you want to be known by to other people. This is used
     * for address outgoing messages.  XXX Note: I don't like this model
     * at all with the new transport framework. The endpoint should probably
     * address the messages. Jbeatty. **/
    public RelayClientMessageSender (RelayServer relayServer,
				     EndpointAddress localAddress) {
	this.relayServer = relayServer;
	this.localAddress = localAddress;
    }

    /**
     * Returns a new messenger for the destination peer.
     * @param dest Contains the destionation's peer id
     * @param destPeerIdUnused <b>This parameter is unused. XXX fixme</b>
     * @param type This parameter is unused
     * @throws IOException This exception is thrown when the destination peer
     *   does not have a valid lease.
     */
    public EndpointMessenger getMessenger (EndpointAddress destAddr)
        throws IOException {

	if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Getting messenger for EndpointAddress: " + destAddr);
        String clientPeerId = getPeerIdFromEndpointAddress (destAddr);
	if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Getting messenger for Peer: " + clientPeerId );
        if (!relayServer.hasLease (clientPeerId)) {
            throw new IOException ("Destination peer has no lease");
	}

	return new RelayClientMessenger (relayServer, localAddress, destAddr);
    }


    /**
     * We do not propagate messages to relay clients
     */
    public void propagate (Message msg,
                           String serviceName,
                           String serviceParams,
                           String prunePeer) throws IOException {

	return;
    }


    /**
     * There is no initialization that we need to do
     */
    public void init(EndpointService p) {
	// no code inteded
    }

    /**
     * The protocol name for router clients is simply "peer"
     */
    public String getProtocolName() {
	return "http";
    }

    /** Return true if the peer currently has a lease; false otherwise **/
    public boolean ping (EndpointAddress addr) {
	boolean retval = false;
	String peerId = null;
	try {
	    peerId = getPeerIdFromEndpointAddress (addr);
	    retval = relayServer.hasLease (peerId);
	} catch(IOException e) {
	    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("EndpointAddress malformed: " + addr);
	}
	if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Ping for " + peerId + " is " + retval);
	return retval;
    }

    /** Returns the peer id given an endpoint address of a router client **/
    private static String getPeerIdFromEndpointAddress (EndpointAddress addr)
	throws IOException {

	String peerId = addr.getProtocolAddress().substring("JxtaHttpClient".length());
	return peerId;
    }

    /** XXX okay, this is half-baked. It's more complicated than this... */
    public boolean isConnectionOriented () {
	return true;
    }


    /**
     * A messenger that simply enqueues messages. This messenger is constructed
     * with a particular queue. The routers will be watching these queues
     * and actually do the work.
     */
    static class RelayClientMessenger implements EndpointMessenger {

	/** the relay server that this messenger is using **/
	RelayServer svr = null;

	/** the local address that the messenger will stick on messages **/
	EndpointAddress localAddress = null;

	/** the destination address **/
	EndpointAddress destAddress = null;

	/** the particular client that this messenger is for **/
	String clientPeerId = null;

	/** we cache this for performance **/
	BoundedQueue queue = null;

	/** Constructs a RelayClientMessenger given the svr and
	    clientPeerId **/
	public RelayClientMessenger (RelayServer svr,
				     EndpointAddress localAddress,
				     EndpointAddress destAddress)
	    throws IOException {

	    this.svr = svr;
	    this.destAddress = destAddress;
	    this.localAddress = localAddress;

	    // snag and cache the peer id from the destination address and
	    // the queue for that client.
	    clientPeerId = RelayClientMessageSender.getPeerIdFromEndpointAddress (destAddress);
	    queue = svr.getOutboundMessageQueueByPeerId (clientPeerId);
	}

	/**
	 * enqueues the message after checking to see if the client still has
	 * a valid lease. The relay client must still have a valid; if
	 * it does not, an IOException is thrown. This method will block for
	 * period of time if the message queue is full; if the queue does not
	 * have space within a specified timeout, IOException will be thrown
	 * (note: we currently are not bounding the relay client's queues, so
	 * this will not currently happen).
	 *
	 * @throws IOException This exception is thrown if the relay client
	 *   that this messenger is for
	 */
	public void sendMessage (Message msg) throws IOException {
	    if (svr.hasLease (clientPeerId)) {
		try {
		    // puts the address information into the message
		    addressMessage (msg);

		    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("enqueing message");
		    queue.enqueue (msg, BoundedQueue.DROP_OLDEST);
		    if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("done enqueueing message");
		} catch (InterruptedException e) {
		    // the queue we're using right now isn't bounded, so
		    // we actually don't need to worry about this exception
		    // occuring right now.
		    if (LOG.isEnabledFor(Priority.INFO)) LOG.info ("Message send interrupted; message thrown away");
		}
	    } else {
		throw new IOException("Client peer no longer has a lease");
	    }
	}

	public void close() {
	    // no code intended. There is nothing to close.
	}

	/** Addresses the message **/
	private void addressMessage (Message msg) {
	    msg.setDestinationAddress (destAddress);
	    msg.setSourceAddress (localAddress);
	}
    }
}

⌨️ 快捷键说明

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