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

📄 xfer.java

📁 有关java 的p2p应用,是一般很好的教程,有兴趣的朋友应该好好阅读一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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: xfer.java,v 1.24 2006/03/15 20:23:24 bondolo Exp $ */package net.jxta.impl.shell.bin.xfer;import java.io.File;import java.io.PrintWriter;import java.io.StringWriter;import java.net.URI;import java.util.Enumeration;import java.util.HashSet;import java.util.Set;import java.io.IOException;import java.net.URISyntaxException;import net.jxta.discovery.DiscoveryService;import net.jxta.document.AdvertisementFactory;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.Message;import net.jxta.endpoint.StringMessageElement;import net.jxta.id.IDFactory;import net.jxta.peer.PeerID;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.pipe.PipeService;import net.jxta.pipe.OutputPipe;import net.jxta.protocol.PipeAdvertisement;import net.jxta.impl.shell.GetOpt;import net.jxta.impl.shell.ShellApp;import net.jxta.impl.shell.ShellEnv;import net.jxta.impl.shell.ShellObject;/** *  send a file from one peer to another. destination may be an endpoint address *  or a pipe. * **/public class xfer extends ShellApp {        static final String XFERFILEINFO_ELEMENT = "JxtaXfer:Fileinfo";    static final String XFERIDENTIFIER_ELEMENT = "JxtaXfer:Identifier";    static final int XFERFILEINFO_VERSION = 2;    static final String XFERSEQUENCE_ELEMENT = "JxtaXfer:Sequence";    static final String XFERDATA_ELEMENT = "JxtaXfer:Data";        private ShellEnv env = null;        private static final long MAX_TRANSMIT_TIME = 5 * 60 * 1000; // 5 Minutes    private static final long MAX_SEARCH_TIME = 1 * 60 * 1000; // 1 Minutes    private static final int WAITINGTIME = 2 * 1000; // 2 seconds    private static final int  MAXRETRIES = 5;    public static final String XFERUSERNAME = "JxtaXferUserName";    public static final String SftpIDTag = "JxtaSftpPipeID";    private static final String ENVNAME = "jxtaxfer";    private static final String XFERSERVICENAME = "jxtaxfer";        PeerGroup group;    DiscoveryService discovery;        private int syntaxError() {        consoleMessage( getCmdShortName() +        "\n\t( (\"register\" [-s|-p] userName) |" +        "\n\t( (\"login\" [-e] userName) |" +        "\n\t( (\"logout\" userName) |" +        "\n\t( (\"send\" [-b blockSize] [-d destAddr] [[-p destPID]] [-s userName] [-a] destUserName filename) |" +        "\n\t( (\"search\") )" );                return ShellApp.appParamError;    }        public String getDescription() {        return "Send a file to another peer";    }        public void help() {        println("NAME");        println("    xfer - send a file to another peer ");        println(" ");        println("SYNOPSIS");        println(" ");        println("xfer  ( (\"register\" [-s|-p] userName) |");        println("      ( (\"login\" [-e] userName) |" );        println("      ( (\"logout\" userName) |" );        println("      ( (\"send\" [-b blockSize] [-d destAddr] [-s userName] [-a] destUserName filename) |" );        println("      ( (\"search\") )" );        println(" ");        println("DESCRIPTION");        println(" ");        println("    The 'xfer' command implements a file transfer between peers. The file transfer ");        println("    can be completed using either pipes or via direct endpoint communication. ");        println(" ");        println("OPTIONS");        println(" ");        println("    register    Register a new user name  ");        println("          [-s]             Use a secure pipe.");        println("          [-p]             Use a propagate pipe.");        println("          username         The name of the new user.");        println(" ");        println("    login       Login user");        println("          [-e]             Create an Endpoint Listener rather than an Input Pipe Listener.");        println("          username         The name of the user.");        println(" ");        println("    logout      Logout user");        println("          username         The name of the user.");        println(" ");        println("    send        Send a file ");        println("          [-b blockSize]   Chunk size in bytes to break file into.");        println("          [-d destAddr]    Destination endpoint.");        println("          [[-p peerID]]    Destination peer id for pipe. Can be repeated.");        println("          [-s userName]    Source user name. Receiver will see this user as the sender.");        println("          [-a]             Asynchronously send file. Used with Endpoint Listener mode.");        println("          destUserName     The name of the intended recipient.");        println("          filename         The path of a local file to be sent.");        println(" ");        println("    search      Search for users.");        println(" ");        println("EXAMPLE");        println(" ");        println("      JXTA>xfer register me");        println("      JXTA>xfer login me");        println("      JXTA>xfer send -s me you /tmp/nihow.jpg");        println(" ");        println("This example shows how a new user 'me'  can register and log into xfer,");        println("and send a file to the user 'you'. User 'you' needs to be similarly");        println("registered and logged on. The above file is written as /xfer/nihow.jpg");        println(" ");        println("SEE ALSO");        println(" ");        println("   talk, sftp");        println(" ");    }        public int startApp(String[] argv) {        env = getEnv();        ShellObject obj = env.get("stdgroup");                group = (PeerGroup) obj.getObject();        discovery = group.getDiscoveryService();                // start processing args                if ( argv.length < 1 ) {            return syntaxError();        }                String command = argv[0].toLowerCase();                if( "register".equals( command ) ) {            boolean secure = false;            boolean propagate = false;            String userName = null;                        if( argv.length < 2 ) {                consoleMessage( "Missing <userName>" );                return syntaxError();            }                        GetOpt options = new GetOpt( argv, 1, "ps" );                        while( true ) {                int option = options.getNextOption();                                if( -1 == option ) {                    break;                }                                switch( option ) {                    case 'p' :                        propagate = true;                        break;                                            case 's' :                        secure = true;                        break;                                            default :                        consoleMessage( "Unrecognized option" );                        return syntaxError();                }                                if( propagate && secure ) {                    consoleMessage( "Secure or propagate but not both allowed" );                    return syntaxError();                }            }                        userName = options.getNextParameter();                        if( null == userName ) {                consoleMessage( "Missing <userName>" );                return syntaxError();            }                        String pipeType = PipeService.UnicastType;                        if( secure ) {                pipeType = PipeService.UnicastSecureType;            }                        if( propagate ) {                pipeType = PipeService.PropagateType;            }                        return registerNewUser( userName, pipeType );        } else if( "login".equals( command ) ) {            boolean endpoint = false;            String userName = null;                        if( argv.length < 2 ) {                consoleMessage( "Missing userName" );                return syntaxError();            }                        GetOpt options = new GetOpt( argv, 1, "e" );                        while( true ) {                int option = options.getNextOption();                                if( -1 == option ) {                    break;                }                                switch( option ) {                    case 'e' :                        endpoint = true;                        break;                                            default :                        consoleMessage( "Unrecognized option" );                        return syntaxError();                }            }                        userName = options.getNextParameter();                        if( null == userName ) {                consoleMessage( "Missing userName" );                return syntaxError();            }                        return login( userName, endpoint );        } else if( "logout".equals( command ) ) {            String userName = null;                        if( argv.length < 2 ) {                consoleMessage( "Missing userName" );                return syntaxError();            }                        GetOpt options = new GetOpt( argv, 1, "" );                        while( true ) {                int option = options.getNextOption();                                if( -1 == option ) {                    break;                }                                switch( option ) {                    default :                        consoleMessage( "Unrecognized option" );                        return syntaxError();                }            }                        userName = options.getNextParameter();                        if( null == userName ) {                consoleMessage( "Missing userName" );                return syntaxError();            }                        return logout( userName );        } else if( "send".equals( command ) ) {            int blockSize = 15360;            EndpointAddress destAddr = null;            Set destPIDs = new HashSet();            String srcUserName = null;            String destUserName = null;            String filename = null;            boolean async = false;                        if( argv.length < 3 ) {                consoleMessage( "Missing destination or filename" );                return syntaxError();            }                        GetOpt options = new GetOpt( argv, 1, "ab:d:s:p:" );                        while( true ) {                int option = options.getNextOption();                                if( -1 == option ) {                    break;                }                                switch( option ) {                    case 'b' :                        try {                            blockSize = Integer.parseInt( options.getOptionArg() );                        } catch( NumberFormatException badnum ) {                            consoleMessage( "bad value for block size" );                            return syntaxError();                        }                                                if( blockSize < 1 ) {                            consoleMessage( "bad value for block size" );                            return syntaxError();                        }                        break;                                            case 'd' :                        try {                            destAddr = new EndpointAddress( options.getOptionArg() );                        } catch( Throwable bad ) {                            consoleMessage( "bad endpoint address" );                            return syntaxError();                        }                        break;                                            case 's' :                        srcUserName = options.getOptionArg();                        break;                                            case 'a' :                        async = true;                        break;                                            case 'p' :                        try {                            URI peerID =  new URI( options.getOptionArg() );                            destPIDs.add( (PeerID) IDFactory.fromURI( peerID ) );                        } catch ( ClassCastException badID ) {                            consoleMessage( "ID is not a peer ID" );                            return syntaxError();                        } catch (URISyntaxException badID ) {                            consoleMessage( "bad peer ID" );                            return syntaxError();                        }                        break;                                            default :                        consoleMessage( "Unrecognized option" );                        return syntaxError();                }            }                        destUserName = options.getNextParameter();                        if( null == destUserName ) {                consoleMessage( "Missing userName" );

⌨️ 快捷键说明

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