IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7940


Ignore:
Timestamp:
Jul 19, 2006, 11:48:49 AM (20 years ago)
Author:
eugene
Message:

fixed some client/server I/O issues

Location:
trunk/Ohana/src/opihi
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/include/pantasks.h

    r7929 r7940  
    196196void InitClients ();
    197197void AddNewClient (int client);
     198int  DeleteClient (int client);
    198199void *ListenClients (void *data);
    199200
  • trunk/Ohana/src/opihi/lib.shell/multicommand.c

    r7938 r7940  
    7070          }
    7171
     72          // receive the command exit status
     73          if (ExpectMessage (server, 2.0, &message)) {
     74            switch (errno) {
     75              case EPIPE:
     76                gprint (GP_ERR, "server connection has died\n");
     77                exit (1);
     78              default:
     79                gprint (GP_ERR, "I/O error sending server message\n");
     80                exit (2);
     81            }
     82          } else {
     83            sscanf (message.buffer, "STATUS %d", &status);
     84          }
     85
    7286          // receive the resulting stderr
    7387          if (ExpectMessage (server, 2.0, &message)) {
  • trunk/Ohana/src/opihi/pantasks/ListenClients.c

    r7938 r7940  
    55static int Nclients;
    66static int *clients;
    7 static IOBuffer *buffers;
     7static IOBuffer **buffers;
    88
    99void InitClients () {
     
    1212  NCLIENTS = 10;
    1313  ALLOCATE (clients, int, NCLIENTS);
    14   ALLOCATE (buffers, IOBuffer, NCLIENTS);
     14  ALLOCATE (buffers, IOBuffer *, NCLIENTS);
    1515}
    1616
     
    2020  if (DEBUG) gprint (GP_LOG, "adding a new client (%d)\n", client);
    2121  clients[Nclients] = client;
    22   InitIOBuffer(&buffers[Nclients], 256);
     22  ALLOCATE (buffers[Nclients], IOBuffer, 1);
     23  InitIOBuffer(buffers[Nclients], 256);
    2324  Nclients ++;
    2425  if (Nclients >= NCLIENTS - 1) {
    2526    NCLIENTS += 10;
    2627    REALLOCATE (clients, int, NCLIENTS);
    27     REALLOCATE (buffers, IOBuffer, NCLIENTS);
     28    REALLOCATE (buffers, IOBuffer *, NCLIENTS);
    2829  }
     30}
     31
     32/* add this client to the client table, create an IOBuffer for it */
     33int DeleteClient (int client) {
     34
     35  int i, j;
     36
     37  if (DEBUG) gprint (GP_LOG, "deleting a client (%d)\n", client);
     38  for (i = 0; i < Nclients; i++) {
     39    if (clients[i] == client) {
     40      FreeIOBuffer (buffers[i]);
     41      free (buffers[i]);
     42      close (clients[i]);
     43      for (j = i; j < Nclients - 1; j++) {
     44        clients[j] = clients[j+1];
     45        buffers[j] = buffers[j+1];
     46      }
     47      Nclients --;
     48      if ((Nclients > 10) && (Nclients / 2 < NCLIENTS)) {
     49        NCLIENTS = Nclients + 10;
     50        REALLOCATE (clients, int, NCLIENTS);
     51        REALLOCATE (buffers, IOBuffer *, NCLIENTS);
     52      }
     53      return TRUE;
     54    }
     55  }
     56  // did not find the client
     57  return FALSE;
    2958}
    3059
     
    5685      Nmax = MAX (Nmax, clients[i]);
    5786      FD_SET (clients[i], &fdSet);
    58       if (FD_ISSET(clients[i], &fdSet)) {
    59         if (DEBUG) gprint (GP_ERR, "fd %d is set\n", clients[i]);
    60       } else {
    61         if (DEBUG) gprint (GP_ERR, "fd %d is not set\n", clients[i]);
    62       }
    6387    }   
    6488    Nmax ++;
     
    6791    if (DEBUG) gprint (GP_ERR, "listening to %d clients\n", Ncurrent);
    6892    status = select (Nmax, &fdSet, NULL, NULL, &timeout);
    69 
    70     if (DEBUG) gprint (GP_ERR, "messages from %d clients\n", status);
    71 
    7293    if (status == -1) {
    7394      perror("select()");
     
    7596    }
    7697
    77     /* if no data, go back for another select */
     98    /* if no data, update client list, wait for another select */
    7899    if (status <= 0) continue;
    79100
    80101    /* loop over the clients with data */
    81102    for (i = 0; i < Ncurrent; i++) {
     103      /* if client has no data, skip it */
    82104      if (!FD_ISSET(clients[i], &fdSet)) continue;
    83105
    84       /* this client has data, handle it */
    85       if (DEBUG) {
    86         gprint (GP_ERR, "data from client %d\n", clients[i]);
    87         gprint (GP_ERR, "start: ", buffers[i].buffer);
    88         gwrite (buffers[i].buffer, 1, buffers[i].Nbuffer, GP_ERR);
    89         gprint (GP_ERR, "...\n");
     106      /* read until the pipe is empty: 0 is closed, -1 is empty, -2 is error */
     107      Nread = 1;
     108      while (Nread > 0) {
     109        Nread = ReadtoIOBuffer (buffers[i], clients[i]);       
    90110      }
    91        
    92       /* read until the pipe is empty: -1 is empty, -2 is error */
    93       Nread = 0;
    94       while (Nread >= 0) {
    95         Nread = ReadtoIOBuffer (&buffers[i], clients[i]);       
    96         if (DEBUG) gprint (GP_ERR, "read %d bytes from socket\n", Nread);
     111      if ((Nread == 0) || (Nread == -2)) {
     112        /* error: do something */
     113        if (DEBUG && (Nread == 0)) gprint (GP_ERR, "socket is closed\n");
     114        if (DEBUG && (Nread == -2)) gprint (GP_ERR, "error reading from socket\n");
     115        DeleteClient (clients[i]);
     116        continue;
     117      }
    97118
    98         if (Nread == -2) {
    99           /* error: do something */
    100           if (1) gprint (GP_ERR, "error reading from socket\n");
    101         }
    102       }
    103       if (DEBUG) gprint (GP_ERR, "read %d total bytes\n", buffers[i].Nbuffer);
     119      if (DEBUG) gprint (GP_ERR, "read %d total bytes\n", buffers[i][0].Nbuffer);
    104120
    105       if (DEBUG) {
    106         gprint (GP_ERR, "end: ", buffers[i].buffer);
    107         gwrite (buffers[i].buffer, 1, buffers[i].Nbuffer, GP_ERR);
    108         gprint (GP_ERR, "...\n");
    109       }
    110        
    111       /* see if we have a complete message waiting */
    112       line = CheckForMessage (&buffers[i]);
     121      /* see if we have a complete message waiting; if not, keep waiting for messages */
     122      line = CheckForMessage (buffers[i]);
    113123      if (line == NULL) continue;
    114124
     
    116126      /* in this thread, we set the print output destination to be an
    117127         internal buffer, which we dump at the end of the execution */
    118       /* XXX : we need to handle ; in the client-parsing of commands */
     128      /* the commands sent to the server should not have ; */
    119129      stripwhite (line);
    120130      if (*line) {
    121131
    122         if (1) fprintf (stderr, "got command: %s\n", line);
    123 
    124         /* this works: we can send a simple message */
    125         # if 0
    126         SendMessage (clients[i], "first");
    127         SendMessage (clients[i], "second");
    128         # endif
    129 
    130         /* this works: we can send a message using the IOBuffers */
    131         # if 0
    132         InitIOBuffer (&testbuffer, 64);
    133         PrintIOBuffer (&testbuffer, "this is a test\n");
    134         SendMessageFixed (clients[i], testbuffer.Nbuffer, testbuffer.buffer);
    135         PrintIOBuffer (&testbuffer, "this is a second test\n");
    136         SendMessageFixed (clients[i], testbuffer.Nbuffer, testbuffer.buffer);
    137         # endif
    138 
    139         /* this works: we can use the printing system to write a message */
    140         # if 0
    141         gprintSetBuffer (GP_LOG);
    142         gprint (GP_LOG, "test is a test line\n");
    143         outbuffer = gprintGetBuffer (GP_LOG);
    144         SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
    145        
    146         gprint (GP_LOG, "test is a second test line\n");
    147         outbuffer = gprintGetBuffer (GP_LOG);
    148         SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
    149         # endif
    150 
    151         /* this works: we can use the gprint service */
    152         # if 0
    153         gprintSetBuffer (GP_LOG);
    154         gprint (GP_LOG, "test is a test line\n");
    155         status = multicommand (line);
    156 
    157         outbuffer = gprintGetBuffer (GP_LOG);
    158         SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
    159         gprintSetFile (GP_LOG, "stdout");
    160        
    161         SendMessage (clients[i], "second");
    162         # endif
    163 
    164         /* this only kind of works: it acts as if we hang */
    165         # if 1
     132        /* set buffers for the output for this client */
    166133        gprintSetBuffer (GP_LOG);
    167134        gprintSetBuffer (GP_ERR);
    168135
    169         // gprint (GP_LOG, "test is a test stdout\n");
    170         // gprint (GP_ERR, "test is a test stderr\n");
    171 
     136        /* run the command, return the exit status */
    172137        status = multicommand (line);
    173         fprintf (stderr, "sending response\n");
     138        SendMessage (clients[i], "STATUS %d", status);
    174139
    175140        // return the stderr messages first
    176141        outbuffer = gprintGetBuffer (GP_ERR);
    177142        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
    178         fprintf (stderr, "sent stderr\n");
    179143
     144        // return the stdout messages first
    180145        outbuffer = gprintGetBuffer (GP_LOG);
    181146        SendMessageFixed (clients[i], outbuffer[0].Nbuffer, outbuffer[0].buffer);
    182         fprintf (stderr, "sent stdout\n");
    183147       
     148        /* clear and reset the output buffers */
    184149        gprintSetFile (GP_LOG, "stdout");
    185150        gprintSetFile (GP_ERR, "stderr");
    186         # endif
    187151      }
    188152      free (line);
    189 
    190      
    191       /* this function should return the output buffer
    192          to the currently selected client */
    193       // DumpOutputBuffer ();
    194153    }
    195 
    196154    /* if Nread == -2, we probably need to drop the client */
    197155    /* check if we need to drop / remove any clients */
     
    204162   only this thread is allowed to decrease Nclients and remove
    205163   a client from the table */
    206 
    207 /* - read data from fd
    208    - if we have a complete line, request a block in the
    209    scheduler loop
    210    - execute the command
    211    - send the response back to the client.
    212    - all commands will have to send their output to a buffer,
    213    to be sent back to the calling process
    214 */
  • trunk/Ohana/src/opihi/pantasks/pantasks_server.c

    r7929 r7940  
    3333  gprintInit ();  // each thread needs to init the printing system
    3434
    35   // signal (SIGPIPE, gotsignal);
     35  signal (SIGPIPE, gotsignal);
    3636  // signal (SIGTSTP, gotsignal);
    3737  // signal (SIGTTIN, gotsignal);
Note: See TracChangeset for help on using the changeset viewer.