view src/main/java/alice/test/topology/aquarium/CheckMyName.java @ 345:8f71c3e6f11d

Change directory structure Maven standard
author sugi
date Wed, 16 Apr 2014 18:26:07 +0900
parents
children aefbe41fcf12
line wrap: on
line source

package alice.test.topology.aquarium;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;


public class CheckMyName extends CodeSegment {
 	private Receiver host = ids.create(CommandType.PEEK);
	private Pattern pattern = Pattern.compile("^(node|cli)([0-9]+)$");
	private String key = "fish";
	
	public CheckMyName(){
		host.setKey("host");
	}
	
	@Override
	public void run(){
		String name = host.asString();
		Matcher matcher = pattern.matcher(name);
		
		matcher.find();
		String type = matcher.group(1);
		int num = new Integer(matcher.group(2));
		ods.put("host_num", num*2);
		MakeFrame frame =  new MakeFrame(name,num*2);
		ods.put("num", 1);
		
		if (type.equals("cli")){
			System.out.println("cli"+num);
			new TakeMynum();	
		}else if (type.equals("node")){
			System.out.println("node"+num);
			if (num != 0){
				new TakeMynum();
			} else {
				new SendMaxsize(0);
			}
		}
		new CheckLocalIndex("maxsize",0);
		
		ods.update(key, new FishPoint(-0.1f,-0.1f));
		new SetLocation(new MakeObject(frame), key, 0);
		new CheckLocalIndex(key,1);
		for (int i = 0; i < 3 ; i++){
			key = "fish"+i;
			if (num == 0) new AutoIncrement(key,0);
			ods.update(key, new FishPoint((float)Math.random(),
					(float)Math.random(), (float)Math.random()));
			new SetLocation(new MakeObject(frame), key, 0);
			new CheckLocalIndex(key,1);
		}
		
		for (int i = 3; i < 6 ; i++){
			key = "fish"+i;
			if (num == 1) new AutoIncrement(key,0);
			Random rnd = new Random();
			int tmp = rnd.nextInt(3);
			int rand = tmp + 1;
			ods.update(key, new FishPoint( (float)rand, 
					-(float)Math.random(), -(float)Math.random()));
			new SetLocation(new MakeObject(frame), key, 0);
			new CheckLocalIndex(key,1);
		}
		
		for (int i = 6; i < 9 ; i++){
			key = "fish"+i;
			if (num == 2) new AutoIncrement(key,0);
			Random rnd = new Random();
			int tmp = rnd.nextInt(3); 
			int rand = tmp + 3;
			ods.update(key, new FishPoint( (float)rand, 
					(float)Math.random(), -(float)Math.random()));
			new SetLocation(new MakeObject(frame), key, 0);
			new CheckLocalIndex(key,1);
		}
		
		for (int i = 9; i < 12 ; i++){
			key = "fish"+i;
			if (num == 3) new AutoIncrement(key,0);
			
			Random rnd = new Random();
			int tmp = rnd.nextInt(3); 
			int rand = tmp + 5;
			
			ods.update(key, new FishPoint( (float)rand, 
					-(float)Math.random(), (float)Math.random()));
			new SetLocation(new MakeObject(frame), key, 0);
			new CheckLocalIndex(key,1);
		}
		
	}
}