21.5 UDP Implementation of Radio Broadcasts

Team-FLY

The simplest strategy for handling multiple receivers of the same audio program is to treat them as completely independent, as described in Section 21.4.2. An alternative strategy, used by some radio stations on the Internet, is to multicast the program in a single stream. Listeners "tune in" at any time and receive the program as it is being broadcast on the air. A third strategy, used by video-on-demand (VOD) providers, broadcasts multiple copies of the same stream (a movie). Each copy starts a few minutes later than the previous one. Customers tune in to the stream that starts next so that they don't miss anything.

Copy UDPSendSeq.c into UDPSendBcast.c and compile it as UDPSendBcast . Modify UDPSendBcast to begin "sending" the file when it starts up. At first the sender has no receivers, so it just reads from the file and sleeps after reading each eight blocks. If the sender has receivers, it sends each message to every receiver. As receiver requests come in, the sender adds these receivers to its list. When a receiver requests the broadcast, the sender responds with a message containing a description of the audio broadcast (in this case, just the name of the file) and the elapsed time (in minutes and seconds) since program transmission started.

Logically, the sender consists of two distinct operations. One operation accepts new requests, and the other transmits the audio program. A possible implementation of both operations with a single process (or thread) generates a signal once per second. The signal handler sends eight messages to all of the receiving hosts , and the main program handles new receivers. The main program and the signal handler share the list of receiving hosts. A correct implementation with signals is only possible if the socket calls and name resolution calls that UICI uses are async-signal-safe or if the main program blocks signals at appropriate points.

Exercise 21.31

Describe an appropriate data structure for the list of receivers.

Answer:

The data type of a receiver could be a u_buf_t structure that holds all the information needed to describe a receiver of a UDP message. (See Section 20.2 for a description.) If the sender sets a maximum number of receivers, it can use an array. Otherwise, the sender can use a linked list of u_buf_t items.

An implementation that does not require the async-signal safety of the UICI calls and that does not use threads has a parent process receiving connection requests and a child process sending the audio stream to a list of remote hosts. The parent process could send u_buf_t messages through a pipe to its child to keep it informed about receivers. The child can set the pipe for nonblocking reads and could attempt to read new receivers from the pipe each time it is awakened by the periodic signals for transmitting messages. The algorithm is as follows .

  1. While the pipe is not empty, do a nonblocking read of a u_buf_t item and update the list of receivers.

  2. Read eight blocks from the audio file and send them to all receivers.

  3. Suspend until the next signal.

Exercise 21.32

How can the parent process determine how far along the child's transmission is so that it can send the information to the requesting receiver?

Answer:

The sender can record the time it starts and calculate the difference between the current time and the start time of the broadcast.

Copy UDPRecvSeq into UDPRecvBcast.c and compile it as UDPRecvBcast . Modify UDPRecvBcast to receive audio from UDPSendBcast . The UDPRecvBcast program displays the initial message from the sender (rather than sending the message to the audio device) and adjusts its state to start in the middle of a broadcast.

Exercise 21.33

Describe a strategy for initially partially filling the receive buffer before sending audio.

Answer:

Care must be taken so that the receiver does not wait for a message that has previously been sent. Since messages can be received out of order, the message after the one with the lowest sequence number may never arrive . Record the first sequence number that comes in and start filling the receive buffer according to the sequence numbers until a message comes in that would overflow the buffer. Then throw away the earliest half of the receive buffer. This should make room for the message just received.

Exercise 21.34

What happens if the sender's first message giving the description of the broadcast is lost?

Answer:

The first message received contains binary audio data. The result of the receiver outputting this type of information to a terminal is unpredictable. The receiver should do a sanity check on the first message and display the message only if it consists of printing characters .

Exercise 21.35

How does the UDP implementation of the radio broadcast behave under the four basic test cases?

Answer:

UDPRecvBcast behaves similarly to the other implementations . The receiver loses data if it is suspended long enough, and the receivers are independent.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net