# include "pcontrol.h"

int ResetJob (Job *job) {
  
  int       status;
  IOBuffer  buffer;
  Host     *host;

  /** must have a valid host : if not, move to pending? **/
  ASSERT (job != NULL, "job missing");

  host = (Host *) job[0].host;
  ASSERT (job != NULL, "host missing");

  InitIOBuffer (&buffer, 0x100);
  
  /* we have tried to reset the job; may not get status */
  job[0].Reset = TRUE;

  status = PclientCommand (host, "reset", PCLIENT_PROMPT, &buffer);

  /* check on success of pclient command */
  switch (status) {
    case PCLIENT_DOWN:
      /*** different behavior for ANYHOST, WANTHOST, NEEDHOST? ***/
      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 PCLIENT_HUNG:
      /*** should we consider a HUNG host DOWN? ***/
      gprint (GP_ERR, "host %s is not responding (ResetJob)\n", host[0].hostname);
      FreeIOBuffer (&buffer);
      return (FALSE);

    case PCLIENT_GOOD:
      gprint (GP_ERR, "message received (ResetJob)\n");  
      FreeIOBuffer (&buffer);
      return (TRUE);

    default:
      gprint (GP_ERR, "unknown status for pclient command: programming error\n");  
      pcontrol_exit (1);
  }

  gprint (GP_ERR, "programming error in GetJobOutput (should not reach here)\n");
  pcontrol_exit (1);
  return (FALSE);
}

/* if machine is down, return FALSE
   this will place job back in BUSY state,
   next check of job will catch state and
   put machine down correctly

*/
