view src/alice/jungle/codesegment/LogUpdateCodeSegment.java @ 47:686057add8a4

confirmed LogUpdateCodeSegment
author one
date Fri, 12 Jul 2013 11:45:12 +0900
parents fd3643699f83
children a89c3539bff2
line wrap: on
line source

package alice.jungle.codesegment;

import java.io.IOException;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
import jungle.test.bbs.JungleManager;
import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;
import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
import alice.jungle.transaction.NetworkDefaultJungleTreeEditor;

public class LogUpdateCodeSegment extends CodeSegment {
	
	public Receiver arg1 = ids.create(CommandType.PEEK);
	public Receiver host = ids.create(CommandType.PEEK);
	
	public LogUpdateCodeSegment() {
		host.setKey("host",0);
	}
	
	public void run() {
		System.out.println("--LogUpdateCodeSegment--");
		int index = arg1.index;
		String h = host.asString();
		DefaultTreeOperationLogContainer container = arg1.asClass(DefaultTreeOperationLogContainer.class);
		if(updaterIsMe(h, container)) {
			LogUpdateCodeSegment updateCS = new LogUpdateCodeSegment();
			updateCS.arg1.setKey("log", index+1);
			return;
		}
		DefaultTreeOperationLog log = null;
		try {
			log = container.convert();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JungleTree tree = JungleManager.getJungle().getTreeByName("boards");
		JungleTreeEditor editor = tree.getTreeEditor();
		Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log);
		if(either.isA()) {
			throw new IllegalStateException();
		}
		editor = either.b();
		either = editor.success();
		if(either.isA()) {
			throw new IllegalStateException();			
		}
		LogUpdateCodeSegment updateCS = new LogUpdateCodeSegment();
		updateCS.arg1.setKey("log", index+1);
	}
	
	private boolean updaterIsMe(String host, DefaultTreeOperationLogContainer container) {
		return host.equals(container.getServerName());
	}
	
	
	
}