/* Copyright (C) 2004 [Jörg Rüdenauer] 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. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package tokenring; import fdda.algorithm.Node; import fdda.algorithm.Message; import fdda.algorithm.WrongStateException; import java.util.List; import java.util.Iterator; /** * Abstract superclass for nodes in a token ring. */ public abstract class TokenRingNode extends Node { /** * Returns the following node in the ring. * @return the next node */ protected final Node getFollowingNode() { List neighbours = this.getNeighbours(); for (Iterator it = neighbours.iterator(); it.hasNext(); ) { Node neighbour = (Node) it.next(); if (this.canSendMessageTo(neighbour)) { return neighbour; } } return null; } /** * Creates a message to the following node in the ring. * @param payload the payload * @param label the label * @return the message, or null if no working outgoing connection exists * @throws WrongStateException */ protected final Message createMessageToNextNode(Object payload, String label) throws WrongStateException { Node neighbour = getFollowingNode(); if (neighbour != null) { Message message = this.createMessage(neighbour, payload); message.setLabel(label); return message; } else { return null; } } /** * Sends a message to the following node in the ring, if it is possible. * @param payload the payload * @param label the label * @throws WrongStateException */ protected final void sendMessageToNextNode(Object payload, String label) throws WrongStateException { Node neighbour = getFollowingNode(); if (neighbour != null) this.sendMessage(neighbour, payload, label); } }