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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值