# include "pcontrol.h"

int CheckDoneJob (Job *job, Host *host) {
  
  int success;

  ASSERT (job, "job not set");
  ASSERT (host, "host not set");

  ASSERT (host == (Host *) job[0].host, "invalid host");
  ASSERT (job == (Job *) host[0].job, "invalid job");

  success = TRUE;
  success &= GetJobOutput ("stdout", host, &job[0].stdout_buff, job[0].stdout_size);
  success &= GetJobOutput ("stderr", host, &job[0].stderr_buff, job[0].stderr_size);

  if (!success) {
    // XXX some kind of error?
    // XXX try again later?
    PutHost (host, PCONTROL_HOST_BUSY, STACK_BOTTOM);
    PutJob (job, PCONTROL_JOB_DONE, STACK_BOTTOM);
    return (FALSE);
  }

  /* job's state is either EXIT or CRASH (verify?) */
  // unlink host & job
  job[0].host = NULL;
  host[0].job = NULL;
  PutHost (host, PCONTROL_HOST_DONE, STACK_BOTTOM);
  PutJob (job, job[0].state, STACK_BOTTOM);

  return (TRUE);
}

/** need to add timeout check here **/
