view src/alice/datasegment/RemoteDataSegmentManager.java @ 54:27a64e771c4c

change connection wait time
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Sun, 05 Feb 2012 18:24:39 +0900
parents f9334781344a
children ebdcab7b9b04
line wrap: on
line source

package alice.datasegment;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

import org.apache.log4j.Logger;
import org.msgpack.type.Value;

import alice.codesegment.CodeSegment;
import alice.daemon.Connection;
import alice.daemon.IncomingTcpConnection;
import alice.daemon.OutboundTcpConnection;

public class RemoteDataSegmentManager extends DataSegmentManager {
	
	Connection connection;
	Logger logger;
	
	public RemoteDataSegmentManager(String connectionKey, final String reverseKey, final String hostName, final int port) {
		logger = Logger.getLogger(connectionKey);
		connection = new Connection();
		final RemoteDataSegmentManager manager = this;
		new Thread(replyThread, "RemoteDataSegmentManager-" + connectionKey).start();
		new Thread("Connect-" + connectionKey) {
			public void run() {
				boolean connect = true;
				do {
					try {
						SocketChannel sc = SocketChannel.open(new InetSocketAddress(hostName, port));
						connection.socket = sc.socket();
						connect = false;
						logger.info("Connect to " + connection.getInfoString());
					} catch (IOException e) {
						try {
							Thread.sleep(50);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
				} while (connect);
				new IncomingTcpConnection(connection, manager, reverseKey).start();
				new OutboundTcpConnection(connection).start();
			}
		}.start();
	}
	
	@Override
	public void put(String key, Value val, CodeSegment cs) {
		Command cmd = new Command(CommandType.PUT, null, key, val, 0, 0, null, cs, null);
		connection.sendCommand(cmd);
		logger.debug(cmd.getCommandString());
	}

	@Override
	public void update(String key, Value val, CodeSegment cs) {
		Command cmd = new Command(CommandType.UPDATE, null, key, val, 0, 0, null, cs, null);
		connection.sendCommand(cmd);
		logger.debug(cmd.getCommandString());
	}

	@Override
	public void take(Receiver receiver, String key, int index, CodeSegment cs) {
		int seq = this.seq.getAndIncrement();
		Command cmd = new Command(CommandType.TAKE, receiver, key, null, index, seq, replyQueue, cs, null);
		seqHash.put(seq, cmd);
		connection.sendCommand(cmd);
		logger.debug(cmd.getCommandString());
	}

	@Override
	public void peek(Receiver receiver, String key, int index, CodeSegment cs) {
		int seq = this.seq.getAndIncrement();
		Command cmd = new Command(CommandType.PEEK, receiver, key, null, index, seq, replyQueue, cs, null);
		seqHash.put(seq, cmd);
		connection.sendCommand(cmd);
		logger.debug(cmd.getCommandString());
	}

	@Override
	public void remove(String key) {
		Command cmd = new Command(CommandType.REMOVE, null, key, null, 0, 0, null, null, null);
		connection.sendCommand(cmd);
		logger.debug(cmd.getCommandString());
	}

	@Override
	public void finish() {
		Command cmd = new Command(CommandType.FINISH, null, null, null, 0, 0, null, null, null);
		connection.sendCommand(cmd);
	}

	@Override
	public void close() {
		Command cmd = new Command(CommandType.CLOSE, null, null, null, 0, 0, null, null, null);
		connection.sendCommand(cmd);
	}

}