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

📄 jxtabeepprofile.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/*
 * 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.
 *
 * $Id: JxtaBeepProfile.java,v 1.3 2001/11/07 23:20:51 jice Exp $
 */

package net.jxta.impl.endpoint.beep;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import java.io.IOException;

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

import org.beepcore.beep.core.Channel;
import org.beepcore.beep.core.DataStream;
import org.beepcore.beep.core.Message;
import org.beepcore.beep.core.Session;
import org.beepcore.beep.core.StartChannelListener;
import org.beepcore.beep.core.StringDataStream;
import org.beepcore.beep.lib.MessageQueue;
import org.beepcore.beep.profile.Profile;
import org.beepcore.beep.profile.ProfileConfiguration;

import org.beepcore.beep.core.BEEPError;
import org.beepcore.beep.core.BEEPException;
import org.beepcore.beep.core.CloseChannelException;
import org.beepcore.beep.core.StartChannelException;

import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.EndpointService;

import net.jxta.impl.endpoint.MessageWireFormatFactory;

/**
 * This is the JXTA profile implementation
 *
 * @version $Revision: 1.3 $
 */
public class JxtaBeepProfile extends Thread implements StartChannelListener, Profile {
    /**
     *  Log4J categorgy
     **/
    private static final Category LOG = Category.getInstance(JxtaBeepProfile.class.getName());
    
    /**
     *  The URI for this profile. Essentially just a canonical string.
     **/
    public static final String JXTA_URI =
    "http://xml.resource.org/profiles/JXTA/Beep";
    
    /**
     *  The endpoint this transport is working for.
     **/
    private EndpointService endpoint = null;
    
    /**
     *  Message queue which our thread waits on for messages.
     **/
    private MessageQueue messages = new MessageQueue();
    
    /**
     *  Default constructor.
     **/
    public JxtaBeepProfile () {
        super( "JxtaBeepProfile Receive Daemon" );
        }
    
        /**
         * Post construction Init.
         *
         * @param uri The profile this instance is handling.
         * @param config Configuration information for this instance
         * @throws BEEPException In the event of an error.
         * @return StartChannelListener this profile will listen for channel opens to itself. Mostly
         * due to bad config or tuning profiles, which aren't currently supported by this
         * profile.
         */
    public StartChannelListener init(String uri, ProfileConfiguration config)
    throws BEEPException {
        this.start();
        return this;
    }
    
    /**
     *  For this profile set the endpoint its working for.
     *
     *  @param endpoint EndpointService that this profile is working for.
     *  @return EndpointService the previous endpoint for this profile or null if none.
     **/
    EndpointService SetEndpoint( EndpointService endpoint ) {
        EndpointService old = this.endpoint;
        this.endpoint = endpoint;
        
        return old;
    }
    
    /**
     * Method startChannel
     *
     *
     * @param channel The channel being started
     * @param encoding encoding being used on the channel
     * @param data data listener to use
     * @throws StartChannelException unknown
     */
    public void startChannel(Channel channel, String encoding, String data)
    throws StartChannelException {
        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "JxtaCCL StartChannel Callback");
        channel.setDataListener(messages);
    }
    
    /**
     * Method closeChannel
     *
     *
     * @param channel channel being closed
     * @throws CloseChannelException thrown if the channel cannot be closed. the channel will be left open.
     */
    public void closeChannel(Channel channel) throws CloseChannelException {
        if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "JxtaCCL CloseChannel Callback");
        channel.setDataListener(null);
        channel.setAppData(null);
    }
    
    /**
     *    Returns true if this profile is public. Must be dynamic beause tuning profiles
     * may render certain profile combinations unusable.
     * @param session session which the profile is to be advertised on.
     * @return boolean true if the profile is public otherwise false.
     */
    public boolean advertiseProfile(Session session) {
        return true;
    }
    
    /**
     *  Our daemon thread which waits for messages and then processes them.
     **/
    public void run() {
        
        try {
            while( true ) {
                Message message;
                
                try {
                    message = messages.getNextMessage();
                } catch( InterruptedException woken ) {
                    Thread.interrupted();
                    // FIXME    20010723    bondolo@jxta.org    we should really
                    // establish  terminating conditions here
                    continue;
                }
                
                receiveMSG( message );
            }
        } catch ( Throwable all ) {
            if (LOG.isEnabledFor(Priority.FATAL)) LOG.fatal( "Uncaught Throwable in thread :" + Thread.currentThread().getName(), all );
        }
        
    }
    
    /**
     *  process a message.
     *
     *  @param  message Message to process.
     **/
    private void receiveMSG( Message message ) {
        
        DataStream ds = message.getDataStream();
        InputStream is = ds.getInputStream();
        
        try {
            net.jxta.endpoint.Message msg = endpoint.newMessage();
            MessageWireFormatFactory.newMessageWireFormat( 
            new MimeMediaType( "application/x-jxta-msg" ) ).readMessage(is, msg);

            // Give the message to the EndpointService Manager
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("  handing message to EndpointService");
            endpoint.demux(msg);
            
            // XXX 20010924 bondolo@jxta.org As of the 0.96 release of beep
            // this is mechanism for replying/acking is not reccommended.
            try {
                message.sendRPY(new StringDataStream("OK") );
            } catch (BEEPException e) {
                try {
                    message.sendERR(BEEPError.CODE_REQUESTED_ACTION_ABORTED,
                    "Error sending RPY");
                } catch (BEEPException x) {
                    message.getChannel().getSession().terminate(x.getMessage());
                }
                return;
            }
            
        } catch (IOException e) {
            message.getChannel().getSession().terminate(e.getMessage());
            return;
        }
    }
    
}

⌨️ 快捷键说明

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