/*
 * KeepAliveThread.java
 */
package org.codehaus.activemq.transport.reliable;

import java.util.Iterator;

import javax.jms.JMSException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.KeepAlive;
import org.codehaus.activemq.util.IdGenerator;

import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;


/**
 * KeepAliveDaemon keeps channels alive by sending KeepAlive packets on a
 * specified interval. If the packets aren't don't get a receipt within a
 * specified time, the channel will be forcefully disconnected.
 */
public class KeepAliveDaemon implements Runnable {

	private static final Log log = LogFactory.getLog(KeepAliveDaemon.class);

	private static KeepAliveDaemon instance = null;

	private long checkInterval = 15000L;
	private long lastCheck = 0;
	private Object lock = new Object();

	private IdGenerator packetIdGenerator;

	private SynchronizedBoolean started = new SynchronizedBoolean(false);
	private SynchronizedBoolean stopped = new SynchronizedBoolean(false);
	private CopyOnWriteArraySet monitoredChannels = new CopyOnWriteArraySet();
	private CopyOnWriteArraySet zombieChannelSuspects = new CopyOnWriteArraySet();


	/**
	 * Constructs a new KeepAliveDaemon which will send KeepAlive packets
	 * throught the wrapped channel.
	 */
	protected KeepAliveDaemon() {
		this.packetIdGenerator = new IdGenerator();
	}

	/**
	 * Gets the current instance. Singletons implemented this way aren't popular
	 * these days, but it might be good here. :)
	 * 
	 * @return the daemon
	 */
	public static synchronized KeepAliveDaemon getInstance() {
		if (instance == null)
			instance = new KeepAliveDaemon();
		return instance;
	}

	public void addMonitoredChannel(ReliableTransportChannel channel) {
		if (channel.getKeepAliveTimeout() <= 0)
			return;
		log.debug("Adding channel " + channel);
		// Check that the timeout isn't lower than our check interval as
		// this would cause the channel to constantly be disconnected.
		// This check should perhaps be done whenever the channel changes it's
		// interval, but in practice, this will probably never happen.
		if (channel.getKeepAliveTimeout() / 2 < checkInterval) {
			setCheckInterval(channel.getKeepAliveTimeout() / 2);
			log.info("Adjusting check interval to " + checkInterval + " as channel " + channel.toString()
					+ " has lower timeout time than the current check interval.");
		}
		monitoredChannels.add(channel);
	}

	public void removeMonitoredChannel(ReliableTransportChannel channel) {
		log.debug("Removing channel " + channel);
		monitoredChannels.remove(channel);
	}

	/**
	 * Sets the number of milliseconds between keep-alive checks are done.
	 * 
	 * @param interval
	 *            the interval
	 */
	public void setCheckInterval(long interval) {
		this.checkInterval = interval;
		if (started.and(!stopped.get())) {
			restart();
		}
	}

	public long getCheckInterval() {
		return checkInterval;
	}
	
	public long getLastCheckTime() {
		return lastCheck;
	}

	public void start() {
		if (started.commit(false, true)) {
			log.debug("Scheduling keep-alive every " + checkInterval + " millisecond.");
			Thread t = new Thread(this);
			t.setName("KeepAliveDaemon");
			t.setDaemon(true);
			t.start();
		}
	}

	public void stop() {
		if (stopped.commit(false, true)) {
			synchronized (lock) {
				lock.notifyAll();
			}
			log.debug("Stopping keep-alive.");
		}
	}

	public void restart() {
		log.debug("Restarting keep-alive.");
		stop();
		start();
	}

	public void run() {
		lastCheck = System.currentTimeMillis() - checkInterval;
		while (!stopped.get()) {
			for (Iterator i = zombieChannelSuspects.iterator(); i.hasNext();) {
				ReliableTransportChannel channel = (ReliableTransportChannel) i.next();
				disconnectIfStillNotUpdated(channel);
			}

			for (Iterator i = monitoredChannels.iterator(); i.hasNext();) {
				ReliableTransportChannel channel = (ReliableTransportChannel) i.next();
				examineChannel(channel);
			}
			lastCheck = System.currentTimeMillis();
			synchronized (lock) {
				try {
					lock.wait(checkInterval);
				} catch (InterruptedException e) {

				}
			}
		}
	}

	private void disconnectIfStillNotUpdated(ReliableTransportChannel channel) {
		if ((channel.getLastReceiptTimestamp() + channel.getKeepAliveTimeout()) < System.currentTimeMillis()) {
			log.debug("Forcing channel " + channel + " to disconnect since it hasn't responded in "
					+ (System.currentTimeMillis() - channel.getLastReceiptTimestamp()) + " millis.");
			channel.forceDisconnect();
		} else {
			// It's alive again
			zombieChannelSuspects.remove(channel);
		}
	}

	private void examineChannel(ReliableTransportChannel channel) {
		if (channel.getLastReceiptTimestamp() < (System.currentTimeMillis() - channel.getKeepAliveTimeout()))
			if (channel.isTransportConnected() && !channel.isPendingStop()) {
				log.debug("Sending keep-alive on channel " + channel.toString());
				KeepAlive packet = new KeepAlive();
				packet.setId(packetIdGenerator.generateId());
				try {
					channel.asyncSendWithReceipt(packet);
					zombieChannelSuspects.add(channel);
				} catch (JMSException e) {
					log.error("Error sending keep-alive to channel " + channel.toString()
							+ ". Treating as temporary problem.", e);
				}
			} else if (channel.isPendingStop()) {
				removeMonitoredChannel(channel);
			}
	}
}
