All pastes #82283 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-07-08 06:41 UTC
rendered paste body
if (result == pid)
            {
               /* Create a new socket for communicating the job finlaization */
               answ_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

               /* Send some data to the client to indicate that the job was finished */
               printf("(TServerJobControl) Sending application termination data to the LRS (%d)...\n", pid);
               temp_port = clnt_addr.sin_port;
               clnt_addr.sin_port = htons(LRS_RECV_PORT);
               //outData.type = JOB_FINISHED;
               //outData.data.execData.pid = pid;
               /* This method of sending just once should be changed by something more secure... */
               /* If we do this, the TRunningJobs have to be changed so that it can answer for this securer method. */
               /* Besides a new socket must be created so that we can listen the ack from the LRS. This 'sock' must not be used */
               done = 0;

               nblocking(answ_sock);
while (!done)
               {
                  int valret;
                    struct timeval tv;
                  /* Set the data type and job pid */
                  outData.type = JOB_FINISHED;
                  outData.data.execData.pid = pid;

                  rst = sendto(answ_sock, &outData, sizeof(outData), 0, (struct sockaddr *)&clnt_addr, (socklen_t)sizeof(clnt_addr));
                  printf("(TServerJobControl) Sending application termination data to the LRS (PID:%d/RST:%d)...\n",
                         pid, rst);

                  if (rst < 0) perror("(TServerJobControl) Sending application finalization notification failed!!!");
                  else
                  {
                    fd_set readfds;
                    /* Sleep for a while, so that we let the answer to come */
                    // sleep(2);    commented by matt
                    FD_ZERO(&readfds);
                    tv.tv_sec=2;
                    tv.tv_usec=0;

                    /* Now, try to get the LoRaS confirmation */
                    printf("(TServerJobControl) Waiting for the 'application termination' confirmation from LRS (%d)...\n", pid);
                    fromlen = sizeof(struct sockaddr_in);

                    // ugly patch by matt
                    // x = 1;
                    FD_SET(answ_sock,&readfds);
                    // while (x<=5 && x!=0) {
                    valret=select(FD_SETSIZE,&readfds,NULL,NULL,&tv);
                    if (FD_ISSET(answ_sock,&readfds)) {
                        printf("(TServerJobControl) Ready to send\n");
                        x=0;
                    } else {
                        printf("(TServerJobControl) ERROR not ready to send\n");
                        x++;
                    }
                    // }

                    x=0;
                    while ((rst = recvfrom(answ_sock, &outData, sizeof(outData), 0, (struct sockaddr *)&answ_addr, (socklen_t *)&fromlen)) == EAGAIN && x
                    {
                         x++;
                         printf("(TServerJobControl) 'Application termination' notification still not recevied after %d tries...\n", x);

                        FD_ZERO(&readfds);
FD_SET(answ_sock,&readfds);
                        valret=select(FD_SETSIZE,&readfds,NULL,NULL,NULL);
                        // sleep(TIME_LIMIT);
                     }

                     if (rst > 0)
                     {
                        if (outData.type == JOB_FINISHED && outData.data.execData.pid == pid)
                        {
                           printf("(TServerJobControl) 'Application termination' notification received from the LRS (%d)...\n",
                                  outData.data.execData.pid);
                           done = 1;
                        }
                        else if (outData.type == NON_RUNNING_JOB)
                        {
                           printf("(TServerJobControl) 'Application termination' notification received from the LRS, but it appears that the job is not ye                                  outData.data.execData.pid);
                        }
                        else printf("(TServerJobControl) 'Application termination' notification ERROR: received data does not match the sent one (%d/%d)\n                             pid, outData.data.execData.pid);
                     }
else
                     {
                        printf("(TServerJobControl) 'Application termination' notification ERROR: receive error (%d)\n", rst);
                        perror("(TServerJobControl) 1-receive error:");
                     }
                  }
               }