view src/fdl/test/topology/ring/RingTopologyManagerEngine.java @ 81:c001797f3fdb

connect bug fix
author one
date Mon, 23 Nov 2009 20:39:39 +0900
parents 04bd4ae97e7c
children
line wrap: on
line source

package fdl.test.topology.ring;

import java.io.IOException;
import java.nio.ByteBuffer;

import fdl.MetaLinda;
import fdl.PSXReply;
import fdl.test.topology.TopologyManagerEngine;

public class RingTopologyManagerEngine extends TopologyManagerEngine {
	private int relayId = 10;

	// Constructor
	public RingTopologyManagerEngine(MetaLinda ml, int nodeNum) {
		super(ml, nodeNum);
	}
	
	public void mainLoop() {
		super.mainLoop();
		startRelay();
		endRelay();
	}
	
	protected void makeTopology() {
		super.makeTopology();
	}
	
	@Override protected void makeConnection() {
		// Ring 状の接続を定義
		for (int i = 0; i < nodes.length; i++) {
			nodes[i].addConnection(nodes[(i+1)%nodes.length]);
		}
	}
	
	private void startRelay() {
		String relayString = "test";
		ByteBuffer data = ByteBuffer.wrap(relayString.getBytes());
		nodes[0].linda.out(relayId, data);
		try {
			nodes[0].linda.sync(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void endRelay() {
		PSXReply reply;
		reply = manager.in(relayId);
		do {
			try {
				manager.sync(1);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} while (!reply.ready());
		ByteBuffer data = reply.getData();
		String resultData = new String(data.array());
		System.out.println("[DEBUG] RelayTime: " + resultData);
	}

}