/* 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. */ import java.awt.Color; import java.util.Iterator; import java.util.List; import fdda.algorithm.Message; import fdda.algorithm.Node; import fdda.algorithm.NodeAdapter; import fdda.algorithm.WrongStateException; /** * Node for the demonstration of the 'echo' algorithm to flood a network. */ public class EchoFlooding extends NodeAdapter { /** how many messages have I received yet? */ private int messageCount; /** who sent the explorer message? */ private Node explorerSender; /** did this node start the algorithm? */ private boolean starter; /* (non-Javadoc) * @see fdda.algorithm.Node#prepareForStart() */ protected void prepareForStart() { setPrimaryColor(Color.WHITE); messageCount = 0; explorerSender = null; starter = false; } /* (non-Javadoc) * @see fdda.algorithm.Node#start() */ protected void start() throws WrongStateException { setPrimaryColor(Color.RED); List neighbours = this.getNeighbours(); for (Iterator it = neighbours.iterator(); it.hasNext(); ) { Node neighbour = (Node) it.next(); Message explorer = this.createMessage(neighbour); explorer.setColor(Color.RED); explorer.setLabel("Info"); explorer.send(); } starter = true; } /* (non-Javadoc) * @see fdda.algorithm.Node#messageReceived(fdda.algorithm.Node, fdda.algorithm.Message) */ protected void messageReceived(Node sender, Message message) throws WrongStateException { if (!starter && (messageCount == 0)) { setPrimaryColor(Color.RED); explorerSender = sender; List neighbours = this.getNeighbours(); for (Iterator it = neighbours.iterator(); it.hasNext(); ) { Node neighbour = (Node) it.next(); if (neighbour == sender) continue; Message explorer = this.createMessage(neighbour); explorer.setColor(Color.RED); explorer.setLabel("Info"); explorer.send(); } } messageCount++; if (messageCount == this.getNeighbours().size()) { setPrimaryColor(Color.GREEN); if (explorerSender != null) { Message echo = this.createMessage(explorerSender); echo.setColor(Color.GREEN); echo.setLabel("Echo"); echo.send(); } // else algorithm is terminated else { assert starter; } } } }