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

📄 fixfingertask.java

📁 一个对等计算发生器
💻 JAVA
字号:
/***************************************************************************
 *                                                                         *
 *                            FixFingerTask.java                           *
 *                            -------------------                          *
 *   date                 : 16.08.2004                                     *
 *   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.service.impl;

import java.util.Random;

import de.uniba.wiai.lspi.chord.com.Node;
import de.uniba.wiai.lspi.chord.data.ID;
import de.uniba.wiai.lspi.util.logging.Logger;

/**
 * Looks up the node for a certain ID and stores the reference of the
 * responsible node in the local finger table.
 * 
 * @author Sven Kaffille, Karsten Loesing * @version 1.0.1
 */
final class FixFingerTask implements Runnable {

	/**
	 * Instance of random generator for randomly picking another finger to fix.
	 */
	private Random random = new Random();

	/**
	 * Parent object for invoking findSuccessor.
	 */
	private NodeImpl parent;

	/**
	 * Object logger.
	 */
	private Logger logger;

	/**
	 * Copy of the local node's ID for determining which ID to look up.
	 */
	private ID localID;

	/**
	 * Reference on routing table.
	 */
	private References references;

	/**
	 * Creates a new instance, but without starting a thread running it.
	 * 
	 * @param parent
	 *            Parent object for invoking findSuccessor.
	 * @param localID
	 *            Copy of the local node's ID for determining which ID to look
	 *            up.
	 * @param references
	 *            Reference on routing table.
	 * @throws NullPointerException
	 *             If either of the parameters has value <code>null</code>.
	 */
	FixFingerTask(NodeImpl parent, ID localID, References references) {
		if (parent == null || localID == null || references == null) {
			throw new NullPointerException(
					"Neither parameter of constructor may be null!");
		}
		this.logger = Logger.getLogger(FixFingerTask.class + "." + localID);

		this.parent = parent;
		this.localID = localID;
		this.references = references;
	}

	public void run() {

		try {

			int nextFingerToFix = this.random.nextInt(this.localID.getLength());

			this.logger.info("fixFingers tries to get finger for key "
					+ this.localID.addPowerOfTwo(nextFingerToFix)
							.toHexString(1));

			// look up reference
			ID lookForID = this.localID.addPowerOfTwo(nextFingerToFix);
			Node newReference;
			// try {
			newReference = this.parent.findSuccessor(lookForID);
			// } catch (CommunicationException e) {
			// this.logger.warn("Attempt to fix finger table for ID "
			// + lookForID + " did not succeed!");
			// return;
			// }

			// add new reference to finger table, if not yet included
			if (newReference != null
					&& !this.references.containsReference(newReference)) {
				this.logger.info("Adding new reference "
						+ newReference.getNodeID().toHexString(2));
				this.references.addReference(newReference);
			}

			this.logger.debug("Invocation of fix fingers was successful");

		} catch (Exception e) {
			this.logger.error(
					"Exception caught in FixFingerTask! From now on, "
							+ "periodic maintenance tasks are not executed "
							+ "any more!", e);
		}
	}
}

⌨️ 快捷键说明

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