view src/main/java/jp/ac/u_ryukyu/alicevnc/CheckCurrentInfo.java @ 47:9354e8a1d03b

working AliceVNC
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Mon, 19 Oct 2015 23:48:18 +0900
parents aace9df1462e
children 57ca704f86a3
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, CodeSegment cs){
        this.aliceDaemon = cs.aliceDaemon;
        info.setKey("currentServer", this);
        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(this);
    }
}