IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7929


Ignore:
Timestamp:
Jul 18, 2006, 6:58:54 AM (20 years ago)
Author:
eugene
Message:

updates to fix comm problems with buffers (motivated by opihi gprint)

Location:
trunk/Ohana/src
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/include/ohana.h

    r7917 r7929  
    173173int ExpectCommand (int device, int length, double timeout, IOBuffer *buffer);
    174174int SendMessage (int device, char *format, ...);
     175int SendMessageFixed (int device, int length, char *messge);
    175176int SendCommand (int device, int length, char *format, ...);
    176177int SendCommandV (int device, int length, char *format, va_list argp);
  • trunk/Ohana/src/libohana/src/CommOps.c

    r7917 r7929  
    8888}
    8989
     90/* send a message of known size, sending the size first */
     91int SendMessageFixed (int device, int length, char *message) {
     92
     93  int status;
     94
     95  status = SendCommand (device, 16, "NBYTES: %6d", length);
     96  status = SendCommand (device, length, message);
     97  return (status);
     98}
     99
    90100int SendCommand (int device, int length, char *format, ...) {
    91101
     
    108118  vsnprintf (string, length + 1, format, argp);
    109119
    110   /* fprintf (stderr, "msg: %s\n", string); */
    111120  write (device, string, length);
    112121  free (string);
  • trunk/Ohana/src/libohana/src/IOBufferOps.c

    r7917 r7929  
    107107  Nbyte = vsnprintf (&tmp, 0, format, argp);
    108108
    109   if (buffer[0].Nbuffer + Nbyte + 1 >= buffer[0].Nalloc) {
     109  if (buffer[0].Nbuffer + Nbyte + 1>= buffer[0].Nalloc) {
    110110    buffer[0].Nalloc = buffer[0].Nbuffer + Nbyte + 64;
    111111    REALLOCATE (buffer[0].buffer, char, buffer[0].Nalloc);
     
    113113
    114114  vsnprintf (&buffer[0].buffer[buffer[0].Nbuffer], Nbyte + 1, format, argp);
    115   buffer[0].Nbuffer += Nbyte + 1;
     115  buffer[0].Nbuffer += Nbyte;
    116116  return (TRUE);
    117117}
  • trunk/Ohana/src/opihi/doc/pantasks.txt

    r7892 r7929  
     1
     2- additional issues:
     3
     4  - missing dependency for shell.h?
     5  - should not return an error message in client if command is not found
     6  - server input needs to place files for input on a stack which is
     7    actually loaded by the RunScheduler loop
     8  - have the client run a special multicommand function which passes
     9    any unfound commands along to the server.  the server should not
     10    be running multicommand, but it probably does not hurt.
     11    (the client is not allowed to send ';' to the server)
     12  - the multicommand needs to be available in the 'for' and other loops
     13    this probably means unifying the client and non-client forms. 
     14    how does multiclient get the value of the server?  I tend to use
     15    globals for things like that, which is probably not the best
     16    practice.  is the client form of the tool generic? are there other
     17    circumstances which would use a client/server beyond pantasks?
     18    maybe pcontrol? 
     19
     20    the two pieces of information which need to be carried to the
     21    multicommand and command functions in client/server mode are the
     22    server socket and the verbosity of the error messages.  these could
     23    be defined on startup with globals and an init function of some sort.
    124
    225I need to do some serious work on the output messages in order
  • trunk/Ohana/src/opihi/include/pantasks.h

    r7917 r7929  
    129129
    130130void InitPantasks ();
     131void InitPantasksServer ();
    131132void InitPantasksClient ();
    132133void InitTasks ();
  • trunk/Ohana/src/opihi/include/shell.h

    r7917 r7929  
    4949int           multicommand              PROTO((char *line));
    5050int           command                   PROTO((char *, char **));
     51int           multicommand_client       PROTO((char *line, int));
    5152int           command_client            PROTO((char *, char **));
    5253char         *expand_vars               PROTO((char *line));
     
    146147FILE *gprintGetFile (gpDest dest);
    147148int gprint (gpDest dest, char *format, ...);
     149int gwrite (char *buffer, int size, int N, gpDest dest);
    148150
    149151# endif
  • trunk/Ohana/src/opihi/lib.shell/Makefile

    r7917 r7929  
    2121$(SDIR)/ListOps.$(ARCH).o               \
    2222$(SDIR)/command.$(ARCH).o               \
     23$(SDIR)/command_client.$(ARCH).o        \
    2324$(SDIR)/CommandOps.$(ARCH).o            \
    2425$(SDIR)/errors.$(ARCH).o                \
     
    3738$(SDIR)/memstr.$(ARCH).o                \
    3839$(SDIR)/multicommand.$(ARCH).o          \
     40$(SDIR)/multicommand_client.$(ARCH).o   \
    3941$(SDIR)/parse.$(ARCH).o                 \
    4042$(SDIR)/parse_commands.$(ARCH).o        \
  • trunk/Ohana/src/opihi/lib.shell/command_client.c

    r7892 r7929  
    22# define VERBOSE 0
    33
    4 /* this function acts like the standard command parser,
    5    but skips the expansion of variables and vectors */
     4// this function is identical to the normal command.c function,
     5// but it returns -1 for a missing command and does not echo the
     6// 'missing command' error message
     7
    68int command_client (char *line, char **outline) {
    79
     
    1719  }
    1820
     21  /* expand anything of the form $fred or $fred$sam, etc */
     22  line = expand_vars (line);     /* line is freed here, new one allocated */
     23  /* expand anything of the form fred[N] */
     24  line = expand_vectors (line);  /* line is freed here, new one allocated */
     25  /* solve math expresions, assign variable, if needed */
     26  line = parse (line);        /* line is freed here, new one allocated */
     27  /* any entry in line of the form {foo} returns value or tmp vector / buffer */
     28
    1929  /* we may have reallocated line, return new pointer */
    2030  *outline = line;
     
    2737  for (i = 0; i < argc; i++) targv[i] = argv[i];
    2838
    29   cmd = MatchCommand (argv[0], TRUE, FALSE);
     39  // use non-verbose mode to skip 'missing command' error message
     40  cmd = MatchCommand (argv[0], FALSE, FALSE);
    3041  if (cmd == NULL) {
    31     status = FALSE;
     42    status = -1;
    3243  } else {
    3344    free (argv[0]);
     
    4051  free (argv);
    4152
     53  if (!status) {
     54    char *msg;
     55    msg = get_variable_ptr ("ERRORMSG");
     56    if (msg != (char *) NULL) gprint (GP_ERR, "%s\n", msg);
     57  }
     58
     59  set_int_variable ("STATUS", status);
     60  if (VERBOSE) gprint (GP_ERR, "command: %s, status: %d\n", line, status);
    4261  return (status);
    4362}
  • trunk/Ohana/src/opihi/lib.shell/gprint.c

    r7917 r7929  
    7777    return (&streams[i]);
    7878  }
    79   fprintf (STDERR, "programming error: gprintInit not called for thread\n");
     79  fprintf (stderr, "programming error: gprintInit not called for thread\n");
    8080  abort ();
    8181}
     
    9090  if (stream[0].file != NULL) {
    9191    fflush (stream[0].file);
    92     if (stream[0].file != stdout) fclose (stream[0].file);
     92    if (stream[0].file == stdout) goto skip_close;
     93    if (stream[0].file == stderr) goto skip_close;
     94    fclose (stream[0].file);
     95
     96  skip_close:
    9397    stream[0].file = NULL;
    9498  }
     
    146150  stream[0].file = fopen (filename, "a");
    147151  if (stream[0].file == NULL) {
    148     fprintf (STDERR, "cannot open file %s\n", filename);
     152    fprintf (stderr, "cannot open file %s\n", filename);
    149153    stream[0].file = (dest == GP_LOG) ? stdout : stderr;
    150154  }
  • trunk/Ohana/src/opihi/pantasks/ListenClients.c

    r7917 r7929  
    4343  struct timeval timeout;
    4444  IOBuffer *outbuffer;
     45  IOBuffer testbuffer;
    4546
    4647  InitClients ();
     48  gprintInit ();  // each thread needs to init the printing system
    4749
    4850  while (0) {
     
    131133
    132134      /* we now have a possible command from the client: run it */
     135      /* in this thread, we set the print output destination to be an
     136         internal buffer, which we dump at the end of the execution */
    133137      /* XXX : we need to handle ; in the client-parsing of commands */
    134138      stripwhite (line);
    135139      if (*line) {
    136         if (1) gprint (GP_ERR, "got command: %s\n", line);
    137         SetOutBuffer ();
     140
     141        if (1) fprintf (stderr, "got command: %s\n", line);
     142
     143        /* this works: we can send a simple message */
     144        # if 0
     145        SendMessage (clients[i], "first");
     146        SendMessage (clients[i], "second");
     147        # endif
     148
     149        /* this works: we can send a message using the IOBuffers */
     150        # if 0
     151        InitIOBuffer (&testbuffer, 64);
     152        PrintIOBuffer (&testbuffer, "this is a test\n");
     153        SendMessageFixed (clients[i], testbuffer.Nbuffer, testbuffer.buffer);
     154        PrintIOBuffer (&testbuffer, "this is a second test\n");
     155        SendMessageFixed (clients[i], testbuffer.Nbuffer, testbuffer.buffer);
     156        # endif
     157
     158        /* this works: we can use the printing system to write a message */
     159        # if 0
     160        gprintSetBuffer (GP_LOG);
     161        gprint (GP_LOG, "test is a test line\n");
     162        outbuffer = gprintGetBuffer (GP_LOG);
     163        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
     164       
     165        gprint (GP_LOG, "test is a second test line\n");
     166        outbuffer = gprintGetBuffer (GP_LOG);
     167        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
     168        # endif
     169
     170        /* this works: we can use the gprint service */
     171        # if 0
     172        gprintSetBuffer (GP_LOG);
     173        gprint (GP_LOG, "test is a test line\n");
    138174        status = multicommand (line);
    139         outbuffer = GetOutBuffer ();
    140 
    141         gprint (GP_ERR, "send message: %d bytes\n", outbuffer[0].Nbuffer);
    142         gwrite (outbuffer[0].buffer, 1, outbuffer[0].Nbuffer, GP_ERR);
    143         gprint (GP_ERR, "end message\n");
    144        
    145         SendMessage (clients[i], outbuffer[0].buffer);
    146         SetOutfile ("stdout");
     175
     176        outbuffer = gprintGetBuffer (GP_LOG);
     177        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
     178        gprintSetFile (GP_LOG, "stdout");
     179       
     180        SendMessage (clients[i], "second");
     181        # endif
     182
     183        /* this only kind of works: it acts as if we hang */
     184        # if 1
     185        gprintSetBuffer (GP_LOG);
     186        gprintSetBuffer (GP_ERR);
     187
     188        // gprint (GP_LOG, "test is a test stdout\n");
     189        // gprint (GP_ERR, "test is a test stderr\n");
     190
     191        status = multicommand (line);
     192        fprintf (stderr, "sending response\n");
     193
     194        // return the stderr messages first
     195        outbuffer = gprintGetBuffer (GP_ERR);
     196        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
     197        fprintf (stderr, "sent stderr\n");
     198
     199        outbuffer = gprintGetBuffer (GP_LOG);
     200        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
     201        fprintf (stderr, "sent stdout\n");
     202       
     203        gprintSetFile (GP_LOG, "stdout");
     204        gprintSetFile (GP_ERR, "stderr");
     205        # endif
    147206      }
    148207      free (line);
  • trunk/Ohana/src/opihi/pantasks/Makefile

    r7917 r7929  
    4848$(SDIR)/kill.$(ARCH).o \
    4949$(SDIR)/delete.$(ARCH).o \
     50$(SDIR)/server.$(ARCH).o \
    5051$(SDIR)/verbose.$(ARCH).o \
    5152$(SDIR)/controller.$(ARCH).o \
  • trunk/Ohana/src/opihi/pantasks/SocketOps.c

    r7917 r7929  
    3030  Address[0].sin_addr.s_addr = INADDR_ANY; // use this line to bind any address / port?
    3131
     32retry_server:
     33
    3234# if (0) 
    3335  status = inet_aton (hostip, &Address[0].sin_addr);
     
    4951  status = bind (InitSocket, (struct sockaddr *) Address, length);
    5052  if (status == -1) {
     53    fprintf (stderr, "errno: %d\n", errno);
     54    fprintf (stderr, "EACCES: %d\n", EACCES);
     55    fprintf (stderr, "EBADF: %d\n", EBADF);
     56    fprintf (stderr, "EINVAL: %d\n", EINVAL);
     57    fprintf (stderr, "ENOTSOCK: %d\n", ENOTSOCK);
     58    fprintf (stderr, "EFAULT: %d\n", EFAULT);
     59    fprintf (stderr, "ELOOP: %d\n", ELOOP);
     60    fprintf (stderr, "ENAMETOOLONG: %d\n", ENAMETOOLONG);
     61    fprintf (stderr, "ENOENT: %d\n", ENOENT);
     62    fprintf (stderr, "ENOMEM: %d\n", ENOMEM);
     63    fprintf (stderr, "ENOTDIR: %d\n", ENOTDIR);
     64    fprintf (stderr, "EROFS: %d\n", EROFS);
     65
     66    Address[0].sin_port ++;
     67    if (Address[0].sin_port > MY_PORT + 10) exit (2);
     68    fprintf (stderr, "trying next port: %d\n", Address[0].sin_port);
     69    goto retry_server;
     70
    5171    perror ("bind: ");
    5272    exit (2);
    5373  }
     74  /* repeated starts of the server are limited by xinetd or something:
     75     requires 60sec timeout of the selected socket */
    5476
    5577  status = listen (InitSocket, 10);
     
    140162  Address.sin_family = AF_INET;
    141163  Address.sin_port   = MY_PORT;
     164
     165retry_client:
    142166  status = inet_aton (hostip, &Address.sin_addr);
    143167  if (!status) {
     
    156180  status = connect (InitSocket, (struct sockaddr *) &Address, length);
    157181  if (status == -1) {
     182    if (errno == ECONNREFUSED) {
     183      Address.sin_port ++;
     184      if (Address.sin_port > MY_PORT + 10) exit (2);
     185      fprintf (stderr, "trying next port: %d\n", Address.sin_port);
     186      goto retry_client;
     187    }
    158188    perror ("connect: ");
    159189    exit (2);
  • trunk/Ohana/src/opihi/pantasks/client_shell.c

    r7917 r7929  
    44int client_shell (int argc, char **argv) {
    55
    6   int Nbad, status, server;
     6  int i, Nbad, status, server;
    77  char *line, *outline, *prompt, *history;
    88  char hostname[256], PASSWORD[256];
     
    5555    /* XXX the exit status of command does not allow us
    5656       to distinguish 'failed command' and 'command not found' */
    57     status = command (line, &outline);
    58     if (outline == NULL) {
    59       gprint (GP_ERR, "programming error: command_client returned NULL\n");
    60       exit (2);
    61     }
     57    /* XXX eventually replace this with a multicommand parsing */
     58    status = multicommand_client (line, server);
     59    add_history (line);
     60    append_history (1, history);
    6261
     62    free (line);
     63  }
     64}
     65
     66# if 0
    6367    /* command was not caught by client, send to server */
    6468    if (*outline && !status ) {
     
    7074      // XXX add this in and print to stdout
    7175      status = ExpectMessage (server, 2.0, &message);
    72       gprint (GP_ERR, "got message: %d bytes\n", message.Nbuffer);
    73       gwrite (message.buffer, 1, message.Nbuffer, GP_ERR);
    74       gprint (GP_ERR, "end message\n");
     76      // fprintf (stderr, "got stdout message: %d bytes\n", message.Nbuffer);
     77      fwrite (message.buffer, 1, message.Nbuffer, stderr);
     78      // fprintf (stderr, "end message\n");
     79
     80      // fprintf (stderr, "got message: %d bytes\n", message.Nbuffer);
     81      // for (i = 0; i < message.Nbuffer; i++) {
     82      // fprintf (stderr, "%d %d %c\n", i, message.buffer[i], message.buffer[i]);
     83      // }
     84      // fwrite (message.buffer, 1, message.Nbuffer, stderr);
     85      // fprintf (stderr, "end message\n");
     86
     87      // XXX add this in and print to stdout
     88      status = ExpectMessage (server, 2.0, &message);
     89      // fprintf (stderr, "got stderr message: %d bytes\n", message.Nbuffer);
     90      fwrite (message.buffer, 1, message.Nbuffer, stderr);
     91      // fprintf (stderr, "end message\n");
    7592    }
    76     free (outline);
    77   }
    78 }
     93# endif
    7994
    8095/*
  • trunk/Ohana/src/opihi/pantasks/init.c

    r7892 r7929  
    6464
    6565}
     66
     67int server         PROTO((int, char **));
     68
     69static Command server_cmds[] = { 
     70  {"server",  server,   "server-specific commands"},
     71};
     72
     73void InitPantasksServer () {
     74 
     75  int i;
     76
     77  InitTasks ();
     78  InitJobs ();
     79  InitJobIDs ();
     80
     81  for (i = 0; i < sizeof (cmds) / sizeof (Command); i++) {
     82    AddCommand (&cmds[i]);
     83  }
     84  for (i = 0; i < sizeof (server_cmds) / sizeof (Command); i++) {
     85    AddCommand (&server_cmds[i]);
     86  }
     87
     88}
  • trunk/Ohana/src/opihi/pantasks/pantasks_client.c

    r7917 r7929  
    1414  InitData ();
    1515  InitPantasksClient ();
     16
     17  gprintInit ();
    1618
    1719  rl_readline_name = opihi_name;
  • trunk/Ohana/src/opihi/pantasks/pantasks_server.c

    r7917 r7929  
    2929  InitBasic ();
    3030  InitData ();
    31   InitPantasks ();
     31  InitPantasksServer ();
    3232 
    33   InitPrint ();
     33  gprintInit ();  // each thread needs to init the printing system
    3434
    3535  // signal (SIGPIPE, gotsignal);
     
    4747
    4848  // pthread_create (&thread, NULL, &RunScheduler, NULL);
    49   // pthread_create (&thread, NULL, &pantasks_RunController, NULL);
     49  // pthread_create (&thread, NULL, &RunController, NULL);
    5050
    5151  /* in this loop, we listen for incoming connections, validate, and
  • trunk/Ohana/src/opihi/pantasks/run.c

    r7917 r7929  
    11# include "pantasks.h"
     2
     3// XXX for client/server, we need to simply start or stop the
     4// appropriate threads
     5// with one thread for each of the major actions, this would
     6// make it easy to keep the controller running and stop the
     7// scheduler (don't run CheckTasks, but run everything else
     8// until nothing is left...
    29
    310int run (int argc, char **argv) {
Note: See TracChangeset for help on using the changeset viewer.