/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.web.loadbalancer.scheduler;

import java.net.URL;

import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
import org.jboss.web.loadbalancer.util.Constants;

/**
 * A class that holds information about a target node.
 *
 * @jmx:mbean name="jboss.web.loadbalancer: type=Host"
 * @author Thomas Peuss <jboss@peuss.de>
 * @version $Revision: 1.2.2.3 $
 */
public class Host extends JBossNotificationBroadcasterSupport implements HostMBean
{
  private URL url;
  private int lbFactor;
  private int state=Constants.STATE_NODE_UP;
  private HostStatistics statistics=new HostStatistics();

  public Host(URL url)
  {
    this.url=url;
  }

  public String toString()
  {
    return "[URL=" + this.getUrl().toExternalForm() + ", State=" + this.getStateString() +
        ", LbFactor=" + this.getLbFactor() + ", Stats=" + this.getStatistics() + "]";
  }

  public int hashCode()
  {
    return url.hashCode();
  }

  public boolean equals(Object obj)
  {
    if (obj==null)
    {
      return false;
    }

    if (!(obj instanceof Host))
    {
      return false;
    }

    return (this.hashCode()==obj.hashCode());
  }

  public void addRequest(int responseTime)
  {
    statistics.addRequest(responseTime);
  }

  /**
   * @jmx:managed-attribute
   */
  public URL getUrl()
  {
    return url;
  }

  public void setUrl(URL url)
  {
    this.url = url;
  }

  /**
   * @jmx:managed-attribute
   */
  public int getLbFactor()
  {
    return lbFactor;
  }

  /**
   * @jmx:managed-attribute
   */
  public void setLbFactor(int lbFactor)
  {
    this.lbFactor = lbFactor;
  }

  /**
   * @jmx:managed-attribute
   */
  public HostStatistics getStatistics()
  {
    return statistics;
  }

  public void setStatistics(HostStatistics statistics)
  {
    this.statistics = statistics;
  }


  /**
   * @jmx:managed-attribute
   * @return
   */
  public int getState()
  {
    return state;
  }

  public void setState(int state)
  {
    this.state = state;
    this.sendNotification(new HostStateChangedNotification(this, "Host: "+this+". State changed to "+this.getStateString()));
  }

  /**
   * @jmx:managed-attribute
   */
  public String getStateString()
  {
    return Constants.STATE_STRINGS[state];
  }

  /**
   * @jmx:managed-operation
   */
  public void markNodeUp()
  {
    this.setState(Constants.STATE_NODE_UP);
  }

  /**
   * @jmx:managed-operation
   */
  public void markNodeDown()
  {
    this.setState(Constants.STATE_NODE_DOWN);
  }

  /**
   * @jmx:managed-operation
   */
  public void markNodeForcedDown()
  {
    this.setState(Constants.STATE_NODE_FORCED_DOWN);
  }

  /**
   * @jmx:managed-operation
   */
  public void resetStatistics()
  {
    statistics.reset();
  }
}