package org.gumbynet.carnivore.server;

import org.gumbynet.carnivore.objects.Host;
import org.gumbynet.carnivore.objects.HostLogEntry;
import org.gumbynet.carnivore.objects.Service;
import org.gumbynet.carnivore.objects.ServiceTestHelper;
import org.gumbynet.carnivore.utils.Ping;
import org.gumbynet.carnivore.constants.StatusCodes;
import org.gumbynet.carnivore.helpers.SMTPHelper;
import org.gumbynet.carnivore.helpers.HTTPHelper;

import java.util.Vector;
import java.util.Iterator;

/**
 * Carnivore Host monitoring thread
 *
     Copyright (c) 2003, Gumbynet & Damien Gardner Jnr
     All rights reserved.

     Redistribution and use in source and binary forms, with or without modification,
     are permitted provided that the following conditions are met:

     * Redistributions of source code must retain the above copyright notice, this list
         of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright notice, this list
         of conditions and the following disclaimer in the documentation and/or other
         materials provided with the distribution.
     * Neither the name of GumbyNet nor the names of its contributors may be used to
         endorse or promote products derived from this software without specific prior written permission.

     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 */
public class HostThread extends Thread {
  Host hstHost;
  HostLogHandlerThread hlhtLog;
  Vector vcrThreadList;

  public HostThread(Host hstHost, HostLogHandlerThread hlhtLog, Vector vcrThreadList) {
    this.hstHost = hstHost;
    this.hlhtLog = hlhtLog;
    this.vcrThreadList = vcrThreadList;
  }

  public void run() {
    // Ok, we have the host details, lets run through and do the monitoring..
    Vector vcrServices = new Vector(hstHost.getServices());

//        System.out.println("Started Host Thread");
    // iterate through services..
    while (vcrServices.size() > 0) {
      Service service = (Service) vcrServices.remove(0);

      // Try for ping
//      if (service.getServiceCode().equals("PING")) {
////        Ping ping = new Ping();
//        int TimeThrough = 0;
//        float pingTime = -1;
//        // We want to do three times at it..
//        while (TimeThrough++ < 3 && pingTime < 0) {
//          pingTime = Ping.SendPing(hstHost.getIp());
//        }
//        // Decrement it one..
//        TimeThrough--;
//
//
//        // Ok, how did we go?
//        // If we had one Time Through, then we add it as an 'OK'
//        if (TimeThrough == 1) {
//          hlhtLog.queueHostLogEntry(new HostLogEntry(hstHost.getId(), service.getServiceID(), StatusCodes.GREEN.getCode().intValue(), "Connection OK", "Ping reply from " + hstHost.getIp() + " in " + pingTime + "ms"));
//        }
//        // if we had more than one, but pingTime > 0, then we add it as a 'WARNING'
//        else if (pingTime > 0) {
//          hlhtLog.queueHostLogEntry(new HostLogEntry(hstHost.getId(), service.getServiceID(), StatusCodes.ORANGE.getCode().intValue(), "WARNING - Connection OK on attempt" + TimeThrough, "Ping reply from " + hstHost.getIp() + " in " + pingTime + "ms"));
//        }
//        // if we had more than one, and pingTime < 0, then we add it as a 'ERROR'
//        else {
//          hlhtLog.queueHostLogEntry(new HostLogEntry(hstHost.getId(), service.getServiceID(), StatusCodes.RED.getCode().intValue(), "ERROR: Can't connect to '" + hstHost.getIp() + "'", "Couldn't Ping host!: " + pingTime));
//        }
//      } else {
        // See if we have a class..
        if (service.getServiceClass() != null && service.getServiceClass().length() > 0) {

          // Must have a class, try instantiating it..
          ServiceTestHelper sth = null;
          try {
            sth = (ServiceTestHelper) Class.forName(service.getServiceClass()).newInstance();
          } catch (Exception e) {
        	  e.printStackTrace();
          }
          if (sth != null) {
            // Rad, it instantiated ok, so lets run the testService method and test this service :)
            HostLogEntry hle = sth.testService(hstHost.getIp());
            hle.setHostID(hstHost.getId());
            hle.setServiceID(service.getServiceID());
            hlhtLog.queueHostLogEntry(hle);
          }
        }
//      }


    }
    vcrThreadList.remove(this);

  }
}
