# include "pcontrol.h"
# define RETRY_BASE 10.0

int StartHost (Host *host) {

  int pid;
  int stdio[3];
  char command[64], shell[64];
  struct timeval now;
  float delta;

  /* perhaps change the name of these config variables... */
  if (VarConfig ("COMMAND", "%s", command) == NULL) strcpy (command, "ssh");
  if (VarConfig ("SHELL", "%s", shell)     == NULL) strcpy (shell, "pclient");

  if (VerboseMode()) gprint (GP_ERR, "starting host within thread %d\n", pthread_self());

  pid = rconnect (command, host[0].hostname, shell, stdio);
  if (!pid) {     
    /** failure to start: extend retry period **/
    if (VerboseMode()) gprint (GP_ERR, "failure to start %s\n", host[0].hostname);
    gettimeofday (&now, (void *) NULL);
    if (ZTIME(host[0].nexttry) || ZTIME(host[0].lasttry)) {
      /* reset retry period if either is zero */
      delta = RETRY_BASE;
    } else {
      delta = 2*DTIME (host[0].nexttry, host[0].lasttry);
    }
    host[0].nexttry.tv_sec  = now.tv_sec  + delta;
    host[0].nexttry.tv_usec = now.tv_usec;
    host[0].lasttry.tv_sec  = now.tv_sec;
    host[0].lasttry.tv_usec = now.tv_usec;
    PutHost (host, PCONTROL_HOST_DOWN, STACK_BOTTOM);
    return (FALSE);
  }
  host[0].nexttry.tv_sec  = 0;
  host[0].nexttry.tv_usec = 0;
  host[0].lasttry.tv_sec  = 0;
  host[0].lasttry.tv_usec = 0;

  host[0].stdin_fd  = stdio[0];
  host[0].stdout_fd = stdio[1];
  host[0].stderr_fd = stdio[2];
  host[0].pid       = pid;
  PutHost (host, PCONTROL_HOST_IDLE, STACK_BOTTOM);
  return (TRUE);
}
