view src/main/java/alice/test/topology/aquarium/CheckMyName.java @ 547:e91a574b69de dispose

remove index
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Tue, 18 Aug 2015 16:15:17 +0900
parents 15eeb439830c
children
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", this);
    }

    @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();
            }
        }
        new CheckLocalIndex("maxsize");

        ods.update(key, new FishPoint(-0.1f,-0.1f));
        new SetLocation(new MakeObject(frame), key);
        new CheckLocalIndex(key);
        for (int i = 0; i < 3 ; i++){
            key = "fish"+i;
            if (num == 0) new AutoIncrement(key);
            ods.update(key, new FishPoint((float)Math.random(),
                    (float)Math.random(), (float)Math.random()));
            new SetLocation(new MakeObject(frame), key);
            new CheckLocalIndex(key);
        }

        for (int i = 3; i < 6 ; i++){
            key = "fish"+i;
            if (num == 1) new AutoIncrement(key);
            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);
            new CheckLocalIndex(key);
        }

        for (int i = 6; i < 9 ; i++){
            key = "fish"+i;
            if (num == 2) new AutoIncrement(key);
            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);
            new CheckLocalIndex(key);
        }

        for (int i = 9; i < 12 ; i++){
            key = "fish"+i;
            if (num == 3) new AutoIncrement(key);

            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);
            new CheckLocalIndex(key);
        }

    }
}