分布式计算ECHO算法(IT部落格)

来源:互联网 发布:vb程序设计课后答案 编辑:程序博客网 时间:2024/06/07 01:31
package org.ustc.scst.dc.simulation.algorithms.echo; import java.awt.Color; import org.ustc.scst.dc.simulation.algorithms.echo.IntMessage;import org.ustc.scst.dc.simulation.model.Message;import org.ustc.scst.dc.simulation.model.Node; /** * This special type of node is performing the Echo Algorithm. Fill in your code * here * * @author Thomas Weise */public class EchoNode extends Node {  /** the serial version uid */  private static final long serialVersionUID = 1L;   // you may add some fields  /** * */private int size=0;/** * */private Node father;  /**   * The node   *   * @param x   *          the x-coordinate   * @param y   *          the y-coordinate   */  public EchoNode(final int x, final int y) {    super(x, y);    //String s="dfsdfsd"; //$NON-NLS-1$  在这里进行属性设置    //this.setText(s);  }   /** Reset this model base to the initial state. */  @Override  public synchronized void reset() {    // add some code here    super.reset();  }   /**   * This method can directly be called from somewhere in order to start   * something. Currently it does nothing, so fill it with life!!!   */  @Override  public synchronized void trigger() {    // add some code here this.setColor(Color.BLUE);  this.broadcast(new IntMessage(0));    super.trigger();  }   /**   * This method is invoked whenever a message arrives at this node.   *   * @param msg   *          the message just received   */  @Override  public synchronized void onMessageReceived(final Message msg) {    // add some code here   //非叶节点  往外发送消息 如果是白色  if(this.getColor()==Color.WHITE)  {   this.father=msg.getSource();   this.setColor(Color.RED);   this.broadcastExcept(new IntMessage(0), this.father);  }  //如果是叶节点 则返回绿色数据  if(this.getNeighborCount()==1 && this.getColor()!=Color.GREEN && this.getColor()!=Color.BLUE)  {   this.setColor(Color.GREEN);   this.broadcast(new IntMessage(1));  }  //如果是非叶节点 收到所有的反馈消息  if(msg.getColor()==Color.GREEN)  {   this.size++;  }  //收到所有的绿色消息  if(this.getNeighborCount()==(this.size+1) && this.getColor()!=Color.GREEN && this.getColor()!=Color.BLUE)  {   this.setColor(Color.GREEN);   this.sendTo(new IntMessage(1), this.father);  }  //如果是头节点  if(this.getNeighborCount()==this.size && this.getColor()==Color.BLUE)  {   this.setColor(Color.GREEN);  }          super.onMessageReceived(msg);  }}


其中模拟视频如连接所示:

http://v.youku.com/v_show/id_XNjc4MDE1NjE2.html



来自IT部落格

0 0