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

📄 stabilizetask.java

📁 一个对等计算发生器
💻 JAVA
字号:
/***************************************************************************
 *                                                                         *
 *                            StabilizeTask.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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

/**
 * Invokes notify method on successor.
 * 
 * @author Karsten Loesing
 * @version 1.0.1
 */
final class StabilizeTask implements Runnable {

	/**
	 * Parent object for performing stabilization.
	 */
	private NodeImpl parent;

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

	/**
	 * Object logger.
	 */
	protected final static Logger logger = Logger.getLogger(StabilizeTask.class);;

	/**
	 * Creates a new instance, but without starting a thread running it.
	 * 
	 * @param parent
	 *            Parent object for performing stabilization.
	 * @param references
	 *            Reference on routing table.
	 * @throws NullPointerException
	 *             If either of the parameters is <code>null</code>.
	 */
	StabilizeTask(NodeImpl parent, References references) {

		if (parent == null || references == null) {
			throw new NullPointerException(
					"Neither parameter to constructor may be null!");
		}

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

	public void run() {

		try {

			// start of method
			StabilizeTask.logger.debug("Stabilize method has been invoked periodically");

			// determine successor
			Node successor = this.references.getSuccessor();
			if (successor == null) {

				// nothing to stabilize
				StabilizeTask.logger.info("Nothing to stabilize, as successor is null");
				return;

			} else {

				// notify successor and obtain its predecessor reference and
				// successor list
				List<Node> mySuccessorsPredecessorAndSuccessorList;
				try {
					mySuccessorsPredecessorAndSuccessorList = successor
							.notify(this.parent);
					StabilizeTask.logger
							.info("Received response to notify request from "
									+ "successor");
				} catch (CommunicationException e) {
					StabilizeTask.logger.warn("Invocation of notify on node "
							+ successor.nodeID
							+ " was not successful due to a "
							+ "communication failure! Successor has "
							+ "failed during stabilization! "
							+ "Removing successor!", e);
					this.references.removeReference(successor);
					return;
				}

				
				// copy my own successor list
				// remove my successor's reference and all references which are contained in my successor's reply, s.t. potentially dead references can be detected
				// send ping message to potentially dead references and remove them from this node's references
				List<Node> copyOfSuccessorList = new ArrayList<Node>(this.references.getSuccessors());
				copyOfSuccessorList.remove(successor);
				for (Node n : mySuccessorsPredecessorAndSuccessorList) {
					copyOfSuccessorList.remove(n);
				}
				for (Node potentiallyDeadReference : copyOfSuccessorList) {
					try {
						potentiallyDeadReference.ping();
					} catch(CommunicationException e) {
						this.references.removeReference(potentiallyDeadReference);
					}
				}
				
				// add new references, if pings are successful
				Set<Node> pingRequestSent = new HashSet<Node>();
				for (final Node newReference : mySuccessorsPredecessorAndSuccessorList) {
					if (newReference != null
							&& !newReference.equals(this.parent)
							&& !this.references.containsReference(newReference)) {

						if (!pingRequestSent.contains(newReference)) {
							StabilizeTask.logger
									.debug("Sending ping request to new reference "
											+ newReference.nodeID.toHexString());
							new Thread(new Runnable() {
								public void run() {
									try {
										newReference.ping();
										StabilizeTask.this.references
												.addReference(newReference);
										StabilizeTask.logger
												.info("Added reference on "
														+ newReference.nodeID
														+ " which responded to "
														+ "ping request");
									} catch (CommunicationException e) {
										StabilizeTask.logger
												.info("New reference "
														+ newReference
														+ " unreachable");
									}
								}
							}).start();
							pingRequestSent.add(newReference);
						}
					}
				}

				
				
				
				StabilizeTask.logger.debug("Invocation of notify on node "
						+ successor.nodeID + " was successful");
			}
		} catch (Exception e) {
			StabilizeTask.logger.error(
					"Exception caught in StabilizeTask! From now on, "
							+ "periodic maintenance tasks are not executed "
							+ "any more!", e);
		}
	}

}

⌨️ 快捷键说明

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