view src/main/java/jp/ac/u_ryukyu/alicevnc/CheckCurrentInfo.java @ 34:aace9df1462e

refactor
author sugi
date Thu, 13 Nov 2014 16:09:09 +0900
parents 7d65ad341cc9
children 9354e8a1d03b
line wrap: on
line source

package jp.ac.u_ryukyu.alicevnc;

import java.util.ArrayList;

import jp.ac.u_ryukyu.alicevnc.AliceVNCMessage.MessageType;
import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;

public class CheckCurrentInfo extends CodeSegment {

    private Receiver info = ids.create(CommandType.TAKE); // current Server Info
    private AliceVNCMessage order;

    public CheckCurrentInfo(AliceVNCMessage order){
        info.setKey("currentServer");
        this.order = order;
    }

    @Override
    public void run() {
        AliceVNCMessage current = info.asClass(AliceVNCMessage.class);

        if (order.name.equals(current.name)) {
            // need check route and node name
            ods.update("currentServer", current);
        } else {
            if (current.allowChange) {
                // send Stop message to current
                current.messageID = MessageType.StopProxy.getId();
                String address = current.getNextAddress();
                ods.put(address, "AliceVNCMessage", current);

                AliceVNCMessage mes = new AliceVNCMessage(MessageType.StartNegotiate);
                mes.route = new ArrayList<String>();
                for (String s : order.route) {
                    mes.route.add(s);
                }
                mes.name = "node0";
                address = mes.getNextAddress();
                ods.put(address, "AliceVNCMessage", mes);
            } else {
                // not allow to change
                order.messageID = MessageType.DeniedRequest.getId();
                order.name = "node0";
                String address = order.getNextAddress();
                ods.put(address, "AliceVNCMessage", order);

                // update current server info
                ods.update("currentServer", current);
            }
        }
        new ReceiveAliceVNCMessage();
    }
}