IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 3, 2005, 5:50:08 PM (21 years ago)
Author:
eugene
Message:

psched / pscontrol / pclient dev work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/pantasks/ControllerOps.c

    r4697 r4705  
    11# include "psched.h"
    2 # define CONTROLLER_TIMEOUT 20
     2/* adding a new host can delay controller up to a second or so */
     3# define CONTROLLER_TIMEOUT 200
    34# define CONNECT_TIMEOUT 300
    45
    56/* local static variables to hold the connection to the controller */
    6 static int status = FALSE;
     7static int ControllerStatus = FALSE;
    78static int stdin_cntl, stdout_cntl, stderr_cntl;
    89static IOBuffer stdout_buffer;
    910static IOBuffer stderr_buffer;
     11static int ControllerPID = 0;
    1012
    1113/* test if the controller is running */
    1214int CheckControllerStatus () {
    13   return (status);
     15  return (ControllerStatus);
    1416}
    1517
     
    2931int CheckControllerJobStatus (Job *job) {
    3032
    31   int outstate;
     33  int outstate, status;
    3234  char cmd[128], status_string[64];
    3335  char *p;
     
    4143  switch (status) {
    4244    case CONTROLLER_DOWN:
    43       fprintf (stderr, "controller is down\n");
     45      if (VerboseMode()) fprintf (stderr, "controller is down\n");
    4446      FreeIOBuffer (&buffer);
    4547      return (FALSE);
    4648
    4749    case CONTROLLER_HUNG:
    48       fprintf (stderr, "controller is not responding\n");
     50      if (VerboseMode()) fprintf (stderr, "controller is not responding\n");
    4951      FreeIOBuffer (&buffer);
    5052      return (FALSE);
    5153
    5254    case CONTROLLER_GOOD:
    53       fprintf (stderr, "message received (CheckControllerJobStatus)\n");
     55      if (VerboseMode()) fprintf (stderr, "message received (CheckControllerJobStatus)\n");
    5456      break;
    5557
    5658    default:
    57       fprintf (stderr, "unknown status for controller command: programming error\n"); 
     59      if (VerboseMode()) fprintf (stderr, "unknown status for controller command: programming error\n"); 
    5860      exit (1);
    5961  }
     
    105107
    106108  /* send command to get appropriate channel */
    107   ALLOCATE (line, char, MAX (1, strlen(cmd) + 15));
    108   sprintf (line, "%s %d\n", cmd, pid);
    109   status = write (stdin_cntl, line, strlen(line));
    110   free (line);
     109  status = write_fmt (stdin_cntl, "%s %d\n", cmd, pid);
    111110
    112111  /* is pipe still open? */
     
    126125  if (status == -1) return (CONTROLLER_HUNG);
    127126
    128   fprintf (stderr, "message received (GetJobOutput : %s)\n", cmd); 
     127  if (VerboseMode()) fprintf (stderr, "message received (GetJobOutput : %s)\n", cmd); 
    129128  /* drop extra bytes from pcontrol (not pclient:job) */
    130129  buffer[0].Nbuffer = Nstart + Nbytes;
     
    161160
    162161  /* construct the controller command portion */
    163   if (!strcmp (job[0].task[0].host, "NONE")) {
     162  if (!strcasecmp (job[0].task[0].host, "ANYHOST")) {
    164163    sprintf (cmd, "job");
    165164  } else {
     
    177176  }
    178177
    179   fprintf (stderr, "sending command to controller: %s\n", cmd);
    180178  InitIOBuffer (&buffer, 0x100);
    181179  ControllerCommand (cmd, CONTROLLER_PROMPT, &buffer);
    182180  free (cmd);
    183 
    184   fprintf (stderr, "response from controller: %s\n", buffer.buffer);
    185181
    186182  /* extract the job PID from the controller response */
     
    203199  char *p;
    204200  char **argv, cmd[128];
    205   int i, pid;
     201  int i, pid, status;
    206202  int stdin_fd[2], stdout_fd[2], stderr_fd[2];
    207203  IOBuffer buffer;
    208204
    209   if (status) return (TRUE);
     205  if (ControllerStatus) return (TRUE);
    210206
    211207  if (VarConfig ("CONTROLLER", "%s", cmd) == NULL) strcpy (cmd, "pcontrol");
     
    262258
    263259  /* send handshake command */
    264   sprintf (cmd, "echo CONNECTED\n");
    265   status = write (stdin_fd[1], cmd, strlen(cmd));
     260  status = write_fmt (stdin_fd[1], "echo CONNECTED\n");
    266261  if ((status == -1) && (errno == EPIPE)) goto pipe_error;
    267262
     
    286281  InitIOBuffer (&stderr_buffer, 0x100);
    287282
     283  ControllerPID = pid;
     284  ControllerStatus = TRUE;
    288285  fprintf (stderr, "Connected\n");
    289286  return (TRUE);
     
    316313  FlushIOBuffer (buffer);
    317314
    318   fprintf (stderr, "send: %s\n", cmd);
    319 
    320   /* send command to client (adding on \n) */
    321   ALLOCATE (line, char, MAX (1, strlen(cmd)));
    322   sprintf (line, "%s\n", cmd);
    323   status = write (stdin_cntl, line, strlen(line));
    324   free (line);
    325 
    326   /* is pipe still open? */
     315  if (VerboseMode()) fprintf (stderr, "send: %s\n", cmd);
     316
     317  /* send command, is pipe still open? */
     318  status = write_fmt (stdin_cntl, "%s\n", cmd);
    327319  if ((status == -1) && (errno == EPIPE)) return (CONTROLLER_DOWN);
    328320 
     
    335327    if (status == -1) usleep (10000);
    336328  }
    337   if (status ==  0) return (CONTROLLER_DOWN);
     329  if (status ==  0) {
     330    ControllerStatus = FALSE;
     331    return (CONTROLLER_DOWN);
     332  }
    338333  if (status == -1) return (CONTROLLER_HUNG);
    339   /* fprintf (stderr, "buffer.buffer: %s\n", buffer[0].buffer); */
     334
     335  /* need to strip off the prompt */
     336  line = memstr (buffer[0].buffer, response, buffer[0].Nbuffer);
     337  if (line != NULL) {
     338    buffer[0].Nbuffer = line - buffer[0].buffer;
     339    bzero (buffer[0].buffer + buffer[0].Nbuffer, buffer[0].Nalloc - buffer[0].Nbuffer);
     340  }
    340341  return (CONTROLLER_GOOD);
    341342}
     
    345346  int Nread;
    346347
    347   if (!status) return (TRUE);
     348  if (!ControllerStatus) return (TRUE);
    348349
    349350  /* read stdout buffer */
     
    390391  IOBuffer buffer;
    391392
    392   if (!CheckControllerStatus()) return (TRUE);
     393  if (!ControllerStatus) return (TRUE);
    393394
    394395  sprintf (cmd, "kill %d", job[0].pid);
     
    403404int QuitController () {
    404405
    405   int status;
     406  int i, status, waitstatus, result;
    406407  char cmd[128];
    407408  IOBuffer buffer;
    408409
    409   if (!CheckControllerStatus()) return (TRUE);
     410  if (!ControllerStatus) return (TRUE);
    410411
    411412  sprintf (cmd, "quit");
    412413  InitIOBuffer (&buffer, 0x100);
    413414  status = ControllerCommand (cmd, CONTROLLER_PROMPT, &buffer);
    414   return (TRUE);
    415 }
    416 
    417 /* memstr returns a view, not an allocated string : don't free */
     415  ControllerStatus = FALSE;
     416  result = waitpid (ControllerPID, &waitstatus, WNOHANG);
     417  for (i = 0; (i < 10) && (result == 0); i++) {
     418    usleep (10000);  /* 10 ms is min */
     419    result = waitpid (ControllerPID, &waitstatus, WNOHANG);
     420  }
     421  ControllerPID = 0;
     422  close (stdin_cntl);
     423  close (stdout_cntl);
     424  close (stderr_cntl);
     425
     426  return (TRUE);
     427}
Note: See TracChangeset for help on using the changeset viewer.