The send_data() Function
The send_data() function sends data over the wire to the client. Again, the server application itself never interacts directly with a socket, but rather writes data to an intermediate queue. When the client is awake, the send_data() function reads data from the intermediate queue and writes it to the client's socket. The send_data() function is in Listing Two. Lines 18 and 19 ensure that the client is still awake and awaits notification of the server application placing data in the queue. The condition on the loop breaks when the client_wants_ sleep flag is set to True via the signal handler that catches a notification from recv_data() of the client transitioning to a sleep state. Next, starting on line 22 we loop through the intermediate queue, sending each message placed into the queue by the server application to the client. Lines 25 through 29 mark the replay buffer used to sync any data that had its transmission interrupted by the client going to sleep. Lines 30 through 44 simply close the queue if the client goes to sleep prior to transmission of all data in the queue, while lines 46 through 48 close the queue upon successful transmission of all its data.
1. void send_data(int s_client, int send_data_fd, struct replay_buffer *replay_buff) { 2. struct gqnode dequeued_data; 3. struct stat queue_stats; 4. struct flock fl; 5. char gt_packet[BUF_SIZE + 2]; 6. char data_notifier; 7. char *p; 8. char c; 9. long queue_file_size; 10. int temp = 0; 11. int bytes_written = 0; 12. int queue_fd; 13. int status; 14. int messages_written; 15. int bytes_read = 0; 16. int i = 0; 17. 18. while (!client_wants_sleep && 19. read(send_data_fd, &data_notifier, sizeof(data_notifier)) > 0) { 20. queue_fd = open (queue_file_path, O_RDWR); 21. fl = acquire_queue_lock (queue_fd); 22. while (read(queue_fd, &dequeued_data, sizeof(dequeued_data)) > 0) { 23. if (!client_wants_sleep && 24. (bytes_written = write (s_client, dequeued_data.buf, dequeued_data.nbytes)) > 0); 25. replay_buff->byte_count = replay_buff->byte_count + bytes_written; 26. for (i = 0; i < bytes_written; i++) { 27. replay_buff->buffer[replay_buff->index] = dequeued_data.buf[i]; 28. replay_buff->index = (replay_buff->index + 1) % RB_MAX; 29. } 30. if (client_wants_sleep) { 31. status = fstat (queue_fd, &queue_stats); 32. queue_file_size = queue_stats.st_size; 33. temp_queue_fd = open (queue_file_path, O_RDWR); 34. messages_written = 0; 35. while (read(queue_fd, &dequeued_data, sizeof(dequeued_data)) > 0) { 36. write(temp_queue_fd, &dequeued_data, sizeof(dequeued_data)); 37. messages_written++; 38. } 39. close(temp_queue_fd); 40. ftruncate(queue_fd, messages_written*sizeof(dequeued_data)); 41. release_queue_lock(queue_fd, fl); 42. close(queue_fd); 43. return; 44. } 45. } 46. ftruncate(queue_fd, 0); 47. release_queue_lock(queue_fd, fl); 48. close(queue_fd); 49. } 50. return; 51. }