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

📄 joinnetwork.java

📁 一个对等计算发生器
💻 JAVA
字号:
/***************************************************************************
 *                                                                         *
 *                             JoinNetwork.java                            *
 *                            -------------------                          *
 *   date                 : 09.09.2004, 11:16                              *
 *   copyright            : (C) 2004/2005 Distributed and                  *
 *								Mobile Systems Group                       *
 *                              Lehrstuhl fuer Praktische Informatik       *
 *                              Universitaet Bamberg                       *
 *                              http://www.lspi.wiai.uni-bamberg.de/       *
 *   email                : sven.kaffille@wiai.uni-bamberg.de              *
 *   						karsten.loesing@wiai.uni-bamberg.de            *
 *                                                                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   A copy of the license can be found in the license.txt file supplied   *
 *   with this software or at: http://www.gnu.org/copyleft/gpl.html        *
 *                                                                         *
 ***************************************************************************/

package de.uniba.wiai.lspi.chord.console.command;

import java.io.PrintStream;

import de.uniba.wiai.lspi.util.console.Command;
import de.uniba.wiai.lspi.util.console.ConsoleException;
import de.uniba.wiai.lspi.chord.data.URL;
import de.uniba.wiai.lspi.chord.service.Chord;

import java.net.MalformedURLException;
import java.util.Iterator;

/**
 * {@link Command} to join a remote chord network. </br>
 * {@link Chord#join(URL, URL)}. 
 * 
 * <p>
 * To get a description of this command type <code>joinN -help</code> 
 * into the {@link de.uniba.wiai.lspi.chord.console.Main console}.
 * </p>
 * 
 * @author  sven
 * @version 1.0.1
 */
public class JoinNetwork extends Command {
   
	/**
	 * The name of this {@link Command}. 
	 */
    public static final String COMMAND_NAME = "joinN";
    
    /**
     * The name of the parameter that defines the bootstrap node to use. 
     */
    public static final String BOOTSTRAP_PARAM = "bootstrap";
    
    /**
     * The port on which the local node should listen. 
     */
    public static final String PORT_PARAM = "port";
    
    /** Creates a new instance of CreateNodes 
     * @param toCommand11 
     * @param out1 */
    public JoinNetwork(Object[] toCommand11, PrintStream out1) {
        super(toCommand11, out1);
    }
    
    public void exec() throws ConsoleException {
        
        int port = -1; 
        if (this.parameters.containsKey(PORT_PARAM)) {
            try {
                port = Integer.parseInt(this.parameters.remove(PORT_PARAM));
            } catch (NumberFormatException e) {
                throw new ConsoleException("Port is no integer value! " + e.getMessage()); 
            }
        }
        
        if (!this.parameters.containsKey(BOOTSTRAP_PARAM)){
            this.out.println("Creating new chord overlay network!");
        }
        String bootStrap = this.parameters.remove(BOOTSTRAP_PARAM);
        
        if (this.parameters.size() > 0) {
            String msg = "Too many parameters. Unknown parameters: ";
            Iterator<String> params = this.parameters.keySet().iterator();
            while(params.hasNext()) {
                msg += params.next() + " "; 
            }
            throw new ConsoleException(msg); 
        }
        
        URL bootstrapURL = null;
        if (bootStrap != null) {
            try {
                bootstrapURL = new URL(URL.KNOWN_PROTOCOLS[URL.SOCKET_PROTOCOL] 
                        + "://" + bootStrap + "/");
            } catch (MalformedURLException e) {
                throw new ConsoleException("URL " + bootStrap + " provided by "
                        + BOOTSTRAP_PARAM
                        + " parameter is malformed!", e);
            }
            this.out.println("Trying to join chord network with boostrap URL " + bootstrapURL );
        }
        RemoteChordNetworkAccess remote
                = (RemoteChordNetworkAccess)this.toCommand[1];
        try {
            remote.join(bootstrapURL, port); 
        } catch (Exception e) {
        	e.printStackTrace(this.out); 
            throw new ConsoleException("Join/Creation of network failed. " 
                    + "Reason: " + e.getMessage(), e); 
        }
        this.out.println("URL of created chord node " 
                + remote.getChordInstance().getURL() + "."); 
    }
    
    public String getCommandName() {
        return COMMAND_NAME;
    }
    
    public void printOutHelp() {
        this.out.println("The " + COMMAND_NAME + " command creates a chord node \n"
                + "to which remote nodes can connect.");
        this.out.println("______________"); 
        this.out.println("Parameters: ");
        this.out.println("'" + BOOTSTRAP_PARAM + "' takes a part of an URL of a remote chord \n" 
                + "node, that is then used as bootstrap node. \n" 
                + "If no bootstrap node is provided a new chord network " 
                + "is created. \n The parameter must be in the form hostname:port");
    }
    
}

⌨️ 快捷键说明

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