Mercurial > hg > FederatedLinda
view src/fdl/test/topology/MetaProtocolEngine.java @ 93:a1d796c0e975 fuchita
Wait Read Tester
author | one |
---|---|
date | Tue, 25 May 2010 22:57:54 +0900 |
parents | 04bd4ae97e7c |
children | 0ea086f0e96f |
line wrap: on
line source
package fdl.test.topology; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.util.ArrayList; import fdl.MetaEngine; import fdl.MetaLinda; import fdl.PSXLinda; import fdl.PSXReply; /** * MetaProtocolEngine * * @author Kazuki Akamine * * 接続する機能までを実装した MetaEngine * これを継承して、具体的な処理を書く * */ public class MetaProtocolEngine implements MetaEngine { // Fields protected static String lastToken = "null"; protected static int port = 10000; protected static int manageId = 60000; protected PSXLinda manager, psxLocal; protected ArrayList<PSXLinda> psxSendServers; protected MetaLinda fdlMeta; protected String managerHostName; protected String localHostName; // Constructor public MetaProtocolEngine(MetaLinda ml, String managerHostName) { this.fdlMeta = ml; this.managerHostName = managerHostName; try { this.localHostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); } this.psxSendServers = new ArrayList<PSXLinda>(); } public void mainLoop() { psxLocal = fdlMeta; initTopologyManager(); initSendServer(); } protected void initSendServer() { sendLocalHostName(); connectSendServers(); sendLocalHostName(); } protected void initTopologyManager() { // Connect to TopologyManager Server try { manager = fdlMeta.open(managerHostName, port); } catch (IOException e) { // manager は先に立ち上げてから実行する。 // while で回して再接続 loop にすべき? e.printStackTrace(); } } protected void sendLocalHostName() { // TopologyManager に自分のホストネームを送信して、起動を伝える ByteBuffer local; local = ByteBuffer.wrap(localHostName.getBytes()); manager.out(manageId, local); try { manager.sync(1); } catch (IOException e) { e.printStackTrace(); } } protected void connectSendServers() { // TopologyManager から、送られてくる ConnectServer の hostName を取得して接続 System.out.println("[DEBUG] MethodCall connectSendServers()"); while (true) { PSXReply reply; reply = psxLocal.in(manageId); do { try { psxLocal.sync(1); } catch (IOException e) { e.printStackTrace(); } } while (!reply.ready()); ByteBuffer data = reply.getData(); String hostName = new String(data.array()); System.out.println("[DEBUG] GetReply " + hostName); if (hostName.equals(lastToken)) break; connectSendServer(hostName); } } protected void connectSendServer(String hostName) { PSXLinda linda; try { linda = fdlMeta.open(hostName, port); psxSendServers.add(linda); } catch (IOException e) { e.printStackTrace(); } System.out.println("Connect to " + hostName); } }