# include "pcontrol.h"

int CheckHost (Host *host) {
  
  int status;
  IOBuffer buffer;

  ASSERT (host, "host not set");

  if (host[0].job != NULL) return (TRUE);

  /* if this host has been marked to be turned off, do that and return */
  if (host[0].markoff) {
    host[0].markoff = FALSE;
    StopHost (host);
    OffHost (host);
    return (TRUE);
  }

  InitIOBuffer (&buffer, 0x100);

  status = PclientCommand (host, "echo OK", PCLIENT_PROMPT, &buffer);
  switch (status) {
    case 0:
      if (VerboseMode()) gprint (GP_ERR, "host %s is down\n", host[0].hostname);
      HarvestHost (host[0].pid);
      PutHost (host, PCONTROL_HOST_DOWN, STACK_BOTTOM);
      FreeIOBuffer (&buffer);
      return (FALSE);
      
    case -1:
      if (VerboseMode()) gprint (GP_ERR, "host %s is not responding\n", host[0].hostname);
      /*** do we mark this in some way (HUNG) ? ***/
      PutHost (host, host[0].stack, STACK_BOTTOM);
      FreeIOBuffer (&buffer);
      return (FALSE);

    default:
      PutHost (host, host[0].stack, STACK_BOTTOM);
      FreeIOBuffer (&buffer);
      return (TRUE);
  }
  ABORT ("should not reach here (Check Host)"); 
}

// if the host has a job, we skip it (down or crash state will be caught elsewhere)
// in fact, just touch the IDLE hosts, not the BUSY hosts?
